|
1 | | -import { scale, sample } from './helpers'; |
| 1 | +import { scale, sample, robustScale, trimmedScale } from './helpers'; |
2 | 2 |
|
3 | 3 | describe('scale function', () => { |
4 | 4 | it('should scale the array values between 0 and 1', () => { |
@@ -57,3 +57,88 @@ describe('sample function', () => { |
57 | 57 | expect(sampledArray).toEqual([1, 1, 2, 2, 3]); // The result will include elements from the original array with repetition |
58 | 58 | }); |
59 | 59 | }); |
| 60 | + |
| 61 | +describe('robustScale', () => { |
| 62 | + test('should return empty array for empty input', () => { |
| 63 | + expect(robustScale([])).toEqual([]); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should return array of zeros for array with identical elements', () => { |
| 67 | + expect(robustScale([1, 1, 1, 1])).toEqual([0, 0, 0, 0]); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should scale values correctly for a simple array', () => { |
| 71 | + const input = [1, 2, 3, 4, 5]; |
| 72 | + const output = robustScale(input); |
| 73 | + |
| 74 | + expect(output[0]).toEqual(0); |
| 75 | + expect(output[1]).toEqual(0.3333333333333333); |
| 76 | + expect(output[2]).toBeGreaterThan(0); |
| 77 | + expect(output[4]).toEqual(1); |
| 78 | + }); |
| 79 | + |
| 80 | + test('should handle arrays with outliers', () => { |
| 81 | + const input = [1, 2, 3, 4, 100]; |
| 82 | + const output = robustScale(input); |
| 83 | + |
| 84 | + expect(output[0]).toEqual(0); |
| 85 | + expect(output[1]).toEqual(0.3333333333333333); |
| 86 | + expect(output[2]).toBeGreaterThan(0); |
| 87 | + expect(output[3]).toBeLessThanOrEqual(1); |
| 88 | + expect(output[4]).toEqual(1); |
| 89 | + }); |
| 90 | + |
| 91 | + test('should handle small arrays', () => { |
| 92 | + const input = [1, 2]; |
| 93 | + const output = robustScale(input); |
| 94 | + |
| 95 | + expect(output[0]).toBeLessThanOrEqual(0); |
| 96 | + expect(output[1]).toBeGreaterThanOrEqual(0); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('trimmedScale function', () => { |
| 101 | + test('should return empty array for empty input', () => { |
| 102 | + expect(trimmedScale([])).toEqual([]); |
| 103 | + }); |
| 104 | + |
| 105 | + test('should return array of zeros for array with identical elements', () => { |
| 106 | + expect(trimmedScale([5, 5, 5, 5])).toEqual([0, 0, 0, 0]); |
| 107 | + }); |
| 108 | + |
| 109 | + test('should scale values correctly for a simple array', () => { |
| 110 | + const input = [10, 20, 30, 40, 50]; |
| 111 | + const output = trimmedScale(input); |
| 112 | + expect(output).toEqual([0, 0.25, 0.5, 0.75, 1]); |
| 113 | + }); |
| 114 | + |
| 115 | + test('should handle arrays with outliers', () => { |
| 116 | + const input = [1, 2, 3, 4, 100]; |
| 117 | + const output = trimmedScale(input); |
| 118 | + expect(output).toEqual([ |
| 119 | + 0, |
| 120 | + expect.any(Number), |
| 121 | + expect.any(Number), |
| 122 | + expect.any(Number), |
| 123 | + 1, |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should handle custom percentiles', () => { |
| 128 | + const input = [1, 2, 3, 4, 5, 100]; |
| 129 | + const output = trimmedScale(input, 0.1, 0.9); |
| 130 | + expect(output).toEqual([ |
| 131 | + expect.any(Number), |
| 132 | + expect.any(Number), |
| 133 | + expect.any(Number), |
| 134 | + expect.any(Number), |
| 135 | + expect.any(Number), |
| 136 | + 1, |
| 137 | + ]); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should handle small arrays', () => { |
| 141 | + const input = [2, 8]; |
| 142 | + expect(trimmedScale(input)).toEqual([0, 1]); |
| 143 | + }); |
| 144 | +}); |
0 commit comments