-
Notifications
You must be signed in to change notification settings - Fork 0
/
fraction.test.ts
121 lines (119 loc) · 4.93 KB
/
fraction.test.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Fraction } from '../src'
import JSBI from 'jsbi'
describe.only('Fraction', () => {
describe('#quotient', () => {
it('floor division', () => {
expect(new Fraction(JSBI.BigInt(8), JSBI.BigInt(3)).quotient).toEqual(JSBI.BigInt(2)) // one below
expect(new Fraction(JSBI.BigInt(12), JSBI.BigInt(4)).quotient).toEqual(JSBI.BigInt(3)) // exact
expect(new Fraction(JSBI.BigInt(16), JSBI.BigInt(5)).quotient).toEqual(JSBI.BigInt(3)) // one above
})
})
describe('#remainder', () => {
it('returns fraction after divison', () => {
expect(new Fraction(JSBI.BigInt(8), JSBI.BigInt(3)).remainder).toEqual(
new Fraction(JSBI.BigInt(2), JSBI.BigInt(3))
)
expect(new Fraction(JSBI.BigInt(12), JSBI.BigInt(4)).remainder).toEqual(
new Fraction(JSBI.BigInt(0), JSBI.BigInt(4))
)
expect(new Fraction(JSBI.BigInt(16), JSBI.BigInt(5)).remainder).toEqual(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(5))
)
})
})
describe('#invert', () => {
it('flips num and denom', () => {
expect(new Fraction(JSBI.BigInt(5), JSBI.BigInt(10)).invert().numerator).toEqual(JSBI.BigInt(10))
expect(new Fraction(JSBI.BigInt(5), JSBI.BigInt(10)).invert().denominator).toEqual(JSBI.BigInt(5))
})
})
describe('#add', () => {
it('multiples denoms and adds nums', () => {
expect(new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).add(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))).toEqual(
new Fraction(JSBI.BigInt(52), JSBI.BigInt(120))
)
})
it('same denom', () => {
expect(new Fraction(JSBI.BigInt(1), JSBI.BigInt(5)).add(new Fraction(JSBI.BigInt(2), JSBI.BigInt(5)))).toEqual(
new Fraction(JSBI.BigInt(3), JSBI.BigInt(5))
)
})
})
describe('#subtract', () => {
it('multiples denoms and subtracts nums', () => {
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).subtract(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(-28), JSBI.BigInt(120)))
})
it('same denom', () => {
expect(
new Fraction(JSBI.BigInt(3), JSBI.BigInt(5)).subtract(new Fraction(JSBI.BigInt(2), JSBI.BigInt(5)))
).toEqual(new Fraction(JSBI.BigInt(1), JSBI.BigInt(5)))
})
})
describe('#lessThan', () => {
it('correct', () => {
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).lessThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toBe(true)
expect(new Fraction(JSBI.BigInt(1), JSBI.BigInt(3)).lessThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))).toBe(
false
)
expect(
new Fraction(JSBI.BigInt(5), JSBI.BigInt(12)).lessThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toBe(false)
})
})
describe('#equalTo', () => {
it('correct', () => {
expect(new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).equalTo(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))).toBe(
false
)
expect(new Fraction(JSBI.BigInt(1), JSBI.BigInt(3)).equalTo(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))).toBe(
true
)
expect(new Fraction(JSBI.BigInt(5), JSBI.BigInt(12)).equalTo(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))).toBe(
false
)
})
})
describe('#greaterThan', () => {
it('correct', () => {
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).greaterThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toBe(false)
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(3)).greaterThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toBe(false)
expect(
new Fraction(JSBI.BigInt(5), JSBI.BigInt(12)).greaterThan(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toBe(true)
})
})
describe('#multiplty', () => {
it('correct', () => {
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).multiply(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(4), JSBI.BigInt(120)))
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(3)).multiply(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(4), JSBI.BigInt(36)))
expect(
new Fraction(JSBI.BigInt(5), JSBI.BigInt(12)).multiply(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(20), JSBI.BigInt(144)))
})
})
describe('#divide', () => {
it('correct', () => {
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(10)).divide(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(12), JSBI.BigInt(40)))
expect(
new Fraction(JSBI.BigInt(1), JSBI.BigInt(3)).divide(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(12), JSBI.BigInt(12)))
expect(
new Fraction(JSBI.BigInt(5), JSBI.BigInt(12)).divide(new Fraction(JSBI.BigInt(4), JSBI.BigInt(12)))
).toEqual(new Fraction(JSBI.BigInt(60), JSBI.BigInt(48)))
})
})
})