File: uint_test.ml

package info (click to toggle)
herdtools7 7.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,732 kB
  • sloc: ml: 128,583; ansic: 3,827; makefile: 670; python: 407; sh: 212; awk: 14
file content (593 lines) | stat: -rw-r--r-- 20,652 bytes parent folder | download
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
(****************************************************************************)
(*                           the diy toolsuite                              *)
(*                                                                          *)
(* Jade Alglave, University College London, UK.                             *)
(* Luc Maranget, INRIA Paris-Rocquencourt, France.                          *)
(*                                                                          *)
(* Copyright 2015-present Institut National de Recherche en Informatique et *)
(* en Automatique and the authors. All rights reserved.                     *)
(*                                                                          *)
(* This software is governed by the CeCILL-B license under French law and   *)
(* abiding by the rules of distribution of free software. You can use,      *)
(* modify and/ or redistribute the software under the terms of the CeCILL-B *)
(* license as circulated by CEA, CNRS and INRIA at the following URL        *)
(* "http://www.cecill.info". We also give a copy in LICENSE.txt.            *)
(****************************************************************************)

(** Tests for Uint module. *)

let tests = [
  (* Tests for Uint64. *)

  "Uint.Uint64.leading_zeros", (fun () ->
    let open Uint in
    let tests = [
      "0b1000000000000000000000000000000000000000000000000000000000000000", 0 ;
      "0b0010000000000000000000000000000000000000000000000000000000000000", 2 ;
      "0b0000000100000000000000000000000000000000000000000000000000000000", 7 ;
      "0b0000000000000000000000000000000000000000000000000000000000000000", 64 ;
    ] in

    List.iter (fun (bits, expected) ->
      let got = Uint64.leading_zeros (Uint64.of_string bits) in
      if got <> expected then
        Test.fail (Printf.sprintf "%s: expected %i, got %i"
          bits
          expected
          got)
    ) tests
  );

  "Uint.Uint64.add", (fun () ->
    let open Uint in
    let tests = [
      Uint64.zero, Uint64.zero, Uint64.zero ;
      Uint64.zero, Uint64.one, Uint64.one ;
      Uint64.one, Uint64.zero, Uint64.one ;
      Uint64.max_int, Uint64.one, Uint64.zero ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.add a b in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint64.to_string a)
          (Uint64.to_string b)
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.sub", (fun () ->
    let open Uint in
    let tests = [
      Uint64.zero, Uint64.zero, Uint64.zero ;
      Uint64.zero, Uint64.one, Uint64.max_int ;
      Uint64.one, Uint64.zero, Uint64.one ;
      Uint64.max_int, Uint64.one, Uint64.of_string "0xFFFFFFFFFFFFFFFE" ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.sub a b in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint64.to_string a)
          (Uint64.to_string b)
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.mul", (fun () ->
    let open Uint in
    let tests = [
      Uint64.one, Uint64.one, Uint64.one ;
      Uint64.one, Uint64.zero, Uint64.zero ;
      Uint64.of_int64 123456789L, Uint64.of_int64 847L, Uint64.of_int64 104567900283L ;
      Uint64.of_int64 123456789L, Uint64.of_int64 847L, Uint64.of_int64 104567900283L ;
      Uint64.of_int64 3L, Uint64.of_string "6148914691236517205", Uint64.max_int ;
      Uint64.of_int64 2L, Uint64.of_string "9223372036854775807", Uint64.sub Uint64.max_int Uint64.one ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.mul a b in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint64.to_string a)
          (Uint64.to_string b)
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.div", (fun () ->
    let open Uint in
    let tests = [
      Uint64.one, Uint64.one, Uint64.one ;
      Uint64.of_int64 123456789L, Uint64.of_int64 847L, Uint64.of_int64 145757L ;
      Uint64.of_int64 123456789L, Uint64.max_int, Uint64.zero ;
      Uint64.max_int, Uint64.max_int, Uint64.one ;
      Uint64.max_int, Uint64.of_int64 3L, Uint64.of_string "6148914691236517205" ;
      Uint64.max_int, Uint64.of_int64 2L, Uint64.of_string "9223372036854775807" ;
      Uint64.of_int64 0x123456789ABCEDFL, Uint64.of_int64 10L , Uint64.of_int64 8198552921648713L ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.div a b in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint64.to_string a)
          (Uint64.to_string b)
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.rem", (fun () ->
    let open Uint in
    let tests = [
      Uint64.one, Uint64.one, Uint64.zero ;
      Uint64.of_int64 123456789L, Uint64.of_int64 847L, Uint64.of_int64 610L ;
      Uint64.of_int64 123456789L, Uint64.max_int, Uint64.of_int64 123456789L ;
      Uint64.max_int, Uint64.max_int, Uint64.zero ;
      Uint64.max_int, Uint64.of_int64 3L, Uint64.zero ;
      Uint64.max_int, Uint64.of_int64 2L, Uint64.of_int64 1L ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.rem a b in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint64.to_string a)
          (Uint64.to_string b)
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.to_string", (fun () ->
    let open Uint in
    let tests = [
      Uint64.zero, "0" ;
      Uint64.one , "1" ;
      Uint64.max_int, "18446744073709551615" ;
    ] in

    List.iter (fun (a, expected) ->
      let got = Uint64.to_string a in
      if String.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          (Uint64.to_string_hex a)
          expected
          got)
    ) tests
  );

  "Uint.Uint64.to_string_hex", (fun () ->
    let open Uint in
    let tests = [
      Uint64.zero, "0x0" ;
      Uint64.one , "0x1" ;
      Uint64.max_int, "0xffffffffffffffff" ;
    ] in

    List.iter (fun (a, expected) ->
      let got = Uint64.to_string_hex a in
      if String.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          (Uint64.to_string a)
          expected
          got)
    ) tests
  );

  "Uint.Uint64.of_string", (fun () ->
    let open Uint in
    let tests = [
      "0", Uint64.zero ;
      "1", Uint64.one ;
      "0xFFFFFFFFFFFFFFFF", Uint64.max_int ;
      "18446744073709551615", Uint64.max_int ;
    ] in

    List.iter (fun (raw, expected) ->
      let got = Uint64.of_string raw in
      if Uint64.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          raw
          (Uint64.to_string expected)
          (Uint64.to_string got))
    ) tests
  );

  "Uint.Uint64.of_string bad input", (fun () ->
    let open Uint in
    let tests = [
      "a" ;
      "-1" ;
      "0xFFFFFFFFFFFFFFFFF" ;
      "184467440737095516158" ;
    ] in

    List.iter (fun raw ->
      let raised =
        try
          let _ = Uint64.of_string raw in false
        with _ -> true
      in
      if not raised then
        Test.fail (Printf.sprintf "%s: did not raise" raw)
    ) tests
  );

  "Uint.Uint64.compare", (fun () ->
    let open Uint in
    let tests = [
      Uint64.zero, Uint64.zero, 0 ;
      Uint64.zero, Uint64.one, -1 ;
      Uint64.one, Uint64.zero, 1 ;
      Uint64.max_int, Uint64.one, 1 ;
      Uint64.max_int, Uint64.zero, 1 ;
      Uint64.max_int, Uint64.of_string "18446744073709551613", 1 ;
      Uint64.max_int, Uint64.of_string "0xFFFFFFFFFFFFFFAA", 1 ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint64.compare a b in
      if got <> expected then
        Test.fail (Printf.sprintf "%s %s: expected %i, got %i"
          (Uint64.to_string a)
          (Uint64.to_string b)
          expected
          got)
    ) tests
  );

  (* Tests for Uint128. *)

  "Uint.Uint128.leading_zeros", (fun () ->
    let open Uint in
    let tests = [
      "0b10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 0 ;
      "0b00100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 2 ;
      "0b00000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000", 71 ;
      "0b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 128 ;
    ] in

    List.iter (fun (bits, expected) ->
      let got = Uint128.leading_zeros (Uint128.of_string bits) in
      if got <> expected then
        Test.fail (Printf.sprintf "%s: expected %i, got %i"
          bits
          expected
          got)
    ) tests
  );

  "Uint.Uint128.shift_left", (fun () ->
    let open Uint in
    let tests = [
      "0x0", 3, "0x0" ;
      "0xF", 3, "0x78" ;
      "0x123456789ABCDEF", 12, "0x123456789ABCDEF000" ;
      "0xF", 0, "0xF" ;
      "0x123456789ABCDEF", 0, "0x123456789ABCDEF" ;
    ] in

    List.iter (fun (bits, shift, expected_bits) ->
      let got = Uint128.shift_left (Uint128.of_string bits) shift in
      let expected = Uint128.of_string expected_bits in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          bits
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.shift_right_logical", (fun () ->
    let open Uint in
    let tests = [
      "0x0", 3, "0x0" ;
      "0x78", 3, "0xF" ;
      "0x123456789ABCDEF000", 12, "0x123456789ABCDEF" ;
    ] in

    List.iter (fun (bits, shift, expected_bits) ->
      let got = Uint128.shift_right_logical (Uint128.of_string bits) shift in
      let expected = Uint128.of_string expected_bits in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          bits
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.shift_right", (fun () ->
    let open Uint in
    let tests = [
      "0x0", 3, "0x0" ;
      "0x78", 3, "0xF" ;
      "0x123456789ABCDEF000", 12, "0x123456789ABCDEF" ;
      "0xA0000000000000123456789ABCDEF000", 12, "0xFFFA0000000000000123456789ABCDEF" ;
    ] in

    List.iter (fun (bits, shift, expected_bits) ->
      let got = Uint128.shift_right (Uint128.of_string bits) shift in
      let expected = Uint128.of_string expected_bits in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          bits
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.add", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, Uint128.zero, Uint128.zero ;
      Uint128.zero, Uint128.one, Uint128.one ;
      Uint128.one, Uint128.zero, Uint128.one ;
      Uint128.max_int, Uint128.one, Uint128.zero ;
      Uint128.of_string "0xFFFFFFFFFFFFFFFF", Uint128.one, Uint128.of_string "0x10000000000000000" ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.add a b in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.sub", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, Uint128.zero, Uint128.zero ;
      Uint128.zero, Uint128.one, Uint128.max_int ;
      Uint128.one, Uint128.zero, Uint128.one ;
      Uint128.of_string "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.of_string "0x123456789ABDCDEF", Uint128.of_string "0x7fffffffffffffffedcba98765423210" ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.sub a b in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.mul", (fun () ->
    let open Uint in
    let tests = [
      Uint128.one, Uint128.one, Uint128.one ;
      Uint128.of_int64 15L, Uint128.of_int64 10L, Uint128.of_int64 150L ;
      Uint128.of_int64 123456789L, Uint128.zero, Uint128.zero ;
      Uint128.of_int64 123456789L, Uint128.of_int64 847L, Uint128.of_int64 0x1858bb887bL ;
      Uint128.of_string "0x7FFFFFFFFFF000000000000FFFFFFFFF", Uint128.of_string "0x123456789ABDCDEF123456789ABCEDF1", Uint128.of_string "0xb21edca9a977999a999a88976543120f" ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.mul a b in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.div", (fun () ->
    let open Uint in
    let tests = [
      Uint128.one, Uint128.one, Uint128.one ;
      Uint128.of_int64 15L, Uint128.of_int64 10L, Uint128.of_int64 1L ;
      Uint128.of_int64 123456789L, Uint128.max_int, Uint128.zero ;
      Uint128.of_int64 123456789L, Uint128.of_int64 847L, Uint128.of_int64 145757L ;
      Uint128.of_string "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.of_string "0xAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.zero ;
      Uint128.of_string "0xAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.of_string "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.one ;
      Uint128.max_int, Uint128.max_int, Uint128.one ;
      Uint128.of_string "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.of_string "0x123456789ABCEDF", Uint128.of_string "0x707fffffffffa3b770" ;
      Uint128.max_int, Uint128.of_int64 2L, Uint128.of_string "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" ;
      Uint128.max_int, Uint128.of_int64 3L, Uint128.of_string "0x55555555555555555555555555555555" ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.div a b in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.rem", (fun () ->
    let open Uint in
    let tests = [
      Uint128.one, Uint128.one, Uint128.zero ;
      Uint128.of_int64 123456789L, Uint128.of_int64 847L, Uint128.of_int64 610L ;
      Uint128.of_int64 123456789L, Uint128.max_int, Uint128.of_int64 123456789L ;
      Uint128.max_int, Uint128.max_int, Uint128.zero ;
      Uint128.one, Uint128.max_int, Uint128.one ;
      Uint128.max_int, Uint128.of_string "0x3", Uint128.zero ;
      Uint128.max_int, Uint128.of_string "0x2", Uint128.one ;
      Uint128.of_int64 15L, Uint128.of_int64 10L, Uint128.of_int64 5L ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.rem a b in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s %s: expected %s, got %s"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.compare", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, Uint128.zero, 0 ;
      Uint128.zero, Uint128.one, -1 ;
      Uint128.one, Uint128.zero, 1 ;
      Uint128.max_int, Uint128.one, 1 ;
      Uint128.max_int, Uint128.zero, 1 ;
      Uint128.max_int, Uint128.of_string "18446744073709551613", 1 ;
      Uint128.max_int, Uint128.of_string "0xFFFFFFFFFFFFFFAA", 1 ;
    ] in

    List.iter (fun (a, b, expected) ->
      let got = Uint128.compare a b in
      if got <> expected then
        Test.fail (Printf.sprintf "%s %s: expected %i, got %i"
          (Uint128.to_string_hex a)
          (Uint128.to_string_hex b)
          expected
          got)
    ) tests
  );

  "Uint.Uint128.to_int", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, 0 ;
      Uint128.one, 1 ;
      Uint128.of_string "0x75BCD15", 123456789 ;
      Uint128.of_string "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", -1 ;
      Uint128.of_string "0xFFFFFFFFFFFFFFFFFFFFFFFFF8A432EB", -123456789 ;
      Uint128.of_string "0x0FFFFFFFFFFFFFFFFFFFFFFFF8A432EB", -123456789 ;
    ] in

    List.iter (fun (a, expected) ->
      let got = Uint128.to_int a in
      if got <> expected then
        Test.fail (Printf.sprintf "%s: expected %i, got %i"
          (Uint128.to_string_hex a)
          expected
          got)
    ) tests
  );

  "Uint.Uint128.of_int", (fun () ->
    let open Uint in
    let tests = [
      0L, Uint128.of_string "0x0" ;
      1L, Uint128.of_string "0x1" ;
      123456789L, Uint128.of_string "0x75BCD15" ;
      -1L, Uint128.of_string "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" ;
      -123456789L, Uint128.of_string "0xFFFFFFFFFFFFFFFFFFFFFFFFF8A432EB" ;
    ] in

    List.iter (fun (i, expected) ->
      let got = Uint128.of_int64 i in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%Li: expected %s, got %s"
          i
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.to_string", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, "0" ;
      Uint128.one , "1" ;
      Uint128.of_int64 10L, "10" ;
      Uint128.of_int64 15L, "15" ;
      Uint128.of_string "0x8" , "8" ;
      Uint128.of_string "0xF" , "15" ;
      Uint128.of_string "0xFF" , "255" ;
      Uint128.of_string "0xFAFF" , "64255" ;
      Uint128.of_string "0xABCDFAFF" , "2882403071" ;
      Uint128.of_string "0x123456789ABCEDF" , "81985529216487135" ;
      Uint128.max_int, "340282366920938463463374607431768211455" ;
    ] in

    List.iter (fun (a, expected) ->
      let got = Uint128.to_string a in
      if String.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          (Uint128.to_string_hex a)
          expected
          got)
    ) tests
  );

  "Uint.Uint128.to_string_hex", (fun () ->
    let open Uint in
    let tests = [
      Uint128.zero, "0x0" ;
      Uint128.one , "0x1" ;
      Uint128.max_int, "0xffffffffffffffffffffffffffffffff" ;
      Uint128.of_string "0xfffafffbfffffffffffffffff", "0xfffafffbfffffffffffffffff" ;
      Uint128.of_string "0xfffafff0000000000ffffffff", "0xfffafff0000000000ffffffff" ;
    ] in

    List.iter (fun (a, expected) ->
      let got = Uint128.to_string_hex a in
      if String.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          (Uint128.to_string_hex a)
          expected
          got)
    ) tests
  );

  "Uint.Uint128.of_string", (fun () ->
    let open Uint in
    let tests = [
      "0", Uint128.zero ;
      "1", Uint128.one ;
      "123456789123456789", Uint128.of_int64 123456789123456789L ;
      "123456789123456789123456789", Uint128.of_string "0x661efdf2e3b19f7c045f15" ;
      "12345", Uint128.of_int64 12345L ;
      "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", Uint128.max_int ;
      "340282366920938463463374607431768211455", Uint128.max_int ;
    ] in

    List.iter (fun (raw, expected) ->
      let got = Uint128.of_string raw in
      if Uint128.compare got expected <> 0 then
        Test.fail (Printf.sprintf "%s: expected %s, got %s"
          raw
          (Uint128.to_string_hex expected)
          (Uint128.to_string_hex got))
    ) tests
  );

  "Uint.Uint128.of_string bad input", (fun () ->
    let open Uint in
    let tests = [
      "a" ;
      "-1" ;
      "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" ;
      (* "3402823669209384634633746074317682114551" ; *)
      "hello" ;
    ] in

    List.iter (fun raw ->
      let raised =
        try
          let _ = Uint128.of_string raw in false
        with _ -> true
      in
      if not raised then
        Test.fail (Printf.sprintf "%s: did not raise" raw)
    ) tests
  );
]

let () = Test.run tests