forked from arduino-libraries/Arduino_CloudUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hex.cpp
53 lines (43 loc) · 1.82 KB
/
test_hex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
This file is part of the Arduino_CloudUtils library.
Copyright (c) 2024 Arduino SA
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <catch2/catch_test_macros.hpp>
#include <hex/chex.h>
SCENARIO( "HEX encoding test" ) {
/* check all possible 16-bit values in network byte order */
unsigned i;
for(i = 0; i <= 0xffff; ++i){
/* encode to hex string using our implementation */
const unsigned char org[2] = {(unsigned char)(i>>8), (unsigned char)i};
char hex[5] = {0,0,0,0,0};
chex_encode(hex, sizeof(hex), org, sizeof(org));
/* decode from hex string using a function from the standard library */
unsigned char bin[2] = {0,0};
int n = sscanf(hex, "%02hhx%02hhx", bin, bin+1);
REQUIRE(n == sizeof(bin));
/* decoded matches the original */
REQUIRE(memcmp(org, bin, sizeof(bin)) == 0);
}
REQUIRE(i == 0x10000);
}
SCENARIO( "HEX decoding test" ) {
/* check all possible 16-bit values in network byte order */
unsigned i;
for(i = 0; i <= 0xffff; ++i){
/* encode to hex string using a function from the standard library */
char hex[5] = {0,0,0,0,0};
snprintf(hex, sizeof(hex), "%02hhX%02hhx", (unsigned char)(i>>8), (unsigned char)i);
/* decode from hex string using our implementation */
unsigned char bin[2];
unsigned n = chex_decode(bin, sizeof(bin), hex, sizeof(hex));
REQUIRE(n == sizeof(bin));
/* decoded matches the original */
const unsigned char org[2] = {(unsigned char)(i>>8), (unsigned char)i};
REQUIRE(memcmp(org, bin, sizeof(bin)) == 0);
}
REQUIRE(i == 0x10000);
}