UNPKG

2.88 kBJavaScriptView Raw
1var rats = require('../')
2 , expect = require('expect.js')
3 , five, negone, one, six, ten, third, thirty, three, two, x, y, zero
4 , inf, origo
5 ;
6
7describe("rats", function() {
8
9 it("should construct a new number object, based on input", function() {
10 expect(rats(1)).to.be.an('object')
11 expect(rats(1).val()).to.be(1)
12 expect(rats.checkInput(rats(1))).to.be(rats(1))
13 })
14
15})
16
17negone = rats(-1)
18zero = rats(0)
19one = rats(1)
20two = rats(2)
21three = rats(3)
22five = rats(5)
23six = rats(6)
24ten = rats(10)
25thirty = rats(30)
26x = rats(3, 10)
27y = rats(3, 14)
28third = rats(1, 3)
29
30
31describe('elementary arithmetic', function() {
32 it('addition', function() {
33 expect(one.plus(two)).to.be(three)
34 expect(one.plus(negone)).to.be(zero)
35 expect(x.plus(y)+'').to.be('18/35')
36 })
37 it('subtraction', function() {
38 expect(one.minus(two)).to.be(negone)
39 expect(one.minus(negone)).to.be(two)
40 expect(x.minus(y)+'').to.be('3/35')
41 expect(two.minus(third)+'').to.be('5/3')
42 })
43 it('multiplication', function() {
44 expect(five.times(six)).to.be(thirty)
45 expect(one.times(negone)).to.be(negone)
46 expect(x.times(y)+'').to.be('9/140')
47 })
48 it('division', function() {
49 expect(five.per(ten)+'').to.be('1/2')
50 expect(one.per(negone)).to.be(negone)
51 expect(x.per(y)+'').to.be('7/5')
52 })
53
54})
55
56inf = rats(1,0)
57origo = rats(0,0)
58
59describe('operations with infinity and the origo → ', function() {
60
61 it('adds to ∞', function(){
62 expect(inf.add(x)).to.be(inf)
63 })
64 it('adds ∞', function(){
65 expect(x.add(inf)).to.be(inf)
66 })
67 it('adds to origo', function(){
68 expect(x.add(origo)).to.be(origo)
69 })
70 it('adds origo', function(){
71 expect(origo.add(x)).to.be(origo)
72 })
73
74 it('subtracts from ∞', function(){
75 expect(inf.sub(x)).to.be(inf)
76 })
77 it('subtracts ∞', function(){
78 expect(x.sub(inf)).to.be(inf)
79 })
80 it('subtracts from origo', function(){
81 expect(x.sub(origo)).to.be(origo)
82 })
83 it('subtracts origo', function(){
84 expect(origo.sub(x)).to.be(origo)
85 })
86
87 it('multiplies with ∞', function(){
88 expect(inf.mul(x)).to.be(inf)
89 })
90 it('multiplies ∞', function(){
91 expect(x.mul(inf)).to.be(inf)
92 })
93 it('multiplies with origo', function(){
94 expect(x.mul(origo)).to.be(origo)
95 })
96 it('multiplies origo', function(){
97 expect(origo.mul(x)).to.be(origo)
98 })
99
100 it('divides with ∞', function(){
101 expect(inf.per(x)).to.be(inf)
102 })
103 it('divides ∞', function(){
104 expect(x.div(inf)).to.be(zero)
105 })
106 it('divides with origo', function(){
107 expect(x.div(origo)).to.be(origo)
108 })
109 it('divides origo', function(){
110 expect(origo.div(x)).to.be(origo)
111 })
112
113})