Skip to content

Commit

Permalink
update encoder test
Browse files Browse the repository at this point in the history
  • Loading branch information
MacOMNI committed Dec 5, 2024
1 parent 14f938c commit 114686f
Showing 1 changed file with 163 additions and 22 deletions.
185 changes: 163 additions & 22 deletions Codec/Tests/CodecTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,254 @@ import Testing

@testable import Codec

extension Double: EncodedSize, @retroactive Error {
public var encodedSize: Int {
MemoryLayout<Double>.size
}

public static var encodeedSizeHint: Int? {
MemoryLayout<Double>.size
}
}

extension Float: EncodedSize, @retroactive Error {
public var encodedSize: Int {
MemoryLayout<Float>.size
}

public static var encodeedSizeHint: Int? {
MemoryLayout<Float>.size
}
}

struct EncoderTests {
struct TestStruct: Codable {
let intValue: Int
let optionalIntValue: Int?
let optionalIntValue2: Int?
let stringValue: String
let optionalStringValue: String?
let optionalStringValue2: String?
let boolValue: Bool
let optionalBoolValue: Bool?
let optionalBoolValue2: Bool?
let uintValue: UInt
let optionalUintValue: UInt?
let optionalUintValue2: UInt?
let int8Value: Int8
let optionalInt8Value: Int8?
let optionalInt8Value2: Int8?
let uint8Value: UInt8
let optionalUint8Value: UInt8?
let optionalUint8Value2: UInt8?
let int16Value: Int16
let optionalInt16Value: Int16?
let optionalInt16Value2: Int16?
let uint16Value: UInt16
let optionalUint16Value: UInt16?
let optionalUint16Value2: UInt16?
let int32Value: Int32
let optionalInt32Value: Int32?
let optionalInt32Value2: Int32?
let uint32Value: UInt32
let optionalUint32Value: UInt32?
let optionalUint32Value2: UInt32?
let int64Value: Int64
let optionalInt64Value: Int64?
let optionalInt64Value2: Int64?
let uint64Value: UInt64
let optionalUint64Value: UInt64?
let optionalUint64Value2: UInt64?
}

@Test
func encodeDecodeFullStruct() throws {
let testObject = TestStruct(
intValue: 42,
optionalIntValue: 42,
optionalIntValue2: nil,
stringValue: "hello",
optionalStringValue: nil,
optionalStringValue2: "world",
boolValue: true,
optionalBoolValue: nil,
optionalBoolValue2: false,
uintValue: 99,
optionalUintValue: nil,
optionalUintValue2: 100,
int8Value: -12,
optionalInt8Value: nil,
optionalInt8Value2: 120,
uint8Value: 255,
optionalUint8Value: nil,
optionalUint8Value2: 128,
int16Value: -1234,
optionalInt16Value: nil,
optionalInt16Value2: 1234,
uint16Value: 65535,
optionalUint16Value: nil,
optionalUint16Value2: 32767,
int32Value: -123_456,
optionalInt32Value: nil,
optionalInt32Value2: 123_456,
uint32Value: 4_294_967_295,
optionalUint32Value: nil,
optionalUint32Value2: 2_147_483_647,
int64Value: -1_234_567_890_123,
optionalInt64Value: nil,
optionalInt64Value2: 1_234_567_890_123,
uint64Value: 18_446_744_073_709_551_615,
optionalUint64Value: nil
optionalUint64Value: nil,
optionalUint64Value2: 9_223_372_036_854_775_807
)

let encoded = try JamEncoder.encode(testObject)

#expect(encoded.count > 0)

let decoded = try JamDecoder.decode(TestStruct.self, from: encoded)

#expect(decoded.intValue == testObject.intValue)
#expect(decoded.optionalIntValue == testObject.optionalIntValue)
#expect(decoded.optionalIntValue2 == testObject.optionalIntValue2)
#expect(decoded.stringValue == testObject.stringValue)
#expect(decoded.optionalStringValue == testObject.optionalStringValue)
#expect(decoded.optionalStringValue2 == testObject.optionalStringValue2)
#expect(decoded.boolValue == testObject.boolValue)
#expect(decoded.optionalBoolValue == testObject.optionalBoolValue)
#expect(decoded.optionalBoolValue2 == testObject.optionalBoolValue2)
#expect(decoded.uintValue == testObject.uintValue)
#expect(decoded.optionalUintValue == testObject.optionalUintValue)
#expect(decoded.optionalUintValue2 == testObject.optionalUintValue2)
#expect(decoded.int8Value == testObject.int8Value)
#expect(decoded.optionalInt8Value == testObject.optionalInt8Value)
#expect(decoded.optionalInt8Value2 == testObject.optionalInt8Value2)
#expect(decoded.uint8Value == testObject.uint8Value)
#expect(decoded.optionalUint8Value == testObject.optionalUint8Value)
#expect(decoded.optionalUint8Value2 == testObject.optionalUint8Value2)
#expect(decoded.int16Value == testObject.int16Value)
#expect(decoded.optionalInt16Value == testObject.optionalInt16Value)
#expect(decoded.optionalInt16Value2 == testObject.optionalInt16Value2)
#expect(decoded.uint16Value == testObject.uint16Value)
#expect(decoded.optionalUint16Value == testObject.optionalUint16Value)
#expect(decoded.optionalUint16Value2 == testObject.optionalUint16Value2)
#expect(decoded.int32Value == testObject.int32Value)
#expect(decoded.optionalInt32Value == testObject.optionalInt32Value)
#expect(decoded.optionalInt32Value2 == testObject.optionalInt32Value2)
#expect(decoded.uint32Value == testObject.uint32Value)
#expect(decoded.optionalUint32Value == testObject.optionalUint32Value)
#expect(decoded.optionalUint32Value2 == testObject.optionalUint32Value2)
#expect(decoded.int64Value == testObject.int64Value)
#expect(decoded.optionalInt64Value == testObject.optionalInt64Value)
#expect(decoded.optionalInt64Value2 == testObject.optionalInt64Value2)
#expect(decoded.uint64Value == testObject.uint64Value)
#expect(decoded.optionalUint64Value == testObject.optionalUint64Value)
#expect(decoded.optionalUint64Value2 == testObject.optionalUint64Value2)
}

@Test func encodeOpStruct() throws {
struct DoubleStruct: Codable {
let value: Double
}
struct OpDoubleStruct: Codable {
let value: Double?
}
struct FloatStruct: Codable {
let value: Float
}
struct OpFloatStruct: Codable {
let value: Float?
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(DoubleStruct(value: 0))
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(OpDoubleStruct(value: 0))
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(OpFloatStruct(value: 0))
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(FloatStruct(value: 0))
}

struct DoubleTest: Encodable {
let doubleValue: Double
func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(doubleValue)
}

Check warning on line 161 in Codec/Tests/CodecTests/EncoderTests.swift

View check run for this annotation

Codecov / codecov/patch

Codec/Tests/CodecTests/EncoderTests.swift#L161

Added line #L161 was not covered by tests
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(DoubleTest(doubleValue: 0))
}

struct FloatTest: Encodable {
let floatValue: Float
func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(floatValue)
}

Check warning on line 172 in Codec/Tests/CodecTests/EncoderTests.swift

View check run for this annotation

Codecov / codecov/patch

Codec/Tests/CodecTests/EncoderTests.swift#L172

Added line #L172 was not covered by tests
}
#expect(throws: Error.self) {
_ = try JamEncoder.encode(FloatTest(floatValue: 0))
}
}

