Skip to content

Commit e74f323

Browse files
author
Christopher Doris
committed
jlwrap array tests
1 parent 33001b7 commit e74f323

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

test/jlwrap.jl

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,121 @@ end
227227
@test pytruth(pyjl(fill(nothing, 1, 2)))
228228
@test pytruth(pyjl(fill(nothing, 1, 2, 3)))
229229
end
230+
@testset "ndim" begin
231+
@test pyeq(Bool, pyjl(fill(nothing)).ndim, 0)
232+
@test pyeq(Bool, pyjl(fill(nothing, 1)).ndim, 1)
233+
@test pyeq(Bool, pyjl(fill(nothing, 1, 1)).ndim, 2)
234+
@test pyeq(Bool, pyjl(fill(nothing, 1, 1, 1)).ndim, 3)
235+
end
236+
@testset "shape" begin
237+
@test pyeq(Bool, pyjl(fill(nothing)).shape, ())
238+
@test pyeq(Bool, pyjl(fill(nothing, 3)).shape, (3,))
239+
@test pyeq(Bool, pyjl(fill(nothing, 3, 5)).shape, (3, 5))
240+
@test pyeq(Bool, pyjl(fill(nothing, 3, 5, 2)).shape, (3, 5, 2))
241+
end
242+
@testset "getitem" begin
243+
x = pyjl([1, 2, 3, 4, 5])
244+
@test pyeq(Bool, x[0], 1)
245+
@test pyeq(Bool, x[1], 2)
246+
@test pyeq(Bool, x[2], 3)
247+
@test pyeq(Bool, x[-1], 5)
248+
@test pyeq(Bool, x[-2], 4)
249+
@test pyeq(Bool, x[-3], 3)
250+
@test pyjlvalue(x[pyslice(3)]) == [1,2,3]
251+
@test pyjlvalue(x[pyslice(2)]) == [1,2]
252+
@test pyjlvalue(x[pyslice(1,2)]) == [2]
253+
@test pyjlvalue(x[pyslice(2,2)]) == []
254+
@test pyjlvalue(x[pyslice(0,-1)]) == [1,2,3,4]
255+
@test pyjlvalue(x[pyslice(-2,nothing)]) == [4,5]
256+
@test pyjlvalue(x[pyslice(0, 3, 1)]) == [1,2,3]
257+
@test pyjlvalue(x[pyslice(nothing,nothing,2)]) == [1,3,5]
258+
@test pyjlvalue(x[pyslice(1,nothing,2)]) == [2,4]
259+
@test pyjlvalue(x[pyslice(0,nothing,3)]) == [1,4]
260+
x = pyjl([1 2; 3 4])
261+
@test pyeq(Bool, x[0, 0], 1)
262+
@test pyeq(Bool, x[0, 1], 2)
263+
@test pyeq(Bool, x[1, 0], 3)
264+
@test pyeq(Bool, x[1, 1], 4)
265+
@test pyjlvalue(x[1, pyslice(nothing)]) == [3, 4]
266+
@test pyjlvalue(x[pyslice(nothing), 1]) == [2, 4]
267+
end
268+
@testset "setitem" begin
269+
x = [0 0; 0 0]
270+
y = pyjl(x)
271+
y[0,0] = 1
272+
@test x == [1 0; 0 0]
273+
y[0,1] = 2
274+
@test x == [1 2; 0 0]
275+
y[1,0] = 3
276+
@test x == [1 2; 3 0]
277+
y[-1,0] = 4
278+
@test x == [1 2; 4 0]
279+
y[-2,pyslice(nothing)] = 5
280+
@test x == [5 5; 4 0]
281+
y[pyslice(nothing),-1] = 6
282+
@test x == [5 6; 4 6]
283+
y[pyslice(nothing),pyslice(nothing)] = 7
284+
@test x == [7 7; 7 7]
285+
end
286+
@testset "delitem" begin
287+
x = [1, 2, 3, 4, 5, 6, 7, 8]
288+
y = pyjl(x)
289+
pydelitem(y, 0)
290+
@test x == [2, 3, 4, 5, 6, 7, 8]
291+
pydelitem(y, 2)
292+
@test x == [2, 3, 5, 6, 7, 8]
293+
pydelitem(y, -3)
294+
@test x == [2, 3, 5, 7, 8]
295+
pydelitem(y, pyslice(1,nothing,2))
296+
@test x == [2, 5, 8]
297+
end
298+
@testset "reshape" begin
299+
x = pyjl([1, 2, 3, 4, 5, 6, 7, 8])
300+
@test pyeq(Bool, x.shape, (8,))
301+
y = x.reshape((2, 4))
302+
@test pyeq(Bool, y.shape, (2, 4))
303+
@test pyjlvalue(y) == [1 3 5 7; 2 4 6 8]
304+
end
305+
@testset "copy" begin
306+
x = pyjl([1 2; 3 4])
307+
y = x.copy()
308+
@test pyis(pytype(y), PythonCall.pyjlarraytype)
309+
@test pyjlvalue(x) == pyjlvalue(y)
310+
@test typeof(pyjlvalue(x)) == typeof(pyjlvalue(y))
311+
@test pyjlvalue(x) !== pyjlvalue(y)
312+
x[0,0] = 0
313+
@test pyjlvalue(x) == [0 2; 3 4]
314+
@test pyjlvalue(y) == [1 2; 3 4]
315+
end
316+
@testset "array_interface" begin
317+
x = pyjl(Float32[1 2 3; 4 5 6]).__array_interface__
318+
@test pyisinstance(x, pybuiltins.dict)
319+
@test pyeq(Bool, x["shape"], (2, 3))
320+
@test pyeq(Bool, x["typestr"], "<f4")
321+
@test pyisinstance(x["data"], pybuiltins.tuple)
322+
@test pylen(x["data"]) == 2
323+
@test pyeq(Bool, x["strides"], (4, 8))
324+
@test pyeq(Bool, x["version"], 3)
325+
end
326+
@testset "array_struct" begin
327+
# TODO (not implemented)
328+
# x = pyjl(Float32[1 2 3; 4 5 6]).__array_struct__
329+
end
330+
@testset "buffer" begin
331+
m = pybuiltins.memoryview(pyjl(Float32[1 2 3; 4 5 6]))
332+
@test !pytruth(m.c_contiguous)
333+
@test pytruth(m.contiguous)
334+
@test pytruth(m.f_contiguous)
335+
@test pyeq(Bool, m.format, "f")
336+
@test pyeq(Bool, m.itemsize, 4)
337+
@test pyeq(Bool, m.nbytes, 4*6)
338+
@test pyeq(Bool, m.ndim, 2)
339+
@test !pytruth(m.readonly)
340+
@test pyeq(Bool, m.shape, (2, 3))
341+
@test pyeq(Bool, m.strides, (4, 8))
342+
@test pyeq(Bool, m.suboffsets, ())
343+
@test pyeq(Bool, m.tolist(), pylist([pylist([1, 2, 3]), pylist([4, 5, 6])]))
344+
end
230345
end
231346

232347
@testitem "base" begin

0 commit comments

Comments
 (0)