|
| 1 | +import { dotProduct } from '../../../src/helpers/utils'; |
| 2 | + |
| 3 | +describe('utils', () => { |
| 4 | + describe('dotProduct()', () => { |
| 5 | + describe('Input Validation', () => { |
| 6 | + it('should return 0 for invalid array inputs', () => { |
| 7 | + expect(dotProduct(null as any, [1, 2])).toBe(0); |
| 8 | + expect(dotProduct([1, 2], null as any)).toBe(0); |
| 9 | + expect(dotProduct(undefined as any, [1, 2])).toBe(0); |
| 10 | + expect(dotProduct([1, 2], undefined as any)).toBe(0); |
| 11 | + expect(dotProduct('string' as any, [1, 2])).toBe(0); |
| 12 | + expect(dotProduct([1, 2], 'string' as any)).toBe(0); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should return 0 for arrays of different lengths', () => { |
| 16 | + expect(dotProduct([1, 2], [3])).toBe(0); |
| 17 | + expect(dotProduct([1], [2, 3, 4])).toBe(0); |
| 18 | + expect(dotProduct([], [1, 2])).toBe(0); // Empty vs non-empty |
| 19 | + expect(dotProduct([], [])).toBe(0); // Both empty |
| 20 | + }); |
| 21 | + |
| 22 | + it('should reject arrays with non-numeric values', () => { |
| 23 | + expect(dotProduct([1, null, 3], [4, 5, 6])).toBe(0); // Should reject null values |
| 24 | + expect(dotProduct([1, undefined, 3], [4, 5, 6])).toBe(0); // Should reject undefined values |
| 25 | + expect(dotProduct([1, 2, 3], [4, null, 6])).toBe(0); // Should reject null values |
| 26 | + expect(dotProduct(['1', '2'], ['3', '4'])).toBe(0); // Should reject string numbers |
| 27 | + expect(dotProduct([1, '2'], [3, '4'])).toBe(0); // Should reject mixed types |
| 28 | + expect(dotProduct([1, 'invalid', 3], [4, 5, 6])).toBe(0); // Should reject invalid strings |
| 29 | + expect(dotProduct([1, {}, 3], [4, 5, 6])).toBe(0); // Should reject objects |
| 30 | + expect(dotProduct([1, [], 3], [4, 5, 6])).toBe(0); // Should reject arrays as values |
| 31 | + }); |
| 32 | + |
| 33 | + it('should reject mixed flat and nested arrays with different structures', () => { |
| 34 | + expect(dotProduct([1, 2], [[3], 4])).toBe(0); // Different structures - should reject |
| 35 | + expect(dotProduct([[1], 2], [3, 4])).toBe(0); // Different structures - should reject |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('Basic Calculations', () => { |
| 40 | + it('should calculate dot product for simple arrays', () => { |
| 41 | + expect(dotProduct([1, 2, 3], [4, 5, 6])).toBe(32); // 1*4 + 2*5 + 3*6 = 32 |
| 42 | + expect(dotProduct([2, 3], [4, 5])).toBe(23); // 2*4 + 3*5 = 23 |
| 43 | + expect(dotProduct([1], [5])).toBe(5); // 1*5 = 5 |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle arrays with zeros', () => { |
| 47 | + expect(dotProduct([1, 0, 3], [4, 5, 6])).toBe(22); // 1*4 + 0*5 + 3*6 = 22 |
| 48 | + expect(dotProduct([0, 0, 0], [1, 2, 3])).toBe(0); // All zeros in first array |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle negative numbers', () => { |
| 52 | + expect(dotProduct([-1, 2], [3, -4])).toBe(-11); // -1*3 + 2*(-4) = -11 |
| 53 | + expect(dotProduct([-2, -3], [-4, -5])).toBe(23); // -2*(-4) + -3*(-5) = 23 |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('Nested Array Support', () => { |
| 58 | + it('should handle nested arrays (single level)', () => { |
| 59 | + expect(dotProduct([1, [2, 3]], [4, [5, 6]])).toBe(32); // Flattened: [1,2,3] • [4,5,6] = 32 |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle deeply nested arrays', () => { |
| 63 | + expect(dotProduct([1, [2, [3, 4]]], [5, [6, [7, 8]]])).toBe(70); // [1,2,3,4] • [5,6,7,8] = 70 |
| 64 | + expect(dotProduct([[[1]], [2]], [[[3]], [4]])).toBe(11); // [1,2] • [3,4] = 11 |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('JavaScript Native Precision', () => { |
| 69 | + it('should handle large × large number multiplication', () => { |
| 70 | + // Test realistic large financial numbers |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision |
| 72 | + const votingPower = [123456789012.456789]; |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision |
| 74 | + const tokenValue = [987654321.123456789]; |
| 75 | + |
| 76 | + const result = dotProduct(votingPower, tokenValue); |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision |
| 78 | + const expected = 123456789012.456789 * 987654321.123456789; |
| 79 | + |
| 80 | + expect(result).toBe(expected); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should handle small × large number multiplication (DeFi scenario)', () => { |
| 84 | + // Test small token values with large voting power |
| 85 | + const smallTokenValues = [1e-18, 1e-12, 1e-6]; // Wei, micro, milli units |
| 86 | + const largeVotingPower = [1e18, 1e15, 1e12]; // Large voting power values |
| 87 | + |
| 88 | + const result = dotProduct(smallTokenValues, largeVotingPower); |
| 89 | + |
| 90 | + // Should equal: 1 + 1000 + 1000000 = 1001001 |
| 91 | + expect(result).toBe(1001001); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle maximum precision decimal numbers', () => { |
| 95 | + // Test JavaScript's precision limits (~15-16 significant digits) |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision |
| 97 | + const maxDecimalA = [1.1234567890123456]; |
| 98 | + const maxDecimalB = [2.9876543210987654]; |
| 99 | + |
| 100 | + const result = dotProduct(maxDecimalA, maxDecimalB); |
| 101 | + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision |
| 102 | + const expected = 1.1234567890123456 * 2.9876543210987654; |
| 103 | + |
| 104 | + expect(result).toBe(expected); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle underflow edge cases', () => { |
| 108 | + // Test numbers that underflow to 0 |
| 109 | + const verySmallA = [1e-200]; |
| 110 | + const verySmallB = [1e-200]; |
| 111 | + |
| 112 | + const result = dotProduct(verySmallA, verySmallB); |
| 113 | + |
| 114 | + expect(result).toBe(0); // JavaScript underflow behavior |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments