Add more tests

This commit is contained in:
Yasuhiro Hatta 2019-01-13 21:08:17 +09:00
parent 7bc4040dd9
commit 1823b71a51
4 changed files with 31 additions and 5 deletions

View File

@ -43,7 +43,7 @@ extension Data {
base32String.removeLast()
}
let result: Data? = base32String.withCString(encodedAs: Unicode.ASCII.self) { (src) in
let result: Data? = base32String.withCString(encodedAs: Unicode.UTF8.self) { (src) in
func _strlen(_ str: UnsafePointer<UInt8>) -> Int {
var str = str
var i = 0

View File

@ -31,7 +31,9 @@ public struct ULID: Hashable, Equatable, Comparable, CustomStringConvertible {
guard string.count == 26, let data = Data(base32Encoded: "000000" + string) else {
return nil
}
self.init(ulidData: data.dropFirst(4))
withUnsafeMutableBytes(of: &ulid) {
$0.copyBytes(from: data.dropFirst(4))
}
}
public init(timestamp: Date = Date()) {

View File

@ -134,6 +134,15 @@ final class Base32Tests: XCTestCase {
XCTAssertEqual("GGGGGGG=", data.base32EncodedString())
}
func testEncodeNoPad() {
let bytes: [UInt8] = [
0b10000100
]
let data = Data(bytes: bytes)
XCTAssertEqual("GG", data.base32EncodedString(padding: false))
}
// MARK: -
// MARK: Decode
@ -203,6 +212,18 @@ final class Base32Tests: XCTestCase {
}
}
func testDecodePadding() {
let data = Data(base32Encoded: "AM======")
XCTAssertNotNil(data)
XCTAssertEqual(1, data!.count)
XCTAssertEqual(0b01010101, data![0])
}
func testDecodeIncorrectLength() {
let data = Data(base32Encoded: "0")
XCTAssertNil(data)
}
// MARK: -
static var allTests = [
@ -219,9 +240,12 @@ final class Base32Tests: XCTestCase {
("testEncodePad2", testEncodePad2),
("testEncodePad3", testEncodePad3),
("testEncodePad4", testEncodePad4),
("testEncodeNoPad", testEncodeNoPad),
("testDecodeBase32", testDecodeBase32),
("testDecodeTable", testDecodeTable),
("testDecodeInvalidCharacter", testDecodeInvalidCharacter)
("testDecodeInvalidCharacter", testDecodeInvalidCharacter),
("testDecodePadding", testDecodePadding),
("testDecodeIncorrectLength", testDecodeIncorrectLength)
]
}

View File

@ -130,13 +130,13 @@ final class ULIDTests: XCTestCase {
func testComparable1() {
let lhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
let rhs = ULID(ulid: (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
let rhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1))
XCTAssertTrue(lhs < rhs)
XCTAssertFalse(lhs > rhs)
}
func testComparable2() {
let lhs = ULID(ulid: (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
let lhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1))
let rhs = ULID(ulid: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
XCTAssertFalse(lhs < rhs)
XCTAssertTrue(lhs > rhs)