math-utils-test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { fromJS } from 'immutable';
  2. describe('MathUtils', () => {
  3. const MathUtils = require('../math-utils');
  4. describe('modulo', () => {
  5. const f = MathUtils.modulo;
  6. it('it should calculate the modulo (also for negatives)', () => {
  7. expect(f(5, 5)).toBe(0);
  8. expect(f(4, 5)).toBe(4);
  9. expect(f(3, 5)).toBe(3);
  10. expect(f(2, 5)).toBe(2);
  11. expect(f(1, 5)).toBe(1);
  12. expect(f(0, 5)).toBe(0);
  13. expect(f(-1, 5)).toBe(4);
  14. expect(f(-2, 5)).toBe(3);
  15. expect(f(-3, 5)).toBe(2);
  16. expect(f(-4, 5)).toBe(1);
  17. expect(f(-5, 5)).toBe(0);
  18. });
  19. });
  20. describe('minEuclideanDistanceBetweenPoints', () => {
  21. const f = MathUtils.minEuclideanDistanceBetweenPoints;
  22. const entryA = { pointA: { x: 0, y: 0 } };
  23. const entryB = { pointB: { x: 30, y: 0 } };
  24. const entryC = { pointC: { x: 0, y: -40 } };
  25. const entryD = { pointD: { x: -1000, y: 567 } };
  26. const entryE = { pointE: { x: -999, y: 567 } };
  27. const entryF = { pointF: { x: 30, y: 0 } };
  28. it('it should return the minimal distance between any two points in the collection', () => {
  29. expect(f(fromJS({}))).toBe(Infinity);
  30. expect(f(fromJS({...entryA}))).toBe(Infinity);
  31. expect(f(fromJS({...entryA, ...entryB}))).toBe(30);
  32. expect(f(fromJS({...entryA, ...entryC}))).toBe(40);
  33. expect(f(fromJS({...entryB, ...entryC}))).toBe(50);
  34. expect(f(fromJS({
  35. ...entryA, ...entryB, ...entryC, ...entryD
  36. }))).toBe(30);
  37. expect(f(fromJS({
  38. ...entryA, ...entryB, ...entryC, ...entryD, ...entryE
  39. }))).toBe(1);
  40. expect(f(fromJS({
  41. ...entryA, ...entryB, ...entryC, ...entryD, ...entryF
  42. }))).toBe(0);
  43. });
  44. });
  45. });