-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.ml
More file actions
executable file
·1696 lines (1597 loc) · 68.7 KB
/
main.ml
File metadata and controls
executable file
·1696 lines (1597 loc) · 68.7 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
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*M
Tests of the Sosa library
=========================
Test Utilities
--------------
M*)
open Nonstd
module String = StringLabels
open Printf
open Sosa
open Sosa_utilities
let say fmt = printf (fmt ^^ "\n%!")
let should_do_benchmarks =
try Sys.argv.(1) = "bench" with _ -> false
let cartesian_product list1 list2 =
if list2 = [] then [] else
let rec loop l1 l2 accum = match l1 with
| [] -> accum
| (hd :: tl) ->
loop tl l2
(List.rev_append
(List.map ~f:(fun x -> (hd,x)) l2)
accum)
in
List.rev (loop list1 list2 [])
let return_code = ref 0
let should_not_return_zero () = return_code := 5
let test_assert msg cond =
if not cond then (
should_not_return_zero ();
say ">> TEST FAILED: [%s]" msg
) else ()
let test_assertf cond fmt =
ksprintf (fun s -> test_assert s cond) fmt
let make_string = String.init
let list_dot_init l f =
Array.init l f |> Array.to_list
let random_string i =
let length = Random.int i in
make_string length (fun _ -> char_of_int (Random.int 256))
let random_ascii_string i =
let length = Random.int i in
make_string length (fun _ -> char_of_int (Random.int 128))
let random_utf8_string i =
let length = Random.int i in
list_dot_init length (fun _ -> Random.int 0x10_FFFF)
|> List.map ~f:Int_utf8_character.to_native_string
|> String.concat ~sep:""
let test_native_subjects =
"" :: "A" :: "\x00" :: "Invalid UTF-8: \197"
:: "Invalid UTF-8 again: \197\000"
:: "Invalid UTF-8 again: \197\000 "
:: list_dot_init 20 (fun i -> random_string (i * 4 + 1))
@ list_dot_init 20 (fun i -> random_ascii_string (i * 4 + 1))
@ list_dot_init 20 (fun i -> random_utf8_string (i * 4 + 1))
(*M
This is a set of common denominator native strings, i.e., string that can be
converted to every other representation. We call them DNA and use only `A`,
`C`, `G`, `T` for future implementations which will be using only 2 or 4
bits per character.
M*)
let dna_test_subjects =
let random_read _ =
make_string (Random.int 300 + 1) (fun _ ->
begin match Random.int 4 with
| 0 -> 'A'
| 1 -> 'C'
| 2 -> 'G'
| _ -> 'T'
end) in
list_dot_init 200 random_read
(*M
Benchmarks
----------
If asked on the command line, each test will run some benchmarks on the
implementation.
M*)
module Benchmark = struct
let now () = Unix.gettimeofday ()
let benchmarks_table = ref []
let add ~implementation ~experiment ~result =
match List.Assoc.get implementation !benchmarks_table with
| Some exps ->
exps := (experiment, result) :: !exps
| None ->
benchmarks_table := (implementation, ref [experiment, result]) :: !benchmarks_table
let measure ?(repeats=1000) f =
let start = now () in
for i = 1 to repeats do
f ()
done;
let stop = (now ()) in
(1000. *. (stop -. start) /. (float repeats))
let declare ?repeats ~implementation ~experiment f =
if should_do_benchmarks then
let time = measure ?repeats f in
let result = sprintf "%.3f ms" time in
add ~implementation ~experiment ~result
else ()
let to_string () =
let experiments =
List.map !benchmarks_table ~f:(fun (_, l) ->
List.map !l ~f:(fun (e, _) -> e))
|> List.concat |> List.dedup in
let first_row =
"Implementation" :: experiments
in
let row_widths =
List.map first_row (fun s -> ref (String.length s)) in
(* say "row widths: %s" (String.concat ~sep:", "
(List.map row_widths (fun r -> sprintf "%d" !r))); *)
let other_rows =
List.map !benchmarks_table (fun (impl, l) ->
let w = List.nth_exn row_widths 0 in
w := max !w (String.length impl);
impl :: List.mapi experiments (fun i exp ->
let res = List.Assoc.get exp !l
|> Option.value_exn ~msg:"assoc experiments" in
let w = List.nth_exn row_widths (i + 1) in
(* say "w: %d, i: %d lgth: %d" !w i (String.length res); *)
w := max !w (String.length res);
res)) in
let row_to_string row =
row
|> List.mapi ~f:(fun i c ->
(* say "%d %s %d %d" i c !(List.nth_exn row_widths i) (String.length c); *)
sprintf "%s%s" c
(String.make (1 + !(List.nth_exn row_widths i) - String.length c) ' '))
|> String.concat ~sep:" "
in
sprintf "%s\n%s\n%s\n"
(first_row |> row_to_string)
(List.map row_widths (fun s -> String.make (!s) '-')
|> String.concat ~sep:" ")
(other_rows
|> List.map ~f:row_to_string
|> String.concat ~sep:"\n")
end
(*M
Test with First-Class Modules
-----------------------------
The function `do_basic_test` below takes a whole OCaml module implementation as
argument; `TEST_STRING` is the expected signature:
M*)
module type TEST_STRING = sig
val test_name: string
val can_have_wrong_char: bool
module Chr: Api.BASIC_CHARACTER
module Str: Api.BASIC_STRING with type character := Chr.t
end
let do_basic_test (module Test : TEST_STRING) =
let open Test in
say "### Test %S" test_name;
test_assertf (Str.length Str.empty = 0) "(length empty)";
test_assertf (Str.is_empty Str.empty) "(is_empty empty)";
begin match Str.of_native_string "" with
| `Ok o -> test_assertf (Str.is_empty o) "(is_empty \"\")";
| `Error _ -> test_assertf false "Str.of_native_string %S -> Error" ""
end;
begin (* test of_/to_ native_string *)
let test_ofto s =
begin match Str.of_native_string s with
| `Ok s2 ->
(* We test that when we can transform from `s2`, the opposite
conversion works: *)
let back = Str.to_native_string s2 in
test_assert (sprintf "test_ofto %S <> %S" s back) (s = back);
(* We test `Str.fold` against a potentially *very* slow
implementation using `Str.length` and `Str.get`. *)
let folding ~init ~f to_string =
let fold = Str.fold s2 ~init ~f in
let refold =
let r = ref init in
for i = 0 to Str.length s2 - 1 do
r := f !r (Option.value_exn ~msg:"folding" (Str.get s2 ~index:i));
done;
!r in
test_assertf (fold = refold) "\nfold: %s\nrefold: %s"
(to_string fold) (to_string refold)
in
folding ~init:[] ~f:(fun p c -> c :: p)
(fun c -> String.concat ~sep:", " (List.map c Chr.to_string_hum));
folding ~init:42 ~f:(fun p c -> Hashtbl.hash (p, c)) (sprintf "%d");
(* This a function that displays an Str.t (extracts the
beginning and its size(s)) *)
let str_to_hum s =
sprintf "[%S.%d,%dB]"
(Str.to_native_string s |> sprintf "%s"
|> (fun s -> String.sub s 0 (min (String.length s) 10)))
(Str.length s)
(Str.to_native_string s |> String.length) in
(* We test `Str.sub` by comparing it with an implementation
based on `Str.get`. *)
let subbing ~index ~length =
let subopt = Str.sub s2 ~index ~length in
let r = ref [] in
for i = index to index + length - 1 do
r := Str.get s2 ~index:i :: !r
done;
begin match subopt with
| Some sub ->
(* If `Str.sub` returned some then `r` contains all the
characters in reverse order: *)
let bus =
test_assertf (List.for_all !r ((<>) None)) "sub %d %d: r has a None"
index length;
Str.concat ~sep:Str.empty
(List.rev_map (List.filter_opt !r) ~f:(Str.of_character)) in
(* We compare the result `sub` and the re-computed version `bus`: *)
test_assertf (Str.compare sub bus = 0) "sub %s %d %d\n→ Some %s ≠ %s"
(str_to_hum s2) index length (str_to_hum sub) (str_to_hum bus)
| None ->
test_assertf (!r = [] || List.exists !r ((=) None))
"sub %s %d %d → None" (str_to_hum s2) index length
end
in
subbing ~index:0 ~length:0;
subbing ~index:0 ~length:(Str.length s2);
subbing ~index:2 ~length:(Str.length s2);
subbing ~index:4 ~length:(Str.length s2);
subbing ~index:5 ~length:(Str.length s2);
subbing ~index:0 ~length:1;
subbing ~index:0 ~length:3;
subbing ~index:0 ~length:30;
subbing ~index:1 ~length:30;
subbing ~index:2 ~length:30;
subbing ~index:4 ~length:3;
subbing ~index:40 ~length:3;
subbing ~index:400 ~length:3;
subbing ~index:0 ~length:(Str.length s2 - 0);
subbing ~index:2 ~length:(Str.length s2 - 2);
subbing ~index:4 ~length:(Str.length s2 - 4);
subbing ~index:5 ~length:(Str.length s2 - 5);
subbing ~index:0 ~length:(Str.length s2 - 0 - 1);
subbing ~index:2 ~length:(Str.length s2 - 2 - 1);
subbing ~index:4 ~length:(Str.length s2 - 4 - 1);
subbing ~index:5 ~length:(Str.length s2 - 5 - 1);
| `Error (`wrong_char_at i) ->
(* If the conversion fails, we check that the error value points
to an invalid character: *)
test_assert (sprintf "test_ofto %S -> wrong char at index %d" s i)
(Chr.read_from_native_string ~buf:s ~index:i = None)
end;
in
List.iter test_native_subjects test_ofto;
end;
begin (* test concat and a bit more *)
let tried_separators = ref 0 in
let rec try_separators n =
if n = 0
then
if !tried_separators < 10 then
say "WARNING: %s -> try_separators did not try much (%d separators)"
test_name !tried_separators
else ()
else
let sep = random_string n in
begin match Str.of_native_string sep with
| `Ok csep ->
let selection =
List.filter test_native_subjects (fun _ -> Random.bool ()) in
let viable_strings, converted =
let zipped =
List.filter_map selection (fun s ->
match Str.of_native_string s with
| `Ok s2 -> Some (s, s2)
| `Error (`wrong_char_at c) -> None) in
List.map zipped ~f:fst, List.map zipped ~f:snd
in
let concated = String.concat ~sep viable_strings in
let concated2 = Str.concat ~sep:csep converted in
(* say "separators %S" sep; *)
incr tried_separators;
test_assertf (Str.to_native_string concated2 = concated)
"try_separators %d (%dth): %S %s →\n %S Vs\n %s" n !tried_separators sep
(String.concat ~sep:", " (List.map viable_strings (sprintf "%S")))
concated
(Str.to_string_hum concated2);
try_separators (n - 1)
| `Error _ -> try_separators (n - 1)
end
in
try_separators 800;
end;
(* This tests `make` against `length` and `get`: *)
for i = 0 to 100 do
let seed = 50 * (i + 1) in
let char = Random.int seed in
let length = Random.int seed in
match Chr.of_int char with
| Some character ->
let s = Str.make length character in
test_assertf (Str.length s = length) "length of make";
for j = 0 to length - 1 do
test_assertf ((Str.get s j) = Some character) "nth char of make"
done;
| None -> ()
done;
begin (* We test the`Str.Make_output` functor with `Buffer.t` by writing
directly the transformable functions and though Out.output and
comparing the resulting buffer contents. *)
let module Out = Str.Make_output(struct
type ('a, 'b, 'c) thread = 'a
type ('a, 'b, 'c) channel = Buffer.t
let return x = x
let bind x f = f x
let output buf s =
(* say "adding %S" s;; *)
Buffer.add_string buf s
end)
in
let buf_ground = Buffer.create 42 in
let buf_through_str = Buffer.create 42 in
let there_was_an_error = ref None in
List.iter test_native_subjects (fun s ->
match Str.of_native_string s with
| `Ok o ->
Out.output buf_through_str o;
Buffer.add_string buf_ground s;
| `Error (`wrong_char_at i) ->
there_was_an_error := Some i);
test_assertf (Buffer.contents buf_ground = Buffer.contents buf_through_str)
"Str.Make_output test %S, %S"
(Buffer.contents buf_ground)
(Buffer.contents buf_through_str);
end;
begin (* Some tests of `for_all` and `exists`: *)
List.iter test_native_subjects (fun str ->
match Str.of_native_string str with
| `Ok o ->
test_assertf (Str.for_all o (fun _ -> true) = true) "∀ true = true";
test_assertf (Str.for_all o (fun _ -> false) = false || Str.is_empty o)
"∀ false in %S = false" str;
test_assertf (Str.exists o (fun _ -> true) = true || Str.is_empty o)
"∃ true => true";
test_assertf (Str.exists o (fun _ -> false) = false)
"∃ false in %S = false" str;
let i_did_false = ref false in
let comp = Str.for_all o (fun _ ->
if Random.bool () then true else (i_did_false := true; false)) in
test_assertf (comp = not !i_did_false) "random test for_all";
let i_did_true = ref false in
let comp = Str.exists o (fun _ ->
if Random.bool () then (i_did_true := true; true) else false) in
test_assertf (comp = !i_did_true) "random test exists";
| `Error (`wrong_char_at i) -> ()
);
end;
begin (* of_native_substring *)
(* First some basic tests of Str.of_native_substring, then the
bigger test with all the test strings. *)
test_assertf (Str.of_native_substring "" ~offset:0 ~length:0 = `Ok Str.empty)
"sub '' 0 0 = ''";
test_assertf (Str.of_native_substring "" ~offset:0 ~length:1 = `Error `out_of_bounds)
"sub '' 0 1 → out_of_bounds";
test_assertf (Str.of_native_substring "" ~offset:1 ~length:0 = `Ok Str.empty)
"sub '' 1 0 → ''";
test_assertf (Str.of_native_substring "" ~offset:1 ~length:1 = `Error `out_of_bounds)
"sub '' 1 1 → out_of_bounds";
let i_have_been_to_ok = ref false in
let i_have_been_to_wrong_char = ref false in
let i_have_been_to_out_of_bounds = ref false in
List.iter test_native_subjects begin fun str ->
let offset = Random.int 42 in
let length = Random.int 42 in
let substr = try (String.sub str offset length) with _ -> "" in
begin match Str.of_native_substring str ~offset ~length with
| `Ok _ as o ->
i_have_been_to_ok := true;
test_assertf (o = (Str.of_native_string substr))
"sub %S %d %d → Ok" str offset length;
| `Error (`wrong_char_at c) ->
i_have_been_to_wrong_char := true;
test_assertf (Str.of_native_string substr = `Error (`wrong_char_at (c - offset)))
"sub %S %d %d → Ok" str offset length;
| `Error `out_of_bounds ->
i_have_been_to_out_of_bounds := true;
test_assertf (substr = "") "sub out_of_bounds"
end;
end;
test_assertf !i_have_been_to_ok "i_have_been_to_ok";
test_assertf (!i_have_been_to_wrong_char || not can_have_wrong_char)
"i_have_been_to_wrong_char";
test_assertf !i_have_been_to_out_of_bounds "i_have_been_to_out_of_bounds";
end;
let int_list_to_string l =
sprintf "[%s]"
(List.map ~f:Int.to_string l |> String.concat ~sep:",") in
let str_to_int_list s =
Str.to_character_list s |> List.map ~f:Chr.to_int in
let int_option_to_string io =
Option.value_map ~default:"None" ~f:(sprintf "Some %d") io in
begin (* index_of_character{,_reverse} *)
let test ?from ?should_find l c =
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
let ch =
Option.value_exn ~msg:"test index_of_character" (Chr.of_int c) in
let res = Str.index_of_character ?from s ch in
test_assertf (res = should_find)
"index_of_character: %s (from: %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string should_find)
(int_option_to_string res);
if from = None then (
let from = Some 0 in
let res = Str.index_of_character ?from s ch in
test_assertf (res = should_find)
"index_of_character: %s (added-from: %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string should_find)
(int_option_to_string res);
);
in
test [] 0;
test [1] 0;
test [1;2;3;4] 0;
test [0] 0 ~should_find:0;
test [1;2;0] 0 ~should_find:2;
test ~from:1 [] 0;
test ~from:1 [1] 0;
test ~from:1 [1;2;3;4] 0;
test ~from:1 [0] 0;
test ~from:1 [1;2;0] 0 ~should_find:2;
test ~from:(-1) [] 0;
test ~from:(-1) [1] 0;
test ~from:(-1) [1;2;3;4] 0;
test ~from:(-1) [0] 0 ~should_find:0;
test ~from:(-1) [1;2;0] 0 ~should_find:2;
test ~from:4 [] 0;
test ~from:4 [1] 0;
test ~from:4 [1;2;3;4] 0;
test ~from:4 [0] 0;
test ~from:4 [1;2;0] 0;
let test ?from ?should_find l c =
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
let ch =
Option.value_exn ~msg:"test index_of_character" (Chr.of_int c) in
let res = Str.index_of_character_reverse ?from s ch in
test_assertf (res = should_find)
"index_of_character_reverse: %s (from: %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string should_find)
(int_option_to_string res);
if from = None then (
let from = Some (Str.length s - 1) in
let res = Str.index_of_character_reverse ?from s ch in
test_assertf (res = should_find)
"index_of_character_reverse: %s (added-from: %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string should_find)
(int_option_to_string res);
);
in
test [] 0;
test [1] 0;
test [1;2;3;4] 0;
test [0] 0 ~should_find:0;
test [1;2;0] 0 ~should_find:2;
test ~from:1 [] 0;
test ~from:1 [1] 0;
test ~from:1 [1;2;3;4] 0;
test ~from:1 [0] 0 ~should_find:0;
test ~from:1 [1;2;0] 0;
test ~from:1 [1;2;0] 1 ~should_find:0;
test ~from:(-1) [] 0;
test ~from:(-1) [1] 0;
test ~from:(-1) [1;2;3;4] 0;
test ~from:(-1) [0] 0;
test ~from:(-1) [1;2;0] 0;
test ~from:4 [] 0;
test ~from:4 [1] 0;
test ~from:4 [1;2;3;4] 0;
test ~from:4 [0] 0 ~should_find:0;
test ~from:4 [1;2;0] 0 ~should_find:2;
(* A test of index_of_character and index_of_character_reverse, we
create a big cartesian product
(nat_string, (from_index, char_to_find)) and we run both searches. *)
let froms = List.init 10 (fun i -> Random.int (i + 1)) in
let chars = List.init 10 (fun i -> Chr.of_int i) |> List.filter_opt in
let to_do =
List.(cartesian_product test_native_subjects (cartesian_product froms chars))
in
let i_went_to_some_index = ref 0 in
let i_went_to_none_from_length = ref 0 in
let i_went_to_none_from_absent = ref 0 in
let rev_i_went_to_some_index = ref 0 in
let rev_i_went_to_none_from_length = ref 0 in
let rev_i_went_to_none_from_absent = ref 0 in
List.iter to_do (fun (nat, (from, char)) ->
match Str.of_native_string nat with
| `Ok o ->
begin match Str.index_of_character o ~from char with
| Some index ->
incr i_went_to_some_index;
test_assertf (Str.get o index = Some char) "find | get";
| None ->
if from > Str.length o - 1 then (incr i_went_to_none_from_length)
else begin
for i = from to Str.length o - 1 do
test_assertf (Str.get o i <> Some char)
"not found => can't find, i: %d, from : %d, length: %d"
i from (Str.length o);
done;
incr i_went_to_none_from_absent;
end
end;
begin match Str.index_of_character_reverse o ~from char with
| Some index ->
incr rev_i_went_to_some_index;
test_assertf (Str.get o index = Some char) "rev, find | get";
| None ->
if from > Str.length o - 1 then (incr rev_i_went_to_none_from_length)
else begin
for i = from downto 0 do
test_assertf (Str.get o i <> Some char)
"rev, not found => can't find, i: %d, from : %d, length: %d"
i from (Str.length o);
done;
incr rev_i_went_to_none_from_absent;
end;
end;
| _ -> ());
test_assertf (!i_went_to_some_index > 0) "";
test_assertf (!i_went_to_none_from_length > 0) "";
test_assertf (!i_went_to_none_from_absent > 0) "";
test_assertf (!rev_i_went_to_some_index > 0) "";
test_assertf (!rev_i_went_to_none_from_length > 0) "";
test_assertf (!rev_i_went_to_none_from_absent > 0) "";
end;
begin (* Test compare_substring{_strict} *)
(* A first test of compare_substring{_strict} with special cases, empty strings,
and small strings, containing 'a', 'c', 'g', 't' → they should
be convertible to any backend :) *)
let is_equivalent resopt expected =
(match resopt with
| None -> false
| Some r -> r = expected || r * expected > 0) in
let test_compare_substring (a, idxa, lena) (b, idxb, lenb) expected =
match Str.of_native_string a, Str.of_native_string b with
| `Ok aa, `Ok bb ->
let res = Str.compare_substring (aa, idxa, lena) (bb, idxb, lenb) in
test_assertf (res = expected || res * expected > 0) (* We test for the sign *)
"test_compare_substring (%S, %d, %d) (%S, %d, %d) = %d, × %d < 0"
a idxa lena b idxb lenb res expected;
let resopt = Str.compare_substring_strict (aa, idxa, lena) (bb, idxb, lenb) in
test_assertf (is_equivalent resopt expected)
"test_compare_substring_strict (%S, %d, %d) (%S, %d, %d) = %d, × %d < 0"
a idxa lena b idxb lenb res expected;
(* And now check commutativity: *)
let invres = Str.compare_substring (bb, idxb, lenb) (aa, idxa, lena) in
test_assertf (invres = (~- expected) || invres * expected < 0) (* We test for the sign *)
"test_compare_substring, commutes (%S, %d, %d) (%S, %d, %d) × -1 = %d, × %d < 0"
a idxa lena b idxb lenb res expected;
let resopt = Str.compare_substring_strict (bb, idxb, lenb) (aa, idxa, lena) in
test_assertf (is_equivalent resopt (~- expected))
"test_compare_substring_strict, commutes (%S, %d, %d) (%S, %d, %d) = %d, × %d < 0"
a idxa lena b idxb lenb res expected;
| _, _ -> test_assertf false "assumption about ACGT is wrong"
in
(* Semantically well-defined tests: *)
test_compare_substring ("", 0, 0) ("", 0, 0) ( 0);
test_compare_substring ("aaa", 0, 0) ("", 0, 0) ( 0);
test_compare_substring ("aaa", 0, 0) ("ggg", 0, 0) ( 0);
test_compare_substring ("aaa", 1, 0) ("ggg", 1, 0) ( 0);
test_compare_substring ("aaa", 1, 0) ("ggg", 1, 0) ( 0);
test_compare_substring ("aaa", 0, 0) ("ggg", 0, 1) (-1);
test_compare_substring ("aaa", 1, 0) ("ggg", 1, 1) (-1);
test_compare_substring ("aaa", 1, 0) ("ggg", 1, 1) (-1);
test_compare_substring ("aaa", 1, 1) ("ggg", 1, 1) (-1);
test_compare_substring ("aga", 1, 1) ("ggc", 1, 1) ( 0);
test_compare_substring ("aga", 1, 1) ("gag", 1, 1) ( 1);
test_compare_substring ("aga", 1, 1) ("gcg", 1, 1) ( 1);
test_compare_substring ("aagg", 2, 2) ("gg", 0, 2) ( 0);
(* A test of the out-of-bounds behavior: *)
let test_compare_substring_strictness (a, idxa, lena) =
match Str.of_native_string a, Str.of_native_string "acgt" with
| `Ok aa, `Ok bb ->
test_assertf (Str.compare_substring_strict (aa, idxa, lena) (Str.empty, 0, 0) = None)
"Str.compare_substring_strict out_of_bounds 1";
test_assertf (Str.compare_substring_strict (aa, idxa, lena) (bb, 1, 2) = None)
"Str.compare_substring_strict out_of_bounds 2";
test_assertf (Str.compare_substring_strict (Str.empty, 0, 0) (aa, idxa, lena) = None)
"Str.compare_substring_strict out_of_bounds 3";
test_assertf (Str.compare_substring_strict (bb, 1, 2) (aa, idxa, lena) = None)
"Str.compare_substring_strict out_of_bounds 4";
| _ -> test_assertf false "assumption about ACGT is wrong"
in
test_compare_substring_strictness ("", 0, 1);
test_compare_substring_strictness ("a", 1, 1);
test_compare_substring_strictness ("a", -1, 1);
test_compare_substring_strictness ("a", 1, -1);
test_compare_substring_strictness ("a", -1, -1);
test_compare_substring_strictness ("aa", 10, 1);
test_compare_substring_strictness ("aa", 10, -1);
(* Now we run a bigger randomized test of compare_substring{_strict}. *)
let been_to_some_0 = ref 0 in
let been_to_some_m = ref 0 in
List.iter test_native_subjects (fun a ->
List.iter test_native_subjects (fun b ->
match Str.of_native_string a, Str.of_native_string b with
| `Ok aa, `Ok bb ->
let rec test n =
let length_a = Str.length aa in
let length_b = Str.length bb in
let lena = Random.int (length_a + 5) in
let idxa = Random.int (lena + 5) in
let idxb, lenb =
if Random.bool ()
then (Random.int (length_b + 5), Random.int (length_b + 5))
else (idxa, lena) (* half of the times with same params *)
in
let res =
Str.compare_substring (aa, idxa, lena) (bb, idxb, lenb) in
let resopt =
Str.compare_substring_strict (aa, idxa, lena) (bb, idxb, lenb) in
begin match res with
| 0 -> (* EQUAL *)
test_assertf (lena = lenb)
"compare_substring: equal but lengths %d ≠ %d" lena lenb;
for i = 0 to min lena lenb - 1 do
test_assertf ((Str.get aa (i + idxa)) = (Str.get bb (i + idxb)))
"compare_substring: equal but different …"
done;
test_assertf (is_equivalent resopt res)
"resopt Vs res in EQUAL case";
incr been_to_some_0;
| m ->
let suba = Str.sub aa ~index:idxa ~length:(lena) in
let subb = Str.sub bb ~index:idxb ~length:(lenb) in
begin match suba, subb with
| Some sa, Some sb ->
(* Well defined case: "sub" returns something for both *)
test_assertf (Str.compare sa sb * m > 0)
"%d instead of %d sa: %s sb:%s (lena: %d, lenb: %d)" m (Str.compare sa sb)
(Str.to_string_hum sa) (Str.to_string_hum sb) lena lenb;
test_assertf (is_equivalent resopt res)
"strict: %s instead of %d sa: %s sb:%s (lena: %d, lenb: %d)"
(Option.value_map ~default:"None" resopt ~f:(sprintf "Some %d"))
(Str.compare sa sb)
(Str.to_string_hum sa) (Str.to_string_hum sb) lena lenb;
| _, _ -> ()
end;
incr been_to_some_m;
()
end;
if n > 0 then test (n - 1);
in
test 4
| _, _ -> ()
)
);
test_assertf (!been_to_some_0 > 5) "been_to_some_0: %d" !been_to_some_0;
test_assertf (!been_to_some_m > 5) "been_to_some_m: %d" !been_to_some_m;
end;
begin (* We test index_of_string and index_of_string_reverse, if we expect
the same result we may give only `~expect` if not, we use
`~expect_rev`. *)
let test_index_of_string ?from ?sub_index ?sub_length ?expect_rev s ~sub ~expect =
match Str.of_native_string s, Str.of_native_string sub with
| `Ok t, `Ok subt ->
let res = Str.index_of_string t ~sub:subt ?from ?sub_index ?sub_length in
let shopt = Option.value_map ~f:(sprintf "Some %d") ~default:"None" in
test_assertf (res = expect)
"Str.index_of_string %s ~sub:%s ?from:%s ?sub_index:%s ?sub_length:%s gave %s not %s"
s sub (shopt from) (shopt sub_index) (shopt sub_length)
(shopt res) (shopt expect);
let erev = match expect_rev with None -> expect | Some opt -> opt in
let res = Str.index_of_string_reverse t ~sub:subt ?from ?sub_index ?sub_length in
test_assertf (res = erev)
"Str.index_of_string_reverse %s ~sub:%s ?from:%s ?sub_index:%s ?sub_length:%s gave %s not %s"
s sub (shopt from) (shopt sub_index) (shopt sub_length)
(shopt res) (shopt erev);
| _, _ -> ()
in
test_index_of_string "aaaa" ~sub:"cc" ~expect:None;
test_index_of_string "aaaa" ~sub:"aa" ~expect:(Some 0) ~expect_rev:(Some 2);
test_index_of_string "ccaa" ~sub:"aa" ~expect:(Some 2);
test_index_of_string "cccca" ~sub:"aa" ~expect:(None);
test_index_of_string "aacca" ~from:1 ~sub:"aa" ~expect:(None) ~expect_rev:(Some 0);
test_index_of_string "aaccaa" ~from:1 ~sub:"aa" ~expect:(Some 4) ~expect_rev:(Some 0);
test_index_of_string "aacca" ~from:1 ~sub:"aa" ~sub_index:1 ~expect:(Some 1) ~expect_rev:(Some 1);
test_index_of_string "aacca" ~from:2 ~sub:"aa" ~sub_index:1 ~expect:(Some 4) ~expect_rev:(Some 1);
test_index_of_string "aacca" ~from:2 ~sub:"aa" ~sub_length:1 ~expect:(Some 4) ~expect_rev:(Some 1);
test_index_of_string "aacca" ~from:2 ~sub:"aa" ~sub_length:0 ~expect:(Some 2) ~expect_rev:(Some 2);
test_index_of_string "aacca" ~from:2 ~sub:"" ~expect:(Some 2) ~expect_rev:(Some 2);
test_index_of_string "caaa" ~from:(-1) ~sub:"aa" ~expect:(Some 1) ~expect_rev:None;
test_index_of_string "aaaa" ~from:3 ~sub:"aa" ~expect:None ~expect_rev:(Some 2);
test_index_of_string "aaaa" ~from:4 ~sub:"aa" ~expect:None ~expect_rev:(Some 2);
test_index_of_string "aaaa" ~from:5 ~sub:"aa" ~expect:None ~expect_rev:(Some 2);
test_index_of_string "caaa" ~sub_index:(-1) ~sub:"aa" ~expect:(Some 1) ~expect_rev:(Some 2);
test_index_of_string "aaaa" ~sub_index:2 ~sub:"aa" ~expect:(Some 0) ~expect_rev:(Some 3);
(* ┗▶ This is searching the empty string ! Find everywhere. *)
test_index_of_string "caaa" ~sub_index:3 ~sub:"aa" ~expect:(Some 0) ~expect_rev:(Some 3);
(* ┗▶ This is also searching the empty string ! *)
test_index_of_string "caaa" ~sub_index:1 ~sub_length:3 ~sub:"aa" ~expect:(Some 1) ~expect_rev:(Some 3);
test_index_of_string "caaa" ~sub_index:(-1) ~sub_length:3 ~sub:"aa" ~expect:(Some 1) ~expect_rev:(Some 2);
end;
begin (* Test `filter_map` *)
let test ?from ?length l ~f ~expect fmt =
let name = ksprintf (fun s -> s) fmt in
(* say "test: %s l : %d" name (List.length l); *)
let s =
List.filter_map l Chr.of_int |> Str.of_character_list in
(* say "test: %s s: %d" name (Str.length s); *)
let filtered =
Str.filter_map ?from ?length s ~f:(fun c ->
(* say "Chr: %d" (Chr.to_int c); *)
match f (Chr.to_int c) with
| Some i -> Chr.of_int i
| None -> None)
in
let res_ints =
Str.to_character_list filtered |> List.map ~f:Chr.to_int in
let before =
Str.to_character_list s |> List.map ~f:Chr.to_int in
test_assertf (expect = res_ints)
"test_filter_map: [%s=%s] → [%s] <> [%s] (%s)"
(List.map l (sprintf "%d") |> String.concat ~sep:",")
(List.map before (sprintf "%d") |> String.concat ~sep:",")
(List.map res_ints (sprintf "%d") |> String.concat ~sep:",")
(List.map expect (sprintf "%d") |> String.concat ~sep:",")
name;
in
let some = fun s -> Some s in
test [] ~f:some ~expect:[] "all empty";
test [1] ~f:some ~expect:[1] "some 1";
test [1;2;3] ~f:some ~expect:[1;2;3] "some 123";
test [1;1;1] ~f:some ~expect:[1;1;1] "some 111";
test [] ~f:(fun _ -> None) ~expect:[] "none";
test [1] ~f:(fun _ -> None) ~expect:[] "none";
test [1;2;3] ~f:(fun _ -> None) ~expect:[] "none";
let opt_of_cond c = fun x -> if c x then Some x else None in
test [1;2;3] ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1";
test [1;2;3] ~f:(opt_of_cond ((<) 2)) ~expect:[3] "opt_of_cond _ > 2";
test [1;2;3] ~from:1 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 from 1";
test [1;2;3] ~from:2 ~f:(opt_of_cond ((<) 1)) ~expect:[3] "opt_of_cond _ > 1 from 2";
test [1;2;3] ~from:3 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 from 3";
test [1;2;3] ~from:4 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 from 4";
test [1;2;3] ~length:0 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 length 0";
test [1;2;3] ~length:1 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 length 1";
test [1;2;3] ~length:2 ~f:(opt_of_cond ((<) 1)) ~expect:[2] "opt_of_cond _ > 1 length 2";
test [1;2;3] ~length:3 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 length 3";
test [1;2;3] ~length:4 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 length 4";
test [1;2;3] ~from:2 ~length:2 ~f:(opt_of_cond ((<) 1)) ~expect:[3] "opt_of_cond _ > 1";
end;
begin (* Test `filter` *)
let test ?from ?length l ~f ~expect fmt =
let name = ksprintf (fun s -> s) fmt in
(* say "test: %s l : %d" name (List.length l); *)
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
(* say "test: %s s: %d" name (Str.length s); *)
let filtered = Str.filter ?from ?length s ~f:(fun c -> f (Chr.to_int c)) in
let res_ints = Str.to_character_list filtered |> List.map ~f:Chr.to_int in
let before = Str.to_character_list s |> List.map ~f:Chr.to_int in
let pp l = List.map l (sprintf "%d") |> String.concat ~sep:";" in
test_assertf (expect = res_ints)
"test_filter: [%s=%s] → [%s] <> [%s] (%s)"
(pp l) (pp before) (pp res_ints) (pp expect)
name;
in
let always_true _ = true in
let always_false _ = false in
test [] ~f:always_true ~expect:[] "all empty";
test [1] ~f:always_true ~expect:[1] "true 1";
test [1;2;3] ~f:always_true ~expect:[1;2;3] "true 123";
test [1;1;1] ~f:always_true ~expect:[1;1;1] "some 111";
test [] ~f:always_false ~expect:[] "all empty";
test [1] ~f:always_false ~expect:[] "false 1";
test [1;2;3] ~f:always_false ~expect:[] "false 123";
let opt_of_cond c = fun x -> c x in
test [1;2;3] ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1";
test [1;2;3] ~f:(opt_of_cond ((<) 2)) ~expect:[3] "opt_of_cond _ > 2";
test [1;2;3] ~from:1 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 from 1";
test [1;2;3] ~from:2 ~f:(opt_of_cond ((<) 1)) ~expect:[3] "opt_of_cond _ > 1 from 2";
test [1;2;3] ~from:3 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 from 3";
test [1;2;3] ~from:4 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 from 4";
test [1;2;3] ~length:0 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 length 0";
test [1;2;3] ~length:1 ~f:(opt_of_cond ((<) 1)) ~expect:[] "opt_of_cond _ > 1 length 1";
test [1;2;3] ~length:2 ~f:(opt_of_cond ((<) 1)) ~expect:[2] "opt_of_cond _ > 1 length 2";
test [1;2;3] ~length:3 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 length 3";
test [1;2;3] ~length:4 ~f:(opt_of_cond ((<) 1)) ~expect:[2;3] "opt_of_cond _ > 1 length 4";
test [1;2;3] ~from:2 ~length:2 ~f:(opt_of_cond ((<) 1)) ~expect:[3] "opt_of_cond _ > 1";
end;
begin (* Test the `split` function *)
let test l ~on ~expect =
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
let on_converted =
match on with
| `C c ->
`Character (Option.value_exn ~msg:"Chr.of_int" (Chr.of_int c))
| `S l ->
`String (List.filter_map l Chr.of_int |> Str.of_character_list)
in
let res = Str.split s ~on:on_converted in
let res_list = List.map res ~f:str_to_int_list
in
test_assertf (res_list = expect)
"split: l: %s = %s on:(%s)\n expect: {%s}\n res: {%s}: %s."
(int_list_to_string l)
(Str.to_string_hum s)
(match on with
| `C c -> sprintf "`Character %d" c
| `S s -> sprintf "`Bytes %s" (int_list_to_string s))
(List.map ~f:int_list_to_string expect |> String.concat ~sep:" -- ")
(List.map ~f:int_list_to_string res_list |> String.concat ~sep:" -- ")
(List.map ~f:Str.to_string_hum res |> String.concat ~sep:"; ")
in
let on_one t ~expect =
test t ~on:(`C 1) ~expect;
test t ~on:(`S [1]) ~expect;
in
on_one [] ~expect:[[]];
on_one [2;3;] ~expect:[[2;3]];
on_one [1] ~expect:[[]; []];
on_one [2;1;2;3;4] ~expect:[[2]; [2;3;4]];
on_one [2;1;2;3;4;1] ~expect:[[2]; [2;3;4]; []];
on_one [1;2;1;2;3;4;1] ~expect:[[]; [2]; [2;3;4]; []];
on_one [1;1;2;1;2;3;4;1] ~expect:[[]; []; [2]; [2;3;4]; []];
let on_123 t ~expect =
test t ~on:(`S [1;2;3]) ~expect;
in
on_123 [] ~expect:[ [] ];
on_123 [1] ~expect:[ [1] ];
on_123 [1;2;4;5] ~expect:[ [1;2;4;5;] ];
on_123 [1;2;3] ~expect:[[]; []];
on_123 [2;1;2;3;4] ~expect:[[2]; [4]];
on_123 [2;1;2;3;4;1] ~expect:[[2]; [4;1]];
on_123 [1;2;1;2;3;4;1] ~expect:[[1;2]; [4;1]];
on_123 [1;2;3;1;2;1;2;3;4;1] ~expect:[[]; [1;2]; [4;1]];
let on_empty t ~expect =
test t ~on:(`S []) ~expect;
in
on_empty [] ~expect:[ [] ];
on_empty [1] ~expect:[ [1] ];
on_empty [1;2;4;5] ~expect:[ [1]; [2]; [4]; [5] ];
end;
begin (* Test `find` *)
let test ?from ?length ?should_find l ~f =
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
let f x = f (Chr.to_int x) in
let res = Str.find ?from ?length s ~f in
test_assertf (res = should_find)
"find: %s (%s, %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string length)
(int_option_to_string should_find)
(int_option_to_string res);
if from = None then (
let from = Some 0 in
let res = Str.find ?from ?length s ~f in
test_assertf (res = should_find)
"find: %s (%s →, %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string length)
(int_option_to_string should_find)
(int_option_to_string res);
);
in
test [] ~f:(fun _ -> true);
test [] ~f:(fun _ -> false);
test [1] ~f:(fun _ -> true) ~should_find:0;
test [1] ~f:(fun _ -> false);
test [1;2;3] ~f:(fun _ -> true) ~should_find:0;
test [1;2;3] ~f:(fun _ -> false);
test [1;2;3] ~f:(fun x -> x > 1) ~should_find:1;
test [1;2;3] ~f:(fun x -> x < 1);
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x > 1) ~should_find:3;
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x < 1);
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x <= 1) ~should_find:2;
test [1;2;3] ~from:2 ~f:(fun _ -> true) ~should_find:2;
test [1;2;3] ~from:3 ~f:(fun _ -> true);
test [1;2;3] ~from:(-2) ~f:(fun _ -> true) ~should_find:0;
test [] ~length:0 ~f:(fun _ -> true);
test [] ~length:0 ~f:(fun _ -> false);
test [1;2;] ~length:0 ~f:(fun _ -> true);
test [1;2;] ~length:0 ~f:(fun _ -> false);
test [1;2;] ~length:1 ~f:(fun _ -> true) ~should_find:0;
test [1;2;] ~length:1 ~f:(fun _ -> false);
test [1;2;] ~from:1 ~length:1 ~f:(fun _ -> true) ~should_find:1;
test [1;2;] ~from:2 ~length:1 ~f:(fun _ -> true);
test [1;2;3;] ~from:2 ~length:1 ~f:(fun _ -> true) ~should_find:2;
test [1;2;] ~from:1 ~length:2 ~f:(fun _ -> true) ~should_find:1;
test [1;2;] ~from:0 ~length:(-1) ~f:(fun _ -> true);
test [1;2;3;4] ~from:3 ~length:2 ~f:(fun _ -> true) ~should_find:3;
end;
begin (* Test `find_reverse` *)
let test ?from ?length ?should_find l ~f =
let s = List.filter_map l Chr.of_int |> Str.of_character_list in
let f x = f (Chr.to_int x) in
let res = Str.find_reverse ?from ?length s ~f in
test_assertf (res = should_find)
"find_reverse: %s (%s, %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string length)
(int_option_to_string should_find)
(int_option_to_string res);
if from = None then (
let from = Some (Str.length s - 1) in
let res = Str.find_reverse ?from ?length s ~f in
test_assertf (res = should_find)
"find_reverse: %s (%s → added, %s) expects %s but got %s"
(str_to_int_list s |> int_list_to_string)
(int_option_to_string from)
(int_option_to_string length)
(int_option_to_string should_find)
(int_option_to_string res);
);
in
test [] ~f:(fun _ -> true);
test [] ~f:(fun _ -> false);
test [1] ~f:(fun _ -> true) ~should_find:0;
test [1] ~f:(fun _ -> false);
test [1;2;3] ~f:(fun _ -> true) ~should_find:2;
test [1;2;3] ~f:(fun _ -> false);
test [1;2;3] ~f:(fun x -> x <= 2) ~should_find:1;
test [1;2;3] ~f:(fun x -> x < 1);
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x > 1);
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x < 1);
test [1;1;1;2;3] ~from:2 ~f:(fun x -> x <= 1) ~should_find:2;
test [1;2;3] ~from:2 ~f:(fun _ -> true) ~should_find:2;
test [1;2;3] ~from:3 ~f:(fun _ -> true) ~should_find:2;
test [1;2;3] ~from:(-2) ~f:(fun _ -> true);
test [] ~length:0 ~f:(fun _ -> true);
test [] ~length:0 ~f:(fun _ -> false);
test [1;2;] ~length:0 ~f:(fun _ -> true);
test [1;2;] ~length:0 ~f:(fun _ -> false);
test [1;2;] ~length:1 ~f:(fun _ -> true) ~should_find:1;
test [1;2;] ~length:1 ~f:(fun _ -> false);