-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtesthex.cpp
More file actions
55 lines (43 loc) · 1.41 KB
/
Copy pathtesthex.cpp
File metadata and controls
55 lines (43 loc) · 1.41 KB
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
54
55
#include "rtmfp/Hex.hpp"
#include <cassert>
using namespace com::zenomt;
static void _testHexDecode(const char *hex, int expectedLength)
{
bool expectPass = expectedLength >= 0;
printf("Hex parse %s expect-%s len:%d ", hex, expectPass ? "pass" : "fail", expectedLength);
std::vector<uint8_t> b;
bool rv = Hex::decode(hex, b);
printf("did-%s len:%d\n", rv ? "pass" : "fail", (int)b.size());
assert(rv == expectPass);
if(expectPass)
assert((size_t)expectedLength == b.size());
if(rv)
printf(" got %s\n", Hex::encode(b.data(), b.size()).c_str());
}
int main(int argc, char *argv[])
{
uint8_t t1[] = { 0, 1, 5, 4, 5 };
auto t1_s = Hex::encode(t1, sizeof(t1));
const char *t1_expected = "0001050405";
printf("Hex::encode expect '%s' got '%s'\n", t1_expected, t1_s.c_str());
assert(t1_s == t1_expected);
_testHexDecode("00 01 02", 3);
_testHexDecode("00 ff 0102", 4);
_testHexDecode("", 0);
_testHexDecode(" f1 ", 1);
_testHexDecode(" f 1", -1);
_testHexDecode(" f1 1", -1);
_testHexDecode("0x33", -1);
_testHexDecode("fo", -1);
_testHexDecode("1", -1);
assert(Hex::decodeByte("") < 0);
assert(Hex::decodeByte("0") < 0);
assert(Hex::decodeByte("0g") < 0);
assert(Hex::decodeByte("g0") < 0);
assert(Hex::decodeByte("00") == 0x00);
assert(Hex::decodeByte("08") == 0x08);
assert(Hex::decodeByte("a0") == 0xa0);
assert(Hex::decodeByte("A0g") == 0xa0);
assert(Hex::decodeByte("Ff") == 0xff);
return 0;
}