-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
87 lines (75 loc) · 3.05 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import 'babel-polyfill';
const expect = require('chai').expect;
import { runBooth, runBoothRadix4, runRestoring, runNonRestoring, runRadix4Srt, runRadix2Srt } from './src/scripts/algorithms';
describe('Booth RADIX-4', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let M = randomNumber();
let Q = randomNumber();
let value = runBoothRadix4(M, Q, false);
expect(value, `M=${M}; Q=${Q}`).to.equal(M * Q);
}
});
});
describe('Booth', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let M = randomNumber();
let Q = randomNumber();
let value = runBooth(M, Q, false);
expect(value, `M=${M}; Q=${Q}`).to.equal(M * Q);
}
});
});
describe('Divsion Restoring', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let Q = randomNumber(1, 900, true);
let M = randomNumber(1, 900, true);
let value = runRestoring(Q, M, false, 10);
expect(value.quotient, `Invalid Quotient; Q=${Q};M=${M}`).to.equal(Math.floor(Q / M));
expect(value.remainder, `Invalid Remainder; Q=${Q};M=${M}`).to.equal(Q % M);
}
});
});
describe('Divsion Non-Restoring', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let Q = randomNumber(1, 900, true);
let M = randomNumber(1, 900, true);
let value = runNonRestoring(Q, M, false, 10);
expect(value.quotient, `Invalid Quotient; Q=${Q};M=${M}`).to.equal(Math.floor(Q / M));
expect(value.remainder, `Invalid Remainder; Q=${Q};M=${M}`).to.equal(Q % M);
}
});
});
describe('Divsion RADIX-4 SRT', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let Q = randomNumber(1, 300, true);
let M = randomNumber(1, 300, true);
let value = runRadix4Srt(Q, M, false, 10);
expect(value.quotient, `Invalid Quotient; Q=${Q};M=${M}`).to.equal(Math.floor(Q / M));
expect(value.remainder, `Invalid Remainder; Q=${Q};M=${M}`).to.equal(Q % M);
}
});
});
describe('Divsion RADIX-2 SRT', function () {
it('Calculates the correct answer', function () {
for (let i = 0; i < 10000; i++) {
let Q = randomNumber(1, 100, true);
let M = randomNumber(1, 100, true);
let value = runRadix2Srt(Q, M, false, 8);
expect(value.quotient, `Invalid Quotient; Q=${Q};M=${M}`).to.equal(Math.floor(Q / M));
expect(value.remainder, `Invalid Remainder; Q=${Q};M=${M}`).to.equal(Q % M);
}
});
});
function randomNumber(from = 1, to = 100, positive = false) {
let number = Math.floor(Math.random() * to) + from;
let sign = Math.random() < 0.5 ? -1 : 1;
if (positive) {
sign = 1;
}
return number * sign;
}