|
1 | | -using ShortStrings |
2 | | -using BitIntegers: UInt256, UInt512, UInt1024, @define_integers |
3 | | -using Test, Random |
4 | | - |
5 | | -include("getindex.jl") |
6 | | - |
7 | | -function basic_test(constructor, max_len) |
8 | | - @testset "$constructor" begin |
9 | | - for string_type in (String, SubString{String}) |
10 | | - @testset "$string_type" begin |
11 | | - basic_test(string_type, constructor, max_len) |
12 | | - end |
13 | | - end |
14 | | - end |
15 | | -end |
16 | | - |
17 | | -function basic_test(string_type, constructor, max_len) |
18 | | - r = string_type.(randstring.(1:max_len)) |
19 | | - @test all(constructor.(r) .== r) |
20 | | - @test all(hash(constructor.(r)) .== hash(r)) |
21 | | - a = constructor.(r) |
22 | | - @test fsort(a) |> issorted |
23 | | - |
24 | | - @test collect(constructor("z"^max_len)) == fill('z', max_len) |
25 | | - @test_throws ErrorException constructor("a"^(max_len+1)) |
26 | | - |
27 | | - # equality |
28 | | - @test constructor("c"^max_len) == "c"^max_len |
29 | | - @test "c"^max_len == constructor("c"^max_len) |
30 | | - @test constructor("c"^max_len) == constructor("c"^max_len) |
31 | | - @test constructor("c"^max_len) != constructor("d"^max_len) |
32 | | - @test constructor("c"^max_len) != constructor("c"^(max_len-1)) |
33 | | - @test constructor("c"^(max_len-1)) != constructor("c"^max_len) |
34 | | - @test constructor("c"^max_len) != "c"^(max_len-1) |
35 | | - @test constructor("c"^(max_len-1)) != "c"^max_len |
36 | | -end |
37 | | - |
38 | | - |
39 | | -basic_test(ShortString3, 3) |
40 | | -basic_test(ShortString7, 7) |
41 | | -basic_test(ShortString15, 15) |
42 | | -basic_test(ShortString30, 30) |
43 | | -basic_test(ShortString62, 62) |
44 | | -basic_test(ShortString126, 126) |
45 | | - |
46 | | -basic_test(ShortString{UInt16}, 1) |
47 | | -basic_test(ShortString{UInt32}, 3) |
48 | | -basic_test(ShortString{UInt64}, 7) |
49 | | -basic_test(ShortString{UInt128}, 15) |
50 | | -basic_test(ShortString{UInt256}, 30) |
51 | | -basic_test(ShortString{UInt512}, 62) |
52 | | -basic_test(ShortString{UInt1024}, 126) |
53 | | - |
54 | | -@define_integers 2048 MyInt2048 MyUInt2048 |
55 | | -basic_test(ShortString{MyUInt2048}, 254) |
56 | | - |
57 | | -@test ss126"Be honest, do you actually need a string longer than this. Seriously. C'mon this is pretty long." === ShortString126("Be honest, do you actually need a string longer than this. Seriously. C'mon this is pretty long.") |
58 | | -@test ss62"Basically a failly long string really" === ShortString62("Basically a failly long string really") |
59 | | -@test ss30"A Longer String!!!" === ShortString30("A Longer String!!!") |
60 | | - |
61 | | -@test ss15"Short String!!!" === ShortString15("Short String!!!") |
62 | | -@test ss7"ShrtStr" === ShortString7("ShrtStr") |
63 | | -@test ss3"ss3" === ShortString3("ss3") |
64 | | - |
65 | | - |
66 | | -@testset "equality of different sized ShortStrings" begin |
67 | | - @test ShortString15("ab") == ShortString3("ab") |
68 | | - @test ShortString3("ab") == ShortString15("ab") |
69 | | - |
70 | | - @test ShortString30("x") != ShortString3("y") |
71 | | - @test ShortString30("y") != ShortString3("x") |
72 | | - |
73 | | - # this one is too big to fit in the other |
74 | | - @test ShortString15("abcd") != ShortString3("ab") |
75 | | - @test ShortString3("ab") != ShortString15("abcd") |
76 | | -end |
| 1 | +using ShortStrings |
| 2 | +using BitIntegers: UInt256, UInt512, UInt1024, @define_integers |
| 3 | +using Test, Random |
| 4 | + |
| 5 | +include("getindex.jl") |
| 6 | + |
| 7 | +function basic_test(constructor, max_len) |
| 8 | + @testset "$constructor" begin |
| 9 | + for string_type in (String, SubString{String}) |
| 10 | + @testset "$string_type" begin |
| 11 | + basic_test(string_type, constructor, max_len) |
| 12 | + end |
| 13 | + end |
| 14 | + end |
| 15 | +end |
| 16 | + |
| 17 | +function basic_test(string_type, constructor, max_len) |
| 18 | + r = string_type.(randstring.(1:max_len)) |
| 19 | + @test all(constructor.(r) .== r) |
| 20 | + @test all(hash(constructor.(r)) .== hash(r)) |
| 21 | + a = constructor.(r) |
| 22 | + @test fsort(a) |> issorted |
| 23 | + |
| 24 | + @test collect(constructor("z"^max_len)) == fill('z', max_len) |
| 25 | + @test_throws ErrorException constructor("a"^(max_len+1)) |
| 26 | + |
| 27 | + # equality |
| 28 | + @test constructor("c"^max_len) == "c"^max_len |
| 29 | + @test "c"^max_len == constructor("c"^max_len) |
| 30 | + @test constructor("c"^max_len) == constructor("c"^max_len) |
| 31 | + @test constructor("c"^max_len) != constructor("d"^max_len) |
| 32 | + @test constructor("c"^max_len) != constructor("c"^(max_len-1)) |
| 33 | + @test constructor("c"^(max_len-1)) != constructor("c"^max_len) |
| 34 | + @test constructor("c"^max_len) != "c"^(max_len-1) |
| 35 | + @test constructor("c"^(max_len-1)) != "c"^max_len |
| 36 | +end |
| 37 | + |
| 38 | + |
| 39 | +basic_test(ShortString3, 3) |
| 40 | +basic_test(ShortString7, 7) |
| 41 | +basic_test(ShortString15, 15) |
| 42 | +basic_test(ShortString30, 30) |
| 43 | +basic_test(ShortString62, 62) |
| 44 | +basic_test(ShortString126, 126) |
| 45 | + |
| 46 | +basic_test(ShortString{UInt16}, 1) |
| 47 | +basic_test(ShortString{UInt32}, 3) |
| 48 | +basic_test(ShortString{UInt64}, 7) |
| 49 | +basic_test(ShortString{UInt128}, 15) |
| 50 | +basic_test(ShortString{UInt256}, 30) |
| 51 | +basic_test(ShortString{UInt512}, 62) |
| 52 | +basic_test(ShortString{UInt1024}, 126) |
| 53 | + |
| 54 | +@define_integers 2048 MyInt2048 MyUInt2048 |
| 55 | +basic_test(ShortString{MyUInt2048}, 254) |
| 56 | + |
| 57 | +@test ss126"Be honest, do you actually need a string longer than this. Seriously. C'mon this is pretty long." === ShortString126("Be honest, do you actually need a string longer than this. Seriously. C'mon this is pretty long.") |
| 58 | +@test ss62"Basically a failly long string really" === ShortString62("Basically a failly long string really") |
| 59 | +@test ss30"A Longer String!!!" === ShortString30("A Longer String!!!") |
| 60 | + |
| 61 | +@test ss15"Short String!!!" === ShortString15("Short String!!!") |
| 62 | +@test ss7"ShrtStr" === ShortString7("ShrtStr") |
| 63 | +@test ss3"ss3" === ShortString3("ss3") |
| 64 | + |
| 65 | + |
| 66 | +@testset "equality of different sized ShortStrings" begin |
| 67 | + @test ShortString15("ab") == ShortString3("ab") |
| 68 | + @test ShortString3("ab") == ShortString15("ab") |
| 69 | + |
| 70 | + @test ShortString30("x") != ShortString3("y") |
| 71 | + @test ShortString30("y") != ShortString3("x") |
| 72 | + |
| 73 | + # this one is too big to fit in the other |
| 74 | + @test ShortString15("abcd") != ShortString3("ab") |
| 75 | + @test ShortString3("ab") != ShortString15("abcd") |
| 76 | +end |
| 77 | + |
| 78 | +@testset "cmp" begin |
| 79 | + @test cmp(ShortString3("abc"), ShortString3("abc")) == 0 |
| 80 | + @test cmp(ShortString3("ab"), ShortString3("abc")) == -1 |
| 81 | + @test cmp(ShortString3("abc"), ShortString3("ab")) == 1 |
| 82 | + @test cmp(ShortString3("ab"), ShortString3("ac")) == -1 |
| 83 | + @test cmp(ShortString3("ac"), ShortString3("ab")) == 1 |
| 84 | + @test cmp(ShortString3("α"), ShortString3("a")) == 1 |
| 85 | + @test cmp(ShortString3("b"), ShortString3("β")) == -1 |
| 86 | + |
| 87 | + @test cmp(ShortString3("abc"), "abc") == 0 |
| 88 | + @test cmp(ShortString3("ab"), "abc") == -1 |
| 89 | + @test cmp(ShortString3("abc"), "ab") == 1 |
| 90 | + @test cmp(ShortString3("ab"), "ac") == -1 |
| 91 | + @test cmp(ShortString3("ac"), "ab") == 1 |
| 92 | + @test cmp(ShortString3("α"), "a") == 1 |
| 93 | + @test cmp(ShortString3("b"), "β") == -1 |
| 94 | +end |
| 95 | + |
| 96 | +@testset "Construction from other ShortStrings" begin |
| 97 | + @test ShortString7(ShortString3("ab")) == "ab" |
| 98 | + @test ShortString7(ShortString3("ab")) isa ShortString7 |
| 99 | + |
| 100 | + @test ShortString3(ShortString7("ab")) == "ab" |
| 101 | + @test ShortString3(ShortString7("ab")) isa ShortString3 |
| 102 | + |
| 103 | + @test ShortString7(ShortString7("ab")) == "ab" |
| 104 | + @test ShortString7(ShortString7("ab")) isa ShortString7 |
| 105 | + |
| 106 | + @test_throws ErrorException ShortString3(ShortString7("123456")) |
| 107 | +end |
0 commit comments