aboutsummaryrefslogtreecommitdiff
path: root/test/base/StringToNumberConverterTest.cpp
blob: 82062bdb5e4af31125d4058462b98f4950aaecd8 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "cru/base/Exception.h"
#include "cru/base/StringToNumberConverter.h"

#include <catch2/catch_test_macros.hpp>

TEST_CASE("StringToIntegerConverterImpl Base0", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter({}, 0);
  Index processed_characters_count;

  REQUIRE(converter.Parse("12345678", &processed_characters_count) ==
          StringToIntegerResult(false, 12345678));
  REQUIRE(processed_characters_count == 8);

  REQUIRE(converter.Parse("0", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 1);

  REQUIRE(converter.Parse("012", &processed_characters_count) ==
          StringToIntegerResult(false, 012));
  REQUIRE(processed_characters_count == 3);

  REQUIRE(converter.Parse("0x12", &processed_characters_count) ==
          StringToIntegerResult(false, 0x12));
  REQUIRE(processed_characters_count == 4);

  REQUIRE(converter.Parse("0X12", &processed_characters_count) ==
          StringToIntegerResult(false, 0x12));
  REQUIRE(processed_characters_count == 4);

  REQUIRE(converter.Parse("0b101", &processed_characters_count) ==
          StringToIntegerResult(false, 0b101));
  REQUIRE(processed_characters_count == 5);

  REQUIRE(converter.Parse("0B101", &processed_characters_count) ==
          StringToIntegerResult(false, 0b101));
  REQUIRE(processed_characters_count == 5);

  REQUIRE(converter.Parse("-123", &processed_characters_count) ==
          StringToIntegerResult(true, 123));
  REQUIRE(processed_characters_count == 4);

  REQUIRE(converter.Parse("-0x10", &processed_characters_count) ==
          StringToIntegerResult(true, 0x10));
  REQUIRE(processed_characters_count == 5);
}

TEST_CASE("StringToIntegerConverterImpl Base0ForError", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter({}, 0);
  Index processed_characters_count;

  REQUIRE(converter.Parse("a", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);

  REQUIRE(converter.Parse("0a", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);

  REQUIRE(converter.Parse("0x", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);

  REQUIRE(converter.Parse("0xx", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);

  REQUIRE(converter.Parse(" 0", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);

  REQUIRE(converter.Parse("0 ", &processed_characters_count) ==
          StringToIntegerResult(false, 0));
  REQUIRE(processed_characters_count == 0);
}

TEST_CASE("StringToIntegerConverterImpl ThrowOnErrorFlag", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(StringToNumberFlags::kThrowOnError, 0);
  Index processed_characters_count;
  REQUIRE_THROWS_AS(converter.Parse("?", &processed_characters_count),
                    Exception);
}

TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroFlag", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(
      StringToNumberFlags::kAllowLeadingSpaces, 0);
  Index processed_characters_count;
  REQUIRE(converter.Parse("   123", &processed_characters_count) ==
          StringToIntegerResult(false, 123));
  REQUIRE(processed_characters_count == 6);
}

TEST_CASE("StringToIntegerConverterImpl AllowTrailingZeroFlag", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(
      StringToNumberFlags::kAllowTrailingSpaces, 0);
  Index processed_characters_count;
  REQUIRE(converter.Parse("123   ", &processed_characters_count) ==
          StringToIntegerResult(false, 123));
  REQUIRE(processed_characters_count == 6);
}

TEST_CASE("StringToIntegerConverterImpl AllowTrailingJunk", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(
      StringToNumberFlags::kAllowTrailingJunk, 0);
  Index processed_characters_count;
  REQUIRE(converter.Parse("123 12", &processed_characters_count) ==
          StringToIntegerResult(false, 123));
  REQUIRE(processed_characters_count == 3);
}

TEST_CASE("StringToIntegerConverterImpl AllowLeadingZeroForInteger",
          "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(
      StringToNumberFlags::kAllowLeadingZeroForInteger, 0);
  Index processed_characters_count;
  REQUIRE(converter.Parse("0x0012", &processed_characters_count) ==
          StringToIntegerResult(false, 0x12));
  REQUIRE(processed_characters_count == 6);

  REQUIRE(converter.Parse("000011", &processed_characters_count) ==
          StringToIntegerResult(false, 000011));
  REQUIRE(processed_characters_count == 6);

  REQUIRE(converter.Parse("0b0011", &processed_characters_count) ==
          StringToIntegerResult(false, 0b0011));
  REQUIRE(processed_characters_count == 6);
}

TEST_CASE("StringToIntegerConverterImpl CompositeFlags", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter(
      StringToNumberFlags::kAllowLeadingSpaces |
          StringToNumberFlags::kAllowTrailingJunk |
          StringToNumberFlags::kAllowLeadingZeroForInteger,
      0);
  Index processed_characters_count;

  REQUIRE(converter.Parse("   0x00123!!!", &processed_characters_count) ==
          StringToIntegerResult(false, 0x00123));
  REQUIRE(processed_characters_count == 10);
}

TEST_CASE("StringToIntegerConverterImpl OtherBase", "[string]") {
  using namespace cru;
  StringToIntegerConverter converter({}, 7);
  Index processed_characters_count;

  REQUIRE(converter.Parse("12", &processed_characters_count) ==
          StringToIntegerResult(false, 9));
  REQUIRE(processed_characters_count == 2);

  REQUIRE(converter.Parse("-12", &processed_characters_count) ==
          StringToIntegerResult(true, 9));
  REQUIRE(processed_characters_count == 3);

  converter.base = 11;
  REQUIRE(converter.Parse("1a", &processed_characters_count) ==
          StringToIntegerResult(false, 21));
  REQUIRE(processed_characters_count == 2);
}