-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhex_string_to_block.cpp
More file actions
64 lines (54 loc) · 2.36 KB
/
hex_string_to_block.cpp
File metadata and controls
64 lines (54 loc) · 2.36 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
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <sstream>
#include <memory>
#include <ByteConvert/ByteConvert.hpp>
int main()
{
std::cout << "Testing hex_string_to_block function" << std::endl;
// Generate all posible values for 1 byte
std::cout << "Generating all posibilities" << std::endl;
std::vector<std::pair<uint8_t, std::string>> test_values;
test_values.reserve(256);
std::stringstream ss("");
for (uint8_t i = 0; i <= 0xff; i++) {
ss << std::hex << unsigned(i);
test_values.push_back(std::make_pair(i,(ss.str().size()==2)? ss.str() : "0" + ss.str()));
ss.str("");
if (i == 0xff)
break;
}
// Test odd length hex strings
std::cout << "Testing" << std::endl;
size_t block_size = 0;
std::string inval("");
for (uint8_t i = 0; i < 0xff;i++) {
inval = test_values[(int)i].second + test_values[((int)i)+1].second;
std::unique_ptr<uint8_t[]> block(ByteConvert::hex_string_to_block(inval,&block_size));
if (block_size != 2 || block[0] != test_values[(int)i].first || block[1] != test_values[((int)i)+1].first) {
uint8_t bl1[2] = {test_values[(int)i].first, test_values[((int)i)+1].first};
uint8_t bl2[2] = {block[0], block[1]};
std::cout << "Expected: " << ByteConvert::block_to_hex_string(bl1,2) << " Got: " << ByteConvert::block_to_hex_string(bl2,2) << std::endl;
return -1;
}
if (i == 0xff)
break;
}
// Test even length hex strings
for (uint8_t i = 0; i < 0xff;i++) {
inval = test_values[(int)i].second[1] + test_values[((int)i)+1].second;
std::unique_ptr<uint8_t[]> block(ByteConvert::hex_string_to_block(inval,&block_size));
if (block_size != 2 || block[0] != static_cast<uint8_t>(test_values[(int)i].first & 0x0f) || block[1] != test_values[((int)i)+1].first) {
uint8_t bl1[2] = {static_cast<uint8_t>(test_values[(int)i].first & 0x0f), test_values[((int)i)+1].first};
uint8_t bl2[2] = {block[0], block[1]};
std::cout << "Expected: " << ByteConvert::block_to_hex_string(bl1,2) << " Got: " << ByteConvert::block_to_hex_string(bl2,2) << std::endl;
return -1;
}
if (i == 0xff)
break;
}
std::cout << "Done" << std::endl;
return 0;
}