1111#include < memory>
1212
1313#include < util/CBORTestUtil.h>
14+ #include < CborEncoder.h>
1415#include < MessageEncoder.h>
16+ #include < catch2/matchers/catch_matchers_vector.hpp>
1517
1618/* *****************************************************************************
1719 TEST CODE
@@ -34,23 +36,22 @@ SCENARIO("Test the encoding of command messages") {
3436 CBORMessageEncoder encoder;
3537 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
3638
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-
4539 // Test the encoding is
4640 // DA 00010000 # tag(65536)
4741 // 81 # array(1)
4842 // 58 20 # bytes(32)
4943 // 01020304
5044 THEN (" The encoding is successful" ) {
5145 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+ }));
5455 }
5556 }
5657
@@ -71,11 +72,6 @@ SCENARIO("Test the encoding of command messages") {
7172 CBORMessageEncoder encoder;
7273 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
7374
74- uint8_t expected_result[] = {
75- 0xda , 0x00 , 0x01 , 0x03 , 0x00 , 0x81 , 0x68 , 0x74 ,
76- 0x68 , 0x69 , 0x6e , 0x67 , 0x5f , 0x69 , 0x64
77- };
78-
7975 // Test the encoding is
8076 // DA 00010300 # tag(66304)
8177 // 81 # array(1)
@@ -84,8 +80,12 @@ SCENARIO("Test the encoding of command messages") {
8480
8581 THEN (" The encoding is successful" ) {
8682 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+ }));
8989 }
9090 }
9191
@@ -102,17 +102,16 @@ SCENARIO("Test the encoding of command messages") {
102102 CBORMessageEncoder encoder;
103103 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
104104
105- uint8_t expected_result[] = {
106- 0xda , 0x00 , 0x01 , 0x05 , 0x00 , 0x80
107- };
108-
109105 // Test the encoding is
110106 // DA 00010500 # tag(66816)
111107 // 80 # array(0)
112108 THEN (" The encoding is successful" ) {
113109 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+ }));
116115 }
117116 }
118117
@@ -132,20 +131,19 @@ SCENARIO("Test the encoding of command messages") {
132131 CBORMessageEncoder encoder;
133132 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
134133
135- uint8_t expected_result[] = {
136- 0xda , 0x00 , 0x01 , 0x07 , 0x00 , 0x81 , 0x65 , 0x32 ,
137- 0x2e , 0x30 , 0x2e , 0x30
138- };
139-
140134 // Test the encoding is
141135 // DA 00010700 # tag(67328)
142136 // 81 # array(1)
143137 // 65 # text(5)
144138 // 322E302E30 # "2.0.0"
145139 THEN (" The encoding is successful" ) {
146140 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+ }));
149147 }
150148 }
151149
@@ -170,13 +168,6 @@ SCENARIO("Test the encoding of command messages") {
170168 CBORMessageEncoder encoder;
171169 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
172170
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-
180171 // Test the encoding is
181172 // DA 00010200 # tag(66048)
182173 // 84 # array(4)
@@ -187,8 +178,14 @@ SCENARIO("Test the encoding of command messages") {
187178 // 18 64 # unsigned(100)
188179 THEN (" The encoding is successful" ) {
189180 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+ }));
192189 }
193190 }
194191
@@ -205,17 +202,16 @@ SCENARIO("Test the encoding of command messages") {
205202 CBORMessageEncoder encoder;
206203 Encoder::Status err = encoder.encode ((Message*)&command, buffer, bytes_encoded);
207204
208- uint8_t expected_result[] = {
209- 0xda , 0x00 , 0x01 , 0x08 , 0x00 , 0x80
210- };
211-
212205 // Test the encoding is
213206 // DA 00010800 # tag(67584)
214207 // 80 # array(0)
215208 THEN (" The encoding is successful" ) {
216209 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+ }));
219215 }
220216 }
221217
0 commit comments