moo/kernel/test-bi.moo

111 lines
2.8 KiB
Smalltalk
Raw Permalink Normal View History

2019-04-08 04:00:47 +00:00
#include "Moo.moo".
2019-04-08 04:00:47 +00:00
////////////////////////////////////////////////////////////////#
// MAIN
////////////////////////////////////////////////////////////////#
2019-04-08 04:00:47 +00:00
class MyObject(Object)
{
var(#class) a := 100.
var(#classinst) K := 200.
method(#class) testBigintDiv
{
| i q r divd divr divd_ubound divr_ubound |
divr_ubound := 16rFFFFFFFFFFFFFFFFFFFFFFFF.
divd_ubound := 16rFFFFFFFFFFFFFFFFFFFFFFFF.
divr := 1.
while (divr <= divr_ubound)
{
("divr => " & divr asString) dump.
2019-04-08 04:00:47 +00:00
divd := 0.
while (divd <= divd_ubound)
{
q := divd div: divr.
r := divd rem: divr.
if (divd ~= (q * divr + r)) { i dump. divd dump. divr dump. q dump. r dump. (q * divr + r) dump. ^false. }.
divd := divd + 1.
}.
divd := divr + 1.
}.
^true
}
method(#class) testBigintDiv2
{
2019-08-24 15:45:08 +00:00
| ffi now |
2019-04-08 04:00:47 +00:00
[ ffi := FFI new: "libc.so.6". ] on: Exception do: [:ex |
[ ffi := FFI new: "libc.so" ] on: Exception do: [ :ex2 | ffi := FFI new: "msvcrt.dll" ]
].
now := ffi call: #time signature: "l>i" arguments: #(0).
ffi call: #srand signature: "i>" arguments: ##(now).
///ffi call: #srandom signature: "i>" arguments: ##(now).
2019-04-08 04:00:47 +00:00
[
2019-08-24 15:45:08 +00:00
| i q r divd divr divd_ubound divr_ubound x |
2019-04-08 04:00:47 +00:00
divr_ubound := 16rFFFFFFFFFFFFFFFFFFFFFFFF.
divd_ubound := 16rFFFFFFFFFFFFFFFFFFFFFFFF.
2019-08-24 15:45:08 +00:00
i := 0.
2019-04-08 04:00:47 +00:00
while (true)
{
x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20.
divd := (ffi call: #rand signature: ">i" arguments: nil).
///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20.
///divd := (ffi call: #random signature: ">l" arguments: nil).
2019-04-08 04:00:47 +00:00
while (x > 0)
{
divd := (divd bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil).
///divd := (divd bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil).
2019-04-08 04:00:47 +00:00
x := x - 1.
}.
x := (ffi call: #rand signature: ">i" arguments: nil) rem: 20.
divr := (ffi call: #rand signature: ">i" arguments: nil).
///x := (ffi call: #random signature: ">l" arguments: nil) rem: 20.
///divr := (ffi call: #random signature: ">l" arguments: nil).
2019-04-08 04:00:47 +00:00
while (x > 0)
{
divr := (divr bitShift: 7) bitOr: (ffi call: #rand signature: ">i" arguments: nil).
//divr := (divr bitShift: 7) bitOr: (ffi call: #random signature: ">l" arguments: nil).
2019-04-08 04:00:47 +00:00
x := x - 1.
}.
if (divr = 0) { divr := 1 }.
2019-08-24 15:45:08 +00:00
i := i + 1.
ffi call: #printf signature: "s | l > l" arguments: ##("%d\r", i).
2019-08-24 15:45:08 +00:00
2019-04-08 04:00:47 +00:00
q := divd div: divr.
r := divd rem: divr.
if (divd ~= (q * divr + r))
{
// dump numbers if result is wrong
divd dump.
divr dump.
q dump.
r dump.
(q * divr + r) dump.
^false.
}.
////((q asString) & " " & (r asString)) dump
2019-04-08 04:00:47 +00:00
}.
] ensure: [ ffi close. ].
^true
}
method(#class) main
{
self testBigintDiv2.
}
}