]> gitweb.factorcode.org Git - factor.git/blob - extra/bare/bare-tests.factor
Revert "Fixes #2966"
[factor.git] / extra / bare / bare-tests.factor
1 USING: bare multiline prettyprint prettyprint.config sequences tools.test ;
2
3 IN: bare.tests
4
5 ! uint
6
7 { 0 } [ B{ 0x00 } uint bare> ] unit-test
8 { B{ 0x00 } } [ 0 uint >bare ] unit-test
9
10 { 1 } [ B{ 0x01 } uint bare> ] unit-test
11 { B{ 0x01 } } [ 1 uint >bare ] unit-test
12
13 { 126 } [ B{ 0x7e } uint bare> ] unit-test
14 { B{ 0x7e } } [ 126 uint >bare ] unit-test
15
16 { 127 } [ B{ 0x7f } uint bare> ] unit-test
17 { B{ 0x7f } } [ 127 uint >bare ] unit-test
18
19 { 128 } [ B{ 0x80 0x01 } uint bare> ] unit-test
20 { B{ 0x80 0x01 } } [ 128 uint >bare ] unit-test
21
22 { 129 } [ B{ 0x81 0x01 } uint bare> ] unit-test
23 { B{ 0x81 0x01 } } [ 129 uint >bare ] unit-test
24
25 { 255 } [ B{ 0xFF 0x01 } uint bare> ] unit-test
26 { B{ 0xFF 0x01 } } [ 255 uint >bare ] unit-test
27
28 ! int
29
30 { 0 } [ B{ 0x00 } int bare> ] unit-test
31 { B{ 0x00 } } [ 0 int >bare ] unit-test
32
33 { 1 } [ B{ 0x02 } int bare> ] unit-test
34 { B{ 0x02 } } [ 1 int >bare ] unit-test
35
36 { -1 } [ B{ 0x01 } int bare> ] unit-test
37 { B{ 0x01 } } [ -1 int >bare ] unit-test
38
39 { 63 } [ B{ 0x7e } int bare> ] unit-test
40 { B{ 0x7e } } [ 63 int >bare ] unit-test
41
42 { -63 } [ B{ 0x7d } int bare> ] unit-test
43 { B{ 0x7d } } [ -63 int >bare ] unit-test
44
45 { 64 } [ B{ 0x80 0x01 } int bare> ] unit-test
46 { B{ 0x80 0x01 } } [ 64 int >bare ] unit-test
47
48 { -64 } [ B{ 0x7f } int bare> ] unit-test
49 { B{ 0x7f } } [ -64 int >bare ] unit-test
50
51 { 65 } [ B{ 0x82 0x01 } int bare> ] unit-test
52 { B{ 0x82 0x01 } } [ 65 int >bare ] unit-test
53
54 { -65 } [ B{ 0x81 0x01 } int bare> ] unit-test
55 { B{ 0x81 0x01 } } [ -65 int >bare ] unit-test
56
57 { 255 } [ B{ 0xFE 0x03 } int bare> ] unit-test
58 { B{ 0xFE 0x03 } } [ 255 int >bare ] unit-test
59
60 { -255 } [ B{ 0xFD 0x03 } int bare> ] unit-test
61 { B{ 0xFD 0x03 } } [ -255 int >bare ] unit-test
62
63 ! u32
64
65 { 0 } [ B{ 0x00 0x00 0x00 0x00 } u32 bare> ] unit-test
66 { B{ 0x00 0x00 0x00 0x00 } } [ 0 u32 >bare ] unit-test
67
68 { 1 } [ B{ 0x01 0x00 0x00 0x00 } u32 bare> ] unit-test
69 { B{ 0x01 0x00 0x00 0x00 } } [ 1 u32 >bare ] unit-test
70
71 { 255 } [ B{ 0xFF 0x00 0x00 0x00 } u32 bare> ] unit-test
72 { B{ 0xFF 0x00 0x00 0x00 } } [ 255 u32 >bare ] unit-test
73
74 ! i16
75
76 { 0 } [ B{ 0x00 0x00 } i16 bare> ] unit-test
77 { B{ 0x00 0x00 } } [ 0 i16 >bare ] unit-test
78
79 { 1 } [ B{ 0x01 0x00 } i16 bare> ] unit-test
80 { B{ 0x01 0x00 } } [ 1 i16 >bare ] unit-test
81
82 { -1 } [ B{ 0xFF 0xFF } i16 bare> ] unit-test
83 { B{ 0xFF 0xFF } } [ -1 i16 >bare ] unit-test
84
85 { 255 } [ B{ 0xFF 0x00 } i16 bare> ] unit-test
86 { B{ 0xFF 0x00 } } [ 255 i16 >bare ] unit-test
87
88 { -255 } [ B{ 0x01 0xFF } i16 bare> ] unit-test
89 { B{ 0x01 0xFF } } [ -255 i16 >bare ] unit-test
90
91 ! f64
92
93 { 0.0 } [ B{ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 } f64 bare> ] unit-test
94 { B{ 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 } } [ 0.0 f64 >bare ] unit-test
95
96 { 1.0 } [ B{ 0x00 0x00 0x00 0x00 0x00 0x00 0xF0 0x3F } f64 bare> ] unit-test
97 { B{ 0x00 0x00 0x00 0x00 0x00 0x00 0xF0 0x3F } } [ 1.0 f64 >bare ] unit-test
98
99 { 2.55 } [ B{ 0x66 0x66 0x66 0x66 0x66 0x66 0x04 0x40 } f64 bare> ] unit-test
100 { B{ 0x66 0x66 0x66 0x66 0x66 0x66 0x04 0x40 } } [ 2.55 f64 >bare ] unit-test
101
102 { -25.5 } [ B{ 0x00 0x00 0x00 0x00 0x00 0x80 0x39 0xC0 } f64 bare> ] unit-test
103 { B{ 0x00 0x00 0x00 0x00 0x00 0x80 0x39 0xC0 } } [ -25.5 f64 >bare ] unit-test
104
105 ! bool
106
107 { t } [ B{ 0x01 } bool bare> ] unit-test
108 { B{ 0x01 } } [ t bool >bare ] unit-test
109
110 { f } [ B{ 0x00 } bool bare> ] unit-test
111 { B{ 0x00 } } [ f bool >bare ] unit-test
112
113 ! str
114
115 { "BARE" } [ B{ 0x04 0x42 0x41 0x52 0x45 } str bare> ] unit-test
116 { B{ 0x04 0x42 0x41 0x52 0x45 } } [ "BARE" str >bare ] unit-test
117
118 ! data
119
120 { B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb } } [
121     B{ 0x10 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb }
122     T{ data } bare>
123 ] unit-test
124
125 { B{ 0x10 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb } } [
126     B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb }
127     T{ data } >bare
128 ] unit-test
129
130 ! data[length]
131
132 { B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb } } [
133     B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb }
134     T{ data f 16 } bare>
135 ] unit-test
136
137 { B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb } } [
138     B{ 0xaa 0xee 0xff 0xee 0xdd 0xcc 0xbb 0xaa 0xee 0xdd 0xcc 0xbb 0xee 0xdd 0xcc 0xbb }
139     T{ data f 16 } >bare
140 ] unit-test
141
142 ! enum
143
144 { "FOO" } [ B{ 0x00 } T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } bare> ] unit-test
145 { B{ 0x00 } } [ "FOO" T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } >bare ] unit-test
146 { "BAR" } [ B{ 0xFF 0x01 } T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } bare> ] unit-test
147 { B{ 0xFF 0x01 } } [ "BAR" T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } >bare ] unit-test
148 { "BUZZ" } [ B{ 0x80 0x02 } T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } bare> ] unit-test
149 { B{ 0x80 0x02 } } [ "BUZZ" T{ enum f { { "FOO" 0 } { "BAR" 255 } { "BUZZ" 256 } } } >bare ] unit-test
150
151 [ B{ 0x03 } T{ enum f { { "A" 0 } { "B" 1 } { "C" 2 } } } bare> ] [ invalid-enum? ] must-fail-with
152 [ "D" T{ enum f { { "A" 0 } { "B" 1 } { "C" 2 } } } >bare ] [ invalid-enum? ] must-fail-with
153
154 ! optional<u32>
155
156 { f } [ B{ 0x00 } T{ optional f u32 } bare> ] unit-test
157 { 0 } [ B{ 0x01 0x00 0x00 0x00 0x00 } T{ optional f u32 } bare> ] unit-test
158 { 1 } [ B{ 0x01 0x01 0x00 0x00 0x00 } T{ optional f u32 } bare> ] unit-test
159 { 255 } [ B{ 0x01 0xFF 0x00 0x00 0x00 } T{ optional f u32 } bare> ] unit-test
160
161 ! list<str>
162
163 { { "foo" "bar" "buzz" } } [
164     B{ 0x03 0x03 0x66 0x6f 0x6f 0x03 0x62 0x61 0x72 0x04 0x62 0x75 0x7A 0x7A }
165     T{ list f str f } bare>
166 ] unit-test
167
168 { B{ 0x03 0x03 0x66 0x6f 0x6f 0x03 0x62 0x61 0x72 0x04 0x62 0x75 0x7A 0x7A } } [
169     { "foo" "bar" "buzz" } T{ list f str f } >bare
170 ] unit-test
171
172 ! list<uint>[10]
173
174 { { 0 1 254 255 256 257 126 127 128 129 } } [
175     B{ 0x00 0x01 0xFE 0x01 0xFF 0x01 0x80 0x02 0x81 0x02 0x7E 0x7F 0x80 0x01 0x81 0x01 }
176     T{ list f uint 10 } bare>
177 ] unit-test
178
179 { B{ 0x00 0x01 0xFE 0x01 0xFF 0x01 0x80 0x02 0x81 0x02 0x7E 0x7F 0x80 0x01 0x81 0x01 } } [
180     { 0 1 254 255 256 257 126 127 128 129 } T{ list f uint 10 } >bare
181 ] unit-test
182
183 ! map<u32><str>
184
185 {
186     B{
187         0x03 0x00 0x00 0x00 0x00 0x04 0x7A 0x65 0x72 0x6F 0x01
188         0x00 0x00 0x00 0x03 0x6F 0x6E 0x65 0xFF 0x00 0x00 0x00
189         0x1B 0x74 0x77 0x6F 0x20 0x68 0x75 0x6E 0x64 0x72 0x65
190         0x64 0x73 0x20 0x61 0x6E 0x64 0x20 0x66 0x69 0x66 0x74
191         0x79 0x20 0x66 0x69 0x76 0x65
192     }
193 } [
194     H{
195         { 0 "zero" }
196         { 1 "one" }
197         { 255 "two hundreds and fifty five" }
198     } T{ bare:map f u32 str } >bare
199 ] unit-test
200
201 {
202     {
203         { 0 "zero" }
204         { 1 "one" }
205         { 255 "two hundreds and fifty five" }
206     }
207 } [
208     B{
209         0x03 0x00 0x00 0x00 0x00 0x04 0x7A 0x65 0x72 0x6F 0x01
210         0x00 0x00 0x00 0x03 0x6F 0x6E 0x65 0xFF 0x00 0x00 0x00
211         0x1B 0x74 0x77 0x6F 0x20 0x68 0x75 0x6E 0x64 0x72 0x65
212         0x64 0x73 0x20 0x61 0x6E 0x64 0x20 0x66 0x69 0x66 0x74
213         0x79 0x20 0x66 0x69 0x76 0x65
214     } T{ bare:map f u32 str } bare>
215 ] unit-test
216
217 ! union
218
219 {
220     {
221         0
222         1
223         1
224         -1
225         255
226         255
227         -255
228         "BARE"
229     }
230 } [
231     {
232         B{ 0x00 0x00 }
233         B{ 0x00 0x02 }
234         B{ 0xFF 0x01 0x01 }
235         B{ 0x00 0x01 }
236         B{ 0x00 0xFE 0x03 }
237         B{ 0xFF 0x01 0xFF 0x01 }
238         B{ 0x00 0xFD 0x03 }
239         B{ 0x80 0x02 0x04 0x42 0x41 0x52 0x45 }
240     } [
241         T{ bare:union f { { int 0 } { uint 255 } { str 256 } } } bare>
242     ] sequences:map
243 ] unit-test
244
245 [
246     B{ 0x03 0x03 } T{ bare:union f { { int 0 } { uint 1 } { str 2 } } } bare>
247 ] [ invalid-union? ] must-fail-with
248
249 ! struct
250
251 {
252     {
253         { "foo" 255 }
254         { "bar" -255 }
255         { "buzz" "BARE" }
256     }
257 } [
258     B{ 0xFF 0x01 0xFD 0x03 0x04 0x42 0x41 0x52 0x45 }
259     T{ struct f { { "foo" uint } { "bar" int } { "buzz" str } } } bare>
260 ] unit-test
261
262 {
263     B{ 0xFF 0x01 0xFD 0x03 0x04 0x42 0x41 0x52 0x45 }
264 } [
265     {
266         { "foo" 255 }
267         { "bar" -255 }
268         { "buzz" "BARE" }
269     } T{ struct f { { "foo" uint } { "bar" int } { "buzz" str } } } >bare
270 ] unit-test
271
272 ! user types / schema
273
274 SCHEMA: [=[
275 type PublicKey data[128]
276 type Time str # ISO 8601
277
278 type Department enum {
279   ACCOUNTING
280   ADMINISTRATION
281   CUSTOMER_SERVICE
282   DEVELOPMENT
283
284   # Reserved for the CEO
285   JSMITH = 99
286 }
287
288 type Address list<str>[4] # street, city, state, country
289
290 type Customer struct {
291   name: str
292   email: str
293   address: Address
294   orders: list<struct {
295     orderId: i64
296     quantity: i32
297   }>
298   metadata: map<str><data>
299 }
300
301 type Employee struct {
302   name: str
303   email: str
304   address: Address
305   department: Department
306   hireDate: Time
307   publicKey: optional<PublicKey>
308   metadata: map<str><data>
309 }
310
311 type TerminatedEmployee void
312
313 type Person union {Customer | Employee | TerminatedEmployee}
314 ]=]
315
316
317 {
318     ! Customer
319     V{
320         { "name" "James Smith" }
321         { "email" "jsmith@example.org" }
322         {
323             "address"
324             { "123 Main St" "Philadelphia" "PA" "United States" }
325         }
326         {
327             "orders"
328             { V{ { "orderId" 4242424242 } { "quantity" 5 } } }
329         }
330         { "metadata" { } }
331     }
332 } [
333     B{
334         0x00 0x0b 0x4a 0x61 0x6d 0x65 0x73 0x20 0x53 0x6d 0x69
335         0x74 0x68 0x12 0x6a 0x73 0x6d 0x69 0x74 0x68 0x40 0x65
336         0x78 0x61 0x6d 0x70 0x6c 0x65 0x2e 0x6f 0x72 0x67 0x0b
337         0x31 0x32 0x33 0x20 0x4d 0x61 0x69 0x6e 0x20 0x53 0x74
338         0x0c 0x50 0x68 0x69 0x6c 0x61 0x64 0x65 0x6c 0x70 0x68
339         0x69 0x61 0x02 0x50 0x41 0x0d 0x55 0x6e 0x69 0x74 0x65
340         0x64 0x20 0x53 0x74 0x61 0x74 0x65 0x73 0x01 0xb2 0x41
341         0xde 0xfc 0x00 0x00 0x00 0x00 0x05 0x00 0x00 0x00 0x00
342     } Person bare>
343 ] unit-test
344
345 {
346     ! Employee
347     V{
348         { "name" "Tiffany Doe" }
349         { "email" "tiffanyd@acme.corp" }
350         {
351             "address"
352             { "123 Main St" "Philadelphia" "PA" "United States" }
353         }
354         { "department" "ADMINISTRATION" }
355         { "hireDate" "2020-06-21T21:18:05Z" }
356         { "publicKey" f }
357         { "metadata" { } }
358     }
359 } [
360     B{
361         0x01 0x0b 0x54 0x69 0x66 0x66 0x61 0x6e 0x79 0x20 0x44
362         0x6f 0x65 0x12 0x74 0x69 0x66 0x66 0x61 0x6e 0x79 0x64
363         0x40 0x61 0x63 0x6d 0x65 0x2e 0x63 0x6f 0x72 0x70 0x0b
364         0x31 0x32 0x33 0x20 0x4d 0x61 0x69 0x6e 0x20 0x53 0x74
365         0x0c 0x50 0x68 0x69 0x6c 0x61 0x64 0x65 0x6c 0x70 0x68
366         0x69 0x61 0x02 0x50 0x41 0x0d 0x55 0x6e 0x69 0x74 0x65
367         0x64 0x20 0x53 0x74 0x61 0x74 0x65 0x73 0x01 0x14 0x32
368         0x30 0x32 0x30 0x2d 0x30 0x36 0x2d 0x32 0x31 0x54 0x32
369         0x31 0x3a 0x31 0x38 0x3a 0x30 0x35 0x5a 0x00 0x00
370     } Person bare>
371 ] unit-test
372
373 {
374     ! TerminatedEmployee
375     f
376 } [
377     B{ 0x02 } Person bare>
378 ] unit-test
379
380 ! enum checks
381
382 [ "type Thing enum {}" parse-schema ] [ not-enough-entries? ] must-fail-with
383 [ "type Thing enum { }" parse-schema ] [ not-enough-entries? ] must-fail-with
384 [
385     "type Alphabet enum {
386       A
387       B
388       C = 0
389       A
390       B = 99
391     }" parse-schema
392 ] [ duplicate-keys? ] must-fail-with
393
394 [
395     "type Alphabet enum {
396       A
397       B
398       C = 0
399       D
400       E = 99
401     }" parse-schema
402 ] [ duplicate-values? ] must-fail-with
403
404 ! data checks
405
406 [ "type Foo data[0]" parse-schema ] [ invalid-length? ] must-fail-with
407 [ "type Foo data[18446744073709551616]" parse-schema ] [ invalid-length? ] must-fail-with
408
409 ! optional checks
410
411 [ "type Foo optional<void>" parse-schema ] [ cannot-be-void? ] must-fail-with
412
413 ! list checks
414
415 [ "type Foo list<void>" parse-schema ] [ cannot-be-void? ] must-fail-with
416 [ "type Foo list<int>[0]" parse-schema ] [ invalid-length? ] must-fail-with
417 [ "type Foo list<int>[18446744073709551616]" parse-schema ] [ invalid-length? ] must-fail-with
418
419 ! map checks
420
421 [ "type Foo map<void><int>" parse-schema ] [ cannot-be-void? ] must-fail-with
422 [ "type Foo map<void><void>" parse-schema ] [ cannot-be-void? ] must-fail-with
423 [ "type Foo map<int><void>" parse-schema ] [ cannot-be-void? ] must-fail-with
424
425 ! union checks
426
427 [ "type Thing union {}" parse-schema ] [ not-enough-entries? ] must-fail-with
428 [ "type Thing union { }" parse-schema ] [ not-enough-entries? ] must-fail-with
429 [ "type Thing union {int=0|int|str=0}" parse-schema ] [ duplicate-keys? ] must-fail-with
430 [ "type Thing union {int=0|uint|str=0}" parse-schema ] [ duplicate-values? ] must-fail-with
431
432 ! struct checks
433
434 [ "type Thing struct {}" parse-schema ] [ not-enough-entries? ] must-fail-with
435 [ "type Thing struct { }" parse-schema ] [ not-enough-entries? ] must-fail-with
436 [ "type Thing struct { a: int b: int a: int }" parse-schema ] [ duplicate-keys? ] must-fail-with
437 [ "type Thing struct { a: void }" parse-schema ] [ cannot-be-void? ] must-fail-with
438
439 ! user checks
440
441 [ "type Thing Other" parse-schema ] [ unknown-type? ] must-fail-with