summaryrefslogtreecommitdiff
path: root/tests/test_string.cppo.ml
blob: f108e8cffbbeeb77ef075ee3fb451f5e26171953 (plain)
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
open EndianString
[@@@warning "-52"]

(* Allow testing deprecated string_set functions *)
[@@@warning "-3"]

let to_t = Bytes.unsafe_to_string
(* do not allocate to avoid breaking tests *)

module BE = BigEndian
module LE = LittleEndian
module NE = NativeEndian

let big_endian = Sys.big_endian

let s = Bytes.make 10 '\x00'

let assert_bound_check2 f v1 v2 =
  try
    ignore(f v1 v2);
    assert false
  with
     | Invalid_argument("index out of bounds") -> ()

let assert_bound_check3 f v1 v2 v3 =
  try
    ignore(f v1 v2 v3);
    assert false
  with
     | Invalid_argument("index out of bounds") -> ()

let test1 () =
  assert_bound_check2 BE.get_int8 (to_t s) (-1);
  assert_bound_check2 BE.get_int8 (to_t s) 10;
  assert_bound_check2 BE.get_uint16 (to_t s) (-1);
  assert_bound_check2 BE.get_uint16 (to_t s) 9;
  assert_bound_check2 BE.get_int32 (to_t s) (-1);
  assert_bound_check2 BE.get_int32 (to_t s) 7;
  assert_bound_check2 BE.get_int64 (to_t s) (-1);
  assert_bound_check2 BE.get_int64 (to_t s) 3;

  assert_bound_check3 BE.set_int8 s (-1) 0;
  assert_bound_check3 BE.set_int8 s 10 0;
  assert_bound_check3 BE.set_int16 s (-1) 0;
  assert_bound_check3 BE.set_int16 s 9 0;
  assert_bound_check3 BE.set_int32 s (-1) 0l;
  assert_bound_check3 BE.set_int32 s 7 0l;
  assert_bound_check3 BE.set_int64 s (-1) 0L;
  assert_bound_check3 BE.set_int64 s 3 0L

