Source

ocaml-llists / tests / test_llistcomp_rev.ml

  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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
(* Revised syntax extension tests
 *
 * Author: Vadim Shender
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)


open OUnit ;
open Test_common ;

open Lazy_list ;


type test_record =
  { x : int ;
    y : float } ;

value test_record_printer = fun
    { x = x; y = y } -> Printf.sprintf "{ %d; %3.2f }" x y ;


value test_lazy_list_gen_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    []
    (to_list [% ]) ;

value test_lazy_list_gen_2 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1]
    (to_list [% 1 ]) ;

value test_lazy_list_gen_3 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2]
    (to_list [% 1; 2 ]) ;

value test_lazy_list_gen_4 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3]
    (to_list [% 1; 2; 3 ]) ;

value test_lazy_list_gen_5 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4]
    (to_list [% 1; 2; 3; 4 ]) ;

value test_lazy_list_gen_6 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1; 2; 3; 4; 5 ]) ;

value test_lazy_list_gen_7 () =
  assert_equal
    ~printer:(list_printer string_printer)
    ["hello"; "hello"; "hello"]
    (to_list [% "hello"; "hello"; "hello" ]) ;

value test_lazy_list_gen_8 () =
  assert_equal
    ~printer:(list_printer (list_printer string_of_int))
    [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]]
    (to_list [% [1; 2; 3]; [4; 5; 6]; [7; 8; 9] ]) ;

value test_lazy_list_gen_9 () =
  assert_equal
    ~printer:(list_printer (array_printer string_of_float))
    [ [| 1.; 2.; 3. |]; [| 4.; 5.; 6. |]; [| 7.; 8.; 9. |] ]
    (to_list [% [| 1.; 2.; 3. |]; [| 4.; 5.; 6. |]; [| 7.; 8.; 9. |] ]) ;

value test_lazy_list_gen_10 () =
  assert_equal
    ~printer:(list_printer test_record_printer)
    [ { x = 1; y = 1. }; { x = 2; y = 2. }; { x = 3; y = 3. } ]
    (to_list [% { x = 1; y = 1. }; { x = 2; y = 2. }; { x = 3; y = 3. } ]) ;


value suite_lazy_list_gen =
  "simple lazy list generator test" >::: [ "test 1"  >:: test_lazy_list_gen_1 ;
                                           "test 2"  >:: test_lazy_list_gen_2 ;
                                           "test 3"  >:: test_lazy_list_gen_3 ;
                                           "test 4"  >:: test_lazy_list_gen_4 ;
                                           "test 5"  >:: test_lazy_list_gen_5 ;
                                           "test 6"  >:: test_lazy_list_gen_6 ;
                                           "test 7"  >:: test_lazy_list_gen_7 ;
                                           "test 8"  >:: test_lazy_list_gen_8 ;
                                           "test 9"  >:: test_lazy_list_gen_9 ;
                                           "test 10" >:: test_lazy_list_gen_10 ] ;


value test_ef_lazy_list_gen_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1 .. 5 ]) ;

value test_ef_lazy_list_gen_2 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5; 7; 9]
    (to_list [% 1 .. 5; 7; 9 ]) ;

value test_ef_lazy_list_gen_3 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5; 7; 8; 9]
    (to_list [% 1 .. 5; 7 .. 9 ]) ;

value test_ef_lazy_list_gen_4 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5; 7; 8; 9; 10; 11; 12; 13]
    (to_list [% 1 .. 5; 7; 8 .. 13 ]) ;

value test_ef_lazy_list_gen_5 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5; 7; 9; 11; 13]
    (to_list [% 1 .. 5; 7; 9 .. 13 ]) ;

value test_ef_lazy_list_gen_6 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 4; 6; 8]
    (to_list [% 2; 4 .. 9 ]) ;

value test_ef_lazy_list_gen_7 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 3; 5; 7; 9]
    (to_list [% 1; 3 .. 10 ]) ;

value test_ef_lazy_list_gen_8 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9]
    (to_list [% 2; 3; 5 .. 9 ]) ;

value test_ef_lazy_list_gen_9 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 5; 7; 9]
    (to_list [% 1; 2; 3; 5 .. 9 ]) ;

value test_ef_lazy_list_gen_10 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [3; 5; 7; 9; 12]
    (to_list [% 3; 5 .. 9; 12 ]) ;

value test_ef_lazy_list_gen_11 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9; 12; 13; 14]
    (to_list [% 2; 3; 5 .. 9; 12 .. 14 ]) ;

value test_ef_lazy_list_gen_12 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9; 12; 14; 16; 18]
    (to_list [% 2; 3; 5 .. 9; 12; 14 .. 18 ]) ;

value suite_ef_lazy_list_gen =
  "extended finite lazy list generator test" >:::
    [ "test 1"  >:: test_ef_lazy_list_gen_1 ;
      "test 2"  >:: test_ef_lazy_list_gen_2 ;
      "test 3"  >:: test_ef_lazy_list_gen_3 ;
      "test 4"  >:: test_ef_lazy_list_gen_4 ;
      "test 5"  >:: test_ef_lazy_list_gen_5 ;
      "test 6"  >:: test_ef_lazy_list_gen_6 ;
      "test 7"  >:: test_ef_lazy_list_gen_7 ;
      "test 8"  >:: test_ef_lazy_list_gen_8 ;
      "test 9"  >:: test_ef_lazy_list_gen_9 ;
      "test 10" >:: test_ef_lazy_list_gen_10 ;
      "test 11" >:: test_ef_lazy_list_gen_11 ;
      "test 12" >:: test_ef_lazy_list_gen_12 ] ;


value test_ei_lazy_list_gen_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list ~limit:5 [% 1 .. ]) ;

value test_ei_lazy_list_gen_2 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 3; 5; 7; 8; 9; 10; 11]
    (to_list ~limit:8 [% 1; 3 .. 7; 8 .. 11]) ;

value test_ei_lazy_list_gen_3 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 3; 5; 7; 9]
    (to_list ~limit:5 [% 1; 3 .. ]) ;

value test_ei_lazy_list_gen_4 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9]
    (to_list ~limit:5 [% 2; 3; 5 .. ]) ;

value suite_ei_lazy_list_gen =
  "extended infinite lazy list generator test" >:::
    [ "test 1" >:: test_ei_lazy_list_gen_1 ;
      "test 2" >:: test_ei_lazy_list_gen_2 ;
      "test 3" >:: test_ei_lazy_list_gen_3 ;
      "test 4" >:: test_ei_lazy_list_gen_4 ] ;


value test_lazy_list_cons_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1]
    (to_list [% 1 :: [% ] ]) ;

value test_lazy_list_cons_2 () =
  assert_equal
    ~printer:(list_printer (list_printer string_of_int))
    [[]]
    (List.map to_list (to_list [% [% ] :: [% ] ])) ;

value test_lazy_list_cons_3 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1 :: [% 2; 3; 4; 5 ] ]) ;

value test_lazy_list_cons_4 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1 :: [% 2 :: [% 3; 4; 5 ] ] ]) ;

value test_lazy_list_cons_5 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1; 2 :: [% 3; 4; 5 ] ]) ;

value test_lazy_list_cons_6 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1 :: [% 2 :: [% 3 :: [% 4 :: [% 5 ] ] ] ] ]) ;

value test_lazy_list_cons_7 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1; 2; 3; 4 :: [% 5 ] ]) ;

value test_lazy_list_cons_8 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1 :: [% 2 :: [% 3 :: [% 4 :: [% 5 :: [% ] ] ] ] ] ]) ;

value test_lazy_list_cons_9 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1; 2; 3; 4; 5 :: [% ] ]) ;

value test_lazy_list_cons_10 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3; 4; 5]
    (to_list [% 1; 2 :: [% 3 .. 5 ] ]) ;

value test_lazy_list_cons_11 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9]
    (to_list ~limit:5 [% 2 :: [% 3; 5 .. ] ]) ;

value test_lazy_list_cons_12 () =
  let l = [% ] in
  assert_equal
    ~printer:(list_printer string_of_int)
    [1; 2; 3]
    (to_list [% 1 :: [% 2 :: [% 3 :: l ] ] ]) ;

value test_lazy_list_cons_13 () =
  let l = [% 3; 5 .. ] in
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 9]
    (to_list ~limit:5 [% 2 :: l ]) ;

