File: test.ml

package info (click to toggle)
ocaml 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,372 kB
  • sloc: ml: 370,196; ansic: 52,820; sh: 27,396; asm: 5,462; makefile: 3,679; python: 974; awk: 278; javascript: 273; perl: 59; fortran: 21; cs: 9
file content (133 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download | duplicates (2)
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
(* TEST *)

let test_not () =
  assert (Bool.not false = true);
  assert (Bool.not true = false);
  ()

let test_and () =
  let wit = ref 0 in
  assert (Bool.( && ) (incr wit; false) (incr wit; false) = false);
  assert (!wit = 1); wit := 0;
  assert (Bool.( && ) (incr wit; false) (incr wit; true) = false);
  assert (!wit = 1); wit := 0;
  assert (Bool.( && ) (incr wit; true) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.( && ) (incr wit; true) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  ()

let test_eager_and () =
  let wit = ref 0 in
  assert (Bool.logand (incr wit; false) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.logand (incr wit; false) (incr wit; true) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.logand (incr wit; true) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.logand (incr wit; true) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  ()

let test_or () =
  let wit = ref 0 in
  assert (Bool.( || ) (incr wit; false) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.( || ) (incr wit; false) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  assert (Bool.( || ) (incr wit; true) (incr wit; false) = true);
  assert (!wit = 1); wit := 0;
  assert (Bool.( || ) (incr wit; true) (incr wit; true) = true);
  assert (!wit = 1); wit := 0;
  ()

let test_eager_or () =
  let wit = ref 0 in
  assert (Bool.logor (incr wit; false) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.logor (incr wit; false) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  assert (Bool.logor (incr wit; true) (incr wit; false) = true);
  assert (!wit = 2); wit := 0;
  assert (Bool.logor (incr wit; true) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  ()

let test_eager_xor () =
  let wit = ref 0 in
  assert (Bool.logxor (incr wit; false) (incr wit; false) = false);
  assert (!wit = 2); wit := 0;
  assert (Bool.logxor (incr wit; false) (incr wit; true) = true);
  assert (!wit = 2); wit := 0;
  assert (Bool.logxor (incr wit; true) (incr wit; false) = true);
  assert (!wit = 2); wit := 0;
  assert (Bool.logxor (incr wit; true) (incr wit; true) = false);
  assert (!wit = 2); wit := 0;
  ()

let test_equal () =
  assert (Bool.equal false false = true);
  assert (Bool.equal false true = false);
  assert (Bool.equal true false = false);
  assert (Bool.equal true true = true);
  ()

let test_compare () =
  assert (Bool.compare false false = 0);
  assert (Bool.compare false true = -1);
  assert (Bool.compare true false = 1);
  assert (Bool.compare true true = 0);
  ()

let test_to_int () =
  assert (Bool.to_int false = 0);
  assert (Bool.to_int true = 1);
  ()

let test_to_float () =
  assert (Bool.to_float false = 0.);
  assert (Bool.to_float true = 1.);
  ()

let test_of_string () =
  (*
  assert (Bool.of_string "false" = Some false);
  assert (Bool.of_string "true" = Some true);
  assert (Bool.of_string "heyho" = None);
  assert (Bool.of_string "1" = None);
  assert (Bool.of_string "0" = None);
*)
  ()

let test_to_string () =
  assert (Bool.to_string false = "false");
  assert (Bool.to_string true = "true");
  ()


let test_hash () =
  let f b =
    assert (Hashtbl.hash b = Bool.hash b);
    assert (Hashtbl.seeded_hash 16 b = Bool.seeded_hash 16 b)
  in
  f true; f false

let tests () =
  test_not ();
  test_and ();
  test_eager_and ();
  test_or ();
  test_eager_or ();
  test_eager_xor ();
  test_equal ();
  test_compare ();
  test_to_int ();
  test_to_float ();
  test_of_string ();
  test_to_string ();
  test_hash ();
  ()

let () =
  tests ();
  print_endline "OK"