]> gitweb.factorcode.org Git - factor.git/blob - basis/locals/locals-tests.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / locals / locals-tests.factor
1 USING: locals math sequences tools.test hashtables words kernel
2 namespaces arrays strings prettyprint io.streams.string parser
3 accessors generic eval combinators combinators.short-circuit
4 combinators.short-circuit.smart math.order math.functions
5 definitions compiler.units fry lexer words.symbol see multiline ;
6 IN: locals.tests
7
8 :: foo ( a b -- a a ) a a ;
9
10 [ 1 1 ] [ 1 2 foo ] unit-test
11
12 :: add-test ( a b -- c ) a b + ;
13
14 [ 3 ] [ 1 2 add-test ] unit-test
15
16 :: sub-test ( a b -- c ) a b - ;
17
18 [ -1 ] [ 1 2 sub-test ] unit-test
19
20 :: map-test ( a b -- seq ) a [ b + ] map ;
21
22 [ { 5 6 7 } ] [ { 1 2 3 } 4 map-test ] unit-test
23
24 :: map-test-2 ( seq inc -- seq ) seq [| elt | elt inc + ] map ;
25
26 [ { 5 6 7 } ] [ { 1 2 3 } 4 map-test-2 ] unit-test
27
28 :: let-test ( c -- d )
29     [let | a [ 1 ] b [ 2 ] | a b + c + ] ;
30
31 [ 7 ] [ 4 let-test ] unit-test
32
33 :: let-test-2 ( a -- a )
34     a [let | a [ ] | [let | b [ a ] | a ] ] ;
35
36 [ 3 ] [ 3 let-test-2 ] unit-test
37
38 :: let-test-3 ( a -- a )
39     a [let | a [ ] | [let | b [ [ a ] ] | [let | a [ 3 ] | b ] ] ] ;
40
41 :: let-test-4 ( a -- b )
42     a [let | a [ 1 ] b [ ] | a b 2array ] ;
43
44 [ { 1 2 } ] [ 2 let-test-4 ] unit-test
45
46 :: let-test-5 ( a b -- b )
47     a b [let | a [ ] b [ ] | a b 2array ] ;
48
49 [ { 2 1 } ] [ 1 2 let-test-5 ] unit-test
50
51 :: let-test-6 ( a -- b )
52     a [let | a [ ] b [ 1 ] | a b 2array ] ;
53
54 [ { 2 1 } ] [ 2 let-test-6 ] unit-test
55
56 [ -1 ] [ -1 let-test-3 call ] unit-test
57
58 [ 5 ] [
59     [let | a [ 3 ] | [wlet | func [ a + ] | 2 func ] ]
60 ] unit-test
61
62 :: wlet-test-2 ( a b -- seq )
63     [wlet | add-b [ b + ] |
64         a [ add-b ] map ] ;
65
66
67 [ { 4 5 6 } ] [ { 2 3 4 } 2 wlet-test-2 ] unit-test
68     
69 :: wlet-test-3 ( a -- b )
70     [wlet | add-a [ a + ] | [ add-a ] ]
71     [let | a [ 3 ] | a swap call ] ;
72
73 [ 5 ] [ 2 wlet-test-3 ] unit-test
74
75 :: wlet-test-4 ( a -- b )
76     [wlet | sub-a [| b | b a - ] |
77         3 sub-a ] ;
78
79 [ -7 ] [ 10 wlet-test-4 ] unit-test
80
81 :: write-test-1 ( n! -- q )
82     [| i | n i + dup n! ] ;
83
84 0 write-test-1 "q" set
85
86 { 1 1 } "q" get must-infer-as
87
88 [ 1 ] [ 1 "q" get call ] unit-test
89
90 [ 2 ] [ 1 "q" get call ] unit-test
91
92 [ 3 ] [ 1 "q" get call ] unit-test
93
94 [ 5 ] [ 2 "q" get call ] unit-test
95
96 :: write-test-2 ( -- q )
97     [let | n! [ 0 ] |
98         [| i | n i + dup n! ] ] ;
99
100 write-test-2 "q" set
101
102 [ 1 ] [ 1 "q" get call ] unit-test
103
104 [ 2 ] [ 1 "q" get call ] unit-test
105
106 [ 3 ] [ 1 "q" get call ] unit-test
107
108 [ 5 ] [ 2 "q" get call ] unit-test
109
110 [ 10 20 ]
111 [
112     20 10 [| a! | [| b! | a b ] ] call call
113 ] unit-test
114
115 :: write-test-3 ( a! -- q ) [| b | b a! ] ;
116
117 [ ] [ 1 2 write-test-3 call ] unit-test
118
119 :: write-test-4 ( x! -- q ) [ [let | y! [ 0 ] | f x! ] ] ;
120
121 [ ] [ 5 write-test-4 drop ] unit-test
122
123 ! Not really a write test; just enforcing consistency
124 :: write-test-5 ( x -- y )
125     [wlet | fun! [ x + ] | 5 fun! ] ;
126
127 [ 9 ] [ 4 write-test-5 ] unit-test
128
129 :: let-let-test ( n -- n ) [let | n [ n 3 + ] | n ] ;
130
131 [ 13 ] [ 10 let-let-test ] unit-test
132
133 GENERIC: lambda-generic ( a b -- c )
134
135 GENERIC# lambda-generic-1 1 ( a b -- c )
136
137 M:: integer lambda-generic-1 ( a b -- c ) a b * ;
138
139 M:: string lambda-generic-1 ( a b -- c )
140     a b CHAR: x <string> lambda-generic ;
141
142 M:: integer lambda-generic ( a b -- c ) a b lambda-generic-1 ;
143
144 GENERIC# lambda-generic-2 1 ( a b -- c )
145
146 M:: integer lambda-generic-2 ( a b -- c )
147     a CHAR: x <string> b lambda-generic ;
148
149 M:: string lambda-generic-2 ( a b -- c ) a b append ;
150
151 M:: string lambda-generic ( a b -- c ) a b lambda-generic-2 ;
152
153 [ 10 ] [ 5 2 lambda-generic ] unit-test
154
155 [ "abab" ] [ "aba" "b" lambda-generic ] unit-test
156
157 [ "abaxxx" ] [ "aba" 3 lambda-generic ] unit-test
158
159 [ "xaba" ] [ 1 "aba" lambda-generic ] unit-test
160
161 [ ] [ \ lambda-generic-1 see ] unit-test
162
163 [ ] [ \ lambda-generic-2 see ] unit-test
164
165 [ ] [ \ lambda-generic see ] unit-test
166
167 :: unparse-test-1 ( a -- ) [let | a! [ 3 ] | ] ;
168
169 [ "[let | a! [ 3 ] | ]" ] [
170     \ unparse-test-1 "lambda" word-prop body>> first unparse
171 ] unit-test
172
173 :: unparse-test-2 ( -- ) [wlet | a! [ ] | ] ;
174
175 [ "[wlet | a! [ ] | ]" ] [
176     \ unparse-test-2 "lambda" word-prop body>> first unparse
177 ] unit-test
178
179 :: unparse-test-3 ( -- b ) [| a! | ] ;
180
181 [ "[| a! | ]" ] [
182     \ unparse-test-3 "lambda" word-prop body>> first unparse
183 ] unit-test
184
185 DEFER: xyzzy
186
187 [ ] [
188     "IN: locals.tests USE: math GENERIC: xyzzy ( a -- b ) M: integer xyzzy ;"
189     <string-reader> "lambda-generic-test" parse-stream drop
190 ] unit-test
191
192 [ 10 ] [ 10 xyzzy ] unit-test
193
194 [ ] [
195     "IN: locals.tests USE: math USE: locals GENERIC: xyzzy ( a -- b ) M:: integer xyzzy ( n -- x ) 5 ;"
196     <string-reader> "lambda-generic-test" parse-stream drop
197 ] unit-test
198
199 [ 5 ] [ 10 xyzzy ] unit-test
200
201 :: let*-test-1 ( a -- b )
202     [let* | b [ a 1 + ]
203             c [ b 1 + ] |
204         a b c 3array ] ;
205
206 [ { 1 2 3 } ] [ 1 let*-test-1 ] unit-test
207
208 :: let*-test-2 ( a -- b )
209     [let* | b [ a 1 + ]
210             c! [ b 1 + ] |
211         a b c 3array ] ;
212
213 [ { 1 2 3 } ] [ 1 let*-test-2 ] unit-test
214
215 :: let*-test-3 ( a -- b )
216     [let* | b [ a 1 + ]
217             c! [ b 1 + ] |
218         c 1 + c!  a b c 3array ] ;
219
220 [ { 1 2 4 } ] [ 1 let*-test-3 ] unit-test
221
222 :: let*-test-4 ( a b -- c d )
223     [let | a [ b ]
224            b [ a ] |
225         [let* | a'  [ a  ]
226                 a'' [ a' ]
227                 b'  [ b  ]
228                 b'' [ b' ] |
229             a'' b'' ] ] ;
230
231 [ "xxx" "yyy" ] [ "yyy" "xxx" let*-test-4 ] unit-test
232
233 GENERIC: next-method-test ( a -- b )
234
235 M: integer next-method-test 3 + ;
236
237 M:: fixnum next-method-test ( a -- b ) a call-next-method 1 + ;
238
239 [ 5 ] [ 1 next-method-test ] unit-test
240
241 : no-with-locals-test ( -- seq ) { 1 2 3 } [| x | x 3 + ] map ;
242
243 [ { 4 5 6 } ] [ no-with-locals-test ] unit-test
244
245 { 3 0 } [| a b c | ] must-infer-as
246
247 [ ] [ 1 [let | a [ ] | ] ] unit-test
248
249 [ 3 ] [ 1 [let | a [ ] | 3 ] ] unit-test
250
251 [ ] [ 1 2 [let | a [ ] b [ ] | ] ] unit-test
252
253 :: a-word-with-locals ( a b -- ) ;
254
255 CONSTANT: new-definition "USING: math ;\nIN: locals.tests\n: a-word-with-locals ( -- x ) 2 3 + ;\n"
256
257 [ ] [ new-definition eval( -- ) ] unit-test
258
259 [ t ] [
260     [ \ a-word-with-locals see ] with-string-writer
261     new-definition =
262 ] unit-test
263
264 CONSTANT: method-definition "USING: locals locals.tests sequences ;\nM:: sequence method-with-locals ( a -- y ) a reverse ;\n"
265
266 GENERIC: method-with-locals ( x -- y )
267
268 M:: sequence method-with-locals ( a -- y ) a reverse ;
269
270 [ t ] [
271     [ \ sequence \ method-with-locals method see ] with-string-writer
272     method-definition =
273 ] unit-test
274
275 :: cond-test ( a b -- c )
276     {
277         { [ a b < ] [ 3 ] }
278         { [ a b = ] [ 4 ] }
279         { [ a b > ] [ 5 ] }
280     } cond ;
281
282 \ cond-test def>> must-infer
283
284 [ 3 ] [ 1 2 cond-test ] unit-test
285 [ 4 ] [ 2 2 cond-test ] unit-test
286 [ 5 ] [ 3 2 cond-test ] unit-test
287
288 :: 0&&-test ( a -- ? )
289     { [ a integer? ] [ a even? ] [ a 10 > ] } 0&& ;
290
291 \ 0&&-test def>> must-infer
292
293 [ f ] [ 1.5 0&&-test ] unit-test
294 [ f ] [ 3 0&&-test ] unit-test
295 [ f ] [ 8 0&&-test ] unit-test
296 [ t ] [ 12 0&&-test ] unit-test
297
298 :: &&-test ( a -- ? )
299     { [ a integer? ] [ a even? ] [ a 10 > ] } && ;
300
301 \ &&-test def>> must-infer
302
303 [ f ] [ 1.5 &&-test ] unit-test
304 [ f ] [ 3 &&-test ] unit-test
305 [ f ] [ 8 &&-test ] unit-test
306 [ t ] [ 12 &&-test ] unit-test
307
308 :: let-and-cond-test-1 ( -- a )
309     [let | a [ 10 ] |
310         [let | a [ 20 ] |
311             {
312                 { [ t ] [ [let | c [ 30 ] | a ] ] }
313             } cond
314         ]
315     ] ;
316
317 \ let-and-cond-test-1 def>> must-infer
318
319 [ 20 ] [ let-and-cond-test-1 ] unit-test
320
321 :: let-and-cond-test-2 ( -- pair )
322     [let | A [ 10 ] |
323         [let | B [ 20 ] |
324             { { [ t ] [ { A B } ] } } cond
325         ]
326     ] ;
327
328 \ let-and-cond-test-2 def>> must-infer
329
330 [ { 10 20 } ] [ let-and-cond-test-2 ] unit-test
331
332 [ { 10       } ] [ 10       [| a     | { a     } ] call ] unit-test
333 [ { 10 20    } ] [ 10 20    [| a b   | { a b   } ] call ] unit-test
334 [ { 10 20 30 } ] [ 10 20 30 [| a b c | { a b c } ] call ] unit-test
335
336 [ { 10 20 30 } ] [ [let | a [ 10 ] b [ 20 ] c [ 30 ] | { a b c } ] ] unit-test
337
338 [ V{ 10 20 30 } ] [ 10 20 30 [| a b c | V{ a b c } ] call ] unit-test
339
340 [ H{ { 10 "a" } { 20 "b" } { 30 "c" } } ]
341 [ 10 20 30 [| a b c | H{ { a "a" } { b "b" } { c "c" } } ] call ] unit-test
342
343 [ T{ slice f 0 3 "abc" } ]
344 [ 0 3 "abc" [| from to seq | T{ slice f from to seq } ] call ] unit-test
345
346 { 3 1 } [| from to seq | T{ slice f from to seq } ] must-infer-as
347
348 ERROR: punned-class x ;
349
350 [ T{ punned-class f 3 } ] [ 3 [| a | T{ punned-class f a } ] call ] unit-test
351
352 :: literal-identity-test ( -- a b )
353     { 1 } V{ } ;
354
355 [ t t ] [
356     literal-identity-test
357     literal-identity-test
358     [ eq? ] [ eq? ] bi-curry* bi*
359 ] unit-test
360
361 :: mutable-local-in-literal-test ( a! -- b ) a 1 + a! { a } ;
362
363 [ { 4 } ] [ 3 mutable-local-in-literal-test ] unit-test
364
365 :: compare-case ( obj1 obj2 lt-quot eq-quot gt-quot -- )
366     obj1 obj2 <=> {
367         { +lt+ [ lt-quot call ] }
368         { +eq+ [ eq-quot call ] }
369         { +gt+ [ gt-quot call ] }
370     } case ; inline
371
372 [ [ ] [ ] [ ] compare-case ] must-infer
373
374 :: big-case-test ( a -- b )
375     a {
376         { 0 [ a 1 + ] }
377         { 1 [ a 1 - ] }
378         { 2 [ a 1 swap / ] }
379         { 3 [ a dup * ] }
380         { 4 [ a sqrt ] }
381         { 5 [ a a ^ ] }
382     } case ;
383
384 \ big-case-test def>> must-infer
385
386 [ 9 ] [ 3 big-case-test ] unit-test
387
388 ! Dan found this problem
389 : littledan-case-problem-1 ( a -- b )
390     {
391         { t [ 3 ] }
392         { f [ 4 ] }
393         [| x | x 12 + { "howdy" } nth ]
394     } case ;
395
396 \ littledan-case-problem-1 def>> must-infer
397
398 [ "howdy" ] [ -12 \ littledan-case-problem-1 def>> call ] unit-test
399 [ "howdy" ] [ -12 littledan-case-problem-1 ] unit-test
400
401 :: littledan-case-problem-2 ( a -- b )
402     a {
403         { t [ a not ] }
404         { f [ 4 ] }
405         [| x | x a - { "howdy" } nth ]
406     } case ;
407
408 \ littledan-case-problem-2 def>> must-infer
409
410 [ "howdy" ] [ -12 \ littledan-case-problem-2 def>> call ] unit-test
411 [ "howdy" ] [ -12 littledan-case-problem-2 ] unit-test
412
413 :: littledan-cond-problem-1 ( a -- b )
414     a {
415         { [ dup 0 < ] [ drop a not ] }
416         { [| y | y y 0 > ] [ drop 4 ] }
417         [| x | x a - { "howdy" } nth ]
418     } cond ;
419
420 \ littledan-cond-problem-1 def>> must-infer
421
422 [ f ] [ -12 \ littledan-cond-problem-1 def>> call ] unit-test
423 [ 4 ] [ 12 \ littledan-cond-problem-1 def>> call ] unit-test
424 [ "howdy" ] [ 0 \ littledan-cond-problem-1 def>> call ] unit-test
425 [ f ] [ -12 littledan-cond-problem-1 ] unit-test
426 [ 4 ] [ 12 littledan-cond-problem-1 ] unit-test
427 [ "howdy" ] [ 0 littledan-cond-problem-1 ] unit-test
428
429 /*
430 :: littledan-case-problem-3 ( a quot -- b )
431     a {
432         { t [ a not ] }
433         { f [ 4 ] }
434         quot
435     } case ; inline
436
437 [ f ] [ t [ ] littledan-case-problem-3 ] unit-test
438 [ 144 ] [ 12 [ sq ] littledan-case-problem-3 ] unit-test
439 [| | [| a | a ] littledan-case-problem-3 ] must-infer
440
441 : littledan-case-problem-4 ( a -- b )
442     [ 1 + ] littledan-case-problem-3 ;
443
444 \ littledan-case-problem-4 def>> must-infer
445 */
446
447 GENERIC: lambda-method-forget-test ( a -- b )
448
449 M:: integer lambda-method-forget-test ( a -- b ) a ;
450
451 [ ] [ [ M\ integer lambda-method-forget-test forget ] with-compilation-unit ] unit-test
452
453 [ 10 ] [ 10 [| A | { [ A ] } ] call first call ] unit-test
454
455 [
456     "USING: locals fry math ; 1 '[ [let | A [ 10 ] | A _ + ] ]"
457     eval( -- ) call
458 ] [ error>> >r/r>-in-fry-error? ] must-fail-with
459     
460 :: (funny-macro-test) ( obj quot -- ? ) obj { quot } 1&& ; inline
461 : funny-macro-test ( n -- ? ) [ odd? ] (funny-macro-test) ;
462
463 \ funny-macro-test def>> must-infer
464
465 [ t ] [ 3 funny-macro-test ] unit-test
466 [ f ] [ 2 funny-macro-test ] unit-test
467
468 ! Some odd parser corner cases
469 [ "USE: locals [let" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
470 [ "USE: locals [let |" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
471 [ "USE: locals [let | a" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
472 [ "USE: locals [|" eval( -- ) ] [ error>> unexpected-eof? ] must-fail-with
473
474 [ 25 ] [ 5 [| a | { [ a sq ] } cond ] call ] unit-test
475 [ 25 ] [ 5 [| | { [| a | a sq ] } ] call first call ] unit-test
476
477 :: FAILdog-1 ( -- b ) { [| c | c ] } ;
478
479 \ FAILdog-1 def>> must-infer
480
481 :: FAILdog-2 ( a -- b ) a { [| c | c ] } cond ;
482
483 \ FAILdog-2 def>> must-infer
484
485 [ 3 ] [ 3 [| a | \ a ] call ] unit-test
486
487 [ "USE: locals [| | { [let | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
488
489 [ "USE: locals [| | { [wlet | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
490
491 [ "USE: locals [| | { [let* | a [ 0 ] | a ] } ]" eval( -- ) ] must-fail
492
493 [ "USE: locals [| | [let | a! [ 0 ] | { a! } ] ]" eval( -- ) ] must-fail
494
495 [ "USE: locals [| | [wlet | a [ 0 ] | { a } ] ]" eval( -- ) ] must-fail
496
497 [ "USE: locals [| | { :> a } ]" eval( -- ) ] must-fail
498
499 [ "USE: locals 3 :> a" eval( -- ) ] must-fail
500
501 [ 3 ] [ 3 [| | :> a a ] call ] unit-test
502
503 [ 3 ] [ 3 [| | :> a! a ] call ] unit-test
504
505 [ 3 ] [ 2 [| | :> a! a 1 + a! a ] call ] unit-test
506
507 :: wlet-&&-test ( a -- ? )
508     [wlet | is-integer? [ a integer? ]
509             is-even? [ a even? ]
510             >10? [ a 10 > ] |
511         { [ is-integer? ] [ is-even? ] [ >10? ] } &&
512     ] ;
513
514 \ wlet-&&-test def>> must-infer
515 [ f ] [ 1.5 wlet-&&-test ] unit-test
516 [ f ] [ 3 wlet-&&-test ] unit-test
517 [ f ] [ 8 wlet-&&-test ] unit-test
518 [ t ] [ 12 wlet-&&-test ] unit-test
519
520 : fry-locals-test-1 ( -- n )
521     [let | | 6 '[ [let | A [ 4 ] | A _ + ] ] call ] ;
522
523 \ fry-locals-test-1 def>> must-infer
524 [ 10 ] [ fry-locals-test-1 ] unit-test
525
526 :: fry-locals-test-2 ( -- n )
527     [let | | 6 '[ [let | A [ 4 ] | A _ + ] ] call ] ;
528
529 \ fry-locals-test-2 def>> must-infer
530 [ 10 ] [ fry-locals-test-2 ] unit-test
531
532 [ 1 ] [ 3 4 [| | '[ [ _ swap - ] call ] call ] call ] unit-test
533 [ -1 ] [ 3 4 [| | [| a | a - ] call ] call ] unit-test
534 [ -1 ] [ 3 4 [| | [| a | a - ] curry call ] call ] unit-test
535 [ -1 ] [ 3 4 [| a | a - ] curry call ] unit-test
536 [ 1 ] [ 3 4 [| | '[ [| a | _ a - ] call ] call ] call ] unit-test
537 [ -1 ] [ 3 4 [| | '[ [| a | a _ - ] call ] call ] call ] unit-test
538
539 [ { 1 2 3 4 } ] [
540     1 3 2 4
541     [| | '[ [| a b | a _ b _ 4array ] call ] call ] call
542 ] unit-test
543
544 [ 10 ] [
545     [| | 0 '[ [let | A [ 10 ] | A _ + ] ] call ] call
546 ] unit-test
547
548 ! littledan found this problem
549 [ "bar" ] [ [let | a [ [let | foo [ "bar" ] | foo ] ] | a ] ] unit-test
550 [ 10 ] [ [let | a [ 10 ] | [let | b [ a ] | b ] ] ] unit-test
551
552 [ { \ + } ] [ [let | x [ \ + ] | { \ x } ] ] unit-test
553
554 [ { \ + 3 } ] [ [let | a [ 3 ] | { \ + a } ] ] unit-test
555
556 [ 3 ] [ [let | a [ \ + ] | 1 2 [ \ a execute ] ] call ] unit-test
557
558 ! erg found this problem
559 :: erg's-:>-bug ( n ? -- n ) ? [ n :> n n ] [ n :> b b ] if ;
560
561 [ 3 ] [ 3 f erg's-:>-bug ] unit-test
562     
563 [ 3 ] [ 3 t erg's-:>-bug ] unit-test
564
565 :: erg's-:>-bug-2 ( n ? -- n ) ? n '[ _ :> n n ] [ n :> b b ] if ;
566
567 [ 3 ] [ 3 f erg's-:>-bug-2 ] unit-test
568     
569 [ 3 ] [ 3 t erg's-:>-bug-2 ] unit-test
570
571 ! dharmatech found this problem
572 GENERIC: ed's-bug ( a -- b )
573
574 M: string ed's-bug reverse ;
575 M: integer ed's-bug neg ;
576
577 :: ed's-test-case ( a -- b )
578    { [ a ed's-bug ] } && ;
579
580 [ t ] [ \ ed's-test-case optimized? ] unit-test