~ubuntu-branches/debian/sid/ocaml/sid

« back to all changes in this revision

Viewing changes to otherlibs/num/test/test_nats.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu
  • Date: 2011-04-21 21:35:08 UTC
  • mfrom: (1.1.11 upstream) (12.1.14 sid)
  • Revision ID: james.westby@ubuntu.com-20110421213508-kg34453aqmb0moha
* Fixes related to -output-obj with g++ (in debian/patches):
  - add Declare-primitive-name-table-as-const-char
  - add Avoid-multiple-declarations-in-generated-.c-files-in
  - fix Embed-bytecode-in-C-object-when-using-custom: the closing
    brace for extern "C" { ... } was missing in some cases

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
open Test;;
2
 
open Nat;;
3
 
 
4
 
(* Can compare nats less than 2**32 *)
5
 
let equal_nat n1 n2 =
6
 
 eq_nat n1 0 (num_digits_nat n1 0 1) 
7
 
        n2 0 (num_digits_nat n2 0 1);;
8
 
 
9
 
testing_function "num_digits_nat";;
10
 
 
11
 
test (-1) eq (false,not true);;
12
 
test 0 eq (true,not false);;
13
 
 
14
 
test 1
15
 
eq_int
16
 
(let r = make_nat 2 in
17
 
  set_digit_nat r 1 1;
18
 
  num_digits_nat r 0 1,1);;
19
 
 
20
 
testing_function "length_nat";;
21
 
 
22
 
test 1
23
 
eq_int
24
 
(let r = make_nat 2 in
25
 
  set_digit_nat r 0 1;
26
 
  length_nat r,2);;
27
 
 
28
 
testing_function "equal_nat";;
29
 
 
30
 
let zero_nat = make_nat 1 in
31
 
 
32
 
test 1
33
 
equal_nat (zero_nat,zero_nat);;
34
 
test 2
35
 
equal_nat (nat_of_int 1,nat_of_int 1);;
36
 
 
37
 
test 3
38
 
equal_nat (nat_of_string "2",nat_of_string "2");;
39
 
test 4
40
 
eq (equal_nat (nat_of_string "2")(nat_of_string "3"),false);;
41
 
 
42
 
testing_function "incr_nat";;
43
 
 
44
 
let zero = nat_of_int 0 in
45
 
let res = incr_nat zero 0 1 1 in
46
 
 test 1
47
 
  equal_nat (zero, nat_of_int 1) &&
48
 
 test 2
49
 
  eq (res,0);;
50
 
 
51
 
let n = nat_of_int 1 in
52
 
let res = incr_nat n 0 1 1 in
53
 
 test 3
54
 
  equal_nat (n, nat_of_int 2) &&
55
 
 test 4
56
 
  eq (res,0);;
57
 
 
58
 
 
59
 
testing_function "decr_nat";;
60
 
 
61
 
let n = nat_of_int 1 in
62
 
let res = decr_nat n 0 1 0 in
63
 
 test 1
64
 
  equal_nat (n, nat_of_int 0) &&
65
 
 test 2
66
 
  eq (res,1);;
67
 
 
68
 
let n = nat_of_int 2 in
69
 
let res = decr_nat n 0 1 0 in
70
 
 test 3
71
 
  equal_nat (n, nat_of_int 1) &&
72
 
 test 4
73
 
  eq (res,1);;
74
 
 
75
 
testing_function "is_zero_nat";;
76
 
 
77
 
let n = nat_of_int 1 in
78
 
test 1 eq (is_zero_nat n 0 1,false) &&
79
 
test 2 eq (is_zero_nat (make_nat 1) 0 1, true) &&
80
 
test 3 eq (is_zero_nat (make_nat 2) 0 2, true) &&
81
 
(let r = make_nat 2 in
82
 
  set_digit_nat r 1 1;
83
 
  test 4 eq (is_zero_nat r 0 1, true))
84
 
;;
85
 
 
86
 
testing_function "string_of_nat";;
87
 
 
88
 
let n = make_nat 4;;
89
 
 
90
 
test 1 eq_string (string_of_nat n, "0");;
91
 
 
92
 
complement_nat n 0 (if sixtyfour then 2 else 4);;
93
 
 
94
 
test 2 eq_string (string_of_nat n, "340282366920938463463374607431768211455");;
95
 
 
96
 
testing_function "string_of_nat && nat_of_string";;
97
 
 
98
 
for i = 1 to 20 do
99
 
  let s = String.make i '0' in
100
 
  String.set s 0 '1';
101
 
  ignore (test i eq_string (string_of_nat (nat_of_string s), s))
102
 
done;;
103
 
 
104
 
let set_mult_digit_nat n1 d1 l1 n2 d2 l2 n3 d3 =
105
 
  ignore (mult_digit_nat n1 d1 l1 n2 d2 l2 n3 d3)
106
 
;;
107
 
 
108
 
let s = "3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" in
109
 
test 21 equal_nat (
110
 
nat_of_string s,
111
 
(let nat = make_nat 15 in 
112
 
  set_digit_nat nat 0 3;
113
 
  set_mult_digit_nat nat 0 15 
114
 
                 (nat_of_string (String.sub s 0 135)) 0 14 
115
 
                 (nat_of_int 10) 0;
116
 
  nat))
117
 
;;
118
 
 
119
 
test 22 eq_string (string_of_nat(nat_of_string "1073741824"), "1073741824");;
120
 
 
121
 
testing_function "gcd_nat";;
122
 
 
123
 
for i = 1 to 20 do
124
 
  let n1 = Random.int 1000000000
125
 
  and n2 = Random.int 100000 in
126
 
  let nat1 = nat_of_int n1
127
 
  and nat2 = nat_of_int n2 in
128
 
  ignore (gcd_nat nat1 0 1 nat2 0 1);
129
 
  ignore (test i eq (int_of_nat nat1, Int_misc.gcd_int n1 n2))
130
 
done
131
 
;;
132
 
 
133
 
testing_function "sqrt_nat";;
134
 
 
135
 
test 1 equal_nat (sqrt_nat (nat_of_int 1) 0 1, nat_of_int 1);;
136
 
test 2 equal_nat (let n = nat_of_string "8589934592" in
137
 
                  sqrt_nat n 0 (length_nat n),
138
 
                  nat_of_string "92681");;
139
 
test 3 equal_nat (let n = nat_of_string "4294967295" in
140
 
                  sqrt_nat n 0 (length_nat n),
141
 
                  nat_of_string "65535");;
142
 
test 4 equal_nat (let n = nat_of_string "18446744065119617025" in
143
 
                  sqrt_nat n 0 (length_nat n),
144
 
                  nat_of_string "4294967295");;
145
 
test 5 equal_nat (sqrt_nat (nat_of_int 15) 0 1,
146
 
                  nat_of_int 3);;