rev: ebafc720a90fa0562f276f82113a43e8e7c3ee88 scopes/testing/test_mutarray.sc -rw-r--r-- 5.9 KiB View raw Log this file
ebafc720a90f — Leonard Ritter * merged default branch 8 days ago
                                                                                
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
using import Array
using import testing

let TESTSIZE = (1:usize << 16:usize)

let i32Arrayx65536 = (Array i32 TESTSIZE)
let i32Arrayx65536_2 = (Array i32 TESTSIZE)
static-assert (i32Arrayx65536 == i32Arrayx65536_2)
static-assert (i32Arrayx65536 < FixedArray)
let i32Array = (Array i32)
let i32Array2 = (Array i32)
static-assert (i32Array == i32Array2)
static-assert (i32Array < GrowingArray)
let i32Arrayx16 = (Array i32 16)
let i32Arrayx32 = (Array i32 32)

let i32ArrayArray = (Array i32Array)
let i32Arrayx16Array = (Array i32Arrayx16)
let i32ArrayArrayx16 = (Array i32Array 16)
let i32Arrayx16Arrayx16 = (Array i32Arrayx16 16)

let StringArray = (Array string)

let fullrange = (range TESTSIZE)

do
    # mutable array with fixed upper capacity
    local a : i32Arrayx65536
    report a
    assert (('capacity a) == TESTSIZE)
    for i in fullrange
        assert ((countof a) == i)
        'append a (i32 i)
    for i in fullrange
        assert ((a @ i) == (i32 i))
    # generator support
    for i k in (enumerate a)
        assert ((a @ i) == i)

inline test-array-of-array (Tx Ty)
    do
        dump "test-array-of-array" Tx Ty
        report "test-array-of-array" Tx Ty
        # array of array
        let i32Array = Tx
        let i32ArrayArray = Ty
        local a : i32ArrayArray
        for x in (range 16)
            let b = ('emplace-append a)
            assert ((countof b) == 0) (repr (countof b))
            for y in (range 16)
                'append b (x * 16 + y)
        assert ((countof a) == 16)
        report a
        for x b in (enumerate a)
            report b
            assert ((countof b) == 16)
            for y n in (enumerate b)
                assert ((x * 16 + y) == n)
    report "done"

test-array-of-array i32Arrayx16 i32Arrayx16Array
test-array-of-array i32Arrayx16 i32Arrayx16Arrayx16
test-array-of-array i32Array i32ArrayArrayx16
test-array-of-array i32Array i32ArrayArray

do
    # mutable array with dynamic capacity
    local a : i32Array
        capacity = 12
    report a
    assert (('capacity a) >= 12)
    for i in fullrange
        assert ((countof a) == i)
        'append a (i32 i)
    assert (('capacity a) >= TESTSIZE)
    for i in fullrange
        assert ((a @ i) == (i32 i))
    # generator support
    for i k in (enumerate a)
        assert ((a @ i) == i)


inline test-sort-array (T)
    dump "testing sorting" T

    let sequence... = 3 1 9 5 0 7 12 3 99 -20
    let sorted-sequence... = -20 0 1 3 3 5 7 9 12 99
    let reverse-sorted-sequence... = 99 12 9 7 5 3 3 1 0 -20

    # sorting a fixed mutable array
    local a : T
    va-lfold none
        inline (key k)
            'append a k
        sequence...

    inline verify-element (i key k)
        assert ((a @ i) == k)

    va-lifold none verify-element sequence...

    'sort a
    va-lifold none verify-element sorted-sequence...

    # custom sorting key
    'sort a (inline (x) (- x))
    va-lifold none verify-element reverse-sorted-sequence...

    print "POINTER" (imply a pointer)
    print "POINTER" (imply a voidstar)
    print "POINTER" (imply a (pointer i32))

    ;

do
    test-sort-array i32Arrayx32
    test-sort-array i32Array

dump "sorting a bunch of values"

do
    let sequence... = "yes" "this" "is" "dog" ""
    let sorted-sequence... = "" "dog" "is" "this" "yes"

    local a : StringArray
    va-lfold none
        inline (key k)
            'append a k
        sequence...
    assert ((countof a) == 5)
    inline verify-element (i key k)
        assert ((a @ i) == k)
    va-lifold none verify-element sequence...
    'sort a
    va-lifold none verify-element sorted-sequence...

dump "sorting big array"
report "big sort"

fn test-sort ()
    local a : i32Array
    let N = 1000000
    for i in (range N)
        'append a
            if ((i % 2) == 0)
                i
            else
                N - i
    report "sorting big array..."
    'sort a
    report "done."
    # verify the array is sorted
    local x = (a @ 0)
    for k in a
        let x1 = k
        assert (x1 >= x)
        x = x1

test-sort;

fn test-one ()
    One.test-refcount-balanced;

    local a : (Array One)
    let N = 1000
    for i in (range N)
        'append a
            if ((i % 2) == 0)
                One i
            else
                One (N - i)
    report "sorting array of ones..."
    'sort a
    report "done."
    # verify the array is sorted
    local x = ('value (a @ 0))
    for k in a
        let x1 = ('value k)
        test (x1 >= x)
        x = x1
    ;

# handling of unique elements
test-one;
One.test-refcount-balanced;

# removal of elements
fn test-remove ()
    One.test-refcount-balanced;

    local a : (Array One)
    'insert a (One 0)
    'insert a (One 1)
    'insert a (One 2)
    'insert a (One 3)
    'insert a (One 4)
    'insert a (One 5)
    test ((countof a) == 6)
    let q = ('pop a)
    test (('value q) == 5)
    test ((countof a) == 5)
    test (('value (a @ 0)) == 0)
    test (('value (a @ 1)) == 1)
    test (('value (a @ 2)) == 2)
    test (('value (a @ 3)) == 3)
    test (('value (a @ 4)) == 4)
    'remove a 2
    test ((countof a) == 4)
    test (('value (a @ 0)) == 0)
    test (('value (a @ 1)) == 1)
    test (('value (a @ 2)) == 3)
    test (('value (a @ 3)) == 4)
    'insert a (One 6) 2
    test ((countof a) == 5)
    test (('value (a @ 0)) == 0)
    test (('value (a @ 1)) == 1)
    test (('value (a @ 2)) == 6)
    test (('value (a @ 3)) == 3)
    test (('value (a @ 4)) == 4)
    'insert a (One 7) 0
    test ((countof a) == 6)
    test (('value (a @ 0)) == 7)
    test (('value (a @ 1)) == 0)
    test (('value (a @ 2)) == 1)
    test (('value (a @ 3)) == 6)
    test (('value (a @ 4)) == 3)
    test (('value (a @ 5)) == 4)
    ;

test-remove;
One.test-refcount-balanced;