|
| 1 | +const { AStar } = require('.'); |
| 2 | + |
| 3 | +describe('A*', () => { |
| 4 | + describe('Completes grid successfully', () => { |
| 5 | + it('A*', () => { |
| 6 | + const inputGrid = [ |
| 7 | + [1, 1, 1, 0, 0, 0], |
| 8 | + [1, 0, 1, 1, 1, 1], |
| 9 | + [1, 0, 0, 0, 0, 0], |
| 10 | + [1, 1, 1, 1, 1, 1], |
| 11 | + ]; |
| 12 | + const ROW = inputGrid.length; |
| 13 | + const COL = inputGrid[0].length; |
| 14 | + const start = { |
| 15 | + i: 0, |
| 16 | + j: 0, |
| 17 | + }; |
| 18 | + const end = { |
| 19 | + i: 3, |
| 20 | + j: 5, |
| 21 | + }; |
| 22 | + const completedPath = [[3, 5], [3, 4], [3, 3], [3, 2], [3, 1], [2, 0], [1, 0], [0, 0]]; |
| 23 | + expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('Completes large grid successfully', () => { |
| 28 | + it('A*', () => { |
| 29 | + const inputGrid = [ |
| 30 | + [1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1], |
| 31 | + [1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1], |
| 32 | + [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1], |
| 33 | + [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], |
| 34 | + [1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1], |
| 35 | + [0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1], |
| 36 | + [0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0], |
| 37 | + [0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0], |
| 38 | + [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], |
| 39 | + ]; |
| 40 | + const ROW = inputGrid.length; |
| 41 | + const COL = inputGrid[0].length; |
| 42 | + const start = { |
| 43 | + i: 0, |
| 44 | + j: 0, |
| 45 | + }; |
| 46 | + const end = { |
| 47 | + i: 8, |
| 48 | + j: 11, |
| 49 | + }; |
| 50 | + const completedPath = [[8, 11], [8, 10], [7, 9], [6, 8], [5, 9], [5, 10], |
| 51 | + [4, 11], [3, 11], [2, 11], [1, 11], [0, 10], [1, 9], [0, 8], [1, 7], |
| 52 | + [1, 6], [2, 5], [2, 4], [2, 3], [2, 2], [2, 1], [1, 0], [0, 0]]; |
| 53 | + expect(AStar(start, end, ROW, COL, inputGrid)).toEqual(completedPath); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('Cannot complete grid successfully', () => { |
| 58 | + it('A*', () => { |
| 59 | + const inputGrid = [ |
| 60 | + [1, 1, 1, 0, 0, 0], |
| 61 | + [0, 0, 0, 0, 0, 0], |
| 62 | + [1, 0, 0, 0, 0, 0], |
| 63 | + [1, 1, 1, 1, 1, 1], |
| 64 | + ]; |
| 65 | + const ROW = inputGrid.length; |
| 66 | + const COL = inputGrid[0].length; |
| 67 | + const start = { |
| 68 | + i: 0, |
| 69 | + j: 0, |
| 70 | + }; |
| 71 | + const end = { |
| 72 | + i: 3, |
| 73 | + j: 5, |
| 74 | + }; |
| 75 | + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint cannot be reached'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('Endpoint out of grid bounds', () => { |
| 80 | + it('A*', () => { |
| 81 | + const inputGrid = [ |
| 82 | + [1, 1, 1, 0, 0, 0], |
| 83 | + [0, 0, 0, 0, 0, 0], |
| 84 | + [1, 0, 0, 0, 0, 0], |
| 85 | + [1, 1, 1, 1, 1, 1], |
| 86 | + ]; |
| 87 | + const ROW = inputGrid.length; |
| 88 | + const COL = inputGrid[0].length; |
| 89 | + const start = { |
| 90 | + i: 0, |
| 91 | + j: 0, |
| 92 | + }; |
| 93 | + const end = { |
| 94 | + i: 5, |
| 95 | + j: 5, |
| 96 | + }; |
| 97 | + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint outside grid bounds'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('Endpoint value is zero', () => { |
| 102 | + it('A*', () => { |
| 103 | + const inputGrid = [ |
| 104 | + [1, 1, 1, 0, 0, 0], |
| 105 | + [0, 0, 0, 0, 0, 0], |
| 106 | + [1, 0, 0, 0, 0, 0], |
| 107 | + [1, 1, 1, 1, 1, 1], |
| 108 | + ]; |
| 109 | + const ROW = inputGrid.length; |
| 110 | + const COL = inputGrid[0].length; |
| 111 | + const start = { |
| 112 | + i: 0, |
| 113 | + j: 0, |
| 114 | + }; |
| 115 | + const end = { |
| 116 | + i: 1, |
| 117 | + j: 3, |
| 118 | + }; |
| 119 | + expect(() => { AStar(start, end, ROW, COL, inputGrid); }).toThrowError('Error: Endpoint is unreachable'); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments