aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/coding_tests/hex_test.cpp
blob: 7e3385de611ff8f5f06d32fbc1937269a27df026 (plain) (blame)
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
65
#include "testing/testing.hpp"

#include "coding/hex.hpp"

#include <cstddef>
#include <cstdint>
#include <random>
#include <string>

namespace hex_test
{
using namespace std;

UNIT_TEST(GoldenRecode)
{
  string data("\x01\x23\x45\x67\x89\xAB\xCD\xEF");
  string hexData("0123456789ABCDEF");

  TEST_EQUAL(ToHex(data), hexData, ());
  TEST_EQUAL(data, FromHex(hexData), ());
}

UNIT_TEST(RandomRecode)
{
  mt19937 rng(0);
  for (size_t i = 0; i < 256; ++i)
  {
    string data(1 + (rng() % 20), 0);
    for (size_t j = 0; j < data.size(); ++j)
      data[j] = static_cast<char>(rng() % 26) + 'A';
    TEST_EQUAL(data, FromHex(ToHex(data)), ());
  }
}

UNIT_TEST(EncodeNumber)
{
  TEST_EQUAL(NumToHex(uint64_t(0x0123456789ABCDEFULL)), "0123456789ABCDEF", ());
}

UNIT_TEST(DecodeLowerCaseHex)
{
  TEST_EQUAL(FromHex("fe"), "\xfe", ());
}

UNIT_TEST(EncodeEmptyString)
{
  TEST_EQUAL(ToHex(string()), "", ());
}

UNIT_TEST(DecodeEmptyString)
{
  TEST_EQUAL(FromHex(""), "", ());
}

UNIT_TEST(FromHex_InvalidInput)
{
  TEST_EQUAL(FromHex("0G"), "", ());
  TEST_EQUAL(FromHex("0g"), "", ());
  TEST_EQUAL(FromHex("G0"), "", ());
  TEST_EQUAL(FromHex("g0"), "", ());
  TEST_EQUAL(FromHex("Zz"), "", ());
  TEST_EQUAL(FromHex("0"), "", ());
  TEST_EQUAL(FromHex("012"), "", ());
}
}  // namespace hex_test