Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 1 | // Tests for util-related things |
Kevin Lubick | d2efe52 | 2019-01-04 15:59:06 -0500 | [diff] [blame] | 2 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000; |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 3 | describe('PathKit\'s CubicMap Behavior', function() { |
| 4 | // Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up. |
| 5 | var PathKit = null; |
| 6 | const LoadPathKit = new Promise(function(resolve, reject) { |
| 7 | if (PathKit) { |
| 8 | resolve(); |
| 9 | } else { |
| 10 | PathKitInit({ |
| 11 | locateFile: (file) => '/pathkit/'+file, |
Kevin Lubick | 275eaff | 2019-01-04 14:38:29 -0500 | [diff] [blame] | 12 | }).ready().then((_PathKit) => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 13 | PathKit = _PathKit; |
| 14 | resolve(); |
| 15 | }); |
| 16 | } |
| 17 | }); |
| 18 | |
| 19 | it('computes YFromX 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 x, expected y |
| 24 | [0.025391, 0.117627], |
| 25 | [0.333984, 0.276221], |
| 26 | [0.662109, 0.366052], |
| 27 | [0.939453, 0.643296], |
| 28 | ]; |
| 29 | for (tc of testcases) { |
| 30 | expect(PathKit.cubicYFromX(0, 0.5, 1.0, 0, tc[0])).toBeCloseTo(tc[1], 5); |
| 31 | } |
| 32 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 33 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 34 | }); |
| 35 | it('computes a point from T correctly', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 36 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 37 | // Spot check a few points |
| 38 | const testcases = [ |
| 39 | // input t, expected x, expected y |
| 40 | [0.25, [0.128125, 0.240625]], |
| 41 | [0.5, [0.35, 0.35]], |
| 42 | [0.75, [0.646875, 0.534375]], |
| 43 | [1.0, [1.0, 1.0]], |
| 44 | ]; |
| 45 | for (tc of testcases) { |
| 46 | let ans = PathKit.cubicPtFromT(0.1, 0.5, 0.5, 0.1, tc[0]); |
| 47 | expect(ans).toBeTruthy(); |
| 48 | expect(ans.length).toBe(2); |
| 49 | expect(ans[0]).toBeCloseTo(tc[1][0]); |
| 50 | expect(ans[1]).toBeCloseTo(tc[1][1]); |
| 51 | } |
| 52 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 53 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 54 | }); |
| 55 | |
| 56 | it('does not leak, with or without cache', function(done) { |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 57 | LoadPathKit.then(catchException(done, () => { |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 58 | // Run it a lot to make sure we don't leak. |
| 59 | for (let i = 0; i < 300000; i++) { |
| 60 | PathKit.cubicYFromX(0.1, 0.5, 0.5, 0.1, 0.1); |
| 61 | PathKit.cubicPtFromT(0.1, 0.5, 0.5, 0.1, 0.1); |
| 62 | } |
| 63 | done(); |
Kevin Lubick | e71e9ef | 2018-11-05 07:51:40 -0500 | [diff] [blame] | 64 | })); |
Kevin Lubick | 774be5a | 2018-09-07 14:00:41 -0400 | [diff] [blame] | 65 | }); |
| 66 | |
Kevin Lubick | faa7387 | 2019-01-06 22:27:54 -0500 | [diff] [blame^] | 67 | }); |