|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { zonesToXEquallySpaced } from '../zonesToXEquallySpaced.ts'; |
| 4 | + |
| 5 | +describe('zonesToXEquallySpaced', () => { |
| 6 | + it('should distribute points equally across a single zone', () => { |
| 7 | + const zones = [{ from: 0, to: 10 }]; |
| 8 | + const result = zonesToXEquallySpaced(zones, 5); |
| 9 | + |
| 10 | + expect(result).toHaveLength(5); |
| 11 | + // Edges are excluded: approx [1.6667, 3.3333, 5, 6.6667, 8.3333] |
| 12 | + expect(result[0]).toBeCloseTo(10 / 6, 5); |
| 13 | + expect(result[2]).toBeCloseTo(5, 5); |
| 14 | + expect(result[4]).toBeCloseTo((10 * 5) / 6, 5); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should distribute points across multiple zones without repeating edges', () => { |
| 18 | + const zones = [ |
| 19 | + { from: 0, to: 5 }, |
| 20 | + { from: 10, to: 15 }, |
| 21 | + ]; |
| 22 | + const result = zonesToXEquallySpaced(zones, 10); |
| 23 | + |
| 24 | + expect(result).toHaveLength(10); |
| 25 | + // Edges excluded: first value ~0.8333, last value ~14.1667 |
| 26 | + expect(result[0]).toBeCloseTo(5 / 6, 5); |
| 27 | + expect(result[9]).toBeCloseTo(15 - 5 / 6, 5); |
| 28 | + |
| 29 | + // Check that zone boundary (5) is not in the result |
| 30 | + const has5 = Array.from(result).some((v) => Math.abs(v - 5) < 0.01); |
| 31 | + |
| 32 | + expect(has5).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle zones with custom from/to options', () => { |
| 36 | + const zones = [{ from: 2, to: 8 }]; |
| 37 | + const result = zonesToXEquallySpaced(zones, 3, { from: 0, to: 10 }); |
| 38 | + |
| 39 | + expect(result).toHaveLength(3); |
| 40 | + // Edges excluded: values are approx [3.5, 5, 6.5] |
| 41 | + expect(result[0]).toBeCloseTo(3.5, 5); |
| 42 | + expect(result[1]).toBeCloseTo(5, 5); |
| 43 | + expect(result[2]).toBeCloseTo(6.5, 5); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should throw error if zones array is empty', () => { |
| 47 | + expect(() => zonesToXEquallySpaced([], 10)).toThrow( |
| 48 | + 'zones array must not be empty', |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw error if numberOfPoints is less than 1', () => { |
| 53 | + const zones = [{ from: 0, to: 10 }]; |
| 54 | + |
| 55 | + expect(() => zonesToXEquallySpaced(zones, 0)).toThrow( |
| 56 | + "'numberOfPoints' must be greater than 0", |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should throw error if from is greater than to', () => { |
| 61 | + const zones = [{ from: 10, to: 0 }]; |
| 62 | + |
| 63 | + expect(() => zonesToXEquallySpaced(zones, 10)).toThrow( |
| 64 | + 'from should be less than or equal to to', |
| 65 | + ); |
| 66 | + }); |
| 67 | +}); |
0 commit comments