let test2 () =
  BE.set_int8 s 0 63; (* in [0; 127] *)
  assert( BE.get_uint8 (to_t s) 0 = 63 );
  assert( BE.get_int8 (to_t s) 0 = 63 );

  BE.set_int8 s 0 155; (* in [128; 255] *)
  assert( BE.get_uint8 (to_t s) 0 = 155 );

  BE.set_int8 s 0 (-103); (* in [-128; -1] *)
  assert( BE.get_int8 (to_t s) 0 = (-103) );

  BE.set_int8 s 0 0x1234; (* outside of the [-127;255] range *)
  assert( BE.get_uint8 (to_t s) 0 = 0x34 );
  assert( BE.get_int8 (to_t s) 0 = 0x34 );

  BE.set_int8 s 0 0xAACD; (* outside of the [-127;255] range, -0x33 land 0xFF = 0xCD*)
  assert( BE.get_uint8 (to_t s) 0 = 0xCD );
  assert( BE.get_int8 (to_t s) 0 = (-0x33) );

  BE.set_int16 s 0 0x1234;
  assert( BE.get_uint16 (to_t s) 0 = 0x1234 );
  assert( BE.get_uint16 (to_t s) 1 = 0x3400 );
  assert( BE.get_uint16 (to_t s) 2 = 0 );

  assert( LE.get_uint16 (to_t s) 0 = 0x3412 );
  assert( LE.get_uint16 (to_t s) 1 = 0x0034 );
  assert( LE.get_uint16 (to_t s) 2 = 0 );

  if big_endian then begin
    assert( BE.get_uint16 (to_t s) 0 = NE.get_uint16 (to_t s) 0 );
    assert( BE.get_uint16 (to_t s) 1 = NE.get_uint16 (to_t s) 1 );
    assert( BE.get_uint16 (to_t s) 2 = NE.get_uint16 (to_t s) 2 );
  end
  else begin
    assert( LE.get_uint16 (to_t s) 0 = NE.get_uint16 (to_t s) 0 );
    assert( LE.get_uint16 (to_t s) 1 = NE.get_uint16 (to_t s) 1 );
    assert( LE.get_uint16 (to_t s) 2 = NE.get_uint16 (to_t s) 2 );
  end;

  assert( BE.get_int16 (to_t s) 0 = 0x1234 );
  assert( BE.get_int16 (to_t s) 1 = 0x3400 );
  assert( BE.get_int16 (to_t s) 2 = 0 );

  BE.set_int16 s 0 0xFEDC;
  assert( BE.get_uint16 (to_t s) 0 = 0xFEDC );
  assert( BE.get_uint16 (to_t s) 1 = 0xDC00 );
  assert( BE.get_uint16 (to_t s) 2 = 0 );

  assert( LE.get_uint16 (to_t s) 0 = 0xDCFE );
  assert( LE.get_uint16 (to_t s) 1 = 0x00DC );
  assert( LE.get_uint16 (to_t s) 2 = 0 );

  if big_endian then begin
    assert( BE.get_uint16 (to_t s) 0 = NE.get_uint16 (to_t s) 0 );
    assert( BE.get_uint16 (to_t s) 1 = NE.get_uint16 (to_t s) 1 );
    assert( BE.get_uint16 (to_t s) 2 = NE.get_uint16 (to_t s) 2 );
  end
  else begin
    assert( LE.get_uint16 (to_t s) 0 = NE.get_uint16 (to_t s) 0 );
    assert( LE.get_uint16 (to_t s) 1 = NE.get_uint16 (to_t s) 1 );
    assert( LE.get_uint16 (to_t s) 2 = NE.get_uint16 (to_t s) 2 );
  end;

  assert( BE.get_int16 (to_t s) 0 = -292 );
  assert( BE.get_int16 (to_t s) 1 = -9216 );
  assert( BE.get_int16 (to_t s) 2 = 0 );

  if big_endian
  then begin
    NE.set_int16 s 0 0x1234;
    assert( BE.get_uint16 (to_t s) 0 = 0x1234 );
    assert( BE.get_uint16 (to_t s) 1 = 0x3400 );
    assert( BE.get_uint16 (to_t s) 2 = 0 )
  end;

  LE.set_int16 s 0 0x1234;
  assert( BE.get_uint16 (to_t s) 0 = 0x3412 );
  assert( BE.get_uint16 (to_t s) 1 = 0x1200 );
  assert( BE.get_uint16 (to_t s) 2 = 0 );

  if not big_endian
  then begin
    NE.set_int16 s 0 0x1234;
    assert( BE.get_uint16 (to_t s) 0 = 0x3412 );
    assert( BE.get_uint16 (to_t s) 1 = 0x1200 );
    assert( BE.get_uint16 (to_t s) 2 = 0 )
  end;

  LE.set_int16 s 0 0xFEDC;
  assert( LE.get_uint16 (to_t s) 0 = 0xFEDC );
  assert( LE.get_uint16 (to_t s) 1 = 0x00FE );
  assert( LE.get_uint16 (to_t s) 2 = 0 );

  BE.set_int32 s 0 0x12345678l;
  assert( BE.get_int32 (to_t s) 0 = 0x12345678l );
  assert( LE.get_int32 (to_t s) 0 = 0x78563412l );
  if big_endian
  then assert( BE.get_int32 (to_t s) 0 = NE.get_int32 (to_t s) 0 )
  else assert( LE.get_int32 (to_t s) 0 = NE.get_int32 (to_t s) 0 );

  LE.set_int32 s 0 0x12345678l;
  assert( LE.get_int32 (to_t s) 0 = 0x12345678l );
  assert( BE.get_int32 (to_t s) 0 = 0x78563412l );

  if big_endian
  then assert( BE.get_int32 (to_t s) 0 = NE.get_int32 (to_t s) 0 )
  else assert( LE.get_int32 (to_t s) 0 = NE.get_int32 (to_t s) 0 );

  NE.set_int32 s 0 0x12345678l;
  if big_endian
  then assert( BE.get_int32 (to_t s) 0 = 0x12345678l )
  else assert( LE.get_int32 (to_t s) 0 = 0x12345678l );

  ()

let test_64 () =
  BE.set_int64 s 0 0x1234567890ABCDEFL;
  assert( BE.get_int64 (to_t s) 0 = 0x1234567890ABCDEFL );
  assert( LE.get_int64 (to_t s) 0 = 0xEFCDAB9078563412L );

  if big_endian
  then assert( BE.get_int64 (to_t s) 0 = NE.get_int64 (to_t s) 0 )
  else assert( LE.get_int64 (to_t s) 0 = NE.get_int64 (to_t s) 0 );

  LE.set_int64 s 0 0x1234567890ABCDEFL;
  assert( LE.get_int64 (to_t s) 0 = 0x1234567890ABCDEFL );
  assert( BE.get_int64 (to_t s) 0 = 0xEFCDAB9078563412L );

  if big_endian
  then assert( BE.get_int64 (to_t s) 0 = NE.get_int64 (to_t s) 0 )
  else assert( LE.get_int64 (to_t s) 0 = NE.get_int64 (to_t s) 0 );

  NE.set_int64 s 0 0x1234567890ABCDEFL;
  if big_endian
  then assert( BE.get_int64 (to_t s) 0 = 0x1234567890ABCDEFL )
  else assert( LE.get_int64 (to_t s) 0 = 0x1234567890ABCDEFL );

  ()