value suite_lazy_list_cons =
  "lazy list cons test" >:::
    [ "test 1"  >:: test_lazy_list_cons_1 ;
      "test 2"  >:: test_lazy_list_cons_2 ;
      "test 3"  >:: test_lazy_list_cons_3 ;
      "test 4"  >:: test_lazy_list_cons_4 ;
      "test 5"  >:: test_lazy_list_cons_5 ;
      "test 6"  >:: test_lazy_list_cons_6 ;
      "test 7"  >:: test_lazy_list_cons_7 ;
      "test 8"  >:: test_lazy_list_cons_8 ;
      "test 9"  >:: test_lazy_list_cons_9 ;
      "test 10" >:: test_lazy_list_cons_10 ;
      "test 11" >:: test_lazy_list_cons_11 ;
      "test 12" >:: test_lazy_list_cons_12 ;
      "test 13" >:: test_lazy_list_cons_13 ] ;


value test_lazy_list_comp_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 4]
    (to_list [% x + 1 | x <- [% 1; 2; 3 ] ]) ;

value test_lazy_list_comp_2 () =
  assert_equal
    [(1, 1); (1, 2); (1, 3); (2, 1); (2, 2); (2, 3)]
    (to_list [% (i, j) | i <- [% 1; 2 ]; j <- [% 1 .. 3 ] ]) ;

value test_lazy_list_comp_3 () =
  assert_equal
    [ (1, 1); (1, 2); (1, 3); (1, 4); (1, 5);
      (1, 6); (1, 7); (1, 8); (1, 9); (1, 10) ]
    (to_list ~limit:10 [% (i, j) | i <- [% 1; 2 ]; j <- [% 1 .. ] ]) ;

value test_lazy_list_comp_4 () =
  assert_equal
    [ [(1, 1); (2, 1)]; [(1, 2); (2, 2)];
      [(1, 3); (2, 3)]; [(1, 4); (2, 4)]; [(1, 5); (2, 5)] ]
    ( List.map to_list
        (to_list ~limit:5 [% [% (i, j) | i <- [% 1; 2] ] | j <- [% 1 .. ] ]) ) ;

value test_lazy_list_comp_5 () =
  assert_equal
    [4; 2]
    (to_list [% x | xs <- [% [% (1, 2); (3, 4) ]; [% (5, 4); (3, 2) ] ]; (3, x) <- xs ]) ;

value suite_lazy_list_comp =
  "lazy list comprehension test" >:::
    [ "test 1" >:: test_lazy_list_comp_1 ;
      "test 2" >:: test_lazy_list_comp_2 ;
      "test 3" >:: test_lazy_list_comp_3 ;
      "test 4" >:: test_lazy_list_comp_4 ;
      "test 5" >:: test_lazy_list_comp_5 ] ;


value test_lazy_list_comp_guard_1 () =
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 4; 6; 8; 10; 12; 14; 16; 18; 20]
    (to_list ~limit:10 [% x | x <- [% 1 .. ]; x mod 2 = 0 ]) ;

value test_lazy_list_comp_guard_2 () =
  let rec gcd a = fun
    [ 0 -> a
    | b -> gcd b (a mod b) ]
  in
  assert_equal
    [ (2, 1); (3, 1); (3, 2); (4, 1); (4, 3);
      (5, 1); (5, 2); (5, 3); (5, 4); (6, 1) ]
    ( to_list 
        ~limit:10
        [% (i, j) | i <- [% 1 .. ]; j <- [% 1 .. i - 1 ]; gcd i j = 1 ] ) ;

value test_lazy_list_comp_guard_3 () =
  assert_equal
    [ (3, 4, 5); (5, 12, 13); (6, 8, 10);
      (8, 15, 17); (9, 12, 15); (12, 16, 20) ]
    (to_list [% (x, y, z) | x <- [% 1 .. 20 ];
                            y <- [% x .. 20 ];
                            z <- [% y .. 20 ]; x * x + y * y = z * z ]) ;

value test_lazy_list_comp_guard_4 () =
  let primes =
    let rec sieve l = [% hd l :: sieve [% x | x <- tl l; x mod hd l > 0 ] ] in
    [% 2 :: sieve [% 3; 5 .. ] ]
  in
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71]
    (to_list ~limit:20 primes) ;

