File: ofstring.ml

package info (click to toggle)
ocaml-zarith 1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: ansic: 2,998; ml: 2,767; sh: 288; makefile: 81
file content (292 lines) | stat: -rw-r--r-- 8,888 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
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
let pow2 n =
  let rec doit acc n =
    if n<=0 then acc else doit (Z.add acc acc) (n-1)
  in
  doit Z.one n

let p30 = pow2 30
let p62 = pow2 62
let p300 = pow2 300
let p120 = pow2 120
let p121 = pow2 121

let test_of_string_Z () =
  let round_trip_Z () =
    let round_trip fmt x=
      (Z.equal (Z.of_string (Z.format fmt x)) x)
    in
    let formats = [
      "%i"; "%#b"; "%#o"; "%#x"; "%#X";
      "%+i"; "%#+b"; "%#+o"; "%#+x"; "%#+X";
      "%+0i"; "%#+0b"; "%#+0o"; "%#+0x"; "%#+0X";
    ] in
    let numbers =
      let (+) = Z.add in
      let  l = [p30; p62; p30 + p62; p300; p120; p121] in
      l @ (List.map Z.neg l)
    in
    List.iter
      (fun fmt ->
         assert
           (
             List.for_all
               (fun x -> round_trip fmt x)
               numbers
           )
      )
      formats
  in
  let fail d f x =
    try
      ignore (f x);
      Printf.printf "%s should fail on %s\n" d x
    with _ -> ()
  in
  let succ d f x y =
    try
      let z = f x in
      if Z.equal z  y
      then ()
      else
        Printf.printf
          "%s(%s) returned %s, expected %s\n"
          d
          x
          (Z.to_string z)
          (Z.to_string y)
    with _ ->
      Printf.printf "%s failed. Expected %s\n" d (Z.to_string y)
  in
  let z_and_int_agree s =
    let f = try Some (int_of_string s) with _ -> None in
    let z = try Some (Z.of_string s) with _ -> None in
    match f,z with
    | None, None -> ()
    | Some i, Some z ->
      if not (Z.equal (Z.of_int i) z)
      then
        Printf.printf
          "Z.of_string (%s) returned %s, expected %s\n"
          s
          (Z.to_string z)
          (string_of_int i)
    | Some i, None ->
      Printf.printf
        "Z.of_string (%s) failed, expected %s\n"
        s
        (string_of_int i)
    | None, Some z ->
      Printf.printf
        "Z.of_string (%s) returned %s, failure expected"
        s
        (Z.to_string z)
  in

  round_trip_Z ();

  fail "Z.of_string" Z.of_string "0b2";
  fail "Z.of_string" Z.of_string "0o8";
  fail "Z.of_string" Z.of_string "0xg";
  fail "Z.of_string" Z.of_string "0xG";
  fail "Z.of_string" Z.of_string "0A";
  succ "Z.of_string" Z.of_string "" Z.zero;
  succ "Z.of_string" Z.of_string "+" Z.zero;
  succ "Z.of_string" Z.of_string "-" Z.zero;
  succ "Z.of_string" Z.of_string "0x" Z.zero;
  succ "Z.of_string" Z.of_string "0b" Z.zero;

  fail "Z.of_substring" (Z.of_substring ~pos:1 ~len:2) "0b2";
  fail "Z.of_substring" (Z.of_substring ~pos:1 ~len:2) "0o8";
  fail "Z.of_substring" (Z.of_substring ~pos:1 ~len:2) "0xg";
  fail "Z.of_substring" (Z.of_substring ~pos:1 ~len:2) "0xG";
  fail "Z.of_substring" (Z.of_substring ~pos:1 ~len:1) "0A";
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:0) "+" Z.zero;
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:1) "-+" Z.zero;
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:2)"--1-" (Z.minus_one);
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:2)"--1\000" (Z.minus_one);
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:2)"\000-1\000" (Z.minus_one);
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:1)"00b1" Z.zero;
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:2)"00b1" Z.zero;
  succ "Z.of_substring" (Z.of_substring ~pos:1 ~len:3)"00b1" Z.one;

  z_and_int_agree "_123";
  z_and_int_agree "1_23";
  z_and_int_agree "12_3";
  z_and_int_agree "123_";
  z_and_int_agree "0x_123";
  z_and_int_agree "0_123";

  let s = Z.format "%#b" p120 in
  let n = String.length s in
  for i = 0 to n - 3 do
    succ "Z.of_substring"
      (Z.of_substring ~pos:0 ~len:(n - i))
      s
      (Z.shift_right p120 i)
  done

let _ = test_of_string_Z ()

