4
(* Can compare nats less than 2**32 *)
6
eq_nat n1 0 (num_digits_nat n1 0 1)
7
n2 0 (num_digits_nat n2 0 1);;
9
testing_function "num_digits_nat";;
11
test (-1) eq (false,not true);;
12
test 0 eq (true,not false);;
16
(let r = make_nat 2 in
18
num_digits_nat r 0 1,1);;
20
testing_function "length_nat";;
24
(let r = make_nat 2 in
28
testing_function "equal_nat";;
30
let zero_nat = make_nat 1 in
33
equal_nat (zero_nat,zero_nat);;
35
equal_nat (nat_of_int 1,nat_of_int 1);;
38
equal_nat (nat_of_string "2",nat_of_string "2");;
40
eq (equal_nat (nat_of_string "2")(nat_of_string "3"),false);;
42
testing_function "incr_nat";;
44
let zero = nat_of_int 0 in
45
let res = incr_nat zero 0 1 1 in
47
equal_nat (zero, nat_of_int 1) &&
51
let n = nat_of_int 1 in
52
let res = incr_nat n 0 1 1 in
54
equal_nat (n, nat_of_int 2) &&
59
testing_function "decr_nat";;
61
let n = nat_of_int 1 in
62
let res = decr_nat n 0 1 0 in
64
equal_nat (n, nat_of_int 0) &&
68
let n = nat_of_int 2 in
69
let res = decr_nat n 0 1 0 in
71
equal_nat (n, nat_of_int 1) &&
75
testing_function "is_zero_nat";;
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
83
test 4 eq (is_zero_nat r 0 1, true))
86
testing_function "string_of_nat";;
90
test 1 eq_string (string_of_nat n, "0");;
92
complement_nat n 0 (if sixtyfour then 2 else 4);;
94
test 2 eq_string (string_of_nat n, "340282366920938463463374607431768211455");;
96
testing_function "string_of_nat && nat_of_string";;
99
let s = String.make i '0' in
101
ignore (test i eq_string (string_of_nat (nat_of_string s), s))
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)
108
let s = "3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333" in
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
119
test 22 eq_string (string_of_nat(nat_of_string "1073741824"), "1073741824");;
121
testing_function "gcd_nat";;
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))
133
testing_function "sqrt_nat";;
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,