Skip to content

Commit aa8b60e

Browse files
committed
Refactor test to add more operators later.
1 parent 20958a0 commit aa8b60e

1 file changed

Lines changed: 43 additions & 23 deletions

File tree

test/test.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,53 +60,73 @@ for(let i = 0; i < 100000; ++i) {
6060
debug(randDouble());
6161
}
6262

63+
interface TestSpec {
64+
expr: string;
65+
libResult: string;
66+
};
67+
68+
type Test = () => TestSpec;
69+
6370
const bc = childProcess.spawn('bc');
71+
72+
let testSpec: TestSpec;
6473
let a = new BigFloat();
6574
let b = new BigFloat();
6675

6776
let total = 0;
77+
let testNum = 0;
6878
let bcResult = '';
6979

7080
bc.stdout.on('data', (data: string) => {
71-
let libResult = a.mul(b).toString(10);
7281
bcResult += data.toString();
7382

7483
// If BC output didn't end in a line break or had a continuation backslash
7584
// before it, then more output is still coming.
7685
if(!bcResult.match(/[^\\]\n$/)) return;
7786

78-
++total;
79-
if(total % 1000 == 0) console.log(total);
80-
8187
bcResult = BigFloat.trim(bcResult.replace(/\\\n/g, '').trim().toLowerCase().replace(/^(-?)\./, '$10.'));
8288

83-
if(libResult != bcResult) {
84-
console.log(a.toString(10) + ' * ' + b.toString(10));
85-
console.log(a.toString(16) + ' * ' + b.toString(16));
86-
console.log('BigFloat: ' + a.mul(b).toString(16));
87-
console.log('BigFloat: ' + libResult);
88-
// Remove trailing zeroes.
89+
if(testSpec.libResult != bcResult) {
90+
console.log(testSpec.expr);
91+
console.log('BigFloat: ' + testSpec.libResult);
8992
console.log('bc: ' + bcResult);
9093
console.log('');
9194
}
9295

93-
bcResult = '';
94-
if(total < 10000) testMul();
95-
else bc.stdin.end();
96+
test();
9697
})
9798

98-
bc.stdin.write('scale=1000\n');
99-
//bc.stdin.write('obase=16\n');
100-
//bc.stdin.write('ibase=16\n');
99+
function test() {
100+
++total;
101+
if(total % 1000 == 0) console.log(total);
102+
103+
if(total >= 10000) {
104+
++testNum;
105+
total = 0;
106+
}
101107

102-
function testMul() {
103-
a.setDouble(randDouble());
104-
b.setDouble(randDouble());
108+
if(testList[testNum]) {
109+
testSpec = testList[testNum]();
105110

106-
// bc.stdin.write(a.toString(16).toUpperCase() + ' * ' + b.toString(16).toUpperCase() + '\n');
107-
bc.stdin.write(a.toString(10).toUpperCase() + ' * ' + b.toString(10).toUpperCase() + '\n');
111+
bcResult = '';
112+
bc.stdin.write(testSpec.expr);
113+
} else bc.stdin.end();
108114
}
109115

110-
console.log('Fuzz-testing mul()...');
116+
let testList: Test[] = [
117+
() => {
118+
a.setDouble(randDouble());
119+
b.setDouble(randDouble());
120+
121+
return({
122+
expr: a.toString(10).toUpperCase() + ' * ' + b.toString(10).toUpperCase() + '\n',
123+
libResult: a.mul(b).toString(10)
124+
});
125+
}
126+
]
127+
128+
console.log('Fuzz-testing...');
129+
130+
bc.stdin.write('scale=1000\n');
111131

112-
testMul();
132+
test();

0 commit comments

Comments
 (0)