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
|
open CCInt
module T = (val Containers_testlib.make ~__FILE__ ())
include T;;
eq ~printer:Q.Print.(list int) [ 0; 1; 2; 3; 4; 5 ] (range 0 5 |> Iter.to_list)
;;
eq ~printer:Q.Print.(list int) [ 0 ] (range 0 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [ 5; 4; 3; 2 ] (range 5 2 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [] (range' 0 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [ 0; 1; 2; 3; 4 ] (range' 0 5 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [ 5; 4; 3 ] (range' 5 2 |> Iter.to_list);;
t @@ fun () -> pow 2 10 = 1024;;
t @@ fun () -> pow 2 15 = 32768;;
t @@ fun () -> pow 10 5 = 100000;;
t @@ fun () -> pow 1 0 = 1;;
t @@ fun () -> pow 0 1 = 0;;
t @@ fun () -> floor_div 3 5 = 0;;
t @@ fun () -> floor_div 5 5 = 1;;
t @@ fun () -> floor_div 20 5 = 4;;
t @@ fun () -> floor_div 12 5 = 2;;
t @@ fun () -> floor_div 0 5 = 0;;
t @@ fun () -> floor_div (-1) 5 = -1;;
t @@ fun () -> floor_div (-5) 5 = -1;;
t @@ fun () -> floor_div (-12) 5 = -3;;
t @@ fun () -> floor_div 0 (-5) = 0;;
t @@ fun () -> floor_div 3 (-5) = -1;;
t @@ fun () -> floor_div 5 (-5) = -1;;
t @@ fun () -> floor_div 9 (-5) = -2;;
t @@ fun () -> floor_div 20 (-5) = -4;;
t @@ fun () -> floor_div (-2) (-5) = 0;;
t @@ fun () -> floor_div (-8) (-5) = 1;;
t @@ fun () -> floor_div (-35) (-5) = 7;;
t @@ fun () ->
try
ignore (floor_div 12 0);
false
with Division_by_zero -> true
;;
t @@ fun () ->
try
ignore (floor_div (-12) 0);
false
with Division_by_zero -> true
;;
q (Q.pair Q.small_signed_int Q.pos_int) (fun (n, m) ->
floor_div n m = int_of_float @@ floor (float n /. float m))
;;
q (Q.pair Q.small_signed_int Q.pos_int) (fun (n, m) ->
floor_div n (-m) = int_of_float @@ floor (float n /. float (-m)))
;;
t @@ fun () -> rem 3 5 = 3;;
t @@ fun () -> rem 5 5 = 0;;
t @@ fun () -> rem 9 5 = 4;;
t @@ fun () -> rem (-1) 5 = 4;;
t @@ fun () -> rem (-5) 5 = 0;;
t @@ fun () -> rem (-20) 5 = 0;;
t @@ fun () -> rem (-9) 5 = 1;;
t @@ fun () -> rem 0 5 = 0;;
t @@ fun () -> rem 0 (-5) = 0;;
t @@ fun () -> rem 3 (-5) = -2;;
t @@ fun () -> rem 5 (-5) = 0;;
t @@ fun () -> rem 9 (-5) = -1;;
t @@ fun () -> rem (-2) (-5) = -2;;
t @@ fun () -> rem (-8) (-5) = -3;;
t @@ fun () -> rem (-35) (-5) = 0;;
t @@ fun () ->
try
ignore (rem 12 0);
false
with Division_by_zero -> true
;;
t @@ fun () ->
try
ignore (rem (-12) 0);
false
with Division_by_zero -> true
;;
q (Q.pair Q.int Q.pos_int) (fun (n, m) ->
let y = rem n m in
y >= 0 && y < m)
;;
q (Q.pair Q.int Q.pos_int) (fun (n, m) ->
let y = rem n (-m) in
y > -m && y <= 0)
;;
q (Q.pair Q.int Q.pos_int) (fun (n, m) -> n = (m * floor_div n m) + rem n m);;
q (Q.pair Q.int Q.pos_int) (fun (n, m) ->
n = (-m * floor_div n (-m)) + rem n (-m))
;;
eq None (of_string "moo");;
eq (Some 42) (of_string "42");;
eq 1 (of_float 1.2);;
eq ~printer:CCFun.id "0b111" (to_string_binary 7);;
eq ~printer:CCFun.id "-0b111" (to_string_binary (-7));;
eq ~printer:CCFun.id "0b0" (to_string_binary 0);;
q ~count:10_000 Q.int (fun n -> n = int_of_string (to_string_binary n));;
(* note: the last test checks that no error occurs due to overflows. *)
eq ~printer:Q.Print.(list int) [ 0 ] (range_by ~step:1 0 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [] (range_by ~step:1 5 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [] (range_by ~step:2 1 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [ 0; 2; 4 ] (range_by ~step:2 0 4 |> Iter.to_list)
;;
eq ~printer:Q.Print.(list int) [ 0; 2; 4 ] (range_by ~step:2 0 5 |> Iter.to_list)
;;
eq ~printer:Q.Print.(list int) [ 0 ] (range_by ~step:~-1 0 0 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [] (range_by ~step:~-1 0 5 |> Iter.to_list);;
eq ~printer:Q.Print.(list int) [] (range_by ~step:~-2 0 1 |> Iter.to_list);;
eq
~printer:Q.Print.(list int)
[ 5; 3; 1 ]
(range_by ~step:~-2 5 1 |> Iter.to_list)
;;
eq
~printer:Q.Print.(list int)
[ 5; 3; 1 ]
(range_by ~step:~-2 5 0 |> Iter.to_list)
;;
eq ~printer:Q.Print.(list int) [ 0 ] (range_by ~step:max_int 0 2 |> Iter.to_list)
;;
q
Q.(pair small_int small_int)
(fun (i, j) ->
let i = min i j and j = max i j in
CCList.equal CCInt.equal
(CCInt.range_by ~step:1 i j |> Iter.to_list)
(CCInt.range i j |> Iter.to_list))
;;
eq 0 (popcount 0);;
eq 1 (popcount 1);;
eq (Sys.word_size - 2) (popcount max_int);;
eq 1 (popcount min_int);;
eq 10 (popcount 0b1110010110110001010);;
eq 5 (popcount 0b1101110000000000)
let simple_popcnt i =
let rec loop n i =
if i = 0 then
n
else if i land 0b1 = 1 then
loop (n + 1) (i lsr 1)
else
loop n (i lsr 1)
in
loop 0 i
;;
eq 0 (simple_popcnt 0);;
eq 1 (simple_popcnt 1);;
eq (Sys.word_size - 2) (simple_popcnt max_int);;
eq 1 (simple_popcnt min_int);;
eq 5 (simple_popcnt 0b1101110000000000);;
q ~count:3_000 ~long_factor:10
Q.(
let g = int in
set_gen (Gen.graft_corners g.gen [ min_int; max_int; 0; -1; 1 ] ()) g)
(fun i ->
if simple_popcnt i <> popcount i then
Q.Test.fail_reportf "on %d: simple-popcount=%d, popcount=%d" i
(simple_popcnt i) (popcount i);
true)
|