Skip to content

Commit

Permalink
update encode & decode
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Dec 9, 2024
1 parent 37c63bd commit 0dbb184
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
12 changes: 10 additions & 2 deletions Codec/Tests/CodecTests/DecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ struct DecoderTests {
}
}

@Test func decodeInvalidLength() throws {
@Test func decodeInvalidInt() throws {
let encoded16 = try JamEncoder.encode(0xFFFF_FFFF)
#expect(throws: Error.self) {
_ = try JamDecoder.decode(UInt16.self, from: encoded16)
}

let maxLength: UInt64 = 0x1_0000_0000
let encoded = try JamEncoder.encode(maxLength)
#expect(encoded.count == 8)
Expand Down Expand Up @@ -51,8 +56,11 @@ struct DecoderTests {
@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")

#expect(throws: Error.self) {
_ = try JamDecoder.decode(String.self, from: Data([6, 104, 101, 108, 108, 111]))
}
}

@Test func decodeArray() throws {
Expand Down
15 changes: 10 additions & 5 deletions Codec/Tests/CodecTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ struct EncoderTests {
@Test func encodeString() throws {
let stringValue = "hello"
let encoded = try JamEncoder.encode(stringValue)

#expect(encoded == Data([5, 104, 101, 108, 108, 111])) // Length prefix of 5 bytes and UTF-8 encoding of "hello"
#expect(encoded == Data([5, 104, 101, 108, 108, 111]))
}

@Test func encodeArray() throws {
Expand Down Expand Up @@ -406,22 +405,28 @@ struct EncoderTests {
let uint16Value: UInt16 = 65535
let nilValue: Int64? = nil

let encodedNil = try JamEncoder.encode(nilValue)
#expect(encodedNil == Data([0]))
try #expect(JamDecoder.decode(Int64?.self, from: encodedNil) == nilValue)

let encodedInt16 = try JamEncoder.encode(int16Value)
#expect(encodedInt16 == Data([1, 0]))
try #expect(JamDecoder.decode(Int16.self, from: encodedInt16) == int16Value)

let encodedInt32 = try JamEncoder.encode(int32Value)
#expect(encodedInt32 == Data([1, 0, 0, 0]))
try #expect(JamDecoder.decode(Int32.self, from: encodedInt32) == int32Value)

let encodedInt64 = try JamEncoder.encode(int64Value)
#expect(encodedInt64 == Data([1, 0, 0, 0, 0, 0, 0, 0]))
try #expect(JamDecoder.decode(Int64.self, from: encodedInt64) == int64Value)

let encodedUInt = try JamEncoder.encode(uintValue)
#expect(encodedUInt == Data([255, 255, 0, 0, 0, 0, 0, 0]))
try #expect(JamDecoder.decode(UInt.self, from: encodedUInt) == uintValue)

let encodedUInt16 = try JamEncoder.encode(uint16Value)
#expect(encodedUInt16 == Data([255, 255]))

let encodedNil = try JamEncoder.encode(nilValue)
#expect(encodedNil == Data([0]))
try #expect(JamDecoder.decode(UInt16.self, from: encodedUInt16) == uint16Value)
}
}
4 changes: 4 additions & 0 deletions Codec/Tests/CodecTests/IntegerCodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ import Testing
}

@Test func multipleDecodes() throws {
var largeData = Data([254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254])
#expect(throws: Error.self) {
_ = try largeData.decodeUInt64()
}
var data = Data([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
#expect(data.decode(length: 8) as UInt64? == 0x0706_0504_0302_0100)
#expect(data.decode(length: 4) as UInt32? == 0x0B0A_0908)
Expand Down
15 changes: 15 additions & 0 deletions Codec/Tests/CodecTests/SortedKeyValuesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import Testing

@testable import Codec

struct SortedKeyValuesTests {
@Test func encode() throws {
let testCase: SortedKeyValues<UInt8, UInt8> = SortedKeyValues(alias: [1: 1, 2: 2, 3: 3])
let encoded = try JamEncoder.encode(testCase)
#expect(encoded == Data([3, 1, 1, 2, 2, 3, 3]))
#expect(throws: DecodingError.self) {
_ = try JamDecoder.decode(SortedKeyValues<UInt8, UInt8>.self, from: Data([3, 3, 3, 2, 2, 1, 1]))
}
}
}
3 changes: 3 additions & 0 deletions Codec/Tests/CodecTests/SortedSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ struct SortedSetTests {
@Test func decode() throws {
let decoded = try JamDecoder.decode(SortedSet<UInt8>.self, from: Data([3, 1, 2, 3]))
#expect(decoded.alias == [1, 2, 3])
#expect(throws: DecodingError.self) {
_ = try JamDecoder.decode(SortedSet<UInt8>.self, from: Data([3, 2, 1, 3]))
}
}

@Test func invalidData() throws {
Expand Down

0 comments on commit 0dbb184

Please sign in to comment.