Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 1 | // Tests for util-related things |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 2 | describe('PathKit\'s CubicMap Behavior', function() { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 3 | it('computes YFromX correctly', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 4 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 5 | // Spot check a few points |
| 6 | const testcases = [ |
| 7 | // input x, expected y |
| 8 | [0.025391, 0.117627], |
| 9 | [0.333984, 0.276221], |
| 10 | [0.662109, 0.366052], |
| 11 | [0.939453, 0.643296], |
| 12 | ]; |
| 13 | for (tc of testcases) { |
| 14 | expect(PathKit.cubicYFromX(0, 0.5, 1.0, 0, tc[0])).toBeCloseTo(tc[1], 5); |
| 15 | } |
| 16 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 17 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 18 | }); |
| 19 | it('computes a point from T correctly', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 20 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 21 | // Spot check a few points |
| 22 | const testcases = [ |
| 23 | // input t, expected x, expected y |
| 24 | [0.25, [0.128125, 0.240625]], |
| 25 | [0.5, [0.35, 0.35]], |
| 26 | [0.75, [0.646875, 0.534375]], |
| 27 | [1.0, [1.0, 1.0]], |
| 28 | ]; |
| 29 | for (tc of testcases) { |
| 30 | let ans = PathKit.cubicPtFromT(0.1, 0.5, 0.5, 0.1, tc[0]); |
| 31 | expect(ans).toBeTruthy(); |
| 32 | expect(ans.length).toBe(2); |
| 33 | expect(ans[0]).toBeCloseTo(tc[1][0]); |
| 34 | expect(ans[1]).toBeCloseTo(tc[1][1]); |
| 35 | } |
| 36 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 37 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 38 | }); |
| 39 | |
| 40 | it('does not leak, with or without cache', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 41 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 42 | // Run it a lot to make sure we don't leak. |
| 43 | for (let i = 0; i < 300000; i++) { |
| 44 | PathKit.cubicYFromX(0.1, 0.5, 0.5, 0.1, 0.1); |
| 45 | PathKit.cubicPtFromT(0.1, 0.5, 0.5, 0.1, 0.1); |
| 46 | } |
| 47 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 48 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 49 | }); |
| 50 | |
Kevin Lubick | faa7387 | 2019-01-06 22:27:54 -0500 | [diff] [blame] | 51 | }); |