Making code a little bit leaner & added ipv6 tests.

Signed-off-by: Adam Rocska <adam.rocska@adams.solutions>
This commit is contained in:
Adam Rocska 2020-05-13 17:09:11 +02:00
parent 64ff06cc22
commit 59d72720e7
2 changed files with 7 additions and 10 deletions

View File

@ -14,11 +14,7 @@ public class InMemoryIndex<Pointer>: Index where Pointer: UnsignedInteger, Point
required public convenience init(metadata: Metadata, stream createStream: @autoclosure () -> InputStream) {
precondition(
metadata.recordSize < (MemoryLayout<Pointer>.size * 8),
"""
The byte size of a record described by the metadata won't fit the memory layout of type \(Pointer.self).
: Metadata Record Size : \(metadata.recordSize) bits
: Memory Layout Size of \(Pointer.self) : \(MemoryLayout<Pointer>.size) bytes.\n
"""
"Can't fit record size of \(metadata.recordSize) bits in \(Pointer.self)'s \(MemoryLayout<Pointer>.size) bytes."
)
let stream = createStream()
precondition(stream.streamStatus == Stream.Status.notOpen, "should have provided an untouched stream.")
@ -69,7 +65,6 @@ public class InMemoryIndex<Pointer>: Index where Pointer: UnsignedInteger, Point
return pointer
}
}
return nil
}

View File

@ -25,10 +25,12 @@ class InMemoryIndexTest: XCTestCase {
stream: InputStream(fileAtPath: InMemoryIndexTest.countryFilePath)!
)
XCTAssertEqual(628171, index.lookup(.v4("80.99.18.166")))
XCTAssertEqual(618846, index.lookup(.v4("202.108.22.220")))
XCTAssertNil(index.lookup(.v4("0.0.0.0")))
XCTAssertNil(index.lookup(.v4("255.255.255.255")))
XCTAssertEqual(628171, index.lookup(IpAddress("80.99.18.166")))
XCTAssertEqual(618846, index.lookup(IpAddress("202.108.22.220")))
XCTAssertNil(index.lookup(IpAddress("0.0.0.0")))
XCTAssertNil(index.lookup(IpAddress("255.255.255.255")))
XCTAssertNil(index.lookup(IpAddress("2001:db8::8a2e:370:7334")))
XCTAssertEqual(627015, index.lookup(IpAddress("2001:428:4c06:f000::280")))
}
}