value suite_lazy_list_comp_guard =
  "lazy list comprehension with guard test" >:::
    [ "test 1" >:: test_lazy_list_comp_guard_1 ;
      "test 2" >:: test_lazy_list_comp_guard_2 ;
      "test 3" >:: test_lazy_list_comp_guard_3 ;
      "test 4" >:: test_lazy_list_comp_guard_4 ] ;


value test_lazy_list_comp_let_1 () =
  assert_equal
    [ (1, 1); (2, 1); (2, 2); (2, 3); (2, 4);
      (3, 1); (3, 2); (3, 3); (3, 4); (3, 5) ]
    ( to_list
        ~limit:10 [% (i, j) | i <- [% 1 .. ]; let k = i * i; j <- [% 1 .. k ] ] ) ;

value test_lazy_list_comp_let_2 () =
  assert_equal
    [(2, 4); (3, 9); (4, 16); (5, 25); (6, 36)]
    (to_list ~limit:5 [% (i, i * i) | i <- [% 1 .. ]; let j = i * i; j > 1 ]) ;

value test_lazy_list_comp_let_3 () = () ;
(*
  assert_equal
    [(1, 1); (2, 4); (3, 9); (4, 16); (5, 25)]
    ( to_list
        ~limit:5
        [% (i, k) | i <- [% 1 ..]; let k = i * i; let j = k * k in i < 10 ] ) ;
      (* Warning Y: unused variable j. *)
 *)

value test_lazy_list_comp_let_4 () = () ;
(*
  assert_equal
    [(2, 4); (3, 9); (4, 16); (5, 25); (6, 36)]
    (to_list ~limit:5 [% (i, i * i) | i <- [% 1 .. ]; let j = i * i; i > 1 ]) ;
      (* Warning Y: unused variable j. *)
 *)

value test_lazy_list_comp_let_5 () =
  assert_equal
    [(2, 4); (3, 9); (4, 16); (5, 25); (6, 36)]
    (to_list ~limit:5 [% (i, j) | i <- [% 1 .. ]; let j = i * i; i > 1 ]) ;

value test_lazy_list_comp_let_6 () =
  assert_equal
    [(2, 4); (3, 9); (4, 16); (5, 25); (6, 36)]
    (to_list ~limit:5 [% (i, j) | i <- [% 1 .. ]; let j = i * i; j > 1 ]) ;

value test_lazy_list_comp_let_7 () =
  assert_equal
    [(1, 1); (2, 1); (2, 2); (2, 3); (2, 4)]
    ( to_list
        ~limit:5 [% (i, j) | i <- [% 1 ..];
                             let k = i * i; let l = k * k; j <- [% 1 .. l ] ] ) ;

value test_lazy_list_comp_let_8 () =
  assert_equal
    [(1, 1); (2, 16); (3, 81); (4, 256); (5, 625)]
    ( to_list
        ~limit:5
        [% (i, j) | i <- [% 1 ..]; let k = i * i; let j = k * k; i < 10 ] ) ;

value test_lazy_list_comp_let_9 () =
  assert_equal
    [(1, 1); (2, 16); (3, 81); (4, 256); (5, 625)]
    ( to_list
        ~limit:5
        [% (i, j) | i <- [% 1 ..]; i < 10; let k = i * i; let j = k * k ] ) ;

value test_lazy_list_comp_let_10 () =
  assert_equal
    [(3, 81); (4, 256); (5, 625); (6, 1296); (7, 2401)]
    ( to_list
        ~limit:5
        [% (i, j) | i <- [% 1 ..]; let k = i * i; let j = k * k; i > 2; i < 10 ] ) ;

value test_lazy_list_comp_let_11 () =
  assert_equal
    [(3, 81); (4, 256); (5, 625); (6, 1296); (7, 2401)]
    ( to_list
        ~limit:5
        [% (i, j) | i <- [% 1 ..]; i > 2; i < 10; let k = i * i; let j = k * k ] ) ;

value test_lazy_list_comp_let_12 () =
  assert_equal
    [(3, 81, 0); (3, 81, 1); (4, 256, 0); (4, 256, 1); (5, 625, 0)]
    ( to_list
        ~limit:5
        [% (i, j, l) | i <- [% 1 ..]; let k = i * i; let j = k * k; i > 2;
                       i < 10; l <- [% 0; 1 ] ] ) ;

