10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-11-18 12:03:40 +01:00
QCaml/common/test/bitstring.ml

124 lines
4.5 KiB
OCaml
Raw Normal View History

2020-12-27 16:36:25 +01:00
(* Tests header :noexport: *)
2020-09-26 12:02:53 +02:00
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*Tests header][Tests header:1]] *)
2020-12-26 01:47:55 +01:00
open Common.Bitstring
2020-12-27 23:08:12 +01:00
let check_bool = Alcotest.(check bool)
let check msg x = check_bool msg true x
2020-12-26 01:47:55 +01:00
let test_all () =
2020-12-27 17:38:04 +01:00
let x = 8745687 in
let one_x = of_int x in
let z = Z.shift_left (Z.of_int x) 64 in
let many_x = of_z z in
2020-12-26 01:47:55 +01:00
(* Tests header:1 ends here *)
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*General implementation][General implementation:3]] *)
2020-12-27 23:08:12 +01:00
check_bool "of_x" true (one_x = (of_int x));
2020-12-29 01:08:37 +01:00
check_bool "of_z" true (one_x = (of_z (Z.of_int x)));
2020-12-27 23:08:12 +01:00
(* General implementation:3 ends here *)
2021-01-28 00:34:26 +01:00
(* Example:
* #+begin_example
2020-12-29 01:08:37 +01:00
* Bitstring.(shift_left (of_int 15) 2);;
* - : Bitstring.t =
* --++++----------------------------------------------------------
*
* Bitstring.(shift_right (of_int 15) 2);;
* - : Bitstring.t =
* ++--------------------------------------------------------------
*
* Bitstring.shift_left_one 32 4;;
* - : Bitstring.t =
* ----+-----------------------------------------------------------
*
* Bitstring.(testbit (of_int 15) 3);;
* - : bool = true
*
* Bitstring.(testbit (of_int 15) 4);;
* - : bool = false
* #+end_example *)
2020-12-27 23:08:12 +01:00
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*General implementation][General implementation:5]] *)
2020-12-29 01:08:37 +01:00
check_bool "shift_left1" true (of_int (x lsl 3) = shift_left one_x 3);
check_bool "shift_left2" true (of_z (Z.shift_left z 3) = shift_left many_x 3);
check_bool "shift_left3" true (of_z (Z.shift_left z 100) = shift_left many_x 100);
check_bool "shift_right1" true (of_int (x lsr 3) = shift_right one_x 3);
check_bool "shift_right2" true (of_z (Z.shift_right z 3) = shift_right many_x 3);
2020-12-27 23:08:12 +01:00
check_bool "shift_left_one1" true (of_int (1 lsl 3) = shift_left_one 4 3);
check_bool "shift_left_one2" true (of_z (Z.shift_left Z.one 200) = shift_left_one 300 200);
check_bool "testbit1" true (testbit (of_int 8) 3);
check_bool "testbit2" false (testbit (of_int 8) 2);
check_bool "testbit3" false (testbit (of_int 8) 4);
check_bool "testbit4" true (testbit (of_z (Z.of_int 8)) 3);
check_bool "testbit5" false (testbit (of_z (Z.of_int 8)) 2);
check_bool "testbit6" false (testbit (of_z (Z.of_int 8)) 4);
2020-12-29 01:08:37 +01:00
(* General implementation:5 ends here *)
2020-12-27 23:08:12 +01:00
2020-12-29 01:08:37 +01:00
2021-01-28 00:34:26 +01:00
(* Example:
* #+begin_example
2020-12-29 01:08:37 +01:00
* Bitstring.(logor (of_int 15) (of_int 73));;
* - : Bitstring.t =
* ++++--+---------------------------------------------------------
*
* Bitstring.(logand (of_int 15) (of_int 10));;
* - : Bitstring.t =
* -+-+------------------------------------------------------------
*
* Bitstring.(logxor (of_int 15) (of_int 73));;
* - : Bitstring.t =
* -++---+---------------------------------------------------------
* #+end_example *)
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*General implementation][General implementation:7]] *)
2020-12-27 23:08:12 +01:00
check_bool "logor1" true (of_int (1 lor 2) = logor (of_int 1) (of_int 2));
check_bool "logor2" true (of_z (Z.of_int (1 lor 2)) = logor (of_z Z.one) (of_z (Z.of_int 2)));
check_bool "logxor1" true (of_int (1 lxor 2) = logxor (of_int 1) (of_int 2));
check_bool "logxor2" true (of_z (Z.of_int (1 lxor 2)) = logxor (of_z Z.one) (of_z (Z.of_int 2)));
check_bool "logand1" true (of_int (1 land 3) = logand (of_int 1) (of_int 3));
check_bool "logand2" true (of_z (Z.of_int (1 land 3)) = logand (of_z Z.one) (of_z (Z.of_int 3)));
2020-12-29 01:08:37 +01:00
(* General implementation:7 ends here *)
2020-12-27 23:08:12 +01:00
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*General implementation][General implementation:11]] *)
2020-12-27 23:08:12 +01:00
check_bool "to_list" true ([ 1 ; 3 ; 4 ; 6 ] = (to_list (of_int 45)));
2020-12-29 01:08:37 +01:00
(* General implementation:11 ends here *)
2020-12-27 23:08:12 +01:00
2020-12-29 01:08:37 +01:00
2021-01-28 00:34:26 +01:00
(* Example:
* #+begin_example
2020-12-29 01:08:37 +01:00
* Bitstring.permutations 2 4;;
* - : Bitstring.t list =
* [++--------------------------------------------------------------;
* +-+-------------------------------------------------------------;
* -++-------------------------------------------------------------;
* +--+------------------------------------------------------------;
* -+-+------------------------------------------------------------;
* --++------------------------------------------------------------]
* #+end_example *)
2022-11-07 14:59:11 +01:00
(* [[file:~/QCaml/common/bitstring.org::*General implementation][General implementation:13]] *)
2020-12-26 01:47:55 +01:00
check "permutations"
2020-12-26 01:54:39 +01:00
(permutations 2 4 = List.map of_int
2020-12-26 01:47:55 +01:00
[ 3 ; 5 ; 6 ; 9 ; 10 ; 12 ]);
2020-12-29 01:08:37 +01:00
(* General implementation:13 ends here *)
2020-12-26 01:47:55 +01:00
2020-12-27 16:36:25 +01:00
(* Tests :noexport: *)
2020-12-26 01:47:55 +01:00
2020-12-28 01:55:03 +01:00
(* [[file:~/QCaml/common/bitstring.org::*Tests][Tests:1]] *)
2020-12-27 17:38:04 +01:00
()
2020-12-27 16:36:25 +01:00
2020-09-26 12:02:53 +02:00
let tests = [
2020-12-27 17:38:04 +01:00
"all", `Quick, test_all;
]
2020-12-26 01:47:55 +01:00
(* Tests:1 ends here *)