From a5c6ad3d94bc1ac1938f15bc7e491e30f066e5c8 Mon Sep 17 00:00:00 2001 From: Dmitriy Borovikov Date: Fri, 2 Feb 2024 20:36:55 +0300 Subject: [PATCH] Add fixed size array tests --- .../CDRCodableFixedArrayTests.swift | 71 +++++++++++++++++++ Tests/CDRCodableTests/CameraInfo.swift | 45 ++++++++++++ Tests/CDRCodableTests/RosCommon.swift | 16 +++++ 3 files changed, 132 insertions(+) create mode 100644 Tests/CDRCodableTests/CDRCodableFixedArrayTests.swift create mode 100644 Tests/CDRCodableTests/CameraInfo.swift create mode 100644 Tests/CDRCodableTests/RosCommon.swift diff --git a/Tests/CDRCodableTests/CDRCodableFixedArrayTests.swift b/Tests/CDRCodableTests/CDRCodableFixedArrayTests.swift new file mode 100644 index 0000000..bab68dd --- /dev/null +++ b/Tests/CDRCodableTests/CDRCodableFixedArrayTests.swift @@ -0,0 +1,71 @@ +import XCTest +@testable import CDRCodable + +class CDRCodableFixedArrayTests: XCTestCase { + let testDump: [UInt8] = +[0x2d, 0x67, 0xb1, 0x65, 0x5a, 0x4a, 0xa0, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x72, 0x61, 0x65, 0x5f, + 0x72, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x63, 0x61, 0x6d, 0x65, 0x72, 0x61, 0x5f, 0x6f, 0x70, 0x74, + 0x69, 0x63, 0x61, 0x6c, 0x5f, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00, + 0x80, 0x02, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, + 0x5f, 0x70, 0x6f, 0x6c, 0x79, 0x6e, 0x6f, 0x6d, 0x69, 0x61, 0x6c, 0x00, 0x08, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xc0, 0x61, 0xbd, 0x71, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xcc, 0x22, 0x74, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0xc0, 0x61, 0xbd, 0x71, 0x40, 0x00, 0x00, 0x00, 0xe0, 0xe4, 0x98, 0x68, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, + 0x00, 0x00, 0x00, 0xa0, 0x68, 0xee, 0x71, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x20, 0xbc, 0x9a, 0x74, 0x40, 0x29, 0x8c, 0x51, 0x7b, 0xef, 0x7c, 0x35, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x68, 0xee, 0x71, 0x40, + 0x00, 0x00, 0x00, 0xc0, 0xdb, 0x4d, 0x6a, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ] + + var decoder: CDRDecoder! + var encoder: CDREncoder! + + override func setUp() { + decoder = CDRDecoder() + encoder = CDREncoder() + } + + func testFixedSizeArrayDecodeEncode() { + + let cameraInfo = CameraInfo( + header: RosHeader(stamp: RosTimeStamp(sec: 1706125101, + nanosec: 513821274), + frameId: "rae_right_camera_optical_frame"), + height: 400, + width: 640, + distortion_model: "rational_polynomial", + d: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + k: [283.83636474609375, 0.0, 322.1748046875, 0.0, 283.83636474609375, 196.77793884277344, 0.0, 0.0, 1.0], + r: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], + p: [286.9005432128906, 0.0, 329.6709289550781, 21.488029200944002, 0.0, 286.9005432128906, 210.43307495117188, 0.0, 0.0, 0.0, 1.0, 0.0], + binningX: 0, + binningY: 0, + roi: CameraInfo.RegionOfInterest(x_offset: 0, + y_offset: 0, + height: 0, + width: 0, + do_rectify: false) + ) + + + let data = Data(testDump) + let value = try! decoder.decode(CameraInfo.self, from: data) + XCTAssertEqual(value, cameraInfo) + + let dataBack = try! encoder.encode(value) + XCTAssertEqual(dataBack, data) + } +} diff --git a/Tests/CDRCodableTests/CameraInfo.swift b/Tests/CDRCodableTests/CameraInfo.swift new file mode 100644 index 0000000..d95bbdd --- /dev/null +++ b/Tests/CDRCodableTests/CameraInfo.swift @@ -0,0 +1,45 @@ +///// +//// CameraInfo.swift +/// Copyright © 2024 Dmitriy Borovikov. All rights reserved. +// + +import Foundation + +struct CameraInfo: Codable, Equatable { + struct RegionOfInterest: Codable, Equatable { + let x_offset: UInt32 // (0 if the ROI includes the left edge of the image) + let y_offset: UInt32 // (0 if the ROI includes the top edge of the image) + let height: UInt32 // + let width: UInt32 // + let do_rectify: Bool + } + + let header: RosHeader + let height: UInt32 + let width: UInt32 + let distortion_model: String + let d: [Double] + + let k: [Double] // 9 + let r: [Double] // 9 + let p: [Double] // 12 + + let binningX: UInt32 + let binningY: UInt32 + let roi: RegionOfInterest + + // Fixed size array discriminators + enum CodingKeys: Int, CodingKey { + case header = 0 + case height = 1 + case width = 2 + case distortion_model = 3 + case d = 4 + case k = 0x90005 + case r = 0x90006 + case p = 0xc0007 + case binningX = 8 + case binningY = 9 + case roi = 10 + } +} diff --git a/Tests/CDRCodableTests/RosCommon.swift b/Tests/CDRCodableTests/RosCommon.swift new file mode 100644 index 0000000..2340dea --- /dev/null +++ b/Tests/CDRCodableTests/RosCommon.swift @@ -0,0 +1,16 @@ +///// +//// RosCommon.swift +/// Copyright © 2024 Dmitriy Borovikov. All rights reserved. +// + +import Foundation + +struct RosTimeStamp: Codable, Equatable { + let sec: Int32 + let nanosec: UInt32 +} + +struct RosHeader: Codable, Equatable { + let stamp: RosTimeStamp + let frameId: String +}