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 (137 lines) | stat: -rw-r--r-- 4,191 bytes parent folder | download | duplicates (3)
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
(* TEST *)

let assert_raise_invalid_argument f v =
  assert (try ignore (f v); false with Invalid_argument _ -> true)

let test_constants () =
  assert (Uchar.(to_int min) = 0x0000);
  assert (Uchar.(to_int max) = 0x10FFFF);
  assert (Uchar.(to_int bom) = 0xFEFF);
  assert (Uchar.(to_int rep) = 0xFFFD);
  ()

let test_succ () =
  assert (Uchar.(to_int (succ min)) = 0x0001);
  assert (Uchar.(to_int (succ (of_int 0xD7FF))) = 0xE000);
  assert (Uchar.(to_int (succ (of_int 0xE000))) = 0xE001);
  assert_raise_invalid_argument Uchar.succ Uchar.max;
  ()

let test_pred () =
  assert_raise_invalid_argument Uchar.pred Uchar.min;
  assert (Uchar.(to_int (pred (of_int 0xD7FF))) = 0xD7FE);
  assert (Uchar.(to_int (pred (of_int 0xE000))) = 0xD7FF);
  assert (Uchar.(to_int (pred max)) = 0x10FFFE);
  ()

let test_is_valid () =
  assert (not (Uchar.is_valid (-1)));
  assert (Uchar.is_valid 0x0000);
  assert (Uchar.is_valid 0xD7FF);
  assert (not (Uchar.is_valid 0xD800));
  assert (not (Uchar.is_valid 0xDFFF));
  assert (Uchar.is_valid 0xE000);
  assert (Uchar.is_valid 0x10FFFF);
  assert (not (Uchar.is_valid 0x110000));
  assert (not (Uchar.is_valid min_int));
  assert (not (Uchar.is_valid max_int));
  ()

let char_max = Uchar.of_int 0x00FF

let test_is_char () =
  assert (Uchar.(is_char Uchar.min));
  assert (Uchar.(is_char char_max));
  assert (Uchar.(not (is_char (of_int 0x0100))));
  assert (not (Uchar.is_char Uchar.max));
  ()

let test_of_char () =
  assert (Uchar.(equal (of_char '\xFF') char_max));
  assert (Uchar.(equal (of_char '\x00') min));
  ()

let test_to_char () =
  assert (Uchar.(to_char min) = '\x00');
  assert (Uchar.(to_char char_max) = '\xFF');
  assert_raise_invalid_argument Uchar.to_char (Uchar.succ char_max);
  assert_raise_invalid_argument Uchar.to_char Uchar.max;
  ()

let test_equal () =
  assert (Uchar.(equal min min));
  assert (Uchar.(equal max max));
  assert (not Uchar.(equal min max));
  ()

let test_compare () =
  assert (Uchar.(compare min min) = 0);
  assert (Uchar.(compare max max) = 0);
  assert (Uchar.(compare min max) = (-1));
  assert (Uchar.(compare max min) = 1);
  ()

let test_hash () =
  let f u =
    assert (Hashtbl.hash u = Uchar.hash u);
    assert (Hashtbl.seeded_hash 42 u = Uchar.seeded_hash 42 u)
  in
  List.iter (Fun.compose f Uchar.of_int)
    [0x0000; 0x002D; 0x00E9; 0x062D; 0x2014; 0x1F349]

let test_utf_decode () =
  let d0 = Uchar.utf_decode 1 Uchar.min in
  let d1 = Uchar.utf_decode 4 Uchar.max in
  let invalid = Uchar.utf_decode_invalid 3 in
  assert (Uchar.utf_decode_is_valid d0);
  assert (Uchar.utf_decode_length d0 = 1);
  assert (Uchar.equal (Uchar.utf_decode_uchar d0) Uchar.min);
  assert (Uchar.utf_decode_is_valid d1);
  assert (Uchar.utf_decode_length d1 = 4);
  assert (Uchar.equal (Uchar.utf_decode_uchar d1) Uchar.max);
  assert (not (Uchar.utf_decode_is_valid invalid));
  assert (Uchar.utf_decode_length invalid = 3);
  assert (Uchar.equal (Uchar.utf_decode_uchar invalid) Uchar.rep);
  ()

let test_utf_8_decode_length_of_byte () =
  assert (Uchar.utf_8_decode_length_of_byte "a".[0] = 1);
  assert (Uchar.utf_8_decode_length_of_byte "é".[0] = 2);
  assert (Uchar.utf_8_decode_length_of_byte "‘".[0] = 3);
  assert (Uchar.utf_8_decode_length_of_byte "🐫".[0] =
          Uchar.max_utf_8_decode_length);
  assert (Uchar.utf_8_decode_length_of_byte "\xFF".[0] = 0);
  ()

let test_utf_x_byte_length () =
  assert (Uchar.utf_8_byte_length Uchar.min = 1);
  assert (Uchar.utf_16_byte_length Uchar.min = 2);
  assert (Uchar.utf_8_byte_length Uchar.max = 4);
  assert (Uchar.utf_16_byte_length Uchar.max = 4);
  let c = Uchar.of_int 0x1F42B in
  assert (Uchar.utf_8_byte_length c = 4);
  assert (Uchar.utf_16_byte_length c = 4);
  let c = Uchar.of_int 0x9A7C in
  assert (Uchar.utf_8_byte_length c = 3);
  assert (Uchar.utf_16_byte_length c = 2);
  ()

let tests () =
  test_constants ();
  test_succ ();
  test_pred ();
  test_is_valid ();
  test_is_char ();
  test_of_char ();
  test_to_char ();
  test_equal ();
  test_compare ();
  test_hash ();
  test_utf_decode ();
  test_utf_8_decode_length_of_byte ();
  test_utf_x_byte_length ();
  ()

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