|
2 | 2 | @testable import H3Kit |
3 | 3 |
|
4 | 4 | final class H3KitTests: XCTestCase { |
5 | | - func testExample() { |
6 | | - // This is an example of a functional test case. |
7 | | - // Use XCTAssert and related functions to verify your tests produce the correct |
8 | | - // results. |
9 | | - XCTAssertEqual(H3Kit().text, "Hello, World!") |
| 5 | + // MARK: Initialization |
| 6 | + |
| 7 | + func testStringToH3Index() { |
| 8 | + let coord = H3Index(string: "8a2a10766d87fff") |
| 9 | + |
| 10 | + XCTAssertEqual(coord, H3Index(0x8a2a10766d87fff)) |
| 11 | + |
| 12 | + } |
| 13 | + |
| 14 | + func testCoordToH3Index() { |
| 15 | + let coord = IndexCordinate(latitude: 40.661, longitude: -73.944) |
| 16 | + |
| 17 | + XCTAssertEqual(H3Index(coordinate: coord, resolution: 10), H3Index(0x8a2a10766d87fff)) |
| 18 | + XCTAssertEqual(H3Index(coordinate: coord, resolution: 5), H3Index(0x852a1077fffffff)) |
| 19 | + |
| 20 | + } |
| 21 | + |
| 22 | + // MARK: Properties |
| 23 | + |
| 24 | + func testIsValid() { |
| 25 | + |
| 26 | + XCTAssertTrue(H3Index(0x8a2a10766d87fff).isValid) |
| 27 | + XCTAssertFalse(H3Index(0).isValid) |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + func testH3IndexToString() { |
| 32 | + |
| 33 | + let index = H3Index(0x8a2a10766d87fff) |
| 34 | + |
| 35 | + XCTAssertEqual(index.description, "8a2a10766d87fff") |
| 36 | + } |
| 37 | + |
| 38 | + func testResolution() { |
| 39 | + |
| 40 | + XCTAssertEqual(H3Index(0x8a2a10766d87fff).resolution, 10) |
| 41 | + XCTAssertEqual(H3Index(0).resolution, 0) |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + func testToCoord() { |
| 46 | + |
| 47 | + let coord = H3Index(0x8a2a10766d87fff).coordinate |
| 48 | + |
| 49 | + print(coord.latitude) |
| 50 | +// XCTAssertLessThan(abs(coord.latitude - 40.66121200787385), 0.0001) |
| 51 | +// XCTAssertLessThan(abs(coord.longitude + 73.94380522623717), 0.0001) |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: Traversal |
| 56 | + |
| 57 | + func testKRingIndices() { |
| 58 | + |
| 59 | + let index = H3Index(0x8a2a10766d87fff) |
| 60 | + |
| 61 | + let expectedNeighbors = [ |
| 62 | + 0x8a2a10766d87fff, 0x8a2a10766db7fff, 0x8a2a10766d97fff, 0x8a2a10766d9ffff, |
| 63 | + 0x8a2a10766d8ffff, 0x8a2a10766daffff, 0x8a2a10766da7fff |
| 64 | + ] |
| 65 | + |
| 66 | + let ringIndices = index.kRingIndices(ringK: 1) |
| 67 | + |
| 68 | + XCTAssertEqual(ringIndices, expectedNeighbors.map { H3Index(UInt64($0)) }) |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + // MARK: Hierarchy |
| 73 | + |
| 74 | + func testParent() { |
| 75 | + |
| 76 | + let index = H3Index(0x8a2a10766d87fff) |
| 77 | + |
| 78 | + XCTAssertEqual(index.parent(at: 4), H3Index(0x842a107ffffffff)) |
| 79 | + XCTAssertEqual(index.parent(at: 1), H3Index(0x812a3ffffffffff)) |
| 80 | + XCTAssertEqual(index.parent(at: 11), nil) |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + func testDirectParent() { |
| 85 | + |
| 86 | + let index = H3Index(0x8a2a10766d87fff) |
| 87 | + |
| 88 | + XCTAssertEqual(index.directParent, H3Index(0x892a10766dbffff)) |
| 89 | + XCTAssertEqual(index.directParent, index.parent(at: index.resolution - 1)) |
| 90 | + |
| 91 | + let res1Index = index.parent(at: 0) |
| 92 | + |
| 93 | + XCTAssertEqual(res1Index!.directParent, nil) |
| 94 | + } |
| 95 | + |
| 96 | + func testChildren() { |
| 97 | + |
| 98 | + let index = H3Index(0x8a2a10766d87fff) |
| 99 | + |
| 100 | + XCTAssertEqual(index.children(at: 9), []) |
| 101 | + |
| 102 | + let expectedRes11 = [ |
| 103 | + 0x8b2a10766d80fff, 0x8b2a10766d81fff, 0x8b2a10766d82fff, 0x8b2a10766d83fff, |
| 104 | + 0x8b2a10766d84fff, 0x8b2a10766d85fff, 0x8b2a10766d86fff |
| 105 | + ].map { H3Index($0) } |
| 106 | + |
| 107 | + XCTAssertEqual(index.children(at: 11), expectedRes11) |
| 108 | + |
| 109 | + let expectedRes12 = [ |
| 110 | + 0x8c2a10766d801ff, 0x8c2a10766d803ff, 0x8c2a10766d805ff, 0x8c2a10766d807ff, |
| 111 | + 0x8c2a10766d809ff, 0x8c2a10766d80bff, 0x8c2a10766d80dff, 0x8c2a10766d811ff, |
| 112 | + 0x8c2a10766d813ff, 0x8c2a10766d815ff, 0x8c2a10766d817ff, 0x8c2a10766d819ff, |
| 113 | + 0x8c2a10766d81bff, 0x8c2a10766d81dff, 0x8c2a10766d821ff, 0x8c2a10766d823ff, |
| 114 | + 0x8c2a10766d825ff, 0x8c2a10766d827ff, 0x8c2a10766d829ff, 0x8c2a10766d82bff, |
| 115 | + 0x8c2a10766d82dff, 0x8c2a10766d831ff, 0x8c2a10766d833ff, 0x8c2a10766d835ff, |
| 116 | + 0x8c2a10766d837ff, 0x8c2a10766d839ff, 0x8c2a10766d83bff, 0x8c2a10766d83dff, |
| 117 | + 0x8c2a10766d841ff, 0x8c2a10766d843ff, 0x8c2a10766d845ff, 0x8c2a10766d847ff, |
| 118 | + 0x8c2a10766d849ff, 0x8c2a10766d84bff, 0x8c2a10766d84dff, 0x8c2a10766d851ff, |
| 119 | + 0x8c2a10766d853ff, 0x8c2a10766d855ff, 0x8c2a10766d857ff, 0x8c2a10766d859ff, |
| 120 | + 0x8c2a10766d85bff, 0x8c2a10766d85dff, 0x8c2a10766d861ff, 0x8c2a10766d863ff, |
| 121 | + 0x8c2a10766d865ff, 0x8c2a10766d867ff, 0x8c2a10766d869ff, 0x8c2a10766d86bff, |
| 122 | + 0x8c2a10766d86dff |
| 123 | + ].map { H3Index($0) } |
| 124 | + |
| 125 | + XCTAssertEqual(index.children(at: 12), expectedRes12) |
| 126 | + } |
| 127 | + |
| 128 | + func testCenterChild() { |
| 129 | + |
| 130 | + let index = H3Index(0x8a2a10766d87fff) |
| 131 | + |
| 132 | + XCTAssertEqual(index.centerChild(at: 11), H3Index(0x8b2a10766d80fff)) |
| 133 | + XCTAssertEqual(index.centerChild(at: 15), H3Index(0x8f2a10766d80000)) |
| 134 | + XCTAssertEqual(index.centerChild(at: 9), nil) |
| 135 | + } |
| 136 | + |
| 137 | + func testDirectCenterChild() { |
| 138 | + |
| 139 | + let index = H3Index(0x8a2a10766d87fff) |
| 140 | + |
| 141 | + XCTAssertEqual(index.directCenterChild, H3Index(0x8b2a10766d80fff)) |
| 142 | + XCTAssertEqual(index.directCenterChild, index.centerChild(at: index.resolution + 1)) |
| 143 | + |
| 144 | + let res15Index = index.centerChild(at: 15) |
| 145 | + |
| 146 | + XCTAssertEqual(res15Index!.directCenterChild, nil) |
10 | 147 | } |
11 | 148 | } |
0 commit comments