forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathutils.test.js
More file actions
587 lines (462 loc) · 19 KB
/
Copy pathmathutils.test.js
File metadata and controls
587 lines (462 loc) · 19 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/**
* @license
* MusicBlocks v3.4.1
* Copyright (C) 2024 omsuneri
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// A basic test file in jest framework for the mathutils.js
const MathUtility = require("../mathutils");
// Mock SOLFEGENAMES for testing doRandom function
const SOLFEGENAMES = ["do", "re", "mi", "fa", "sol", "la", "ti"];
global.SOLFEGENAMES = SOLFEGENAMES;
describe("MathUtility", () => {
describe("doRandom", () => {
test("returns random number between two numbers", () => {
const result = MathUtility.doRandom(1, 10);
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});
test("returns random solfege array between two solfege names", () => {
const result = MathUtility.doRandom("do", "sol", 4);
expect(result).toHaveLength(2);
expect(SOLFEGENAMES).toContain(result[0]);
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
});
test("throws error for invalid inputs", () => {
expect(() => MathUtility.doRandom("invalid", 10)).toThrow("NanError");
});
// Edge case tests
test("handles swapped min/max (n1 > n2)", () => {
const result = MathUtility.doRandom(10, 1);
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(10);
});
test("returns same number when min equals max", () => {
const result = MathUtility.doRandom(5, 5);
expect(result).toBe(5);
});
test("handles negative number range", () => {
const result = MathUtility.doRandom(-10, -1);
expect(result).toBeGreaterThanOrEqual(-10);
expect(result).toBeLessThanOrEqual(-1);
});
test("handles range crossing zero", () => {
const result = MathUtility.doRandom(-5, 5);
expect(result).toBeGreaterThanOrEqual(-5);
expect(result).toBeLessThanOrEqual(5);
});
test("returns solfege with swapped order (n1 > n2 in solfege)", () => {
const result = MathUtility.doRandom("sol", "do", 4);
expect(result).toHaveLength(2);
expect(SOLFEGENAMES).toContain(result[0]);
});
test("returns solfege without octave parameter (default octave)", () => {
const result = MathUtility.doRandom("do", "mi");
expect(result).toHaveLength(2);
expect(SOLFEGENAMES).toContain(result[0]);
expect(Number(result[1])).toBeGreaterThanOrEqual(4);
});
test("throws error when mixing number and string", () => {
expect(() => MathUtility.doRandom(1, "do")).toThrow("NanError");
});
test("throws error for one valid solfege and one invalid", () => {
expect(() => MathUtility.doRandom("do", "invalid")).toThrow("NanError");
});
});
describe("doOneOf", () => {
test("returns either a or b", () => {
const result = MathUtility.doOneOf("a", "b");
expect(["a", "b"]).toContain(result);
});
// Edge case tests
test("works with numbers", () => {
const result = MathUtility.doOneOf(1, 2);
expect([1, 2]).toContain(result);
});
test("works with same values", () => {
const result = MathUtility.doOneOf("same", "same");
expect(result).toBe("same");
});
test("works with null values", () => {
const result = MathUtility.doOneOf(null, "b");
expect([null, "b"]).toContain(result);
});
test("works with undefined values", () => {
const result = MathUtility.doOneOf(undefined, "b");
expect([undefined, "b"]).toContain(result);
});
test("works with objects", () => {
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const result = MathUtility.doOneOf(obj1, obj2);
expect([obj1, obj2]).toContain(result);
});
});
describe("doMod", () => {
test("calculates modulus correctly", () => {
expect(MathUtility.doMod(10, 3)).toBe(1);
});
test("throws error for invalid inputs", () => {
expect(() => MathUtility.doMod("a", 3)).toThrow("NanError");
});
// Edge case tests
test("handles negative dividend", () => {
expect(MathUtility.doMod(-10, 3)).toBe(-1);
});
test("handles negative divisor", () => {
expect(MathUtility.doMod(10, -3)).toBe(1);
});
test("handles both negative", () => {
expect(MathUtility.doMod(-10, -3)).toBe(-1);
});
test("returns 0 when evenly divisible", () => {
expect(MathUtility.doMod(9, 3)).toBe(0);
});
test("handles modulus with zero dividend", () => {
expect(MathUtility.doMod(0, 5)).toBe(0);
});
test("handles floating point numbers", () => {
expect(MathUtility.doMod(5.5, 2)).toBeCloseTo(1.5);
});
test("throws error when second arg is string", () => {
expect(() => MathUtility.doMod(10, "a")).toThrow("NanError");
});
});
describe("doSqrt", () => {
test("calculates square root", () => {
expect(MathUtility.doSqrt(9)).toBe(3);
});
test("throws error for negative input", () => {
expect(() => MathUtility.doSqrt(-1)).toThrow("NoSqrtError");
});
// Edge case tests
test("returns 0 for sqrt of 0", () => {
expect(MathUtility.doSqrt(0)).toBe(0);
});
test("returns 1 for sqrt of 1", () => {
expect(MathUtility.doSqrt(1)).toBe(1);
});
test("handles non-perfect squares", () => {
expect(MathUtility.doSqrt(2)).toBeCloseTo(1.414, 2);
});
test("handles large numbers", () => {
expect(MathUtility.doSqrt(1000000)).toBe(1000);
});
test("handles decimal numbers", () => {
expect(MathUtility.doSqrt(0.25)).toBe(0.5);
});
test("throws NanError for string input", () => {
expect(() => MathUtility.doSqrt("9")).toThrow("NanError");
});
});
describe("doPlus", () => {
test("adds two numbers", () => {
expect(MathUtility.doPlus(2, 3)).toBe(5);
});
test("concatenates strings", () => {
expect(MathUtility.doPlus("a", "b")).toBe("ab");
});
// Edge case tests
test("concatenates number and string", () => {
expect(MathUtility.doPlus(1, "b")).toBe("1b");
});
test("concatenates string and number", () => {
expect(MathUtility.doPlus("a", 2)).toBe("a2");
});
test("handles negative numbers", () => {
expect(MathUtility.doPlus(-2, 3)).toBe(1);
});
test("handles zero", () => {
expect(MathUtility.doPlus(0, 5)).toBe(5);
});
test("handles floating point addition", () => {
expect(MathUtility.doPlus(0.1, 0.2)).toBeCloseTo(0.3);
});
test("concatenates empty strings", () => {
expect(MathUtility.doPlus("", "")).toBe("");
});
test("concatenates with empty string", () => {
expect(MathUtility.doPlus("hello", "")).toBe("hello");
});
});
describe("doMinus", () => {
test("subtracts numbers", () => {
expect(MathUtility.doMinus(5, 3)).toBe(2);
});
test("throws error for string inputs", () => {
expect(() => MathUtility.doMinus("a", 3)).toThrow("NanError");
});
// Edge case tests
test("handles negative result", () => {
expect(MathUtility.doMinus(3, 5)).toBe(-2);
});
test("handles subtracting negative numbers", () => {
expect(MathUtility.doMinus(5, -3)).toBe(8);
});
test("handles both negative numbers", () => {
expect(MathUtility.doMinus(-5, -3)).toBe(-2);
});
test("returns 0 when subtracting same numbers", () => {
expect(MathUtility.doMinus(5, 5)).toBe(0);
});
test("handles floating point subtraction", () => {
expect(MathUtility.doMinus(0.3, 0.1)).toBeCloseTo(0.2);
});
test("throws error when second arg is string", () => {
expect(() => MathUtility.doMinus(5, "3")).toThrow("NanError");
});
});
describe("doMultiply", () => {
test("multiplies numbers", () => {
expect(MathUtility.doMultiply(2, 3)).toBe(6);
});
test("throws error for string inputs", () => {
expect(() => MathUtility.doMultiply("a", 3)).toThrow("NanError");
});
// Edge case tests
test("handles multiplication by zero", () => {
expect(MathUtility.doMultiply(5, 0)).toBe(0);
});
test("handles multiplication by one", () => {
expect(MathUtility.doMultiply(5, 1)).toBe(5);
});
test("handles negative multiplication", () => {
expect(MathUtility.doMultiply(-2, 3)).toBe(-6);
});
test("handles both negative", () => {
expect(MathUtility.doMultiply(-2, -3)).toBe(6);
});
test("handles floating point multiplication", () => {
expect(MathUtility.doMultiply(0.1, 0.2)).toBeCloseTo(0.02);
});
test("throws error when second arg is string", () => {
expect(() => MathUtility.doMultiply(5, "3")).toThrow("NanError");
});
});
describe("doDivide", () => {
test("divides numbers", () => {
expect(MathUtility.doDivide(6, 2)).toBe(3);
});
test("throws error for division by zero", () => {
expect(() => MathUtility.doDivide(6, 0)).toThrow("DivByZeroError");
});
// Edge case tests
test("handles division resulting in decimal", () => {
expect(MathUtility.doDivide(5, 2)).toBe(2.5);
});
test("handles division of zero", () => {
expect(MathUtility.doDivide(0, 5)).toBe(0);
});
test("handles negative dividend", () => {
expect(MathUtility.doDivide(-6, 2)).toBe(-3);
});
test("handles negative divisor", () => {
expect(MathUtility.doDivide(6, -2)).toBe(-3);
});
test("handles both negative", () => {
expect(MathUtility.doDivide(-6, -2)).toBe(3);
});
test("throws NanError for string dividend", () => {
expect(() => MathUtility.doDivide("6", 2)).toThrow("NanError");
});
test("throws NanError for string divisor", () => {
expect(() => MathUtility.doDivide(6, "2")).toThrow("NanError");
});
});
describe("doCalculateDistance", () => {
test("calculates Euclidean distance", () => {
expect(MathUtility.doCalculateDistance(0, 0, 3, 4)).toBe(5);
});
// Edge case tests
test("returns 0 for same point", () => {
expect(MathUtility.doCalculateDistance(5, 5, 5, 5)).toBe(0);
});
test("handles negative coordinates", () => {
expect(MathUtility.doCalculateDistance(-3, -4, 0, 0)).toBe(5);
});
test("handles distance along x-axis only", () => {
expect(MathUtility.doCalculateDistance(0, 0, 5, 0)).toBe(5);
});
test("handles distance along y-axis only", () => {
expect(MathUtility.doCalculateDistance(0, 0, 0, 5)).toBe(5);
});
test("handles floating point coordinates", () => {
expect(MathUtility.doCalculateDistance(0, 0, 1.5, 2)).toBeCloseTo(2.5);
});
test("throws NanError for string x1", () => {
expect(() => MathUtility.doCalculateDistance("0", 0, 3, 4)).toThrow("NanError");
});
test("throws NanError for string y1", () => {
expect(() => MathUtility.doCalculateDistance(0, "0", 3, 4)).toThrow("NanError");
});
test("throws NanError for string x2", () => {
expect(() => MathUtility.doCalculateDistance(0, 0, "3", 4)).toThrow("NanError");
});
test("throws NanError for string y2", () => {
expect(() => MathUtility.doCalculateDistance(0, 0, 3, "4")).toThrow("NanError");
});
});
describe("doPower", () => {
test("calculates power", () => {
expect(MathUtility.doPower(2, 3)).toBe(8);
});
// Edge case tests
test("handles power of 0", () => {
expect(MathUtility.doPower(5, 0)).toBe(1);
});
test("handles power of 1", () => {
expect(MathUtility.doPower(5, 1)).toBe(5);
});
test("handles base of 0", () => {
expect(MathUtility.doPower(0, 5)).toBe(0);
});
test("handles base of 1", () => {
expect(MathUtility.doPower(1, 100)).toBe(1);
});
test("handles negative exponent", () => {
expect(MathUtility.doPower(2, -2)).toBe(0.25);
});
test("handles negative base with odd exponent", () => {
expect(MathUtility.doPower(-2, 3)).toBe(-8);
});
test("handles negative base with even exponent", () => {
expect(MathUtility.doPower(-2, 2)).toBe(4);
});
test("handles fractional exponent", () => {
expect(MathUtility.doPower(4, 0.5)).toBe(2);
});
test("throws NanError for string base", () => {
expect(() => MathUtility.doPower("2", 3)).toThrow("NanError");
});
test("throws NanError for string exponent", () => {
expect(() => MathUtility.doPower(2, "3")).toThrow("NanError");
});
});
describe("doAbs", () => {
test("calculates absolute value", () => {
expect(MathUtility.doAbs(-5)).toBe(5);
});
// Edge case tests
test("returns positive number unchanged", () => {
expect(MathUtility.doAbs(5)).toBe(5);
});
test("returns 0 for 0", () => {
expect(MathUtility.doAbs(0)).toBe(0);
});
test("handles floating point negative", () => {
expect(MathUtility.doAbs(-3.14)).toBeCloseTo(3.14);
});
test("handles very small negative numbers", () => {
expect(MathUtility.doAbs(-0.0001)).toBeCloseTo(0.0001);
});
test("throws NanError for string input", () => {
expect(() => MathUtility.doAbs("-5")).toThrow("NanError");
});
});
describe("doNegate", () => {
test("negates number", () => {
expect(MathUtility.doNegate(5)).toBe(-5);
});
test("reverses string", () => {
expect(MathUtility.doNegate("abc")).toBe("cba");
});
// Edge case tests
test("negates negative number (double negative)", () => {
expect(MathUtility.doNegate(-5)).toBe(5);
});
test("negates zero", () => {
expect(MathUtility.doNegate(0)).toBe(0);
});
test("reverses single character string", () => {
expect(MathUtility.doNegate("a")).toBe("a");
});
test("reverses empty string", () => {
expect(MathUtility.doNegate("")).toBe("");
});
test("reverses palindrome", () => {
expect(MathUtility.doNegate("radar")).toBe("radar");
});
test("reverses string with spaces", () => {
expect(MathUtility.doNegate("hello world")).toBe("dlrow olleh");
});
test("throws NoNegError for array", () => {
expect(() => MathUtility.doNegate([1, 2, 3])).toThrow("NoNegError");
});
test("throws NoNegError for object", () => {
expect(() => MathUtility.doNegate({ a: 1 })).toThrow("NoNegError");
});
test("throws NoNegError for null", () => {
expect(() => MathUtility.doNegate(null)).toThrow("NoNegError");
});
test("throws NoNegError for undefined", () => {
expect(() => MathUtility.doNegate(undefined)).toThrow("NoNegError");
});
});
describe("doInt", () => {
test("rounds to nearest integer", () => {
expect(MathUtility.doInt(4.6)).toBe(5);
});
// Edge case tests
test("rounds down for .4", () => {
expect(MathUtility.doInt(4.4)).toBe(4);
});
test("rounds up for .5", () => {
expect(MathUtility.doInt(4.5)).toBe(5);
});
test("handles already integer value", () => {
expect(MathUtility.doInt(5)).toBe(5);
});
test("handles negative with rounding up (towards zero)", () => {
expect(MathUtility.doInt(-4.4)).toBe(-4);
});
test("handles negative with rounding down (away from zero)", () => {
expect(MathUtility.doInt(-4.6)).toBe(-5);
});
test("handles zero", () => {
expect(MathUtility.doInt(0)).toBe(0);
});
test("handles numeric string (converts to number)", () => {
// Note: doInt doesn't throw for numeric strings, it converts them
expect(MathUtility.doInt("4.6")).toBe(5);
});
test("returns NaN for non-numeric string", () => {
expect(MathUtility.doInt("abc")).toBeNaN();
});
});
describe("edge cases - Infinity, NaN, and boundary values", () => {
test("doMod throws an error when divisor is zero", () => {
expect(() => MathUtility.doMod(5, 0)).toThrow("DivByZeroError");
});
test("doSqrt handles Infinity", () => {
expect(MathUtility.doSqrt(Infinity)).toBe(Infinity);
});
test("doAbs handles negative Infinity", () => {
expect(MathUtility.doAbs(-Infinity)).toBe(Infinity);
});
test("doDivide returns Infinity for Infinity dividend", () => {
expect(MathUtility.doDivide(Infinity, 1)).toBe(Infinity);
});
test("doPower returns Infinity for very large exponent", () => {
expect(MathUtility.doPower(10, 309)).toBe(Infinity);
});
test("doNegate handles floating-point numbers", () => {
expect(MathUtility.doNegate(-3.14)).toBeCloseTo(3.14);
});
test("doPlus coerces booleans via numeric branch", () => {
// booleans are not strings, so Number(true) + Number(true) = 2
expect(MathUtility.doPlus(true, true)).toBe(2);
});
});
});