forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialize.jl
277 lines (237 loc) · 6.79 KB
/
serialize.jl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# This file is a part of Julia. License is MIT: http://julialang.org/license
using Base.Test
function create_serialization_stream(f::Function)
s = IOBuffer()
f(s)
close(s)
end
# Tags
create_serialization_stream() do s
Serializer.writetag(s, Bool)
@test takebuf_array(s)[end] == UInt8(Serializer.ser_tag[Bool])
end
create_serialization_stream() do s
Serializer.write_as_tag(s, Bool)
@test takebuf_array(s)[end] == UInt8(Serializer.ser_tag[Bool])
end
create_serialization_stream() do s
Serializer.write_as_tag(s, Symbol)
data = takebuf_array(s)
@test data[end-1] == 0x00
@test data[end] == UInt8(Serializer.ser_tag[Symbol])
end
# Boolean & Empty & Nothing
create_serialization_stream() do s
serialize(s, true)
serialize(s, ())
serialize(s, nothing)
seek(s, 0)
@test deserialize(s) === true
@test deserialize(s) === ()
@test deserialize(s) === nothing
end
# Ptr
create_serialization_stream() do s
@test_throws ErrorException serialize(s, C_NULL)
end
# Integer
create_serialization_stream() do s
serialize(s, 0x1)
serialize(s, 32)
serialize(s, 33)
seek(s,0)
@test deserialize(s) === 0x01
@test deserialize(s) === 32
@test deserialize(s) === 33
end
# Tuple
create_serialization_stream() do s
tpl = (0x1,'a')
serialize(s, tpl)
len = 257
lt = ntuple(len, i->0x1)
serialize(s, lt)
seek(s, 0)
@test deserialize(s) === tpl
@test deserialize(s) === lt
end
# Symbol
create_serialization_stream() do s
gensym(len) = symbol(repeat("A", len))
smbl = gensym(1)
serialize(s, smbl)
smbl2 = gensym(10)
serialize(s, smbl2)
smbl3 = gensym(257)
serialize(s, smbl3)
seek(s, 0)
@test deserialize(s) === smbl
@test deserialize(s) === smbl2
@test deserialize(s) === smbl3
end
# Module
create_serialization_stream() do s # user-defined module
mod = b"SomeModule"
modstring = bytestring(mod)
eval(parse("module $(modstring); end"))
modtype = eval(parse("$(modstring)"))
serialize(s, modtype)
seek(s, 0)
@test deserialize(s) === modtype
end
# DataType
create_serialization_stream() do s # standard types
typ1 = UInt8
serialize(s, typ1)
typ2 = Bool
serialize(s, typ2)
seek(s, 0)
@test deserialize(s) == typ1
@test deserialize(s) == typ2
end
create_serialization_stream() do s # user-defined type
usertype = "SerializeSomeType"
eval(parse("abstract $(usertype)"))
utype = eval(parse("$(usertype)"))
serialize(s, utype)
seek(s, 0)
@test deserialize(s) === utype
end
create_serialization_stream() do s # user-defined type
usertype = "SerializeSomeType1"
eval(parse("type $(usertype); end"))
utype = eval(parse("$(usertype)"))
serialize(s, utype)
seek(s, 0)
@test deserialize(s) === utype
end
create_serialization_stream() do s # user-defined type
usertype = "SerializeSomeType2"
eval(parse("abstract $(usertype){T}"))
utype = eval(parse("$(usertype)"))
serialize(s, utype)
seek(s, 0)
@test deserialize(s) === utype
end
create_serialization_stream() do s # immutable type with 1 field
usertype = "SerializeSomeType3"
eval(parse("immutable $(usertype){T}; a::T; end"))
utype = eval(parse("$(usertype)"))
serialize(s, utype)
seek(s, 0)
@test deserialize(s) === utype
end
create_serialization_stream() do s # immutable type with 2 field
usertype = "SerializeSomeType4"
eval(parse("immutable $(usertype){T}; a::T; b::T; end"))
utval = eval(parse("$(usertype)(1,2)"))
serialize(s, utval)
seek(s, 0)
@test deserialize(s) === utval
end
create_serialization_stream() do s # immutable type with 3 field
usertype = "SerializeSomeType5"
eval(parse("immutable $(usertype){T}; a::T; b::T; c::T; end"))
utval = eval(parse("$(usertype)(1,2,3)"))
serialize(s, utval)
seek(s, 0)
@test deserialize(s) === utval
end
create_serialization_stream() do s # immutable type with 4 field
usertype = "SerializeSomeType6"
eval(parse("immutable $(usertype){T}; a::T; b::T; c::T; d::T; end"))
utval = eval(parse("$(usertype)(1,2,3,4)"))
serialize(s, utval)
seek(s, 0)
@test deserialize(s) === utval
end
# Expression
create_serialization_stream() do s
expr = parse("a = 1")
serialize(s, expr)
expr2 = parse(repeat("a = 1;", 300))
serialize(s, expr2)
seek(s, 0)
@test deserialize(s) == expr
@test deserialize(s) == expr2
end
# Array
type TA1
v::UInt8
end
create_serialization_stream() do s # small 1d array
arr1 = fill(0x01, 10) # small 1d array
serialize(s, arr1)
arr2 = fill(0x01, 3, 3) # small 2d array
serialize(s, arr2)
arr3 = fill(0x0001, 50) # large 1d array
serialize(s, arr3)
arr4 = reshape([true, false, false, false, true, false, false, false, true], 3, 3)
serialize(s, arr4) # boolean array
arr5 = Array(TA1, 3)
arr5[2] = TA1(0x01)
serialize(s, arr5)
seek(s, 0)
@test deserialize(s) == arr1
@test deserialize(s) == arr2
@test deserialize(s) == arr3
@test deserialize(s) == arr4
result = deserialize(s)
@test !isdefined(result,1)
@test !isdefined(result,3)
@test result[2].v == arr5[2].v
end
# SubArray
create_serialization_stream() do s # slices
slc1 = slice(ones(UInt8, 4), 2:3)
serialize(s, slc1)
slc2 = slice(ones(UInt8, 4, 4) .+ [0x00, 0x01, 0x02, 0x03], 1, 2:4)
serialize(s, slc2)
seek(s, 0)
@test deserialize(s) == slc1
@test deserialize(s) == slc2
end
# Function
serialize_test_function() = 1
serialize_test_function2 = ()->1
create_serialization_stream() do s # Base generic function
serialize(s, sin)
serialize(s, typeof)
serialize(s, serialize_test_function)
serialize(s, serialize_test_function2)
seek(s, 0)
@test deserialize(s) === sin
@test deserialize(s) === typeof
@test deserialize(s) == serialize_test_function
@test deserialize(s).code.ast == Base.uncompressed_ast(serialize_test_function2.code)
end
# Task
create_serialization_stream() do s # user-defined type array
f = () -> begin task_local_storage(:v, 2); return 1+1 end
t = Task(f)
yieldto(t)
serialize(s, t)
seek(s, 0)
r = deserialize(s)
@test r.code.code.ast == Base.uncompressed_ast(f.code)
@test r.storage[:v] == 2
@test r.state == :done
@test r.exception == nothing
end
create_serialization_stream() do s # user-defined type array
t = Task(()->error("Test"))
@test_throws ErrorException yieldto(t)
serialize(s, t)
seek(s, 0)
r = deserialize(s)
@test r.state == :failed
@test isa(t.exception, ErrorException)
end
# corner case: undefined inside immutable type
create_serialization_stream() do s
serialize(s, Nullable{Any}())
seekstart(s)
n = deserialize(s)
@test isa(n, Nullable)
@test !isdefined(n, :value)
end