value test_lazy_list_comp_let_13 () =
  assert_equal
    [(3, 81, 0); (3, 81, 1); (4, 256, 0); (4, 256, 1); (5, 625, 0)]
    ( to_list
        ~limit:5
        [% (i, j, l) | i <- [% 1 ..]; i > 2; i < 10;
                       let k = i * i; let j = k * k; l <- [% 0; 1 ] ] ) ;

value test_lazy_list_comp_let_14 () = () ;
(*
  assert_equal
    [(3, 81, 0); (3, 81, 1); (4, 256, 0); (4, 256, 1); (5, 625, 0)]
    ( to_list
        ~limit:5
        [% (i, j, l) | i <- [% 1 ..]; i > 2; i < 10;
                       let k = i * i and j = i * i * i * i; l <- [% 0; 1 ] ] ) ;
      (* Warning Y: unused variable k. *)
 *)

value test_lazy_list_comp_let_15 () =
  assert_equal
    [(1, 1); (2, 2); (3, 6); (4, 24); (5, 120)]
    ( to_list
        ~limit:5
        [% (i, fact i) | i <- [% 1 .. ];
                         let rec fact = fun [ 0 -> 1
                                            | n -> n * fact (n - 1) ] ] ) ;

value test_lazy_list_comp_let_16 () =
  assert_equal
    [(1, 1); (2, 2); (3, 6); (4, 24); (5, 120)]
    ( to_list
        ~limit:5
        [% (i, j) | i <- [% 1 .. ];
                    let rec fact = fun [ 0 -> 1 | n -> n * fact (n - 1) ];
                    let j = fact i ] ) ;

value test_lazy_list_comp_let_17 () =
  assert_equal
    [True; False; True; False; True; False; True; False; True; False]
    (to_list [% odd i | i <- [% 1 .. 10];
                        let rec odd  = fun [ 0 -> False | n -> even (n - 1) ]
                        and     even = fun [ 0 -> True  | n -> odd (n - 1) ] ]) ;

value suite_lazy_list_comp_let =
  "lazy list comprehension with let binding test" >:::
    [ "test 1"  >:: test_lazy_list_comp_let_1 ;
      "test 2"  >:: test_lazy_list_comp_let_2 ;
      "test 3"  >:: test_lazy_list_comp_let_3 ;
      "test 4"  >:: test_lazy_list_comp_let_4 ;
      "test 5"  >:: test_lazy_list_comp_let_5 ;
      "test 6"  >:: test_lazy_list_comp_let_6 ;
      "test 7"  >:: test_lazy_list_comp_let_7 ;
      "test 8"  >:: test_lazy_list_comp_let_8 ;
      "test 9"  >:: test_lazy_list_comp_let_9 ;
      "test 10" >:: test_lazy_list_comp_let_10 ;
      "test 11" >:: test_lazy_list_comp_let_11 ;
      "test 12" >:: test_lazy_list_comp_let_12 ;
      "test 13" >:: test_lazy_list_comp_let_13 ;
      "test 14" >:: test_lazy_list_comp_let_14 ;
      "test 15" >:: test_lazy_list_comp_let_15 ;
      "test 16" >:: test_lazy_list_comp_let_16 ;
      "test 17" >:: test_lazy_list_comp_let_17 ] ;


value test_lazy_list_pattern_matching_1 () =
  let primes =
    let rec sieve = fun
        [ [% hd :: tl ] -> [% hd :: sieve [% x | x <- tl; x mod hd > 0 ] ] ]
    in
    [% 2 :: sieve [% 3; 5 .. ] ]
  in
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71]
    (to_list ~limit:20 primes) ;

value test_lazy_list_pattern_matching_2 () =
  let primes_upto_50 =
    let rec sieve = fun
      [ [% ]          -> [% ]
      | [% hd :: tl ] -> [% hd :: sieve [% x | x <- tl; x mod hd > 0 ] ] ]
    in
    [% 2 :: sieve [% 3; 5 .. 50 ] ]
  in
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47]
    (to_list primes_upto_50) ;

