-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathtest_int64.ml
170 lines (159 loc) · 3.83 KB
/
test_int64.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
open! Import
open! Int64
let%expect_test "hash coherence" =
check_int_hash_coherence [%here] (module Int64);
[%expect {| |}]
;;
let numbers =
[ 0x0000_0000_0000_1020L
; 0x0000_0000_0011_2233L
; 0x0000_0000_1122_3344L
; 0x0000_0011_2233_4455L
; 0x0000_1122_3344_5566L
; 0x0011_2233_4455_6677L
; 0x1122_3344_5566_7788L
]
;;
let test = test_conversion ~to_string:Int64.Hex.to_string_hum
let%expect_test "bswap16" =
List.iter numbers ~f:(test bswap16);
[%expect
{|
0x1020 --> 0x2010
0x11_2233 --> 0x3322
0x1122_3344 --> 0x4433
0x11_2233_4455 --> 0x5544
0x1122_3344_5566 --> 0x6655
0x11_2233_4455_6677 --> 0x7766
0x1122_3344_5566_7788 --> 0x8877
|}]
;;
let%expect_test "bswap32" =
List.iter numbers ~f:(test bswap32);
[%expect
{|
0x1020 --> 0x2010_0000
0x11_2233 --> 0x3322_1100
0x1122_3344 --> 0x4433_2211
0x11_2233_4455 --> 0x5544_3322
0x1122_3344_5566 --> 0x6655_4433
0x11_2233_4455_6677 --> 0x7766_5544
0x1122_3344_5566_7788 --> 0x8877_6655
|}]
;;
let%expect_test "bswap48" =
List.iter numbers ~f:(test bswap48);
[%expect
{|
0x1020 --> 0x2010_0000_0000
0x11_2233 --> 0x3322_1100_0000
0x1122_3344 --> 0x4433_2211_0000
0x11_2233_4455 --> 0x5544_3322_1100
0x1122_3344_5566 --> 0x6655_4433_2211
0x11_2233_4455_6677 --> 0x7766_5544_3322
0x1122_3344_5566_7788 --> 0x8877_6655_4433
|}]
;;
let%expect_test "bswap64" =
List.iter numbers ~f:(test bswap64);
[%expect
{|
0x1020 --> 0x2010_0000_0000_0000
0x11_2233 --> 0x3322_1100_0000_0000
0x1122_3344 --> 0x4433_2211_0000_0000
0x11_2233_4455 --> 0x5544_3322_1100_0000
0x1122_3344_5566 --> 0x6655_4433_2211_0000
0x11_2233_4455_6677 --> 0x7766_5544_3322_1100
0x1122_3344_5566_7788 --> -0x7788_99aa_bbcc_ddef
|}]
;;
let%expect_test "of_string" =
let test s =
let result = Or_error.try_with (fun () -> of_string s) in
print_s [%sexp (result : t Or_error.t)]
in
test "0";
[%expect {| (Ok 0) |}];
test "-1";
[%expect {| (Ok -1) |}];
test "0xBEEF";
[%expect {| (Ok 48_879) |}];
(* max_value *)
test "9_223_372_036_854_775_807";
[%expect {| (Ok 9_223_372_036_854_775_807) |}];
(* max_value + 1 *)
test "9_223_372_036_854_775_808";
[%expect {| (Error (Failure Int64.of_string)) |}];
(* min_value *)
test "-9_223_372_036_854_775_808";
[%expect {| (Ok -9_223_372_036_854_775_808) |}];
(* min_value - 1 *)
test "-9_223_372_036_854_775_809";
[%expect {| (Error (Failure Int64.of_string)) |}];
(*
* Bases other than 10 are more permissive:
*)
(* max_value (hex) *)
test "0x7fff_ffff_ffff_ffff";
[%expect {| (Ok 9_223_372_036_854_775_807) |}];
(* max_value + 1 (hex) *)
test "0x8000_0000_0000_0000";
[%expect {| (Ok -9_223_372_036_854_775_808) |}];
(* min_value (hex) *)
test "-0x8000_0000_0000_0000";
[%expect {| (Ok -9_223_372_036_854_775_808) |}];
(* min_value - 1 (hex) *)
test "-0x8000_0000_0000_0001";
[%expect {| (Ok 9_223_372_036_854_775_807) |}];
()
;;
let%expect_test "binary" =
quickcheck_m
(module struct
type t = int64 [@@deriving quickcheck, sexp_of]
end)
~f:(fun (t : t) -> ignore (Binary.to_string t : string));
[%expect {| |}]
;;
let test_binary i =
Binary.to_string_hum i |> print_endline;
Binary.to_string i |> print_endline;
print_s [%sexp (i : Binary.t)]
;;
let%expect_test "binary" =
test_binary 0b01L;
[%expect
{|
0b1
0b1
0b1
|}];
test_binary 0b100L;
[%expect
{|
0b100
0b100
0b100
|}];
test_binary 0b101L;
[%expect
{|
0b101
0b101
0b101
|}];
test_binary 0b10_1010_1010_1010L;
[%expect
{|
0b10_1010_1010_1010
0b10101010101010
0b10_1010_1010_1010
|}];
test_binary 0b11_1111_0000_0000L;
[%expect
{|
0b11_1111_0000_0000
0b11111100000000
0b11_1111_0000_0000
|}]
;;