Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix blob equality
  • Loading branch information
jberkel committed Oct 23, 2022
commit 0715e9512daf86fe42e592b54eb633a049adb4b7
8 changes: 7 additions & 1 deletion Sources/SQLite/Core/Blob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public final class Blob {
}

public convenience init(bytes: [UInt8]) {
guard bytes.count > 0 else {
self.init(data: NSData())
return
}
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bytes.count)
for idx in 0..<bytes.count {
buffer.advanced(by: idx).pointee = bytes[idx]
Expand All @@ -54,10 +58,12 @@ public final class Blob {
}

public init(data: NSData) {
precondition(!(data is NSMutableData), "Blob cannot be initialized with mutable data")
self.data = data
}

public func toHex() -> String {
guard length > 0 else { return "" }
let bytes = bytes.assumingMemoryBound(to: UInt8.self)

var hex = ""
Expand Down Expand Up @@ -85,5 +91,5 @@ extension Blob: Equatable {
}

public func ==(lhs: Blob, rhs: Blob) -> Bool {
lhs.bytes == rhs.bytes
lhs.data == rhs.data
}
16 changes: 15 additions & 1 deletion Tests/SQLiteTests/Core/BlobTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ class BlobTests: XCTestCase {

func test_toHex() {
let blob = Blob(bytes: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 250, 255])

XCTAssertEqual(blob.toHex(), "000a141e28323c46505a6496faff")
}

func test_toHex_empty() {
let blob = Blob(bytes: [])
XCTAssertEqual(blob.toHex(), "")
}

func test_init_array() {
let blob = Blob(bytes: [42, 42, 42])
XCTAssertEqual(blob.byteArray, [42, 42, 42])
Expand All @@ -20,6 +24,16 @@ class BlobTests: XCTestCase {
let blob = Blob(bytes: pointer, length: 3)
XCTAssertEqual(blob.byteArray, [42, 42, 42])
}

func test_equality() {
let blob1 = Blob(bytes: [42, 42, 42])
let blob2 = Blob(bytes: [42, 42, 42])
let blob3 = Blob(bytes: [42, 42, 43])

XCTAssertEqual(Blob(bytes: []), Blob(bytes: []))
XCTAssertEqual(blob1, blob2)
XCTAssertNotEqual(blob1, blob3)
}
}

extension Blob {
Expand Down