struct UnkeyedTest: Encodable, Decodable {
let boolValue: Bool
let stringValue: String
let intValue: Int
let int8Value: Int8
let int16Value: Int16
let int32Value: Int32
let int64Value: Int64
let uintValue: UInt
let uint8Value: UInt8
let uint16Value: UInt16
let uint32Value: UInt32
let uint64Value: UInt64
let dataValue: Data
let nestedValues: [UnkeyedTest]

func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(boolValue)
try container.encode(stringValue)
try container.encode(intValue)
try container.encode(int8Value)
try container.encode(int16Value)
try container.encode(int32Value)
try container.encode(int64Value)
try container.encode(uintValue)
try container.encode(uint8Value)
try container.encode(uint16Value)
try container.encode(uint32Value)
try container.encode(uint64Value)
try container.encode(dataValue)
try container.encode(nestedValues)
}
}

@Test func testUnkeyedContainer() throws {
let testData = UnkeyedTest(
boolValue: true,
stringValue: "Hello",
intValue: 42,
int8Value: Int8(8),
int16Value: Int16(16),
int32Value: Int32(32),
int64Value: Int64(64),
uintValue: UInt(128),
uint8Value: UInt8(8),
uint16Value: UInt16(16),
uint32Value: UInt32(32),
uint64Value: UInt64(64),
dataValue: Data([0x01, 0x02, 0x03]),
nestedValues: [
UnkeyedTest(
boolValue: false,
stringValue: "Nested",
intValue: 99,
int8Value: 1,
int16Value: 2,
int32Value: 3,
int64Value: 4,
uintValue: 5,
uint8Value: 6,
uint16Value: 7,
uint32Value: 8,
uint64Value: 9,
dataValue: Data([0x01]),
nestedValues: []
),
]
)

let encoded = try JamEncoder.encode(testData)
#expect(encoded.count > 0)
let decoded = try JamDecoder.decode(UnkeyedTest.self, from: encoded)
#expect(testData.intValue == decoded.intValue)
#expect(testData.dataValue == decoded.dataValue)
}

@Test func encodeDouble() throws {
Expand Down

0 comments on commit 114686f

Please sign in to comment.