-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcorelibrary_test07.tea
More file actions
280 lines (226 loc) · 9.66 KB
/
Copy pathcorelibrary_test07.tea
File metadata and controls
280 lines (226 loc) · 9.66 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
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
#!/opt/TeaAgeSolutions/TeaScript/bin/TeaScript
/*
* SPDX-FileCopyrightText: Copyright (C) 2026 Florian Thake <contact@tea-age.solutions>. All rights reserved.
*/
// This is a follow up Unittest for the TeaScript Core Library written in TeaScript for versions >= 0.17
// This test focusses on the new Map type
// NOTE: assuming all C++ UnitTests and test01.tea + test02.tea + test03.tea passed 100%!
##minimum_version 0.17
// pre-check
if( not is_defined _core_config or (_core_config bit_and 0xf) < 8 /* LevelFull */ ) {
return "Tests must always run with CoreLibrary LevelFull."
}
// --- pre check done...
// helper construct for (optional feature) color printing:
// If cprint is not defined, we define it but just call print without color.
// HINT: You need to add the {fmt} lib to your project in order to have color and format printing.
// The pre-build TeaScript Host Application has this feature always enabled.
is_defined cprint or (func cprint( color, text )
{
print( text )
})
func print_failure( text )
{
// red color
cprint( make_rgb( 255, 0, 0 ), text )
}
func print_success( text )
{
// green color
cprint( make_rgb( 0, 255, 0 ), text )
}
func print_warning( text )
{
// pink color
cprint( make_rgb( 255, 0, 255 ), text )
}
// --- UnitTest function definitions
func TEST_EQ( val, expected, msg )
{
if( typeof val != typeof expected ) {
print_failure( "\nFAILED:\n" )
fail_with_message( "expected type %(typeof expected) but was %(typeof val). " % msg, _exit_failure )
}
def res := val == expected
if( not res ) {
print_failure( "\nFAILED:\n" )
fail_with_message( "expected %(expected) but was %(val). " % msg, _exit_failure )
}
}
func TEST_TRUE( c, msg )
{
// enforce a boolean!
TEST_EQ( not not c, true, msg )
}
func TEST_FALSE( c, msg )
{
// enforce a boolean!
TEST_EQ( not not c, false, msg )
}
// --- Start the test
_out( "Start testing TeaScript Core Library ...\n" )
// basics
print( "testing map basics... " )
{
def map := _map_create( (2, 7), (5, "Hello"), (9, (1,2,3) ) )
TEST_EQ( typeof map, Map, "wrong type!" )
TEST_EQ( _map_size( map ), 3, "wrong size!" )
TEST_FALSE( _map_contains( map, 1 ), "_map_contains (1) failed!" )
TEST_TRUE( _map_contains( map, 2 ), "_map_contains (2) failed!" )
// can make only positive tests, missing keys will throw!
TEST_EQ( map[2], 7, "wrong value for key 2!" )
TEST_EQ( map[5], "Hello", "wrong value for key 5!" )
TEST_EQ( map[9], (1,2,3), "wrong value for key 9!" )
map[2] := 23
TEST_EQ( map[2], 23, "wrong value after assign!" )
map[3] := 42
TEST_EQ( map[3], 42, "wrong value after insert!" )
TEST_EQ( _map_size( map ), 4, "wrong size after insert!" )
}
print_success( "ok.\n" )
// utility functions
print( "testing map utility functions... " )
{
def map := _map_create( (2, 7), (5, "Hello"), (9, (1,2,3) ) )
TEST_EQ( _map_at( map, 5 ), "Hello", "wrong value for key 5!" )
TEST_EQ( typeof _map_at( map, 6 ), Error, "no Error for key 6!" )
// insert
TEST_TRUE( _map_insert( map, 3, 8 ), "_map_insert failed!" )
TEST_EQ( _map_size( map ), 4, "wrong size after insert!" )
TEST_TRUE( _map_contains( map, 3 ), "_map_contains after insert failed!" )
TEST_EQ( _map_at( map, 3 ), 8, "wrong value for key 3 after insert!" )
TEST_FALSE( _map_insert( map, 3, 8 ), "_map_insert failed (negative test)!" )
TEST_EQ( _map_size( map ), 4, "wrong size after failed insert!" )
TEST_TRUE( _map_contains( map, 3 ), "_map_contains after failed insert failed!" )
TEST_EQ( typeof _map_insert( map, _buf(1), 1 ), Error, "no Error non comparable key insert attempt!" )
TEST_EQ( _map_size( map ), 4, "wrong size after failed insert!" )
// assign
TEST_TRUE( _map_assign( map, 2, 23 ), "_map_assign failed!" )
TEST_EQ( _map_size( map ), 4, "wrong size after assign!" )
TEST_TRUE( _map_contains( map, 2 ), "_map_contains after assign failed!" )
TEST_EQ( _map_at( map, 2 ), 23, "wrong value for key 2 after assign!" )
TEST_FALSE( _map_assign( map, 7, 23 ), "_map_assign failed (negative test)!" )
TEST_EQ( _map_size( map ), 4, "wrong size after failed assign!" )
TEST_FALSE( _map_contains( map, 7 ), "_map_contains after failed assign failed!" )
TEST_EQ( typeof _map_assign( map, _buf(1), 1 ), Error, "no Error non comparable key assign attempt!" )
TEST_EQ( _map_size( map ), 4, "wrong size after failed assign!" )
// insert or assign (true for insert, false for assign)
TEST_TRUE( _map_insert_or_assign( map, 7, 42 ), "_map_insert_or_assign failed (insert)!" )
TEST_EQ( _map_size( map ), 5, "wrong size after insert or assign!" )
TEST_TRUE( _map_contains( map, 7 ), "_map_contains after insert or assign failed!" )
TEST_EQ( _map_at( map, 7 ), 42, "wrong value for key 2 after insert or assign!" )
TEST_FALSE( _map_insert_or_assign( map, 7, 93 ), "_map_insert_or_assign failed (assign)!" ) // yes, FALSE for assign!
TEST_EQ( _map_size( map ), 5, "wrong size after insert or assign!" )
TEST_TRUE( _map_contains( map, 7 ), "_map_contains after insert or assign failed!" )
TEST_EQ( _map_at( map, 7 ), 93, "wrong value for key 2 after insert or assign!" )
TEST_EQ( typeof _map_insert_or_assign( map, _buf(1), 1 ), Error, "no Error non comparable key insert or assign attempt!" )
TEST_EQ( _map_size( map ), 5, "wrong size after failed insert or assign!" )
// remove
TEST_TRUE( _map_remove( map, 7 ), "_map_removed failed!" )
TEST_EQ( _map_size( map ), 4, "wrong size after remove!" )
TEST_FALSE( _map_contains( map, 7 ), "_map_contains after remove failed!" )
TEST_FALSE( _map_remove( map, 8 ), "_map_removed failed (negative test)!" )
TEST_EQ( _map_size( map ), 4, "wrong size after failed remove!" )
// keys
def keys := _map_keys( map )
TEST_EQ( typeof keys, Tuple, "wrong type for keys!" )
TEST_EQ( _tuple_size( keys ), 4, "wrong size for keys!" )
TEST_EQ( keys[0], 2, "wrong value key 0!" )
TEST_EQ( keys[1], 3, "wrong value key 1!" )
TEST_EQ( keys[2], 5, "wrong value key 2!" )
TEST_EQ( keys[3], 9, "wrong value key 3!" )
// values
def values := _map_values( map )
TEST_EQ( typeof values, Tuple, "wrong type for values!" )
TEST_EQ( _tuple_size( values ), 4, "wrong size for values!" )
TEST_EQ( values[0], 23, "wrong value 0!" )
TEST_EQ( values[1], 8, "wrong value 1!" )
TEST_EQ( values[2], "Hello", "wrong value 2!" )
TEST_EQ( values[3], (1,2,3), "wrong value 3!" )
// kv Tuple
def kv := _map_kv_tuple( map )
TEST_EQ( typeof kv, Tuple, "wrong type for values!" )
TEST_EQ( _tuple_size( kv ), 4, "wrong size for values!" )
TEST_EQ( kv[0], (2,23), "wrong kv value 0!" )
TEST_EQ( kv[1], (3,8), "wrong kv value 1!" )
TEST_EQ( kv[2], (5,"Hello"), "wrong kv value 2!" )
TEST_EQ( kv[3], (9, (1,2,3)), "wrong kv value 3!" )
}
print_success( "ok.\n" )
// multi key types
print( "testing map multi key types... " )
{
def map := _map_create( (2, 7), (5u8, "Hello"), (9u64, (1,2,3) ), ("17", _buf(4) ) )
TEST_EQ( _map_at( map, 2 ), 7, "wrong value!" )
TEST_EQ( _map_at( map, 5u8 ), "Hello", "wrong value!" )
TEST_EQ( _map_at( map, 9u64 ), (1,2,3), "wrong value!" )
TEST_EQ( _map_at( map, "17"), _buf(4), "wrong value!" )
def keys := _map_keys( map )
TEST_EQ( _tuple_size( keys ), 4, "wrong key count!" )
TEST_EQ( typeof keys[0], i64, "wrong type key 0!" )
TEST_EQ( typeof keys[1], u8, "wrong type key 1!" )
TEST_EQ( typeof keys[2], u64, "wrong type key 2!" )
TEST_EQ( typeof keys[3], String, "wrong type key 3!" )
}
print_success( "ok.\n" )
// forall loop + map iterator
print( "testing forall loop + map iterator... " )
{
def n := 0
def q := 0
def map := _map_create( (2, 7), (5, "Hello"), (9, (1,2,3) ) )
forall( it in map ) {
TEST_EQ( typeof it, Tuple, "wrong iterator type!" )
TEST_TRUE( is_defined it.key, "it.key not defined!" )
TEST_TRUE( is_defined it.idx, "it.idx not defined!" )
TEST_TRUE( is_defined it._all_keys, "it._all_keys not defined!" ) // Internal, may change!
TEST_TRUE( _map_contains( map, it.key ), "it.key does not exist!" )
TEST_EQ( it.idx, n, "unexpected idx!" )
if( n == 0 ) {
TEST_EQ( it.key, 2, "unexpected key for idx 0!" )
} else if( n == 1 ) {
TEST_EQ( it.key, 5, "unexpected key for idx 1!" )
} else if( n == 2 ) {
TEST_EQ( it.key, 9, "unexpected key for idx 2!" )
} else {
TEST_TRUE( false, "invalid n reached!!" )
}
n := n + 1
q := q + it.idx
}
TEST_EQ( q, 0+1+2, "unexpected sum!" )
n := 0
q := 0
// testing tuple forall explicitly as well because this was done only implicit before.
def tup := (2,4,6,8)
forall( idx in tup ) {
TEST_EQ( idx, n, "unexpected idx!" )
n := n + 1
q := q + idx
}
TEST_EQ( q, 0+1+2+3, "unexpected sum!" )
}
print_success( "ok.\n" )
// noop forall loop
print( "testing special case no-op forall loop... " )
{
def n := 0
forall( i in _seq(0,0,0) ) {
n := n + 1
}
TEST_EQ( n, 0, "forall loop body was executed with empty sequence!" )
n := 0
def tup := _tuple_create()
forall( i2 in tup ) {
n := n + 1
}
TEST_EQ( n, 0, "forall loop body was executed with empty tuple!" )
n := 0
def map := _map_create()
forall( i3 in map ) {
n := n + 1
}
TEST_EQ( n, 0, "forall loop body was executed with empty map!" )
}
print_success( "ok.\n" )
print_success( "=== TEST PASSED ===\n" )