aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/coding_tests/value_opt_string_test.cpp
blob: af554b34332cb43cc6b606e025efd2306e950d49 (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
66
67
68
69
70
71
72
73
74
75
76
#include "testing/testing.hpp"

#include "coding/reader.hpp"
#include "coding/value_opt_string.hpp"
#include "coding/writer.hpp"

#include <algorithm>
#include <string>
#include <vector>

namespace
{

template <class T>
void TestStringCodingT(T const * arr, size_t count, size_t maxSize)
{
  for (size_t i = 0; i < count; ++i)
  {
    auto const ethalon = strings::to_string(arr[i]);

    StringNumericOptimal s;
    s.Set(ethalon);

    std::vector<char> buffer;
    MemWriter<std::vector<char>> w(buffer);

    s.Write(w);

    size_t const sz = buffer.size();
    TEST_GREATER(sz, 0, ());
    TEST_LESS_OR_EQUAL(sz, maxSize, ());

    MemReader r(&buffer[0], sz);
    ReaderSource<MemReader> src(r);
    s.Read(src);

    TEST_EQUAL(ethalon, s.Get(), ());
  }
}

}  // namespace

UNIT_TEST(StringNumericOptimal_Zero)
{
  int t1 = 0;
  TestStringCodingT(&t1, 1, 1);  // should be coded as VarUint

  std::string t2 = "01";
  TestStringCodingT(&t2, 1, 3);  // should be coded as String
}

UNIT_TEST(StringNumericOptimal_IntCoding1)
{
  int arr[] = {0, 1, 2, 666, 0x0FFFFFFF, 0x7FFFFFFF - 1, 0x7FFFFFFF};
  TestStringCodingT(arr, ARRAY_SIZE(arr), 5);  // should be coded as VarUint
}

UNIT_TEST(StringNumericOptimal_IntCoding2)
{
  int arr[] = {-1, -2, -666666, static_cast<int>(0xFFFFFFFE), static_cast<int>(0xFFFFFFFF)};
  TestStringCodingT(arr, ARRAY_SIZE(arr), 12);  // should be coded as String
}

UNIT_TEST(StringNumericOptimal_StringCoding)
{
  char const * arr[] = {"xxx", "yyy", "a", "0xFFFFFF", "123456UL"};
  TestStringCodingT(arr, ARRAY_SIZE(arr), 12);  // should be coded as String
}

UNIT_TEST(StringNumericOptimal_LargeStringCoding)
{
  std::string s;
  fill_n(back_inserter(s), 10000, 'x');

  TestStringCodingT(&s, 1, 10006);
}