-
Notifications
You must be signed in to change notification settings - Fork 6
/
runtests.jl
457 lines (393 loc) · 15.1 KB
/
runtests.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using SurrealNumbers
@static if VERSION < v"0.7.0-DEV.2005"
using Base.Test
else
using Test
end
@static if VERSION < v"0.7.0"
const devnull = DevNull
end
# recursive construction and one and zero
x0 = SurrealFinite("0", ϕ, ϕ)
x1 = SurrealFinite("1", [x0], ϕ)
x11 = SurrealFinite("-1", ϕ, [x0])
x111 = SurrealDyadic("-1", ϕ, [x0])
x4 = SurrealFinite( [x11], [x1,x0])
x5 = SurrealFinite( [x11,x0,x4], [x1])
x21 = SurrealFinite("2", [x1], ϕ)
x22 = SurrealFinite( [x0, x1], ϕ)
x23 = SurrealFinite( [x11,x1], ϕ)
x24 = SurrealFinite( [x11,x0,x1], ϕ)
x25 = [x11,x0,x1] ≀ ϕ
z = zero(x1)
z0 = zero(SurrealFinite)
o = one(x1)
o1 = one(SurrealFinite)
x00 = SurrealFinite([x11],[x1])
x3 = convert(SurrealFinite, 3)
x41 = convert(SurrealFinite, 4)
x42 = SurrealFinite( [x3], ϕ)
x43 = convert(SurrealFinite, 2) * convert(SurrealFinite, 2)
x44 = SurrealFinite(4)
x6 = convert(SurrealFinite, 2) * convert(SurrealFinite, 3)
print("x1 = ")
pf(x1)
println()
println(" z = ", z)
println("x4 = ", x4)
println("x5 = ", x5)
s1 = convert(SurrealFinite, 1//2)
s2 = convert(SurrealFinite, 3//4)
s2a = convert(SurrealShort, 3//4)
s2b = convert(SurrealDyadic, 3//4)
print("s1 = ")
pf(s1)
println()
print("s2 = ")
pf(s2)
println()
println("x41 = ", x41, ", x42 = ", x42, ", x43 = ", x43)
pf(x43)
println()
spf(x43)
println()
@testset "constructor test" begin
@test SurrealFinite("1/2", [0], [1]) ≅ 1/2
@test_throws ErrorException SurrealFinite("1", [x1], [x0])
@test_throws DomainError convert(SurrealFinite, NaN )
@test_throws ErrorException SurrealFinite(1//3)
@test_throws ErrorException SurrealFinite(100000 + 1//2)
@test_throws ErrorException SurrealFinite(100000.000012)
@test convert(SurrealFinite, 1.3 ) == 5854679515581645//4503599627370496
@test all( convert(Array{SurrealFinite}, [1,2]) .== convert.(SurrealFinite, [1,2]) )
@test z === zero(SurrealFinite)
@test z0 === zero(SurrealFinite)
@test o === one(SurrealFinite)
@test o1 === one(SurrealFinite)
@test s2a === s2
@test s2b === s2
@test x11 == x111
@test dali(0) === SurrealZero
@test dali(1) === SurrealOne
@test dali(-1) == SurrealMinusOne
@test dali(2) == SurrealTwo
end
@testset "comparisons" begin
@test x0 <= x1
@test x0 < x1
@test x0 <= x0
@test x1 >= x11
@test x11 < x0
@test !( x11 ≅ x1 )
@test x21 ≅ x22 ≅ x23 ≅ x24 # should be an equivalence class
@test x41 ≅ x42 ≅ x43
@test x41 == x42
@test x44 == x41
@test x41 != x43
@test x0 ≺ x5
@test x4 ≺ x5
@test x5 ≻ x4
@test !(x5 ≺ x4)
@test !(x4 ≺ x4)
@test x0 ⪯ x5
@test x5 ⪯ x5
@test x41 ⪰ x3
@test s1 ⪰ s1
@test x1 ⪯ s2
@test x21 ≺ x43
@test isancestor(x21, x43)
@test all( parents(x24) .== [x11,x0,x1] )
@test all( parents(x5) .== sort([x11,x0,x4,x1]) )
end
@testset "basic operators" begin
@test -x1 == x11
@test - -x0 == x0
@test - -x4 == x4
# @test - -x4 === x4 # still flaky for version 0.7
@test x1 + x11 ≅ x0
@test x1 - x1 ≅ x0
@test x1 + x0 == x1
@test x1*x1 ≅ x1
@test x0*x1 ≅ x0
@test x4*x0 ≅ x0 # this case broke things before
@test x11/one(x11) ≅ -x1
@test x22 / x22 ≅ one(x22)
@test x22/x1 ≅ x22
@test x4/x1 ≅ x4
@test float(x1/x4) == float(x1)/float(x4)
@test_throws ErrorException x1/x0
@test_throws ErrorException x1/x3
@test convert(SurrealFinite, 6)/ convert(SurrealFinite, 3) ≅ convert(SurrealFinite, 2)
end
@testset "convert to surreal" begin
@test convert(SurrealFinite,2)*convert(SurrealFinite,2) ≅ convert(SurrealFinite,4)
@test convert(SurrealFinite,2)*convert(SurrealFinite,-3) ≅ convert(SurrealFinite,-6)
@test convert(SurrealFinite,2)*x0 ≅ x0
@test convert(SurrealFinite,2)*x1 ≅ convert(SurrealFinite,2)
@test x4 ≅ -s1
@test convert(SurrealFinite, 0.5) == convert(SurrealFinite, 1//2 )
@test convert(SurrealFinite, 1.000000000001) == 1.000000000001
# tests promotion
# float(562949953421875//562949953421312)
# f = 1122342342.23422522
# convert(SurrealFinite, 1122342342.23422522)
@test convert(SurrealFinite, 0.5) == dali(0.5)
end
A = [x0, x1]
B = [x11, x4]
X = [ x0, x1, x1, x11, x1, x11, x23]
Y = copy(X)
unique2!(Y)
@testset "array operations" begin
@test all( sort(X) .≅ convert.(SurrealFinite, [-1, -1, 0, 1, 1, 1, 2] ))
@test all( unique(X) .≅ convert.(SurrealFinite, [0, 1, -1, 2] ))
# @test all( Y .≅ convert.(SurrealFinite, [-1, 0, 1, 2] ))
@test all( sort(float.(A + B)) .== [-1.0, -0.5, 0.0, 0.5])
@test all( sort(float.(A - B)) .== [0.5, 1.0, 1.5, 2.0])
@test A + ϕ == ϕ
@test ϕ - B == ϕ
@test ϕ <= A # can see from this that <= is not a proper ordering on 'sets' of surreals
@test A <= ϕ # its just a convenience comparison operator
@test ϕ < A
@test A < ϕ
@test B <= A
@test B < A
@test !(B > A)
@test !(B >= A)
@test all( x1*A .≅ A )
@test all( x23*A .≅ A*x23 )
@test hash(A) == 0x54b75ebf9d6abfa4
end
V = [-6.25, -4.0, -3.25, -2.5, -2.0, -1.625, -1.0, -0.25, 0.0, 1.0, 7.0, 0.5, 1.625, 2.25, 4.5, 6.75, 8.0, 12.125]
@testset "simple functions" begin
@test sign(x0) == x0
@test sign(x1) == x1
@test sign(x4) == -one(x4)
@test sign(-x4) == one(x4)
@test round(x22) ≅ x22
@test round(x23) ≅ x23
@test round(x4) ≇ x4
@test round(-x4) ≇ -x4
@test round( convert(SurrealFinite, -0.75) ) ≅ round( -0.75 )
@test round( convert(SurrealFinite, -1.25) ) ≅ round( -1.25 )
@test floor(zero(SurrealFinite)) ≅ 0.0
@test floor(one(SurrealFinite)) ≅ 1.0
@test floor(x22) ≅ x22
@test floor(x4) ≅ -one(x4)
@test floor(x41) ≅ 4
@test floor(x4) ≇ x4
@test floor(-x4) ≇ -x4
@test floor(-x4) == zero(x4)
for v in V
println("v = $v")
@test floor( convert(SurrealFinite, v) ) ≅ floor( v )
@test floor( Int, convert(SurrealFinite, v) ) == floor( v )
@test ceil( convert(SurrealFinite, v) ) ≅ ceil( v )
@test round( convert(SurrealFinite, v) ) ≅ round( v, RoundNearestTiesUp )
@test trunc( convert(SurrealFinite, v) ) ≅ trunc( v )
@test isinteger( convert(SurrealFinite, v) ) == isinteger( v )
end
@test_throws ErrorException floor( convert(SurrealFinite, 10000) )
@test_throws ErrorException floor( convert(SurrealFinite, -10000) )
# @test floor2(zero(SurrealFinite)) ≅ 0.0
# @test floor2(one(SurrealFinite)) ≅ 1.0
# @test floor2(x22) ≅ x22
@test ceil(zero(SurrealFinite)) ≅ 0.0
@test ceil(one(SurrealFinite)) ≅ 1.0
@test ceil(x22) ≅ x22
@test ceil(x4) ≅ zero(x4)
@test ceil(x4) ≇ x4
@test ceil(-x4) ≇ -x4
@test ceil(-x4) == one(x4)
@test mod(zero(SurrealFinite), x21) == 0
@test mod(one(SurrealFinite), x21) == 1
@test mod(x22, x21) ≅ 0.0
@test mod(x41, x21) ≅ zero(x4)
@test mod(x3, x43) ≅ 3
@test mod(-x43, x3) ≅ 2
@test mod(s1 + 2, x21) ≅ s1
@test_throws ErrorException mod( x3, s2 )
@test isinteger(x0) == true
@test isinteger(x22) == true
@test isinteger(s1) == false
@test isinteger(x43) == true
@test isinteger(x41) == true
@test isinteger( convert(SurrealFinite,-4) ) == true
@test isinteger( convert(SurrealFinite,-1//4) ) == false
@test iseven(x0) == true
@test iseven(x22) == true
@test iseven(s1) == false
@test iseven(x3) == false
@test isodd(s1) == false
@test isodd(x3) == true
@test iseven( convert(SurrealFinite,-4) ) == true
@test iseven( convert(SurrealFinite,-1) ) == false
@test isinf(s1) == false
@test isnan(s1) == false
@test isfinite(s1)
end
@testset "simplifications" begin
@test canonicalise(x22) ≅ x22
@test canonicalise(x43) ≅ convert(SurrealFinite,4)
@test canonicalise(x43) != convert(SurrealFinite,2)*convert(SurrealFinite,2)
@test canonicalise(x43) ≅ convert(SurrealFinite,2)*convert(SurrealFinite,2)
@test !iscanonical(x22)
@test iscanonical(x21)
end
g3 = SurrealFinite( [ 7//16 ], [ 15//16 ] )
g4 = SurrealFinite( [convert(SurrealFinite,3)], [convert(SurrealFinite,17)] )
# g4 ≅ convert(SurrealFinite,4)
@testset "conversion from surreal back to real" begin
@test convert(Int64, convert(SurrealFinite, -2)) == -2
@test convert(Int32, convert(SurrealFinite, 6)) == 6
@test convert(Int, convert(SurrealFinite, 0)) == 0
# @test Int64( convert(SurrealFinite, 0) ) == 0
@test convert(Int64, convert(SurrealFinite, -2)) == -2
@test convert(UInt32, convert(SurrealFinite, 6)) == 6
@test convert(Int8, convert(SurrealFinite, 0)) == 0
@test_throws InexactError convert(UInt8, convert(SurrealFinite, -1))
@test convert(Rational, convert(SurrealFinite, 1//8)) == 1//8
@test convert(Rational, convert(SurrealFinite, 9//8)) == 9//8
@test convert(Rational, convert(SurrealFinite, -9//8)) == -9//8
@test convert(Rational, convert(SurrealFinite, -27//16) ) == -27//16
@test convert(Rational, x4) == -1//2
@test convert(Rational, convert(SurrealFinite,2)*convert(SurrealFinite,2) ) == 4
@test g3 ≅ convert(SurrealFinite, 0.5)
@test convert(Rational, SurrealFinite( [ 7//16 ], [ 15//16 ] ) ) == 1//2
@test convert(Rational{Int32}, SurrealFinite( [ 7//16 ], [ 15//16 ] ) ) == 1//2
@test convert(Float64, x43 ) == 4.0
@test convert(Float64, x4) == -0.5
@test convert(AbstractFloat, x43 ) == 4.0
@test convert(AbstractFloat, x4) == -0.5
@test float( x4 ) == -0.5
@test float( s2 ) == 0.75
@test float( g4 ) == 4.0
@test all(convert.(SurrealFinite, [-1, 0, 1, 2] ) == [ convert(SurrealFinite, i) for i=-1:2 ])
@test convert(String, x1) == "1"
@test convert(String, x4) == "-1/2"
end
@testset "promotion" begin
# lots of this has already been implicitly tested
# note promoted type will be in canonical form, but other side might not
@test x0 <= 1.0
@test x1 == 1//1
@test 1.0 + x1 == 2.0
@test canonicalise(1//2 + x1) == 1.5
@test 1//2 + x1 ≅ 1.5
@test 1//2 + x1 ≇ 2.5
end
out_dir = "Data/"
a4 = convert(SurrealFinite, 13//16)
a5 = convert(SurrealFinite, -2//16)
s = "{ {{{{{|0}|{0|}}|{{0|}|}}|{{{0|}|}|}}|{{{{0|}|}|}|}} | }" # from http://goodmath.scientopia.org/2006/08/21/arithmetic-with-surreal-numbers/
ss = convert(SurrealFinite, s)
@testset "I/O and string parsing" begin
@test_throws Meta.ParseError convert(SurrealFinite, "{1//2| \\phi, 1}")
@test convert(SurrealFinite, "{1//2 | 2, 1}") == SurrealFinite( [1//2], [2,1] )
@test convert(SurrealFinite, raw"{-1//2 | {|\phi}}") == SurrealFinite( [-1//2], [zero(SurrealFinite)] )
@test convert(SurrealFinite, "{-1//2 | 2.0, 3}") == SurrealFinite( [-1//2], [2,3] )
R = read("$(out_dir)test_surreals.dat", SurrealFinite, 4)
@test all( float(R[1:3]) .== [0.0, -1.0, 0.0] )
@test_throws UndefRefError R[4]
io_test_file = "$(out_dir)test_surreals_io.dat"
io = open(io_test_file, "w")
pf(io, a4)
pf(io, a5)
println(io, expand(a4))
println(io, expand(a5))
close(io)
YY = read(io_test_file, SurrealFinite, 2)
@test YY[1] == a4
@test YY[2] == a5
io_test_file = "$(out_dir)test_surreals_io.tex"
io = open(io_test_file, "w")
surreal2tex(io, a4; level = 0)
surreal2tex(io, a4; level = 1)
surreal2tex(io, a4; level = 2)
surreal2tex(a4; level = 2)
close(io)
# should compare this to a calibration file
# but not sure if this might introduce potential for system dependencies that aren't real errors
@test ss ≅ 4.0
@test ss == convert(SurrealShort, 2.0) * convert(SurrealDyadic, 2.0)
end
@testset "structured output" begin
@test surreal2dot(devnull, x0) == 1
@test surreal2dot(x0) == 1
@test surreal2dot(devnull, x23) == 5
@test surreal2dot(devnull, x43) == tree_nodes(x43)
@test surreal2dag(devnull, x0) == 1
@test surreal2dag(x0) == 1
@test surreal2dag(devnull, x23) == 4
@test surreal2dag(devnull, x43) == nodes(x43)
@test surreal2dag(devnull, -x43) == nodes(x43)
@test surreal2dag(devnull, -s2) == nodes(s2)
# should compare these against a reference, but sort could get non-unique order, so potential for difference
end
x32 = convert(SurrealFinite, 3/4) + convert(SurrealFinite, 3/4)
@testset "DAG statistics" begin
@test dag_stats(x1) == SurrealDAGstats(2,2,1,1,ϕ,1,1,0,1)
@test dag_stats(x41) == SurrealDAGstats(5,5,4,4,ϕ,1,4,0,4)
@test dag_stats(x43) == SurrealDAGstats(11,21,14,6,ϕ,5,4,-1,4)
@test dag_stats(x00) == SurrealDAGstats(4,5,4,2,ϕ,2,0,-1,1)
@test dag_stats(x5) == SurrealDAGstats(5,12,9,3,ϕ,6,1//2,-1,1)
@test dag_stats(s2) == SurrealDAGstats(4,7,5,3,ϕ,3,3//4,0,1)
@test dag_stats(x6) == SurrealDAGstats(45,2574,82,12,ϕ,625,6,-3,7)
@test dag_stats(x1, Dict{SurrealFinite,SurrealDAGstats}())[2] == Dict(x0=>SurrealDAGstats(1,1,0,0,ϕ,1,0,0,0),
x1=>SurrealDAGstats(2,2,1,1,ϕ,1,1,0,1) )
@test dag_stats(x43).nodes == 11
@test dag_stats(x43).nodes == nodes(x43)
@test dag_stats(x43).tree_nodes == 21
@test dag_stats(x43).tree_nodes == tree_nodes(x43)
@test dag_stats(x43).edges == 14
@test dag_stats(x43).edges == edges(x43)
@test dag_stats(x43).generation == 6
@test dag_stats(x43).paths == 5
@test dag_stats(x43).value == 4
@test dag_stats(x43).minval == -1
@test dag_stats(x43).maxval == 4
@test generation(x0) == 0
@test generation(x1) == 1
@test generation(x21) == 2
@test generation(x22) == 2
@test generation(x4) == 2
@test generation(x5) == 3
@test generation( x43 ) == 6
@test generation( convert(SurrealFinite,4) ) == 4
@test nodes(x0) == 1
@test nodes(x1) == 2
@test nodes(x41) == 5
@test nodes(x43) == 11
@test nodes(x5) == 5
@test nodes(s2) == 4
@test breadth(x1) == 1
@test breadth(x43) == 5
@test paths(x43) == 5
@test dag_stats(x32).generation == 6
@test dag_stats(x32).generation == generation(x32)
# @test dag_stats(x41; LP=true) == SurrealDAGstats(5, 4, 4, [0, 1, 2, 3, 4], 1,4,0,4)
@test dag_stats(x41; LP=true).longest_path == convert.(SurrealFinite, [0, 1, 2, 3, 4])
end
d33 = dali(33//2)
@testset "memory allocation and caching" begin
# @test Base.summarysize( ) == 943
# 1775 was the old size before we made "dali" reuse cached numbers
# 1743 comes up in v0.7, instead of the number above, not sure why versions are so different,
# but if it is implementation dependent it shouldn't be a test
# depends on previous results -- could be changed by earlier operations
@test ExistingCanonicals[-1//2] == -1//2
@test ExistingProducts[0x013b60c22ac142fa][0x84037d1bb0afff04] == -1
clearcache()
@test isempty(ExistingSurreals)
@test isempty(ExistingCanonicals)
@test isempty(ExistingProducts)
@test isempty(ExistingSums)
@test isempty(ExistingNegations)
Count['+'] == 0
Count['*'] == 0
m = dali(2) * dali(2)
Count['+'] == 4
Count['+'] == 13
Count['*'] == 6
Count['c'] == 20
end