Skip to content

Commit a5c6ad3

Browse files
committed
Add fixed size array tests
1 parent 5474195 commit a5c6ad3

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import XCTest
2+
@testable import CDRCodable
3+
4+
class CDRCodableFixedArrayTests: XCTestCase {
5+
let testDump: [UInt8] =
6+
[0x2d, 0x67, 0xb1, 0x65, 0x5a, 0x4a, 0xa0, 0x1e, 0x1f, 0x00, 0x00, 0x00, 0x72, 0x61, 0x65, 0x5f,
7+
0x72, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x63, 0x61, 0x6d, 0x65, 0x72, 0x61, 0x5f, 0x6f, 0x70, 0x74,
8+
0x69, 0x63, 0x61, 0x6c, 0x5f, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x00, 0x00, 0x90, 0x01, 0x00, 0x00,
9+
0x80, 0x02, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c,
10+
0x5f, 0x70, 0x6f, 0x6c, 0x79, 0x6e, 0x6f, 0x6d, 0x69, 0x61, 0x6c, 0x00, 0x08, 0x00, 0x00, 0x00,
11+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15+
0x00, 0x00, 0x00, 0xc0, 0x61, 0xbd, 0x71, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16+
0x00, 0x00, 0x00, 0x00, 0xcc, 0x22, 0x74, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17+
0x00, 0x00, 0x00, 0xc0, 0x61, 0xbd, 0x71, 0x40, 0x00, 0x00, 0x00, 0xe0, 0xe4, 0x98, 0x68, 0x40,
18+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
20+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
22+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f,
24+
0x00, 0x00, 0x00, 0xa0, 0x68, 0xee, 0x71, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25+
0x00, 0x00, 0x00, 0x20, 0xbc, 0x9a, 0x74, 0x40, 0x29, 0x8c, 0x51, 0x7b, 0xef, 0x7c, 0x35, 0x40,
26+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x68, 0xee, 0x71, 0x40,
27+
0x00, 0x00, 0x00, 0xc0, 0xdb, 0x4d, 0x6a, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
32+
33+
var decoder: CDRDecoder!
34+
var encoder: CDREncoder!
35+
36+
override func setUp() {
37+
decoder = CDRDecoder()
38+
encoder = CDREncoder()
39+
}
40+
41+
func testFixedSizeArrayDecodeEncode() {
42+
43+
let cameraInfo = CameraInfo(
44+
header: RosHeader(stamp: RosTimeStamp(sec: 1706125101,
45+
nanosec: 513821274),
46+
frameId: "rae_right_camera_optical_frame"),
47+
height: 400,
48+
width: 640,
49+
distortion_model: "rational_polynomial",
50+
d: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
51+
k: [283.83636474609375, 0.0, 322.1748046875, 0.0, 283.83636474609375, 196.77793884277344, 0.0, 0.0, 1.0],
52+
r: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
53+
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],
54+
binningX: 0,
55+
binningY: 0,
56+
roi: CameraInfo.RegionOfInterest(x_offset: 0,
57+
y_offset: 0,
58+
height: 0,
59+
width: 0,
60+
do_rectify: false)
61+
)
62+
63+
64+
let data = Data(testDump)
65+
let value = try! decoder.decode(CameraInfo.self, from: data)
66+
XCTAssertEqual(value, cameraInfo)
67+
68+
let dataBack = try! encoder.encode(value)
69+
XCTAssertEqual(dataBack, data)
70+
}
71+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/////
2+
//// CameraInfo.swift
3+
/// Copyright © 2024 Dmitriy Borovikov. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
struct CameraInfo: Codable, Equatable {
9+
struct RegionOfInterest: Codable, Equatable {
10+
let x_offset: UInt32 // (0 if the ROI includes the left edge of the image)
11+
let y_offset: UInt32 // (0 if the ROI includes the top edge of the image)
12+
let height: UInt32 //
13+
let width: UInt32 //
14+
let do_rectify: Bool
15+
}
16+
17+
let header: RosHeader
18+
let height: UInt32
19+
let width: UInt32
20+
let distortion_model: String
21+
let d: [Double]
22+
23+
let k: [Double] // 9
24+
let r: [Double] // 9
25+
let p: [Double] // 12
26+
27+
let binningX: UInt32
28+
let binningY: UInt32
29+
let roi: RegionOfInterest
30+
31+
// Fixed size array discriminators
32+
enum CodingKeys: Int, CodingKey {
33+
case header = 0
34+
case height = 1
35+
case width = 2
36+
case distortion_model = 3
37+
case d = 4
38+
case k = 0x90005
39+
case r = 0x90006
40+
case p = 0xc0007
41+
case binningX = 8
42+
case binningY = 9
43+
case roi = 10
44+
}
45+
}

Tests/CDRCodableTests/RosCommon.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/////
2+
//// RosCommon.swift
3+
/// Copyright © 2024 Dmitriy Borovikov. All rights reserved.
4+
//
5+
6+
import Foundation
7+
8+
struct RosTimeStamp: Codable, Equatable {
9+
let sec: Int32
10+
let nanosec: UInt32
11+
}
12+
13+
struct RosHeader: Codable, Equatable {
14+
let stamp: RosTimeStamp
15+
let frameId: String
16+
}

0 commit comments

Comments
 (0)