Skip to content

Commit

Permalink
Add fixed size array tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaRU committed Feb 2, 2024
1 parent 5474195 commit a5c6ad3
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Tests/CDRCodableTests/CDRCodableFixedArrayTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
45 changes: 45 additions & 0 deletions Tests/CDRCodableTests/CameraInfo.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
16 changes: 16 additions & 0 deletions Tests/CDRCodableTests/RosCommon.swift
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a5c6ad3

Please sign in to comment.