Skip to content

Commit

Permalink
Update some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yaslab committed Jul 24, 2024
1 parent 010dce3 commit 9054b92
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
20 changes: 10 additions & 10 deletions Tests/HexTests/Collection+HexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,54 @@ import Testing
struct CollectionHexTests {
@Test func encodeIntoHexData() {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Collection<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexData = (bytes as any Collection<UInt8>).hexEncodedData()
let hexData = bytes.hexEncodedData()

// Assert
#expect(hexData == Data("000123456789abcdefff".utf8))
}

@Test func encodeIntoHexString() {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Collection<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexString = (bytes as any Collection<UInt8>).hexEncodedString()
let hexString = bytes.hexEncodedString()

// Assert
#expect(hexString == "000123456789abcdefff")
}

@Test func encodeIntoUppercaseString() throws {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Collection<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexString = (bytes as any Collection<UInt8>).hexEncodedString(options: .uppercase)
let hexString = bytes.hexEncodedString(options: .uppercase)

// Assert
#expect(hexString == "000123456789ABCDEFFF")
}

@Test func encodeFoobar() throws {
// Arrange
let data = Data("foobar".utf8)
let data: some Collection<UInt8> = Data("foobar".utf8)

// Act
let hexString = (data as any Collection<UInt8>).hexEncodedString(options: .uppercase)
let hexString = data.hexEncodedString(options: .uppercase)

// Assert
#expect(hexString == "666F6F626172")
}

@Test func encodeEmpty() {
// Arrange
let data = Data()
let data: some Collection<UInt8> = Data()

// Act
let hexString = (data as any Collection<UInt8>).hexEncodedString()
let hexString = data.hexEncodedString()

// Assert
#expect(hexString == "")
Expand Down
20 changes: 10 additions & 10 deletions Tests/HexTests/Sequence+HexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,54 @@ import Testing
struct SequenceHexTests {
@Test func encodeIntoHexData() {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Sequence<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexData = (bytes as any Sequence<UInt8>).hexEncodedData()
let hexData = bytes.hexEncodedData()

// Assert
#expect(hexData == Data("000123456789abcdefff".utf8))
}

@Test func encodeIntoHexString() {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Sequence<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexString = (bytes as any Sequence<UInt8>).hexEncodedString()
let hexString = bytes.hexEncodedString()

// Assert
#expect(hexString == "000123456789abcdefff")
}

@Test func encodeIntoUppercaseString() throws {
// Arrange
let bytes: [UInt8] = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]
let bytes: some Sequence<UInt8> = [0x00, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xff]

// Act
let hexString = (bytes as any Sequence<UInt8>).hexEncodedString(options: .uppercase)
let hexString = bytes.hexEncodedString(options: .uppercase)

// Assert
#expect(hexString == "000123456789ABCDEFFF")
}

@Test func encodeFoobar() throws {
// Arrange
let data = Data("foobar".utf8)
let data: some Sequence<UInt8> = Data("foobar".utf8)

// Act
let hexString = (data as any Sequence<UInt8>).hexEncodedString(options: .uppercase)
let hexString = data.hexEncodedString(options: .uppercase)

// Assert
#expect(hexString == "666F6F626172")
}

@Test func encodeEmpty() {
// Arrange
let data = Data()
let data: some Sequence<UInt8> = Data()

// Act
let hexString = (data as any Sequence<UInt8>).hexEncodedString()
let hexString = data.hexEncodedString()

// Assert
#expect(hexString == "")
Expand Down
42 changes: 21 additions & 21 deletions benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ cat << EOF > ./Sources/benchmark/main.swift
import Foundation
import Hex
let length = 5120
let loop = 25600
let _length = 5120
let _loop = 25600
let hexData = Data((0 ..< length).map({ _ in 0x30 }))
let hexString = String(repeating: "0", count: length)
let _hexData = Data((0 ..< _length).map({ _ in 0x30 }))
let _hexString = String(repeating: "0", count: _length)
let bytes = Data(count: length)
let _bytes = Data(count: _length)
print("method | time (s)")
print("--- | ---")
@inline(never)
func decodeData(_ n: Int) {
func decodeData(_ n: Int, hexData: Data) {
let start = Date()
for _ in 0 ..< n {
Expand All @@ -60,7 +60,7 @@ func decodeData(_ n: Int) {
}
@inline(never)
func decodeString(_ n: Int) {
func decodeString(_ n: Int, hexString: String) {
let start = Date()
for _ in 0 ..< n {
Expand All @@ -71,55 +71,55 @@ func decodeString(_ n: Int) {
}
@inline(never)
func encodeCollectionIntoData(_ n: Int) {
func encodeCollectionIntoData<T: Collection<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = (bytes as any Collection<UInt8>).hexEncodedData()
_ = bytes.hexEncodedData()
}
print("\`Collection.hexEncodedData()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeCollectionIntoString(_ n: Int) {
func encodeCollectionIntoString<T: Collection<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = (bytes as any Collection<UInt8>).hexEncodedString()
_ = bytes.hexEncodedString()
}
print("\`Collection.hexEncodedString()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeSequenceIntoData(_ n: Int) {
func encodeSequenceIntoData<T: Sequence<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = (bytes as any Sequence<UInt8>).hexEncodedData()
_ = bytes.hexEncodedData()
}
print("\`Sequence.hexEncodedData()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
@inline(never)
func encodeSequenceIntoString(_ n: Int) {
func encodeSequenceIntoString<T: Sequence<UInt8>>(_ n: Int, bytes: T) {
let start = Date()
for _ in 0 ..< n {
_ = (bytes as any Sequence<UInt8>).hexEncodedString()
_ = bytes.hexEncodedString()
}
print("\`Sequence.hexEncodedString()\` | " + String(format: "%.4f", Date().timeIntervalSince(start)))
}
decodeData(loop)
decodeString(loop)
encodeCollectionIntoData(loop)
encodeCollectionIntoString(loop)
encodeSequenceIntoData(loop)
encodeSequenceIntoString(loop)
decodeData(_loop, hexData: _hexData)
decodeString(_loop, hexString: _hexString)
encodeCollectionIntoData(_loop, bytes: _bytes)
encodeCollectionIntoString(_loop, bytes: _bytes)
encodeSequenceIntoData(_loop, bytes: _bytes)
encodeSequenceIntoString(_loop, bytes: _bytes)
EOF

swift package clean
Expand Down

0 comments on commit 9054b92

Please sign in to comment.