source: node_modules/fraction.js/tests/fraction.test.js@ 2058e5c

Last change on this file since 2058e5c was 2058e5c, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Working / before login

  • Property mode set to 100644
File size: 31.8 KB
Line 
1const Fraction = require('fraction.js');
2const assert = require('assert');
3
4var DivisionByZero = function () { return new Error("Division by Zero"); };
5var InvalidParameter = function () { return new Error("Invalid argument"); };
6var NonIntegerParameter = function () { return new Error("Parameters must be integer"); };
7
8var tests = [{
9 set: "",
10 expectError: InvalidParameter()
11}, {
12 set: "foo",
13 expectError: InvalidParameter()
14}, {
15 set: " 123",
16 expectError: InvalidParameter()
17}, {
18 set: 0,
19 expect: 0
20}, {
21 set: .2,
22 expect: "0.2"
23}, {
24 set: .333,
25 expect: "0.333"
26}, {
27 set: 1.1,
28 expect: "1.1"
29}, {
30 set: 1.2,
31 expect: "1.2"
32}, {
33 set: 1.3,
34 expect: "1.3"
35}, {
36 set: 1.4,
37 expect: "1.4"
38}, {
39 set: 1.5,
40 expect: "1.5"
41}, {
42 set: 2.555,
43 expect: "2.555"
44}, {
45 set: 1e12,
46 expect: "1000000000000"
47}, {
48 set: " - ",
49 expectError: InvalidParameter()
50}, {
51 set: ".5",
52 expect: "0.5"
53}, {
54 set: "2_000_000",
55 expect: "2000000"
56}, {
57 set: "-.5",
58 expect: "-0.5"
59}, {
60 set: "123",
61 expect: "123"
62}, {
63 set: "-123",
64 expect: "-123"
65}, {
66 set: "123.4",
67 expect: "123.4"
68}, {
69 set: "-123.4",
70 expect: "-123.4"
71}, {
72 set: "123.",
73 expect: "123"
74}, {
75 set: "-123.",
76 expect: "-123"
77}, {
78 set: "123.4(56)",
79 expect: "123.4(56)"
80}, {
81 set: "-123.4(56)",
82 expect: "-123.4(56)"
83}, {
84 set: "123.(4)",
85 expect: "123.(4)"
86}, {
87 set: "-123.(4)",
88 expect: "-123.(4)"
89}, {
90 set: "0/0",
91 expectError: DivisionByZero()
92}, {
93 set: "9/0",
94 expectError: DivisionByZero()
95}, {
96 label: "0/1+0/1",
97 set: "0/1",
98 param: "0/1",
99 expect: "0"
100}, {
101 label: "1/9+0/1",
102 set: "1/9",
103 param: "0/1",
104 expect: "0.(1)"
105}, {
106 set: "123/456",
107 expect: "0.269(736842105263157894)"
108}, {
109 set: "-123/456",
110 expect: "-0.269(736842105263157894)"
111}, {
112 set: "19 123/456",
113 expect: "19.269(736842105263157894)"
114}, {
115 set: "-19 123/456",
116 expect: "-19.269(736842105263157894)"
117}, {
118 set: "123.(22)123",
119 expectError: InvalidParameter()
120}, {
121 set: "+33.3(3)",
122 expect: "33.(3)"
123}, {
124 set: "3.'09009'",
125 expect: "3.(09009)"
126}, {
127 set: "123.(((",
128 expectError: InvalidParameter()
129}, {
130 set: "123.((",
131 expectError: InvalidParameter()
132}, {
133 set: "123.()",
134 expectError: InvalidParameter()
135}, {
136 set: null,
137 expect: "0" // I would say it's just fine
138}, {
139 set: [22, 7],
140 expect: '3.(142857)' // We got Pi! - almost ;o
141}, {
142 set: "355/113",
143 expect: "3.(1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168)" // Yay, a better PI
144}, {
145 set: "3 1/7",
146 expect: '3.(142857)'
147}, {
148 set: [36, -36],
149 expect: "-1"
150}, {
151 set: [1n, 3n],
152 expect: "0.(3)"
153}, {
154 set: 1n,
155 set2: 3n,
156 expect: "0.(3)"
157}, {
158 set: { n: 1n, d: 3n },
159 expect: "0.(3)"
160}, {
161 set: { n: 1n, d: 3n },
162 expect: "0.(3)"
163}, {
164 set: [1n, 3n],
165 expect: "0.(3)"
166}, {
167 set: "9/12",
168 expect: "0.75"
169}, {
170 set: "0.09(33)",
171 expect: "0.09(3)"
172}, {
173 set: 1 / 2,
174 expect: "0.5"
175}, {
176 set: 1 / 3,
177 expect: "0.(3)"
178}, {
179 set: "0.'3'",
180 expect: "0.(3)"
181}, {
182 set: "0.00002",
183 expect: "0.00002"
184}, {
185 set: 7 / 8,
186 expect: "0.875"
187}, {
188 set: 0.003,
189 expect: "0.003"
190}, {
191 set: 4,
192 expect: "4"
193}, {
194 set: -99,
195 expect: "-99"
196}, {
197 set: "-92332.1192",
198 expect: "-92332.1192"
199}, {
200 set: '88.92933(12111)',
201 expect: "88.92933(12111)"
202}, {
203 set: '-192322.823(123)',
204 expect: "-192322.8(231)"
205}, {
206 label: "-99.12 % 0.09(34)",
207 set: '-99.12',
208 fn: "mod",
209 param: "0.09(34)",
210 expect: "-0.07(95)"
211}, {
212 label: "0.4 / 0.1",
213 set: .4,
214 fn: "div",
215 param: ".1",
216 expect: "4"
217}, {
218 label: "1 / -.1",
219 set: 1,
220 fn: "div",
221 param: "-.1",
222 expect: "-10"
223}, {
224 label: "1 - (-1)",
225 set: 1,
226 fn: "sub",
227 param: "-1",
228 expect: "2"
229}, {
230 label: "1 + (-1)",
231 set: 1,
232 fn: "add",
233 param: "-1",
234 expect: "0"
235}, {
236 label: "-187 % 12",
237 set: '-187',
238 fn: "mod",
239 param: "12",
240 expect: "-7"
241}, {
242 label: "Negate by 99 * -1",
243 set: '99',
244 fn: "mul",
245 param: "-1",
246 expect: "-99"
247}, {
248 label: "0.5050000000000000000000000",
249 set: "0.5050000000000000000000000",
250 expect: "101/200",
251 fn: "toFraction",
252 param: true
253}, {
254 label: "0.505000000(0000000000)",
255 set: "0.505000000(0000000000)",
256 expect: "101/200",
257 fn: "toFraction",
258 param: true
259}, {
260 set: [20, -5],
261 expect: "-4",
262 fn: "toFraction",
263 param: true
264}, {
265 set: [-10, -7],
266 expect: "1 3/7",
267 fn: "toFraction",
268 param: true
269}, {
270 set: [21, -6],
271 expect: "-3 1/2",
272 fn: "toFraction",
273 param: true
274}, {
275 set: "10/78",
276 expect: "5/39",
277 fn: "toFraction",
278 param: true
279}, {
280 set: "0/91",
281 expect: "0",
282 fn: "toFraction",
283 param: true
284}, {
285 set: "-0/287",
286 expect: "0",
287 fn: "toFraction",
288 param: true
289}, {
290 set: "-5/20",
291 expect: "-1/4",
292 fn: "toFraction",
293 param: true
294}, {
295 set: "42/9",
296 expect: "4 2/3",
297 fn: "toFraction",
298 param: true
299}, {
300 set: "71/23",
301 expect: "3 2/23",
302 fn: "toFraction",
303 param: true
304}, {
305 set: "6/3",
306 expect: "2",
307 fn: "toFraction",
308 param: true
309}, {
310 set: "28/4",
311 expect: "7",
312 fn: "toFraction",
313 param: true
314}, {
315 set: "105/35",
316 expect: "3",
317 fn: "toFraction",
318 param: true
319}, {
320 set: "4/6",
321 expect: "2/3",
322 fn: "toFraction",
323 param: true
324}, {
325 label: "99.(9) + 66",
326 set: '99.(999999)',
327 fn: "add",
328 param: "66",
329 expect: "166"
330}, {
331 label: "123.32 / 33.'9821'",
332 set: '123.32',
333 fn: "div",
334 param: "33.'9821'",
335 expect: "3.628958880242975"
336}, {
337 label: "-82.124 / 66.(3)",
338 set: '-82.124',
339 fn: "div",
340 param: "66.(3)",
341 expect: "-1.238(050251256281407035175879396984924623115577889447236180904522613065326633165829145728643216080402010)"
342}, {
343 label: "100 - .91",
344 set: '100',
345 fn: "sub",
346 param: ".91",
347 expect: "99.09"
348}, {
349 label: "381.(33411) % 11.119(356)",
350 set: '381.(33411)',
351 fn: "mod",
352 param: "11.119(356)",
353 expect: "3.275(997225017295217)"
354}, {
355 label: "13/26 mod 1",
356 set: '13/26',
357 fn: "mod",
358 param: "1.000",
359 expect: "0.5"
360}, {
361 label: "381.(33411) % 1", // Extract fraction part of a number
362 set: '381.(33411)',
363 fn: "mod",
364 param: "1",
365 expect: "0.(33411)"
366}, {
367 label: "-222/3",
368 set: {
369 n: 3,
370 d: 222,
371 s: -1
372 },
373 fn: "inverse",
374 param: null,
375 expect: "-74"
376}, {
377 label: "inverse",
378 set: 1 / 2,
379 fn: "inverse",
380 param: null,
381 expect: "2"
382}, {
383 label: "abs(-222/3)",
384 set: {
385 n: -222,
386 d: 3
387 },
388 fn: "abs",
389 param: null,
390 expect: "74"
391}, {
392 label: "9 % -2",
393 set: 9,
394 fn: "mod",
395 param: "-2",
396 expect: "1"
397}, {
398 label: "-9 % 2",
399 set: '-9',
400 fn: "mod",
401 param: "-2",
402 expect: "-1"
403}, {
404 label: "1 / 195312500",
405 set: '1',
406 fn: "div",
407 param: "195312500",
408 expect: "0.00000000512"
409}, {
410 label: "10 / 0",
411 set: 10,
412 fn: "div",
413 param: 0,
414 expectError: DivisionByZero()
415}, {
416 label: "-3 / 4",
417 set: [-3, 4],
418 fn: "inverse",
419 param: null,
420 expect: "-1.(3)"
421}, {
422 label: "-19.6",
423 set: [-98, 5],
424 fn: "equals",
425 param: '-19.6',
426 expect: "true" // actually, we get a real bool but we call toString() in the test below
427}, {
428 label: "-19.6",
429 set: [98, -5],
430 fn: "equals",
431 param: '-19.6',
432 expect: "true"
433}, {
434 label: "99/88",
435 set: [99, 88],
436 fn: "equals",
437 param: [88, 99],
438 expect: "false"
439}, {
440 label: "99/88",
441 set: [99, -88],
442 fn: "equals",
443 param: [9, 8],
444 expect: "false"
445}, {
446 label: "12.5",
447 set: 12.5,
448 fn: "add",
449 param: 0,
450 expect: "12.5"
451}, {
452 label: "0/1 -> 1/0",
453 set: 0,
454 fn: "inverse",
455 param: null,
456 expectError: DivisionByZero()
457}, {
458 label: "abs(-100.25)",
459 set: -100.25,
460 fn: "abs",
461 param: null,
462 expect: "100.25"
463}, {
464 label: "0.022222222",
465 set: '0.0(22222222)',
466 fn: "abs",
467 param: null,
468 expect: "0.0(2)"
469}, {
470 label: "1.5 | 100.5",
471 set: 100.5,
472 fn: "divisible",
473 param: '1.5',
474 expect: "true"
475}, {
476 label: "1.5 | 100.6",
477 set: 100.6,
478 fn: "divisible",
479 param: 1.6,
480 expect: "false"
481}, {
482 label: "(1/6) | (2/3)", // == 4
483 set: [2, 3],
484 fn: "divisible",
485 param: [1, 6],
486 expect: "true"
487}, {
488 label: "(1/6) | (2/5)",
489 set: [2, 5],
490 fn: "divisible",
491 param: [1, 6],
492 expect: "false"
493}, {
494 label: "0 | (2/5)",
495 set: [2, 5],
496 fn: "divisible",
497 param: 0,
498 expect: "false"
499}, {
500 label: "6 | 0",
501 set: 0,
502 fn: "divisible",
503 param: 6,
504 expect: "true"
505}, {
506 label: "fmod(4.55, 0.05)", // http://phpjs.org/functions/fmod/ (comment section)
507 set: 4.55,
508 fn: "mod",
509 param: 0.05,
510 expect: "0"
511}, {
512 label: "fmod(99.12, 0.4)",
513 set: 99.12,
514 fn: "mod",
515 param: "0.4",
516 expect: "0.32"
517}, {
518 label: "fmod(fmod(1.0,0.1))", // http://stackoverflow.com/questions/4218961/why-fmod1-0-0-1-1
519 set: 1.0,
520 fn: "mod",
521 param: 0.1,
522 expect: "0"
523}, {
524 label: "bignum",
525 set: [5385020324, 1673196525],
526 fn: "add",
527 param: 0,
528 expect: "3.21(840276592733181776121606516006839065124164060763872313206005492988936251824931324190982287630557922656455433410609073551596098372245902196097377144624418820138297860736950789447760776337973807350574075570710380240599651018280712721418065340531352107607323652551812465663589637206543923464101146157950573080469432602963360804254598843372567965379918536467197121390148715495330113717514444395585868193217769203770011415724163065662594535928766646225254382476081224230369471990147720394052336440275597631903998844367669243157195775313960803259497565595290726533154854597848271290188102679689703515252041298615534717298077104242133182771222884293284077911887845930112722413166618308629346454087334421161315763550250022184333666363549254920906556389124702491239037207539024741878423396797336762338781453063321417070239253574830368476888869943116813489676593728283053898883754853602746993512910863832926021645903191198654921901657666901979730085800889408373591978384009612977172541043856160291750546158945674358246709841810124486123947693472528578195558946669459524487119048971249805817042322628538808374587079661786890216019304725725509141850506771761314768448972244907094819599867385572056456428511886850828834945135927771544947477105237234460548500123140047759781236696030073335228807028510891749551057667897081007863078128255137273847732859712937785356684266362554153643129279150277938809369688357439064129062782986595074359241811119587401724970711375341877428295519559485099934689381452068220139292962014728066686607540019843156200674036183526020650801913421377683054893985032630879985)"
529}, {
530 label: "ceil(0.4)",
531 set: 0.4,
532 fn: "ceil",
533 param: null,
534 expect: "1"
535},
536
537
538{
539 label: "1 < 2",
540 set: 1,
541 fn: "lt",
542 param: 2,
543 expect: "true"
544}, {
545 label: "2 < 2",
546 set: 2,
547 fn: "lt",
548 param: 2,
549 expect: "false"
550}, {
551 label: "3 > 2",
552 set: 3,
553 fn: "gt",
554 param: 2,
555 expect: "true"
556}, {
557 label: "2 > 2",
558 set: 2,
559 fn: "gt",
560 param: 2,
561 expect: "false"
562}, {
563 label: "1 <= 2",
564 set: 1,
565 fn: "lte",
566 param: 2,
567 expect: "true"
568}, {
569 label: "2 <= 2",
570 set: 2,
571 fn: "lte",
572 param: 2,
573 expect: "true"
574}, {
575 label: "3 <= 2",
576 set: 3,
577 fn: "lte",
578 param: 2,
579 expect: "false"
580}, {
581 label: "3 >= 2",
582 set: 3,
583 fn: "gte",
584 param: 2,
585 expect: "true"
586}, {
587 label: "2 >= 2",
588 set: 2,
589 fn: "gte",
590 param: 2,
591 expect: "true"
592}, {
593 label: "ceil(0.5)",
594 set: 0.5,
595 fn: "ceil",
596 param: null,
597 expect: "1"
598}, {
599 label: "ceil(0.23, 2)",
600 set: 0.23,
601 fn: "ceil",
602 param: 2,
603 expect: "0.23"
604}, {
605 label: "ceil(0.6)",
606 set: 0.6,
607 fn: "ceil",
608 param: null,
609 expect: "1"
610}, {
611 label: "ceil(-0.4)",
612 set: -0.4,
613 fn: "ceil",
614 param: null,
615 expect: "0"
616}, {
617 label: "ceil(-0.5)",
618 set: -0.5,
619 fn: "ceil",
620 param: null,
621 expect: "0"
622}, {
623 label: "ceil(-0.6)",
624 set: -0.6,
625 fn: "ceil",
626 param: null,
627 expect: "0"
628}, {
629 label: "floor(0.4)",
630 set: 0.4,
631 fn: "floor",
632 param: null,
633 expect: "0"
634}, {
635 label: "floor(0.4, 1)",
636 set: 0.4,
637 fn: "floor",
638 param: 1,
639 expect: "0.4"
640}, {
641 label: "floor(0.5)",
642 set: 0.5,
643 fn: "floor",
644 param: null,
645 expect: "0"
646}, {
647 label: "floor(0.6)",
648 set: 0.6,
649 fn: "floor",
650 param: null,
651 expect: "0"
652}, {
653 label: "floor(-0.4)",
654 set: -0.4,
655 fn: "floor",
656 param: null,
657 expect: "-1"
658}, {
659 label: "floor(-0.5)",
660 set: -0.5,
661 fn: "floor",
662 param: null,
663 expect: "-1"
664}, {
665 label: "floor(-0.6)",
666 set: -0.6,
667 fn: "floor",
668 param: null,
669 expect: "-1"
670}, {
671 label: "floor(10.4)",
672 set: 10.4,
673 fn: "floor",
674 param: null,
675 expect: "10"
676}, {
677 label: "floor(10.4, 1)",
678 set: 10.4,
679 fn: "floor",
680 param: 1,
681 expect: "10.4"
682}, {
683 label: "floor(10.5)",
684 set: 10.5,
685 fn: "floor",
686 param: null,
687 expect: "10"
688}, {
689 label: "floor(10.6)",
690 set: 10.6,
691 fn: "floor",
692 param: null,
693 expect: "10"
694}, {
695 label: "floor(-10.4)",
696 set: -10.4,
697 fn: "floor",
698 param: null,
699 expect: "-11"
700}, {
701 label: "floor(-10.5)",
702 set: -10.5,
703 fn: "floor",
704 param: null,
705 expect: "-11"
706}, {
707 label: "floor(-10.6)",
708 set: -10.6,
709 fn: "floor",
710 param: null,
711 expect: "-11"
712}, {
713 label: "floor(-10.543,3)",
714 set: -10.543,
715 fn: "floor",
716 param: 3,
717 expect: "-10.543"
718}, {
719 label: "floor(10.543,3)",
720 set: 10.543,
721 fn: "floor",
722 param: 3,
723 expect: "10.543"
724}, {
725 label: "round(-10.543,3)",
726 set: -10.543,
727 fn: "round",
728 param: 3,
729 expect: "-10.543"
730}, {
731 label: "round(10.543,3)",
732 set: 10.543,
733 fn: "round",
734 param: 3,
735 expect: "10.543"
736}, {
737 label: "round(10.4)",
738 set: 10.4,
739 fn: "round",
740 param: null,
741 expect: "10"
742}, {
743 label: "round(10.5)",
744 set: 10.5,
745 fn: "round",
746 param: null,
747 expect: "11"
748}, {
749 label: "round(10.5, 1)",
750 set: 10.5,
751 fn: "round",
752 param: 1,
753 expect: "10.5"
754}, {
755 label: "round(10.6)",
756 set: 10.6,
757 fn: "round",
758 param: null,
759 expect: "11"
760}, {
761 label: "round(-10.4)",
762 set: -10.4,
763 fn: "round",
764 param: null,
765 expect: "-10"
766}, {
767 label: "round(-10.5)",
768 set: -10.5,
769 fn: "round",
770 param: null,
771 expect: "-10"
772}, {
773 label: "round(-10.6)",
774 set: -10.6,
775 fn: "round",
776 param: null,
777 expect: "-11"
778}, {
779 label: "round(-0.4)",
780 set: -0.4,
781 fn: "round",
782 param: null,
783 expect: "0"
784}, {
785 label: "round(-0.5)",
786 set: -0.5,
787 fn: "round",
788 param: null,
789 expect: "0"
790}, {
791 label: "round(-0.6)",
792 set: -0.6,
793 fn: "round",
794 param: null,
795 expect: "-1"
796}, {
797 label: "round(-0)",
798 set: -0,
799 fn: "round",
800 param: null,
801 expect: "0"
802}, {
803 label: "round(big fraction)",
804 set: [
805 '409652136432929109317120'.repeat(100),
806 '63723676445298091081155'.repeat(100)
807 ],
808 fn: "round",
809 param: null,
810 expect: "6428570341270001560623330590225448467479093479780591305451264291405695842465355472558570608574213642"
811}, {
812 label: "round(big numerator)",
813 set: ['409652136432929109317'.repeat(100), 10],
814 fn: "round",
815 param: null,
816 expect: '409652136432929109317'.repeat(99) + '40965213643292910932'
817}, {
818 label: "17402216385200408/5539306332998545",
819 set: [17402216385200408, 5539306332998545],
820 fn: "add",
821 param: 0,
822 expect: "3.141587653589870"
823}, {
824 label: "17402216385200401/553930633299855",
825 set: [17402216385200401, 553930633299855],
826 fn: "add",
827 param: 0,
828 expect: "31.415876535898660"
829}, {
830 label: "1283191/418183",
831 set: [1283191, 418183],
832 fn: "add",
833 param: 0,
834 expect: "3.068491545567371"
835}, {
836 label: "1.001",
837 set: "1.001",
838 fn: "add",
839 param: 0,
840 expect: "1.001"
841}, {
842 label: "99+1",
843 set: [99, 1],
844 fn: "add",
845 param: 1,
846 expect: "100"
847}, {
848 label: "gcd(5/8, 3/7)",
849 set: [5, 8],
850 fn: "gcd",
851 param: [3, 7],
852 expect: "0.017(857142)" // == 1/56
853}, {
854 label: "gcd(52, 39)",
855 set: 52,
856 fn: "gcd",
857 param: 39,
858 expect: "13"
859}, {
860 label: "gcd(51357, 3819)",
861 set: 51357,
862 fn: "gcd",
863 param: 3819,
864 expect: "57"
865}, {
866 label: "gcd(841, 299)",
867 set: 841,
868 fn: "gcd",
869 param: 299,
870 expect: "1"
871}, {
872 label: "gcd(2/3, 7/5)",
873 set: [2, 3],
874 fn: "gcd",
875 param: [7, 5],
876 expect: "0.0(6)" // == 1/15
877}, {
878 label: "lcm(-3, 3)",
879 set: -3,
880 fn: "lcm",
881 param: 3,
882 expect: "3"
883}, {
884 label: "lcm(3,-3)",
885 set: 3,
886 fn: "lcm",
887 param: -3,
888 expect: "3"
889}, {
890 label: "lcm(0,3)",
891 set: 0,
892 fn: "lcm",
893 param: 3,
894 expect: "0"
895}, {
896 label: "lcm(3, 0)",
897 set: 3,
898 fn: "lcm",
899 param: 0,
900 expect: "0"
901}, {
902 label: "lcm(0, 0)",
903 set: 0,
904 fn: "lcm",
905 param: 0,
906 expect: "0"
907}, {
908 label: "lcm(200, 333)",
909 set: 200,
910 fn: "lcm",
911 param: 333, expect: "66600"
912},
913{
914 label: "1 + -1",
915 set: 1,
916 fn: "add",
917 param: -1,
918 expect: "0"
919}, {
920 label: "3/10+3/14",
921 set: "3/10",
922 fn: "add",
923 param: "3/14",
924 expect: "0.5(142857)"
925}, {
926 label: "3/10-3/14",
927 set: "3/10",
928 fn: "sub",
929 param: "3/14",
930 expect: "0.0(857142)"
931}, {
932 label: "3/10*3/14",
933 set: "3/10",
934 fn: "mul",
935 param: "3/14",
936 expect: "0.06(428571)"
937}, {
938 label: "3/10 / 3/14",
939 set: "3/10",
940 fn: "div",
941 param: "3/14",
942 expect: "1.4"
943}, {
944 label: "1-2",
945 set: "1",
946 fn: "sub",
947 param: "2",
948 expect: "-1"
949}, {
950 label: "1--1",
951 set: "1",
952 fn: "sub",
953 param: "-1",
954 expect: "2"
955}, {
956 label: "0/1*1/3",
957 set: "0/1",
958 fn: "mul",
959 param: "1/3",
960 expect: "0"
961}, {
962 label: "3/10 * 8/12",
963 set: "3/10",
964 fn: "mul",
965 param: "8/12",
966 expect: "0.2"
967}, {
968 label: ".5+5",
969 set: ".5",
970 fn: "add",
971 param: 5, expect: "5.5"
972},
973{
974 label: "10/12-5/60",
975 set: "10/12",
976 fn: "sub",
977 param: "5/60",
978 expect: "0.75"
979}, {
980 label: "10/15 / 3/4",
981 set: "10/15",
982 fn: "div",
983 param: "3/4",
984 expect: "0.(8)"
985}, {
986 label: "1/4 + 3/8",
987 set: "1/4",
988 fn: "add",
989 param: "3/8",
990 expect: "0.625"
991}, {
992 label: "2-1/3",
993 set: "2",
994 fn: "sub",
995 param: "1/3",
996 expect: "1.(6)"
997}, {
998 label: "5*6",
999 set: "5",
1000 fn: "mul",
1001 param: 6,
1002 expect: "30"
1003}, {
1004 label: "1/2-1/5",
1005 set: "1/2",
1006 fn: "sub",
1007 param: "1/5",
1008 expect: "0.3"
1009}, {
1010 label: "1/2-5",
1011 set: "1/2",
1012 fn: "add",
1013 param: -5,
1014 expect: "-4.5"
1015}, {
1016 label: "1*-1",
1017 set: "1",
1018 fn: "mul",
1019 param: -1,
1020 expect: "-1"
1021}, {
1022 label: "5/10",
1023 set: 5.0,
1024 fn: "div",
1025 param: 10,
1026 expect: "0.5"
1027}, {
1028 label: "1/-1",
1029 set: "1",
1030 fn: "div",
1031 param: -1,
1032 expect: "-1"
1033}, {
1034 label: "4/5 + 13/2",
1035 set: "4/5",
1036 fn: "add",
1037 param: "13/2",
1038 expect: "7.3"
1039}, {
1040 label: "4/5 + 61/2",
1041 set: "4/5",
1042 fn: "add",
1043 param: "61/2",
1044 expect: "31.3"
1045}, {
1046 label: "0.8 + 6.5",
1047 set: "0.8",
1048 fn: "add",
1049 param: "6.5",
1050 expect: "7.3"
1051}, {
1052 label: "2/7 inverse",
1053 set: "2/7",
1054 fn: "inverse",
1055 param: null,
1056 expect: "3.5"
1057}, {
1058 label: "neg 1/3",
1059 set: "1/3",
1060 fn: "neg",
1061 param: null,
1062 expect: "-0.(3)"
1063}, {
1064 label: "1/2+1/3",
1065 set: "1/2",
1066 fn: "add",
1067 param: "1/3",
1068 expect: "0.8(3)"
1069}, {
1070 label: "1/2+3",
1071 set: ".5",
1072 fn: "add",
1073 param: 3,
1074 expect: "3.5"
1075}, {
1076 label: "1/2+3.14",
1077 set: "1/2",
1078 fn: "add",
1079 param: "3.14",
1080 expect: "3.64"
1081}, {
1082 label: "3.5 < 4.1",
1083 set: 3.5,
1084 fn: "compare",
1085 param: 4.1,
1086 expect: "-1"
1087}, {
1088 label: "3.5 > 4.1",
1089 set: 4.1,
1090 fn: "compare",
1091 param: 3.1,
1092 expect: "1"
1093}, {
1094 label: "-3.5 > -4.1",
1095 set: -3.5,
1096 fn: "compare",
1097 param: -4.1,
1098 expect: "1"
1099}, {
1100 label: "-3.5 > -4.1",
1101 set: -4.1,
1102 fn: "compare",
1103 param: -3.5,
1104 expect: "-1"
1105}, {
1106 label: "4.3 == 4.3",
1107 set: 4.3,
1108 fn: "compare",
1109 param: 4.3,
1110 expect: "0"
1111}, {
1112 label: "-4.3 == -4.3",
1113 set: -4.3,
1114 fn: "compare",
1115 param: -4.3,
1116 expect: "0"
1117}, {
1118 label: "-4.3 < 4.3",
1119 set: -4.3,
1120 fn: "compare",
1121 param: 4.3,
1122 expect: "-1"
1123}, {
1124 label: "4.3 == -4.3",
1125 set: 4.3,
1126 fn: "compare",
1127 param: -4.3,
1128 expect: "1"
1129}, {
1130 label: "2^0.5",
1131 set: 2,
1132 fn: "pow",
1133 param: 0.5,
1134 expect: "null"
1135}, {
1136 label: "(-8/27)^(1/3)",
1137 set: [-8, 27],
1138 fn: "pow",
1139 param: [1, 3],
1140 expect: "null"
1141}, {
1142 label: "(-8/27)^(2/3)",
1143 set: [-8, 27],
1144 fn: "pow",
1145 param: [2, 3],
1146 expect: "null"
1147}, {
1148 label: "(-32/243)^(5/3)",
1149 set: [-32, 243],
1150 fn: "pow",
1151 param: [5, 3],
1152 expect: "null"
1153}, {
1154 label: "sqrt(0)",
1155 set: 0,
1156 fn: "pow",
1157 param: 0.5,
1158 expect: "0"
1159}, {
1160 label: "27^(2/3)",
1161 set: 27,
1162 fn: "pow",
1163 param: "2/3",
1164 expect: "9"
1165}, {
1166 label: "(243/1024)^(2/5)",
1167 set: "243/1024",
1168 fn: "pow",
1169 param: "2/5",
1170 expect: "0.5625"
1171}, {
1172 label: "-0.5^-3",
1173 set: -0.5,
1174 fn: "pow",
1175 param: -3,
1176 expect: "-8"
1177}, {
1178 label: "",
1179 set: -3,
1180 fn: "pow",
1181 param: -3,
1182 expect: "-0.(037)"
1183}, {
1184 label: "-3",
1185 set: -3,
1186 fn: "pow",
1187 param: 2,
1188 expect: "9"
1189}, {
1190 label: "-3",
1191 set: -3,
1192 fn: "pow",
1193 param: 3,
1194 expect: "-27"
1195}, {
1196 label: "0^0",
1197 set: 0,
1198 fn: "pow",
1199 param: 0,
1200 expect: "1"
1201}, {
1202 label: "2/3^7",
1203 set: [2, 3],
1204 fn: "pow",
1205 param: 7,
1206 expect: "0.(058527663465935070873342478280749885688157293095564700502972107910379515317786922725194330132601737540009144947416552354823959762231367169638774577046181984453589391860996799268404206675811614083219021490626428898033836305441243712848651120256)"
1207}, {
1208 label: "-0.6^4",
1209 set: -0.6,
1210 fn: "pow",
1211 param: 4,
1212 expect: "0.1296"
1213}, {
1214 label: "8128371:12394 - 8128371/12394",
1215 set: "8128371:12394",
1216 fn: "sub",
1217 param: "8128371/12394",
1218 expect: "0"
1219}, {
1220 label: "3/4 + 1/4",
1221 set: "3/4",
1222 fn: "add",
1223 param: "1/4",
1224 expect: "1"
1225}, {
1226 label: "1/10 + 2/10",
1227 set: "1/10",
1228 fn: "add",
1229 param: "2/10",
1230 expect: "0.3"
1231}, {
1232 label: "5/10 + 2/10",
1233 set: "5/10",
1234 fn: "add",
1235 param: "2/10",
1236 expect: "0.7"
1237}, {
1238 label: "18/10 + 2/10",
1239 set: "18/10",
1240 fn: "add",
1241 param: "2/10",
1242 expect: "2"
1243}, {
1244 label: "1/3 + 1/6",
1245 set: "1/3",
1246 fn: "add",
1247 param: "1/6",
1248 expect: "0.5"
1249}, {
1250 label: "1/3 + 2/6",
1251 set: "1/3",
1252 fn: "add",
1253 param: "2/6",
1254 expect: "0.(6)"
1255}, {
1256 label: "3/4 / 1/4",
1257 set: "3/4",
1258 fn: "div",
1259 param: "1/4",
1260 expect: "3"
1261}, {
1262 label: "1/10 / 2/10",
1263 set: "1/10",
1264 fn: "div",
1265 param: "2/10",
1266 expect: "0.5"
1267}, {
1268 label: "5/10 / 2/10",
1269 set: "5/10",
1270 fn: "div",
1271 param: "2/10",
1272 expect: "2.5"
1273}, {
1274 label: "18/10 / 2/10",
1275 set: "18/10",
1276 fn: "div",
1277 param: "2/10",
1278 expect: "9"
1279}, {
1280 label: "1/3 / 1/6",
1281 set: "1/3",
1282 fn: "div",
1283 param: "1/6",
1284 expect: "2"
1285}, {
1286 label: "1/3 / 2/6",
1287 set: "1/3",
1288 fn: "div",
1289 param: "2/6",
1290 expect: "1"
1291}, {
1292 label: "3/4 * 1/4",
1293 set: "3/4",
1294 fn: "mul",
1295 param: "1/4",
1296 expect: "0.1875"
1297}, {
1298 label: "1/10 * 2/10",
1299 set: "1/10",
1300 fn: "mul",
1301 param: "2/10",
1302 expect: "0.02"
1303}, {
1304 label: "5/10 * 2/10",
1305 set: "5/10",
1306 fn: "mul",
1307 param: "2/10",
1308 expect: "0.1"
1309}, {
1310 label: "18/10 * 2/10",
1311 set: "18/10",
1312 fn: "mul",
1313 param: "2/10",
1314 expect: "0.36"
1315}, {
1316 label: "1/3 * 1/6",
1317 set: "1/3",
1318 fn: "mul",
1319 param: "1/6",
1320 expect: "0.0(5)"
1321}, {
1322 label: "1/3 * 2/6",
1323 set: "1/3",
1324 fn: "mul",
1325 param: "2/6",
1326 expect: "0.(1)"
1327}, {
1328 label: "5/4 - 1/4",
1329 set: "5/4",
1330 fn: "sub",
1331 param: "1/4",
1332 expect: "1"
1333}, {
1334 label: "5/10 - 2/10",
1335 set: "5/10",
1336 fn: "sub",
1337 param: "2/10",
1338 expect: "0.3"
1339}, {
1340 label: "9/10 - 2/10",
1341 set: "9/10",
1342 fn: "sub",
1343 param: "2/10",
1344 expect: "0.7"
1345}, {
1346 label: "22/10 - 2/10",
1347 set: "22/10",
1348 fn: "sub",
1349 param: "2/10",
1350 expect: "2"
1351}, {
1352 label: "2/3 - 1/6",
1353 set: "2/3",
1354 fn: "sub",
1355 param: "1/6",
1356 expect: "0.5"
1357}, {
1358 label: "3/3 - 2/6",
1359 set: "3/3",
1360 fn: "sub",
1361 param: "2/6",
1362 expect: "0.(6)"
1363}, {
1364 label: "0.006999999999999999",
1365 set: 0.006999999999999999,
1366 fn: "add",
1367 param: 0,
1368 expect: "0.007"
1369}, {
1370 label: "1/7 - 1",
1371 set: 1 / 7,
1372 fn: "add",
1373 param: -1,
1374 expect: "-0.(857142)"
1375}, {
1376 label: "0.0065 + 0.0005",
1377 set: 0.0065,
1378 fn: "add",
1379 param: 0.0005,
1380 expect: "0.007"
1381}, {
1382 label: "6.5/.5",
1383 set: 6.5,
1384 fn: "div",
1385 param: .5,
1386 expect: "13"
1387}, {
1388 label: "0.999999999999999999999999999",
1389 set: 0.999999999999999999999999999,
1390 fn: "sub",
1391 param: 1,
1392 expect: "0"
1393}, {
1394 label: "0.5833333333333334",
1395 set: 0.5833333333333334,
1396 fn: "add",
1397 param: 0,
1398 expect: "0.58(3)"
1399}, {
1400 label: "1.75/3",
1401 set: 1.75 / 3,
1402 fn: "add",
1403 param: 0,
1404 expect: "0.58(3)"
1405}, {
1406 label: "3.3333333333333",
1407 set: 3.3333333333333,
1408 fn: "add",
1409 param: 0,
1410 expect: "3.(3)"
1411}, {
1412 label: "4.285714285714285714285714",
1413 set: 4.285714285714285714285714,
1414 fn: "add",
1415 param: 0,
1416 expect: "4.(285714)"
1417}, {
1418 label: "-4",
1419 set: -4,
1420 fn: "neg",
1421 param: 0,
1422 expect: "4"
1423}, {
1424 label: "4",
1425 set: 4,
1426 fn: "neg",
1427 param: 0,
1428 expect: "-4"
1429}, {
1430 label: "0",
1431 set: 0,
1432 fn: "neg",
1433 param: 0,
1434 expect: "0"
1435}, {
1436 label: "6869570742453802/5329686054127205",
1437 set: "6869570742453802/5329686054127205",
1438 fn: "neg",
1439 param: 0,
1440 expect: "-1.288925965373540"
1441}, {
1442 label: "686970702/53212205",
1443 set: "686970702/53212205",
1444 fn: "neg",
1445 param: 0,
1446 expect: "-12.910021338149772"
1447}, {
1448 label: "1/3000000000000000",
1449 set: "1/3000000000000000",
1450 fn: "add",
1451 param: 0,
1452 expect: "0.000000000000000(3)"
1453}, {
1454 label: "toString(15) .0000000000000003",
1455 set: ".0000000000000003",
1456 fn: "toString",
1457 param: 15,
1458 expect: "0.000000000000000"
1459}, {
1460 label: "toString(16) .0000000000000003",
1461 set: ".0000000000000003",
1462 fn: "toString",
1463 param: 16,
1464 expect: "0.0000000000000003"
1465}, {
1466 label: "12 / 4.3",
1467 set: 12,
1468 set2: 4.3,
1469 fn: "toString",
1470 param: null,
1471 expectError: NonIntegerParameter()
1472}, {
1473 label: "12.5 / 4",
1474 set: 12.5,
1475 set2: 4,
1476 fn: "toString",
1477 param: null,
1478 expectError: NonIntegerParameter()
1479}, {
1480 label: "0.9 round to multiple of 1/8",
1481 set: .9,
1482 fn: "roundTo",
1483 param: "1/8",
1484 expect: "0.875"
1485}, {
1486 label: "1/3 round to multiple of 1/16",
1487 set: 1 / 3,
1488 fn: "roundTo",
1489 param: "1/16",
1490 expect: "0.3125"
1491}, {
1492 label: "1/3 round to multiple of 1/16",
1493 set: -1 / 3,
1494 fn: "roundTo",
1495 param: "1/16",
1496 expect: "-0.3125"
1497}, {
1498 label: "1/2 round to multiple of 1/4",
1499 set: 1 / 2,
1500 fn: "roundTo",
1501 param: "1/4",
1502 expect: "0.5"
1503}, {
1504 label: "1/4 round to multiple of 1/2",
1505 set: 1 / 4,
1506 fn: "roundTo",
1507 param: "1/2",
1508 expect: "0.5"
1509}, {
1510 label: "10/3 round to multiple of 1/2",
1511 set: "10/3",
1512 fn: "roundTo",
1513 param: "1/2",
1514 expect: "3.5"
1515}, {
1516 label: "-10/3 round to multiple of 1/2",
1517 set: "-10/3",
1518 fn: "roundTo",
1519 param: "1/2",
1520 expect: "-3.5"
1521}, {
1522 label: "log_2(8)",
1523 set: "8",
1524 fn: "log",
1525 param: "2",
1526 expect: "3" // because 2^3 = 8
1527}, {
1528 label: "log_2(3)",
1529 set: "3",
1530 fn: "log",
1531 param: "2",
1532 expect: 'null' // because 2^(p/q) != 3
1533}, {
1534 label: "log_10(1000)",
1535 set: "1000",
1536 fn: "log",
1537 param: "10",
1538 expect: "3" // because 10^3 = 1000
1539}, {
1540 label: "log_27(81)",
1541 set: "81",
1542 fn: "log",
1543 param: "27",
1544 expect: "1.(3)" // because 27^(4/3) = 81
1545}, {
1546 label: "log_27(9)",
1547 set: "9",
1548 fn: "log",
1549 param: "27",
1550 expect: "0.(6)" // because 27^(2/3) = 9
1551}, {
1552 label: "log_9/4(27/8)",
1553 set: "27/8",
1554 fn: "log",
1555 param: "9/4",
1556 expect: "1.5" // because (9/4)^(3/2) = 27/8
1557}];
1558
1559describe('Fraction', function () {
1560 for (var i = 0; i < tests.length; i++) {
1561
1562 (function (i) {
1563 var action;
1564
1565 if (tests[i].fn) {
1566 action = function () {
1567 var x = Fraction(tests[i].set, tests[i].set2)[tests[i].fn](tests[i].param);
1568 if (x === null) return "null";
1569 return x.toString();
1570 };
1571 } else {
1572 action = function () {
1573 var x = new Fraction(tests[i].set, tests[i].set2);
1574 if (x === null) return "null";
1575 return x.toString();
1576 };
1577 }
1578
1579 it(String(tests[i].label || tests[i].set), function () {
1580 if (tests[i].expectError) {
1581 assert.throws(action, tests[i].expectError);
1582 } else {
1583 assert.equal(action(), tests[i].expect);
1584 }
1585 });
1586
1587 })(i);
1588 }
1589});
1590
1591describe('JSON', function () {
1592
1593 it("Should be possible to stringify the object", function () {
1594
1595 if (typeof Fraction(1).n !== 'number') {
1596 return;
1597 }
1598 assert.equal('{"s":1,"n":14623,"d":330}', JSON.stringify(new Fraction("44.3(12)")));
1599 assert.equal('{"s":-1,"n":2,"d":1}', JSON.stringify(new Fraction(-1 / 2).inverse()));
1600 });
1601});
1602
1603describe('Arguments', function () {
1604
1605 it("Should be possible to use different kind of params", function () {
1606
1607 // String
1608 var fraction = new Fraction("0.1");
1609 assert.equal("1/10", fraction.n + "/" + fraction.d);
1610
1611 var fraction = new Fraction("6234/6460");
1612 assert.equal("3117/3230", fraction.n + "/" + fraction.d);
1613
1614 // Two params
1615 var fraction = new Fraction(1, 2);
1616 assert.equal("1/2", fraction.n + "/" + fraction.d);
1617
1618 // Object
1619 var fraction = new Fraction({ n: 1, d: 3 });
1620 assert.equal("1/3", fraction.n + "/" + fraction.d);
1621
1622 // Array
1623 var fraction = new Fraction([1, 4]);
1624 assert.equal("1/4", fraction.n + "/" + fraction.d);
1625 });
1626});
1627
1628describe('fractions', function () {
1629
1630 it("Should pass 0.08 = 2/25", function () {
1631
1632 var fraction = new Fraction("0.08");
1633 assert.equal("2/25", fraction.n + "/" + fraction.d);
1634 });
1635
1636 it("Should pass 0.200 = 1/5", function () {
1637
1638 var fraction = new Fraction("0.200");
1639 assert.equal("1/5", fraction.n + "/" + fraction.d);
1640 });
1641
1642 it("Should pass 0.125 = 1/8", function () {
1643
1644 var fraction = new Fraction("0.125");
1645 assert.equal("1/8", fraction.n + "/" + fraction.d);
1646 });
1647
1648 it("Should pass 8.36 = 209/25", function () {
1649
1650 var fraction = new Fraction(8.36);
1651 assert.equal("209/25", fraction.n + "/" + fraction.d);
1652 });
1653
1654});
1655
1656describe('constructors', function () {
1657
1658 it("Should pass 0.08 = 2/25", function () {
1659
1660 var tmp = new Fraction({ d: 4, n: 2, s: -1 });
1661 assert.equal("-1/2", tmp.s * tmp.n + "/" + tmp.d);
1662
1663 var tmp = new Fraction(-88.3);
1664 assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
1665
1666 var tmp = new Fraction(-88.3).clone();
1667 assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
1668
1669 var tmp = new Fraction("123.'3'");
1670 assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
1671
1672 var tmp = new Fraction("123.'3'").clone();
1673 assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
1674
1675 var tmp = new Fraction([-1023461776, 334639305]);
1676 tmp = tmp.add([4, 25]);
1677 assert.equal("-4849597436/1673196525", tmp.s * tmp.n + "/" + tmp.d);
1678 });
1679});
1680
1681describe('Latex Output', function () {
1682
1683 it("Should pass 123.'3' = \\frac{370}{3}", function () {
1684
1685 var tmp = new Fraction("123.'3'");
1686 assert.equal("\\frac{370}{3}", tmp.toLatex());
1687 });
1688
1689 it("Should pass 1.'3' = \\frac{4}{3}", function () {
1690
1691 var tmp = new Fraction("1.'3'");
1692 assert.equal("\\frac{4}{3}", tmp.toLatex());
1693 });
1694
1695 it("Should pass -1.0000000000 = -1", function () {
1696
1697 var tmp = new Fraction("-1.0000000000");
1698 assert.equal('-1', tmp.toLatex());
1699 });
1700
1701 it("Should pass -0.0000000000 = 0", function () {
1702
1703 var tmp = new Fraction("-0.0000000000");
1704 assert.equal('0', tmp.toLatex());
1705 });
1706});
1707
1708describe('Fraction Output', function () {
1709
1710 it("Should pass 123.'3' = 123 1/3", function () {
1711
1712 var tmp = new Fraction("123.'3'");
1713 assert.equal('370/3', tmp.toFraction());
1714 });
1715
1716 it("Should pass 1.'3' = 1 1/3", function () {
1717
1718 var tmp = new Fraction("1.'3'");
1719 assert.equal('4/3', tmp.toFraction());
1720 });
1721
1722 it("Should pass -1.0000000000 = -1", function () {
1723
1724 var tmp = new Fraction("-1.0000000000");
1725 assert.equal('-1', tmp.toFraction());
1726 });
1727
1728 it("Should pass -0.0000000000 = 0", function () {
1729
1730 var tmp = new Fraction("-0.0000000000");
1731 assert.equal('0', tmp.toFraction());
1732 });
1733
1734 it("Should pass 1/-99/293 = -1/29007", function () {
1735
1736 var tmp = new Fraction(-99).inverse().div(293);
1737 assert.equal('-1/29007', tmp.toFraction());
1738 });
1739
1740 it('Should work with large calculations', function () {
1741 var x = Fraction(1123875);
1742 var y = Fraction(1238750184);
1743 var z = Fraction(1657134);
1744 var r = Fraction(77344464613500, 92063);
1745 assert.equal(x.mul(y).div(z).toFraction(), r.toFraction());
1746 });
1747});
1748
1749describe('Fraction toContinued', function () {
1750
1751 it("Should pass 415/93", function () {
1752
1753 var tmp = new Fraction(415, 93);
1754 assert.equal('4,2,6,7', tmp.toContinued().toString());
1755 });
1756
1757 it("Should pass 0/2", function () {
1758
1759 var tmp = new Fraction(0, 2);
1760 assert.equal('0', tmp.toContinued().toString());
1761 });
1762
1763 it("Should pass 1/7", function () {
1764
1765 var tmp = new Fraction(1, 7);
1766 assert.equal('0,7', tmp.toContinued().toString());
1767 });
1768
1769 it("Should pass 23/88", function () {
1770
1771 var tmp = new Fraction('23/88');
1772 assert.equal('0,3,1,4,1,3', tmp.toContinued().toString());
1773 });
1774
1775 it("Should pass 1/99", function () {
1776
1777 var tmp = new Fraction('1/99');
1778 assert.equal('0,99', tmp.toContinued().toString());
1779 });
1780
1781 it("Should pass 1768/99", function () {
1782
1783 var tmp = new Fraction('1768/99');
1784 assert.equal('17,1,6,14', tmp.toContinued().toString());
1785 });
1786
1787 it("Should pass 1768/99", function () {
1788
1789 var tmp = new Fraction('7/8');
1790 assert.equal('0,1,7', tmp.toContinued().toString());
1791 });
1792
1793});
1794
1795
1796describe('Fraction simplify', function () {
1797
1798 it("Should pass 415/93", function () {
1799
1800 var tmp = new Fraction(415, 93);
1801 assert.equal('9/2', tmp.simplify(0.1).toFraction());
1802 assert.equal('58/13', tmp.simplify(0.01).toFraction());
1803 assert.equal('415/93', tmp.simplify(0.0001).toFraction());
1804 });
1805
1806});
Note: See TracBrowser for help on using the repository browser.