value test_lazy_list_pattern_matching_3 () =
  let primes_upto_50 =
    let rec sieve = fun
      [ [% hd :: tl ] -> [% hd :: sieve [% x | x <- tl; x mod hd > 0 ] ]
      | [% ]          -> [% ] ]
    in
    [% 2 :: sieve [% 3; 5 .. 50 ] ]
  in
  assert_equal
    ~printer:(list_printer string_of_int)
    [2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47]
    (to_list primes_upto_50) ;

value test_lazy_list_pattern_matching_4 () = () ;
  match [% (1, 2, [% 3; 4]); (5, 6, [% 7; 8])] with
  [ [% (a, b, [% c; d ]) :: _ ] ->
      do { assert_equal ~printer:string_of_int 1 a ;
           assert_equal ~printer:string_of_int 2 b ;
           assert_equal ~printer:string_of_int 3 c ;
           assert_equal ~printer:string_of_int 4 d }
  | _ -> assert_failure "match error" ] ;

value test_lazy_list_pattern_matching_5 () = () ;
  ( fun
    [ [% [% x ]; [% y ]; [% z ]] ->
        do { assert_equal
               ~printer:(list_printer string_of_int)
               [1; 2; 3] (to_list x) ;
             assert_equal
               ~printer:(list_printer string_of_int)
               [4; 5; 6] (to_list y) ;
             assert_equal
               ~printer:(list_printer string_of_int)
               [7; 8; 9] (to_list z) }
    | _ -> assert_failure "match error" ] ) [% [% [% 1; 2; 3] ];
                                               [% [% 4; 5; 6] ];
                                               [% [% 7; 8; 9] ] ] ;

value test_lazy_list_pattern_matching_6 () = () ;
  match [% { x = 1; y = 2. }; { x = 3; y = 4. } ] with
  [ [% { x = 1; y = 3. } :: _ ] ->
      assert_failure "match error"
  | [% { x = 1; y = y } :: tl ] ->
      do { assert_equal ~printer:string_of_float 2. y ;
           assert_equal
             ~printer:(list_printer test_record_printer)
             [ { x = 3; y = 4. } ]
             (to_list tl) }
  | [% { x = x; y = y } :: _ ] ->
      assert_failure "match_error" ] ;

value test_lazy_list_pattern_matching_7 () =
  match [% (1, 2); (3, 4); (5, 6); (7, 8) ] with
  [ [% (1, 2); x; y; (a, b) ] ->
      do { assert_equal ~printer:(pair_printer string_of_int) (3, 4) x ;
           assert_equal ~printer:(pair_printer string_of_int) (5, 6) y ;
           assert_equal ~printer:(pair_printer string_of_int) (7, 8) (a, b) } ] ;

value test_lazy_list_pattern_matching_8 () =
  match [% (1, 2); (3, 4); (5, 6); (7, 8) ] with
  [ [% (1, 2); x :: tl ] ->
      do { assert_equal ~printer:(pair_printer string_of_int) (3, 4) x ;
           assert_equal
             ~printer:(list_printer (pair_printer string_of_int))
             [(5, 6); (7, 8)]
             (to_list tl) } ] ;

value suite_lazy_list_pattern_matching =
  "lazy list pattern matching test" >:::
    [ "test 1"  >:: test_lazy_list_pattern_matching_1 ;
      "test 2"  >:: test_lazy_list_pattern_matching_2 ;
      "test 3"  >:: test_lazy_list_pattern_matching_3 ;
      "test 4"  >:: test_lazy_list_pattern_matching_4 ;
      "test 5"  >:: test_lazy_list_pattern_matching_5 ;
      "test 6"  >:: test_lazy_list_pattern_matching_6 ;
      "test 7"  >:: test_lazy_list_pattern_matching_7 ;
      "test 8"  >:: test_lazy_list_pattern_matching_8 ] ;


value tests =
  "Lazy list comprehension (revised syntax) test" >:::
    [ suite_lazy_list_gen ;
      suite_ef_lazy_list_gen ;
      suite_ei_lazy_list_gen ;
      suite_lazy_list_cons ;
      suite_lazy_list_comp ;
      suite_lazy_list_comp_guard ;
      suite_lazy_list_comp_let ;
      suite_lazy_list_pattern_matching ] ;

value _ = run_test_tt tests ;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.