-
Notifications
You must be signed in to change notification settings - Fork 21
/
arithmetic.js
256 lines (252 loc) · 9.01 KB
/
arithmetic.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
describe('Arithmetic', function() {
describe('Validation', function() {
it('rejects missing operation', function(done) {
request.get('/arithmetic?operand1=21&operand2=21')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Unspecified operation" });
done();
});
});
it('rejects invalid operation', function(done) {
request.get('/arithmetic?operation=foobar&operand1=21&operand2=21')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Invalid operation: foobar" });
done();
});
});
it('rejects missing operand1', function(done) {
request.get('/arithmetic?operation=add&operand2=21')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Invalid operand1: undefined" });
done();
});
});
it('rejects missing operand2', function(done) {
request.get('/arithmetic?operation=add&operand1=21')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Invalid operand2: undefined" });
done();
});
});
it('rejects operands with invalid sign', function(done) {
request.get('/arithmetic?operation=add&operand1=4.2-1&operand2=4')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Invalid operand1: 4.2-1" });
done();
});
});
it('rejects operands with invalid decimals', function(done) {
request.get('/arithmetic?operation=add&operand1=4.2.1&operand2=4')
.expect(400)
.end(function(err, res) {
expect(res.body).to.eql({ error: "Invalid operand1: 4.2.1" });
done();
});
});
});
describe('Addition', function() {
it('adds two positive integers', function(done) {
request.get('/arithmetic?operation=add&operand1=21&operand2=21')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
it('adds zero to an integer', function(done) {
request.get('/arithmetic?operation=add&operand1=42&operand2=0')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
it('adds a negative integer to a positive integer', function(done) {
request.get('/arithmetic?operation=add&operand1=21&operand2=-42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -21 });
done();
});
});
it('adds two negative integers', function(done) {
request.get('/arithmetic?operation=add&operand1=-21&operand2=-21')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -42 });
done();
});
});
it('adds an integer to a floating point number', function(done) {
request.get('/arithmetic?operation=add&operand1=2.5&operand2=-5')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -2.5 });
done();
});
});
it('adds with negative exponent', function(done) {
request.get('/arithmetic?operation=add&operand1=1.2e-5&operand2=-1.2e-5')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
});
describe('Subtraction', function() {
it('subtracts two positive integers', function(done) {
request.get('/arithmetic?operation=subtract&operand1=42&operand2=21')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 21 });
done();
});
});
it('subtracts an integer from itself', function(done) {
request.get('/arithmetic?operation=subtract&operand1=42&operand2=42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
it('subtracts a larger integer from another', function(done) {
request.get('/arithmetic?operation=subtract&operand1=21&operand2=42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -21 });
done();
});
});
it('subtracts a negative integer from a positive integer', function(done) {
request.get('/arithmetic?operation=subtract&operand1=21&operand2=-21')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
it('subtracts an integer from a floating point number', function(done) {
request.get('/arithmetic?operation=add&operand1=-2.5&operand2=5')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 2.5 });
done();
});
});
});
describe('Multiplication', function() {
it('multiplies two positive integers', function(done) {
request.get('/arithmetic?operation=multiply&operand1=21&operand2=2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
it('multiplies a positive integer with zero', function(done) {
request.get('/arithmetic?operation=multiply&operand1=21&operand2=0')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
it('multiplies a positive integer and negative integer', function(done) {
request.get('/arithmetic?operation=multiply&operand1=21&operand2=-2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -42 });
done();
});
});
it('multiplies two negative integers', function(done) {
request.get('/arithmetic?operation=multiply&operand1=-21&operand2=-2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
it('multiplies two floating point numbers', function(done) {
request.get('/arithmetic?operation=multiply&operand1=.5&operand2=0.5')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0.25 });
done();
});
});
it('multiplies supporting exponential notation', function(done) {
request.get('/arithmetic?operation=multiply&operand1=4.2e1&operand2=1e0')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 42 });
done();
});
});
});
describe('Division', function() {
it('divides a positive integer by an integer factor ', function(done) {
request.get('/arithmetic?operation=divide&operand1=42&operand2=2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 21 });
done();
});
});
it('divides a negative integer by an integer factor ', function(done) {
request.get('/arithmetic?operation=divide&operand1=-42&operand2=2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -21 });
done();
});
});
it('divides a positive integer by a non-factor', function(done) {
request.get('/arithmetic?operation=divide&operand1=21&operand2=42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0.5 });
done();
});
});
it('divides a positive integer by a negative integer', function(done) {
request.get('/arithmetic?operation=divide&operand1=21&operand2=-42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: -0.5 });
done();
});
});
it('divides zero by a positive integer', function(done) {
request.get('/arithmetic?operation=divide&operand1=0&operand2=42')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0 });
done();
});
});
it('divides by zero', function(done) {
request.get('/arithmetic?operation=divide&operand1=0.5&operand2=2')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: 0.25 });
done();
});
});
it('divides by zero', function(done) {
request.get('/arithmetic?operation=divide&operand1=21&operand2=0')
.expect(200)
.end(function(err, res) {
expect(res.body).to.eql({ result: null });
done();
});
});
});
});