| 1 |
functor Test (I: INTEGER) = |
| 2 |
struct |
| 3 |
fun foreach (l, f) = List.app f l |
| 4 |
|
| 5 |
val m = concat ["Int", Int.toString (valOf I.precision)] |
| 6 |
|
| 7 |
val _ = print (concat ["Testing ", m, "\n"]) |
| 8 |
|
| 9 |
val nums = |
| 10 |
[valOf I.maxInt, |
| 11 |
I.- (valOf I.maxInt, I.fromInt 1)] |
| 12 |
@ (List.foldl |
| 13 |
(fn (i, ac) => |
| 14 |
case SOME (I.fromInt i) handle Overflow => NONE of |
| 15 |
NONE => ac |
| 16 |
| SOME i => i :: ac) |
| 17 |
[] |
| 18 |
[100, 10, 5, 2, 1, 0, ~1, ~2, ~5, ~10, ~100]) |
| 19 |
@ [I.+ (I.fromInt 1, valOf I.minInt), |
| 20 |
valOf I.minInt] |
| 21 |
|
| 22 |
fun err msg = print (concat [m, ": ", concat msg, "\n"]) |
| 23 |
|
| 24 |
datatype z = datatype StringCvt.radix |
| 25 |
val _ = |
| 26 |
foreach |
| 27 |
(nums, fn i => |
| 28 |
foreach |
| 29 |
([("toString", I.toString, LargeInt.toString), |
| 30 |
("fmt BIN", I.fmt BIN, LargeInt.fmt BIN), |
| 31 |
("fmt OCT", I.fmt OCT, LargeInt.fmt OCT), |
| 32 |
("fmt DEC", I.fmt DEC, LargeInt.fmt DEC), |
| 33 |
("fmt HEX", I.fmt HEX, LargeInt.fmt HEX)], |
| 34 |
fn (name, f, f') => |
| 35 |
let |
| 36 |
val s = f i |
| 37 |
val s' = f' (I.toLarge i) handle Overflow => "Overflow" |
| 38 |
in |
| 39 |
if s = s' |
| 40 |
then () |
| 41 |
else err [name, " ", s, " <> ", name, " ", s'] |
| 42 |
end)) |
| 43 |
|
| 44 |
structure Answer = |
| 45 |
struct |
| 46 |
datatype t = |
| 47 |
Div |
| 48 |
| Int of I.int |
| 49 |
| Overflow |
| 50 |
|
| 51 |
val toString = |
| 52 |
fn Div => "Div" |
| 53 |
| Int i => I.toString i |
| 54 |
| Overflow => "Overflow" |
| 55 |
|
| 56 |
fun run (f: unit -> I.int): t = |
| 57 |
Int (f ()) |
| 58 |
handle General.Div => Div |
| 59 |
| General.Overflow => Overflow |
| 60 |
|
| 61 |
val equals: t * t -> bool = op = |
| 62 |
end |
| 63 |
|
| 64 |
val _ = |
| 65 |
foreach |
| 66 |
(nums, fn i => |
| 67 |
let |
| 68 |
val a1 = Answer.Int i |
| 69 |
val a2 = Answer.run (fn () => I.fromLarge (I.toLarge i)) |
| 70 |
in |
| 71 |
if Answer.equals (a1, a2) |
| 72 |
then () |
| 73 |
else err ["fromLarge (toLarge ", I.toString i, ") = ", |
| 74 |
Answer.toString a2] |
| 75 |
end) |
| 76 |
|
| 77 |
val _ = |
| 78 |
foreach |
| 79 |
([("abs", I.abs, LargeInt.abs), |
| 80 |
("~", I.~, LargeInt.~), |
| 81 |
("fromString o toString", |
| 82 |
valOf o I.fromString o I.toString, |
| 83 |
valOf o LargeInt.fromString o LargeInt.toString)], |
| 84 |
fn (name, f, f') => |
| 85 |
foreach |
| 86 |
(nums, fn i => |
| 87 |
let |
| 88 |
val a = Answer.run (fn () => f i) |
| 89 |
val a' = Answer.run (fn () => I.fromLarge (f' (I.toLarge i))) |
| 90 |
in |
| 91 |
if Answer.equals (a, a') |
| 92 |
then () |
| 93 |
else err [name, " ", I.toString i, |
| 94 |
" = ", Answer.toString a, |
| 95 |
" <> ", Answer.toString a'] |
| 96 |
end)) |
| 97 |
|
| 98 |
val _ = |
| 99 |
foreach |
| 100 |
(nums, fn i => |
| 101 |
foreach |
| 102 |
([("BIN", BIN), ("OCT", OCT), ("DEC", DEC), ("HEX", HEX)], |
| 103 |
fn (rName, r) => |
| 104 |
let |
| 105 |
val i' = valOf (StringCvt.scanString (I.scan r) (I.fmt r i)) |
| 106 |
in |
| 107 |
if i = i' |
| 108 |
then () |
| 109 |
else err ["scan ", rName, " ", I.toString i, " = ", I.toString i'] |
| 110 |
end)) |
| 111 |
|
| 112 |
val _ = |
| 113 |
foreach |
| 114 |
([("sign", I.sign, LargeInt.sign), |
| 115 |
("toInt", I.toInt, LargeInt.toInt)], |
| 116 |
fn (name, f, f') => |
| 117 |
foreach |
| 118 |
(nums, fn i => |
| 119 |
let |
| 120 |
val a = Answer.run (fn () => I.fromInt (f i)) |
| 121 |
val a' = Answer.run (fn () => I.fromInt (f' (I.toLarge i))) |
| 122 |
in |
| 123 |
if Answer.equals (a, a') |
| 124 |
then () |
| 125 |
else err [name, " ", I.toString i, |
| 126 |
" = ", Answer.toString a, |
| 127 |
" <> ", Answer.toString a'] |
| 128 |
end)) |
| 129 |
|
| 130 |
val _ = |
| 131 |
foreach |
| 132 |
([("+", I.+, LargeInt.+), |
| 133 |
("-", I.-, LargeInt.-), |
| 134 |
("*", I.*, LargeInt.* ), |
| 135 |
("div", I.div, LargeInt.div), |
| 136 |
("max", I.max, LargeInt.max), |
| 137 |
("min", I.min, LargeInt.min), |
| 138 |
("mod", I.mod, LargeInt.mod), |
| 139 |
("quot", I.quot, LargeInt.quot), |
| 140 |
("rem", I.rem, LargeInt.rem)], |
| 141 |
fn (name, |
| 142 |
f: I.int * I.int -> I.int, |
| 143 |
f': LargeInt.int * LargeInt.int -> LargeInt.int) => |
| 144 |
foreach |
| 145 |
(nums, fn i: I.int => |
| 146 |
foreach |
| 147 |
(nums, fn j: I.int => |
| 148 |
let |
| 149 |
val a = Answer.run (fn () => f (i, j)) |
| 150 |
val a' = Answer.run (fn () => |
| 151 |
I.fromLarge (f' (I.toLarge i, I.toLarge j))) |
| 152 |
in |
| 153 |
if Answer.equals (a, a') |
| 154 |
then () |
| 155 |
else err [I.toString i, " ", name, " ", I.toString j, |
| 156 |
" = ", Answer.toString a, " <> ", Answer.toString a'] |
| 157 |
end))) |
| 158 |
|
| 159 |
val _ = |
| 160 |
foreach |
| 161 |
([(">", I.>, LargeInt.>), |
| 162 |
(">=", I.>=, LargeInt.>=), |
| 163 |
("<", I.<, LargeInt.<), |
| 164 |
("<=", I.<=, LargeInt.<=), |
| 165 |
("sameSign", I.sameSign, LargeInt.sameSign)], |
| 166 |
fn (name, f, f') => |
| 167 |
foreach |
| 168 |
(nums, fn i: I.int => |
| 169 |
foreach |
| 170 |
(nums, fn j: I.int => |
| 171 |
let |
| 172 |
val b = f (i, j) |
| 173 |
val b' = f' (I.toLarge i, I.toLarge j) |
| 174 |
in |
| 175 |
if b = b' |
| 176 |
then () |
| 177 |
else err [I.toString i, " ", name, " ", I.toString j, |
| 178 |
" = ", Bool.toString b, " <> ", Bool.toString b'] |
| 179 |
end))) |
| 180 |
|
| 181 |
structure Order = |
| 182 |
struct |
| 183 |
datatype t = datatype order |
| 184 |
|
| 185 |
val equals: t * t -> bool = op = |
| 186 |
|
| 187 |
val toString = |
| 188 |
fn EQUAL => "EQUAL" |
| 189 |
| GREATER => "GREATER" |
| 190 |
| LESS => "LESS" |
| 191 |
end |
| 192 |
|
| 193 |
val _ = |
| 194 |
foreach |
| 195 |
(nums, fn i => |
| 196 |
foreach |
| 197 |
(nums, fn j => |
| 198 |
let |
| 199 |
val ord = I.compare (i, j) |
| 200 |
val ord' = LargeInt.compare (I.toLarge i, I.toLarge j) |
| 201 |
in |
| 202 |
if Order.equals (ord, ord') |
| 203 |
then () |
| 204 |
else err ["compare (", I.toString i, ", ", |
| 205 |
I.toString j, ") = ", |
| 206 |
Order.toString ord, |
| 207 |
" <> ", |
| 208 |
Order.toString ord'] |
| 209 |
end)) |
| 210 |
|
| 211 |
end |
| 212 |
|
| 213 |
structure S = Test (Int2) |
| 214 |
structure S = Test (Int3) |
| 215 |
structure S = Test (Int4) |
| 216 |
structure S = Test (Int7) |
| 217 |
structure S = Test (Int8) |
| 218 |
structure S = Test (Int9) |
| 219 |
structure S = Test (Int13) |
| 220 |
structure S = Test (Int16) |
| 221 |
structure S = Test (Int17) |
| 222 |
structure S = Test (Int20) |
| 223 |
structure S = Test (Int25) |
| 224 |
structure S = Test (Int30) |
| 225 |
structure S = Test (Int31) |
| 226 |
structure S = Test (Int32) |
| 227 |
structure S = Test (Int64) |