-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathruntests.jl
More file actions
149 lines (130 loc) · 3.48 KB
/
Copy pathruntests.jl
File metadata and controls
149 lines (130 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using NPZ
import NPZ: readheader
using Test
Debug = false
tmp = mktempdir()
if Debug
println("temporary directory: $tmp")
end
TestArrays = Any[
true,
false,
Int8(-42),
Int16(-42),
Int32(-42),
Int64(-42),
UInt8(42),
UInt16(42),
UInt32(42),
UInt64(42),
Float16(3.1415),
Float32(3.1415),
Float64(3.1415),
Complex{Float32}(1, 7),
Complex{Float64}(1, 7),
Bool[0, 1, 0, 1, 1, 0],
Int8[-42, 0, 1, 2, 3, 4],
Int16[-42, 0, 1, 2, 3, 4],
Int32[-42, 0, 1, 2, 3, 4],
Int64[-42, 0, 1, 2, 3, 4],
UInt8[0, 1, 2, 3, 4],
UInt16[0, 1, 2, 3, 4],
UInt32[0, 1, 2, 3, 4],
UInt64[0, 1, 2, 3, 4],
Float64[-42, 0, 1, 2, 3.14, 4],
Float32[-42, 0, 1, 2, 3.14, 4],
Float16[-42, 0, 1, 2, 3.14, 4],
Complex{Float32}[1+2im, 3, 4+5im, 6im, 7+8im],
Complex{Float64}[1+2im, 3, 4+5im, 6im, 7+8im],
[i-j for i in 1:3, j in 1:5],
[i-j+k for i in 1:3, j in 1:4, k in 1:5],
rand(512, 512, 16)
]
# Write a NPZ file with all the test arrays and numbers,
# and read it back in.
old = Dict{String, Any}()
for (i, x) in enumerate(TestArrays)
old["testvar_" * string(i)] = x
end
filename = "$tmp/big.npz"
npzwrite(filename, old)
output = npzread(filename)
for (k, v) in old
@test v == output[k]
end
@test old == output
testdict = Dict("one"=>21, "two"=>34)
dictfilename = "$tmp/dict.npz"
npzwrite(dictfilename, testdict)
dictoutput = npzread(dictfilename)
for (k, v) in testdict
@test v == dictoutput[k]
d = npzread(dictfilename, [k])
@test typeof(d) == Dict{String,typeof(v)}
val_read = d[k]
@test typeof(val_read) == typeof(v)
@test val_read == v
end
# Write and then read NPY files for each array
for x in TestArrays
let filename = "$tmp/test.npy"
npzwrite(filename, x)
y = npzread(filename)
@test typeof(x) == typeof(y)
@test size(x) == size(y)
@test x == y
end
end
#v2 tests
v2_solution = Array{Float64,1}([0.1*i for i in 0:10])
v2_read = npzread("linspace.npy")
@test(typeof(v2_read) == typeof(v2_solution))
@test(size( v2_read) == size(v2_solution))
@test( v2_read == v2_solution)
if !Debug
run(`rm -rf $tmp`)
end
@test NPZ.parseinteger("123Hello") == (123, "Hello")
@test NPZ.parseinteger("123LHello") == (123, "Hello")
@testset "npzwrite kwargs" begin
f = tempname()
npzwrite(f, ones(2,2), x = ones(3), y = 3)
d = npzread(f)
@test d["arr_0"] == ones(2,2)
@test d["x"] == ones(3)
@test d["y"] == 3
npzwrite(f, x = 4)
@test npzread(f, ["x"])["x"] == 4
rm(f)
end
@testset "zero-dim array" begin
f = tempname()
npzwrite(f, zeros())
@test npzread(f) == 0.0
end
@testset "readheader" begin
f = tempname()
arr = zeros(2,3)
npzwrite(f, arr)
hdr = readheader(f)
@test size(hdr) == size(arr)
@test ndims(hdr) == ndims(arr)
@test eltype(hdr) == eltype(arr)
npzwrite(f, ones(2,2), x = ones(Int,3), y = 3)
hdr = readheader(f)
@test size(hdr["arr_0"]) == (2,2)
@test eltype(hdr["arr_0"]) == eltype(npzread(f, ["arr_0"])["arr_0"])
@test size(hdr["x"]) == (3,)
@test eltype(hdr["x"]) == eltype(npzread(f, ["x"])["x"])
@test size(hdr["y"]) == ()
@test eltype(hdr["y"]) == eltype(npzread(f, ["y"])["y"])
end
@testset "read_npz_lazy" begin
ark = NPZ.npzread_lazy("data.npz")
arr_r = ark["range"]
@test arr_r == collect(Int64, 0:99)
arr_l = ark["linspace"]
@test length(arr_l) == 50
@test arr_l[1] == 0.0
@test arr_l[50] == 1.0
end