let test_of_string_Q () =
  let round_trip_Q () =
    let round_trip fmt x=
      let os = Q.of_string (Z.to_string x) in
      let ob = Q.of_bigint x in
      if Q.equal os ob then
        true
      else begin
          Format.printf "%a not equal to %a\n" Q.pp_print os Q.pp_print ob;
          false
        end
    in
    let formats = [
      "%i"; "%#b"; "%#o"; "%#x"; "%#X";
      "%+i"; "%#+b"; "%#+o"; "%#+x"; "%#+X";
      "%+0i"; "%#+0b"; "%#+0o"; "%#+0x"; "%#+0X";
    ] in
    let numbers =
      let (+) = Z.add in
      let  l = [p30; p62; p30 + p62; p300; p120; p121] in
      (l @ (List.map Z.neg l))
    in
    List.iter
      (fun fmt ->
         assert
           (
             List.for_all
               (fun x -> round_trip fmt x)
               numbers
           )
      )
      formats
  in
  let fail d f x =
    try
      let s = f x in
      Printf.printf "%s should fail on %s. Got %s\n" d x (Q.to_string s)
    with _ -> ()
  in
  let succ d f x y =
    try
      let z = f x in
      if Q.equal z  y
      then ()
      else
        Printf.printf
          "%s(%s) returned %s, expected %s\n"
          d
          x
          (Q.to_string z)
          (Q.to_string y)
    with exc ->
      Printf.printf "%s failed. Expected %s. Got %s\n" d (Q.to_string y)
                    (Printexc.to_string exc)
  in
  let q_and_float_agree s =
    let f = try Some (float_of_string s) with _ -> None in
    let q = try Some (Q.of_string s) with _ -> None in
    match f,q with
    | None, None -> ()
    | Some f, Some q ->
      if not ((Q.to_float q) = f)
      then
        Printf.printf
        "Q.of_string (%s) returned %s, expected %s\n"
        s
        (Q.to_string q)
        (string_of_float f)
    | Some f, None ->
      Printf.printf
        "Q.of_string (%s) failed, expected %s\n"
        s
        (string_of_float f)
    | None, Some q ->
      Printf.printf
        "Q.of_string (%s) returned %s, failure expected"
        s
        (Q.to_string q)
  in


  round_trip_Q ();

  fail "Q.of_string" Q.of_string "0b2";
  fail "Q.of_string" Q.of_string "0o8";
  fail "Q.of_string" Q.of_string "0xg";
  fail "Q.of_string" Q.of_string "0xG";
  fail "Q.of_string" Q.of_string "0A";
  succ "Q.of_string" Q.of_string "" Q.zero;
  succ "Q.of_string" Q.of_string "+" Q.zero;
  succ "Q.of_string" Q.of_string "-" Q.zero;
  succ "Q.of_string" Q.of_string "0x" Q.zero;
  succ "Q.of_string" Q.of_string "0X" Q.zero;
  succ "Q.of_string" Q.of_string "0o" Q.zero;
  succ "Q.of_string" Q.of_string "0O" Q.zero;
  succ "Q.of_string" Q.of_string "0b" Q.zero;
  succ "Q.of_string" Q.of_string "0B" Q.zero;
  succ "Q.of_string" Q.of_string "0b101" (Q.of_string "5");
  succ "Q.of_string" Q.of_string "0B101" (Q.of_string "5");
  succ "Q.of_string" Q.of_string "0o101" (Q.of_string "65");
  succ "Q.of_string" Q.of_string "0O101" (Q.of_string "65");

  fail "Q.of_string" Q.of_string "0b2";
  fail "Q.of_string" Q.of_string "0o8";
  fail "Q.of_string" Q.of_string "0xg";
  fail "Q.of_string" Q.of_string "0xG";
  fail "Q.of_string" Q.of_string "0A";
  fail "Q.of_string" Q.of_string "-0b0.1e1";
  fail "Q.of_string" Q.of_string "-0o0.1E1";
  fail "Q.of_string" Q.of_string "-0b0.1P1";
  fail "Q.of_string" Q.of_string "-0o0.1p1";
  fail "Q.of_string" Q.of_string "-0.1P1";
  fail "Q.of_string" Q.of_string "-0.1p1";
  succ "Q.of_string" Q.of_string "0x1e2" (Q.of_int 482);
  succ "Q.of_string" Q.of_string "1e2" (Q.of_int 100);
  succ "Q.of_string" Q.of_string "+" Q.zero;
  succ "Q.of_string" Q.of_string "-+" Q.zero;
  succ "Q.of_string" Q.of_string "-1" Q.minus_one;
  succ "Q.of_string" Q.of_string "+0xFF.8" (Q.of_float 255.5);
  succ "Q.of_string" Q.of_string "+0xff.8" (Q.of_float 255.5);
  succ "Q.of_string" Q.of_string "-0xFF.8" (Q.of_float (-255.5));
  succ "Q.of_string" Q.of_string "-0xff.8" (Q.of_float (-255.5));
  succ "Q.of_string" Q.of_string "-0.1e1" (Q.of_float (float_of_string "-0.1e1")) ;
  succ "Q.of_string" Q.of_string "-0.1E1" (Q.of_float (float_of_string "-0.1E1")) ;
  succ "Q.of_string" Q.of_string "-0x0.1P1" (Q.of_float (float_of_string "-0x0.1P1")) ;
  succ "Q.of_string" Q.of_string "-0x0.1p1" (Q.of_float (float_of_string "-0x0.1p1")) ;
  succ "Q.of_string" Q.of_string "6.674e-11" (Q.of_string "0.00000000006674") ;

  q_and_float_agree "-0x0.1p1" ;
  q_and_float_agree "-0x0.1P1" ;
  q_and_float_agree "-0x0.1p10" ;
  q_and_float_agree "-0x0.1p10" ;

  q_and_float_agree "1_2.34e03";
  q_and_float_agree "12_.34e03";
  q_and_float_agree "12._34e03";
  q_and_float_agree "12.3_4e03";
  q_and_float_agree "12.34_e03";
  (* float_of_string accept leading underscores after ( 'e' | 'E'), Q does not. *)
  (* q_and_float_agree "12.34e_03"; *)
  q_and_float_agree "12.34e0_3";
  q_and_float_agree "12.34e03_";

  q_and_float_agree "000_001";
  q_and_float_agree "001_000";

  q_and_float_agree "123.";

  (* underscores right after dot are accepted. *)
  q_and_float_agree "1._001";
  q_and_float_agree "._001";
  (* float_of_string doesn't accept strings without digits, Q and Z do (e.g. "+", "-", "0x", "." *)
  (* q_and_float_agree "."; *)
  (* q_and_float_agree "._"; *)


  q_and_float_agree "0.x00a";
  q_and_float_agree ".-001";

  ()


let _ = test_of_string_Q ()