11
11
#include < memory>
12
12
13
13
#include < util/CBORTestUtil.h>
14
+ #include < CborEncoder.h>
14
15
#include < MessageEncoder.h>
16
+ #include < catch2/matchers/catch_matchers_vector.hpp>
15
17
16
18
/* *****************************************************************************
17
19
TEST CODE
@@ -34,23 +36,22 @@ SCENARIO("Test the encoding of command messages") {
34
36
CBORMessageEncoder encoder;
35
37
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
36
38
37
- uint8_t expected_result[] = {
38
- 0xda , 0x00 , 0x01 , 0x00 , 0x00 , 0x81 , 0x58 , 0x20 ,
39
- 0x01 , 0x02 , 0x03 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 ,
40
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
41
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
42
- 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
43
- };
44
-
45
39
// Test the encoding is
46
40
// DA 00010000 # tag(65536)
47
41
// 81 # array(1)
48
42
// 58 20 # bytes(32)
49
43
// 01020304
50
44
THEN (" The encoding is successful" ) {
51
45
REQUIRE (err == Encoder::Status::Complete);
52
- REQUIRE (bytes_encoded == sizeof (expected_result));
53
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
46
+ std::vector<int > res (buffer, buffer+bytes_encoded);
47
+
48
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
49
+ 0xda , 0x00 , 0x01 , 0x00 , 0x00 , 0x81 , 0x58 , 0x20 ,
50
+ 0x01 , 0x02 , 0x03 , 0x04 , 0x00 , 0x00 , 0x00 , 0x00 ,
51
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
52
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
53
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
54
+ }));
54
55
}
55
56
}
56
57
@@ -71,11 +72,6 @@ SCENARIO("Test the encoding of command messages") {
71
72
CBORMessageEncoder encoder;
72
73
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
73
74
74
- uint8_t expected_result[] = {
75
- 0xda , 0x00 , 0x01 , 0x03 , 0x00 , 0x81 , 0x68 , 0x74 ,
76
- 0x68 , 0x69 , 0x6e , 0x67 , 0x5f , 0x69 , 0x64
77
- };
78
-
79
75
// Test the encoding is
80
76
// DA 00010300 # tag(66304)
81
77
// 81 # array(1)
@@ -84,8 +80,12 @@ SCENARIO("Test the encoding of command messages") {
84
80
85
81
THEN (" The encoding is successful" ) {
86
82
REQUIRE (err == Encoder::Status::Complete);
87
- REQUIRE (bytes_encoded == sizeof (expected_result));
88
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
83
+ std::vector<int > res (buffer, buffer+bytes_encoded);
84
+
85
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
86
+ 0xda , 0x00 , 0x01 , 0x03 , 0x00 , 0x81 , 0x68 , 0x74 ,
87
+ 0x68 , 0x69 , 0x6e , 0x67 , 0x5f , 0x69 , 0x64
88
+ }));
89
89
}
90
90
}
91
91
@@ -102,17 +102,16 @@ SCENARIO("Test the encoding of command messages") {
102
102
CBORMessageEncoder encoder;
103
103
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
104
104
105
- uint8_t expected_result[] = {
106
- 0xda , 0x00 , 0x01 , 0x05 , 0x00 , 0x80
107
- };
108
-
109
105
// Test the encoding is
110
106
// DA 00010500 # tag(66816)
111
107
// 80 # array(0)
112
108
THEN (" The encoding is successful" ) {
113
109
REQUIRE (err == Encoder::Status::Complete);
114
- REQUIRE (bytes_encoded == sizeof (expected_result));
115
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
110
+ std::vector<int > res (buffer, buffer+bytes_encoded);
111
+
112
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
113
+ 0xda , 0x00 , 0x01 , 0x05 , 0x00 , 0x80
114
+ }));
116
115
}
117
116
}
118
117
@@ -132,20 +131,19 @@ SCENARIO("Test the encoding of command messages") {
132
131
CBORMessageEncoder encoder;
133
132
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
134
133
135
- uint8_t expected_result[] = {
136
- 0xda , 0x00 , 0x01 , 0x07 , 0x00 , 0x81 , 0x65 , 0x32 ,
137
- 0x2e , 0x30 , 0x2e , 0x30
138
- };
139
-
140
134
// Test the encoding is
141
135
// DA 00010700 # tag(67328)
142
136
// 81 # array(1)
143
137
// 65 # text(5)
144
138
// 322E302E30 # "2.0.0"
145
139
THEN (" The encoding is successful" ) {
146
140
REQUIRE (err == Encoder::Status::Complete);
147
- REQUIRE (bytes_encoded == sizeof (expected_result));
148
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
141
+ std::vector<int > res (buffer, buffer+bytes_encoded);
142
+
143
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
144
+ 0xda , 0x00 , 0x01 , 0x07 , 0x00 , 0x81 , 0x65 , 0x32 ,
145
+ 0x2e , 0x30 , 0x2e , 0x30
146
+ }));
149
147
}
150
148
}
151
149
@@ -170,13 +168,6 @@ SCENARIO("Test the encoding of command messages") {
170
168
CBORMessageEncoder encoder;
171
169
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
172
170
173
- uint8_t expected_result[] = {
174
- 0xda , 0x00 , 0x01 , 0x02 , 0x00 , 0x84 , 0x50 , 0x00 ,
175
- 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
176
- 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0xe1 ,
177
- 0x20 , 0x18 , 0x64
178
- };
179
-
180
171
// Test the encoding is
181
172
// DA 00010200 # tag(66048)
182
173
// 84 # array(4)
@@ -187,8 +178,14 @@ SCENARIO("Test the encoding of command messages") {
187
178
// 18 64 # unsigned(100)
188
179
THEN (" The encoding is successful" ) {
189
180
REQUIRE (err == Encoder::Status::Complete);
190
- REQUIRE (bytes_encoded == sizeof (expected_result));
191
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
181
+ std::vector<int > res (buffer, buffer+bytes_encoded);
182
+
183
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
184
+ 0xda , 0x00 , 0x01 , 0x02 , 0x00 , 0x84 , 0x50 , 0x00 ,
185
+ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ,
186
+ 0x09 , 0x0a , 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0xe1 ,
187
+ 0x20 , 0x18 , 0x64
188
+ }));
192
189
}
193
190
}
194
191
@@ -205,17 +202,16 @@ SCENARIO("Test the encoding of command messages") {
205
202
CBORMessageEncoder encoder;
206
203
Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
207
204
208
- uint8_t expected_result[] = {
209
- 0xda , 0x00 , 0x01 , 0x08 , 0x00 , 0x80
210
- };
211
-
212
205
// Test the encoding is
213
206
// DA 00010800 # tag(67584)
214
207
// 80 # array(0)
215
208
THEN (" The encoding is successful" ) {
216
209
REQUIRE (err == Encoder::Status::Complete);
217
- REQUIRE (bytes_encoded == sizeof (expected_result));
218
- REQUIRE (memcmp (buffer, expected_result, sizeof (expected_result)) == 0 );
210
+ std::vector<int > res (buffer, buffer+bytes_encoded);
211
+
212
+ REQUIRE_THAT (res, Catch::Matchers::Equals (std::vector<int >{
213
+ 0xda , 0x00 , 0x01 , 0x08 , 0x00 , 0x80
214
+ }));
219
215
}
220
216
}
221
217
0 commit comments