-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
283 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Codec | ||
|
||
struct DecoderTests { | ||
@Test func decodeData() throws { | ||
let encodedData = Data([3, 0, 1, 2]) | ||
let decoded = try JamDecoder.decode(Data.self, from: encodedData) | ||
|
||
#expect(decoded == Data([0, 1, 2])) | ||
} | ||
|
||
@Test func decodeBool() throws { | ||
let encodedTrue = Data([1]) | ||
let encodedFalse = Data([0]) | ||
|
||
let decodedTrue = try JamDecoder.decode(Bool.self, from: encodedTrue) | ||
let decodedFalse = try JamDecoder.decode(Bool.self, from: encodedFalse) | ||
|
||
#expect(decodedTrue == true) | ||
#expect(decodedFalse == false) | ||
} | ||
|
||
@Test func decodeString() throws { | ||
let encoded = Data([5, 104, 101, 108, 108, 111]) | ||
let decoded = try JamDecoder.decode(String.self, from: encoded) | ||
|
||
#expect(decoded == "hello") | ||
} | ||
|
||
@Test func decodeArray() throws { | ||
let encoded = Data([3, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0]) | ||
let decoded = try JamDecoder.decode([Int].self, from: encoded) | ||
|
||
#expect(decoded == [1, 2, 3]) | ||
} | ||
|
||
@Test func decodeInt() throws { | ||
let encoded = Data([1, 0, 0, 0, 0, 0, 0, 0]) | ||
let decoded = try JamDecoder.decode(Int.self, from: encoded) | ||
|
||
#expect(decoded == 1) | ||
} | ||
|
||
@Test func decodeOptional() throws { | ||
let encodedSome = Data([1, 1, 0, 0, 0, 0, 0, 0, 0]) | ||
let encodedNone = Data([0]) | ||
|
||
let decodedSome = try JamDecoder.decode(Int?.self, from: encodedSome) | ||
let decodedNone = try JamDecoder.decode(Int?.self, from: encodedNone) | ||
|
||
#expect(decodedSome == .some(1)) | ||
#expect(decodedNone == .none) | ||
} | ||
|
||
@Test func decodeFixedWidthInteger() throws { | ||
let encodedInt8 = Data([251]) | ||
let encodedUInt64 = Data([21, 205, 91, 7, 0, 0, 0, 0]) | ||
|
||
let decodedInt8 = try JamDecoder.decode(Int8.self, from: encodedInt8) | ||
let decodedUInt64 = try JamDecoder.decode(UInt64.self, from: encodedUInt64) | ||
|
||
#expect(decodedInt8 == -5) | ||
#expect(decodedUInt64 == 123_456_789) | ||
} | ||
|
||
@Test func decodeInvalidData() throws { | ||
let invalidEncodedData = Data([0, 0, 0, 123]) | ||
#expect(throws: Error.self) { | ||
_ = try JamDecoder.decode(Int8.self, from: invalidEncodedData) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Codec | ||
|
||
extension Int: EncodedSize, @retroactive Error { | ||
public var encodedSize: Int { | ||
MemoryLayout<Int>.size | ||
} | ||
|
||
public static var encodeedSizeHint: Int? { | ||
MemoryLayout<Int>.size | ||
} | ||
} | ||
|
||
struct EncodeSizeTests { | ||
@Test | ||
func encodeFixedWidthInteger() throws { | ||
#expect(Int(42).encodedSize == MemoryLayout<Int>.size) | ||
#expect(Int8(-5).encodedSize == MemoryLayout<Int8>.size) | ||
#expect(UInt32(123_456).encodedSize == MemoryLayout<UInt32>.size) | ||
#expect(Int.encodeedSizeHint == MemoryLayout<Int>.size) | ||
#expect(Int8.encodeedSizeHint == MemoryLayout<Int8>.size) | ||
#expect(UInt32.encodeedSizeHint == MemoryLayout<UInt32>.size) | ||
} | ||
|
||
@Test | ||
func encodeBool() throws { | ||
#expect(true.encodedSize == 1) | ||
#expect(false.encodedSize == 1) | ||
#expect(Bool.encodeedSizeHint == 1) | ||
} | ||
|
||
@Test | ||
func encodeStringAndData() throws { | ||
#expect("test".encodedSize == 4) | ||
#expect("".encodedSize == 0) | ||
#expect(Data([0x01, 0x02, 0x03]).encodedSize == 4) | ||
#expect(Data().encodedSize == 1) | ||
#expect(String.encodeedSizeHint == nil) | ||
#expect(Data.encodeedSizeHint == nil) | ||
} | ||
|
||
@Test | ||
func encodeArrayAndSet() throws { | ||
let intArray = [1, 2, 3] | ||
let emptyArray: [Int] = [] | ||
let intSet: Set<Int> = [4, 5, 6] | ||
let emptySet: Set<Int> = [] | ||
|
||
#expect(intArray.encodedSize == UInt32(3).variableEncodingLength() + 3 * MemoryLayout<Int>.size) | ||
#expect(emptyArray.encodedSize == UInt32(0).variableEncodingLength()) | ||
#expect(intSet.encodedSize >= UInt32(3).variableEncodingLength()) | ||
#expect(emptySet.encodedSize == UInt32(0).variableEncodingLength()) | ||
#expect([Int].encodeedSizeHint == nil) | ||
#expect(Set<Int>.encodeedSizeHint == nil) | ||
} | ||
|
||
@Test | ||
func encodeDictionary() throws { | ||
let dict: [Int: String] = [1: "one", 2: "two"] | ||
let emptyDict: [Int: String] = [:] | ||
|
||
let expectedSize = UInt32(2).variableEncodingLength() + | ||
1.encodedSize + "one".encodedSize + | ||
1.encodedSize + "two".encodedSize | ||
|
||
#expect(dict.encodedSize == expectedSize) | ||
#expect(emptyDict.encodedSize == UInt32(0).variableEncodingLength()) | ||
#expect([Int: String].encodeedSizeHint == nil) | ||
} | ||
|
||
@Test | ||
func encodeOptional() throws { | ||
let someValue: Int? = 42 | ||
let noneValue: Int? = nil | ||
|
||
#expect(someValue.encodedSize == 1 + MemoryLayout<Int>.size) | ||
#expect(noneValue.encodedSize == 1) | ||
#expect(Int?.encodeedSizeHint == nil) | ||
} | ||
|
||
@Test | ||
func encodeResult() throws { | ||
let successResult: Result<String, Int> = .success("OK") | ||
let failureResult: Result<String, Int> = .failure(404) | ||
|
||
#expect(successResult.encodedSize == 1 + "OK".encodedSize) | ||
#expect(failureResult.encodedSize == 1 + MemoryLayout<Int>.size) | ||
#expect(Result<String, Int>.encodeedSizeHint == nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.