Lolly 1.4.28
Loading...
Searching...
No Matches
numeral_test.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : numberal_test.cpp
4 * DESCRIPTION: tests on numberal
5 * COPYRIGHT : (C) 2023 Darcy Shen
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "a_lolly_test.hpp"
14#include <climits>
15#include <cstdint>
16
17using namespace lolly::data;
18
19TEST_CASE ("to_roman") {
20 SUBCASE ("0-9") {
21 string_eq (to_roman (0), "o");
22 string_eq (to_roman (1), "i");
23 string_eq (to_roman (2), "ii");
24 string_eq (to_roman (3), "iii");
25 string_eq (to_roman (4), "iv");
26 string_eq (to_roman (5), "v");
27 string_eq (to_roman (6), "vi");
28 string_eq (to_roman (7), "vii");
29 string_eq (to_roman (8), "viii");
30 string_eq (to_roman (9), "ix");
31 string_eq (to_roman (-1), "-i");
32 string_eq (to_roman (-2), "-ii");
33 string_eq (to_roman (-3), "-iii");
34 string_eq (to_roman (-4), "-iv");
35 string_eq (to_roman (-5), "-v");
36 string_eq (to_roman (-6), "-vi");
37 string_eq (to_roman (-7), "-vii");
38 string_eq (to_roman (-8), "-viii");
39 string_eq (to_roman (-9), "-ix");
40 }
41 SUBCASE ("10-99") {
42 string_eq (to_roman (10), "x");
43 string_eq (to_roman (20), "xx");
44 string_eq (to_roman (30), "xxx");
45 string_eq (to_roman (40), "xl");
46 string_eq (to_roman (50), "l");
47 string_eq (to_roman (60), "lx");
48 string_eq (to_roman (70), "lxx");
49 string_eq (to_roman (80), "lxxx");
50 string_eq (to_roman (90), "xc");
51 string_eq (to_roman (99), "xcix");
52 string_eq (to_roman (-10), "-x");
53 string_eq (to_roman (-20), "-xx");
54 string_eq (to_roman (-30), "-xxx");
55 string_eq (to_roman (-40), "-xl");
56 string_eq (to_roman (-50), "-l");
57 string_eq (to_roman (-60), "-lx");
58 string_eq (to_roman (-70), "-lxx");
59 string_eq (to_roman (-80), "-lxxx");
60 string_eq (to_roman (-90), "-xc");
61 string_eq (to_roman (-99), "-xcix");
62 }
63 SUBCASE ("100-999") {
64 string_eq (to_roman (100), "c");
65 string_eq (to_roman (200), "cc");
66 string_eq (to_roman (300), "ccc");
67 string_eq (to_roman (400), "cd");
68 string_eq (to_roman (500), "d");
69 string_eq (to_roman (600), "dc");
70 string_eq (to_roman (700), "dcc");
71 string_eq (to_roman (800), "dccc");
72 string_eq (to_roman (900), "cm");
73 string_eq (to_roman (999), "cmxcix");
74 string_eq (to_roman (-100), "-c");
75 string_eq (to_roman (-200), "-cc");
76 string_eq (to_roman (-300), "-ccc");
77 string_eq (to_roman (-400), "-cd");
78 string_eq (to_roman (-500), "-d");
79 string_eq (to_roman (-600), "-dc");
80 string_eq (to_roman (-700), "-dcc");
81 string_eq (to_roman (-800), "-dccc");
82 string_eq (to_roman (-900), "-cm");
83 string_eq (to_roman (-999), "-cmxcix");
84 }
85 SUBCASE ("1000-3999") {
86 string_eq (to_roman (3999), "mmmcmxcix");
87 string_eq (to_roman (-3999), "-mmmcmxcix");
88 }
89 SUBCASE ("max int32 or min int32") {
90 string_eq (to_roman (4000), "?");
93 }
94}
95
96TEST_CASE ("to_hanzi") {
97 string_eq (to_hanzi (-1605), "负一千六百零五");
98 string_eq (to_hanzi (0), "零");
99 string_eq (to_hanzi (1), "一");
100 string_eq (to_hanzi (10), "十");
101 string_eq (to_hanzi (11), "十一");
102 string_eq (to_hanzi (42), "四十二");
103 string_eq (to_hanzi (90), "九十");
104 string_eq (to_hanzi (100), "一百");
105 string_eq (to_hanzi (102), "一百零二");
106 string_eq (to_hanzi (110), "一百一十");
107 string_eq (to_hanzi (123), "一百二十三");
108 string_eq (to_hanzi (1000), "一千");
109 string_eq (to_hanzi (1001), "一千零一");
110 string_eq (to_hanzi (1024), "一千零二十四");
111 string_eq (to_hanzi (1030), "一千零三十");
112 string_eq (to_hanzi (1600), "一千六百");
113 string_eq (to_hanzi (1605), "一千六百零五");
114 string_eq (to_hanzi (10000), "一万");
115 string_eq (to_hanzi (10001), "一万零一");
116 string_eq (to_hanzi (153457), "十五万三千四百五十七");
117 string_eq (to_hanzi (300153457), "三亿零一十五万三千四百五十七");
118 string_eq (to_hanzi (0x7FFFFFFF), "二十一亿四千七百四十八万三千六百四十七");
119 string_eq (to_hanzi (0x80000000), "负二十一亿四千七百四十八万三千六百四十八");
120}
121
122TEST_CASE ("to_padded_hex") {
123 SUBCASE ("0~255") {
124 string_eq (to_padded_hex ((uint8_t) 0), "00");
125 string_eq (to_padded_hex ((uint8_t) 1), "01");
126 string_eq (to_padded_hex ((uint8_t) 255), "ff");
127 }
128 SUBCASE ("overflow") {
129 string_eq (to_padded_hex ((uint8_t) -1), "ff");
132 }
133}
134
135TEST_CASE ("to_hex/to_Hex") {
136 SUBCASE ("0-15 (+/-)") {
137 for (int i= 1; i < 10; i++) {
138 string_eq (to_hex (i), as_string (i));
139 string_eq (to_Hex (i), as_string (i));
140 string_eq (to_hex (-i), "-" * as_string (i));
141 string_eq (to_hex (-i), "-" * as_string (i));
142 }
143 string_eq (to_hex (0), "0");
144 string_eq (to_hex (10), "a");
145 string_eq (to_hex (11), "b");
146 string_eq (to_hex (12), "c");
147 string_eq (to_hex (13), "d");
148 string_eq (to_hex (14), "e");
149 string_eq (to_hex (15), "f");
150 }
151 SUBCASE ("min/max/overflow") {
152 string_eq (to_hex (INT32_MIN), "-80000000");
153 string_eq (to_hex (INT32_MIN - 1), "7fffffff");
154 string_eq (to_hex (INT32_MAX), "7fffffff");
155 string_eq (to_hex (INT32_MAX + 1), "-80000000");
156 }
157}
158
159TEST_CASE ("as_hexadecimal fixed") {
160 SUBCASE ("0-15 (+/-)") {
161 for (int i= 1; i < 10; i++) {
162 string_eq (as_hexadecimal (i, 1), as_string (i));
163 string_eq (as_hexadecimal (i, 1), as_string (i));
164 }
165 string_eq (as_hexadecimal (0, 1), "0");
166 string_eq (as_hexadecimal (10, 1), "A");
167 string_eq (as_hexadecimal (11, 1), "B");
168 string_eq (as_hexadecimal (12, 1), "C");
169 string_eq (as_hexadecimal (13, 1), "D");
170 string_eq (as_hexadecimal (14, 1), "E");
171 string_eq (as_hexadecimal (15, 1), "F");
172 }
173 SUBCASE ("arbitary") {
174 string_eq (as_hexadecimal (0x12, 2), "12");
175 string_eq (as_hexadecimal (0xABC, 3), "ABC");
176 }
177 SUBCASE ("max") { string_eq (as_hexadecimal (UINT32_MAX, 8), "FFFFFFFF"); }
178}
179
180TEST_CASE ("uint32_to_Hex") {
181 SUBCASE ("0-15 (+/-)") {
182 for (int i= 1; i < 10; i++) {
185 }
186 string_eq (uint32_to_Hex (0), "0");
187 string_eq (uint32_to_Hex (10), "A");
188 string_eq (uint32_to_Hex (11), "B");
189 string_eq (uint32_to_Hex (12), "C");
190 string_eq (uint32_to_Hex (13), "D");
191 string_eq (uint32_to_Hex (14), "E");
192 string_eq (uint32_to_Hex (15), "F");
193 }
194 SUBCASE ("arbitary") {
195 string_eq (uint32_to_Hex (0x12), "12");
196 string_eq (uint32_to_Hex (0xABC), "ABC");
197 string_eq (uint32_to_Hex (0x80000001), "80000001");
198 }
199 SUBCASE ("max") { string_eq (uint32_to_Hex (UINT32_MAX), "FFFFFFFF"); }
200}
201
202TEST_CASE ("binary to hexadecimal") {
203 string header_of_png;
204 header_of_png << "\xff\xd8\xff\xe0";
205 header_of_png << '\x00';
206 header_of_png << "\x10\x4A\x46\x49\x46";
207 string_eq (binary_to_hexadecimal (header_of_png), "FFD8FFE000104A464946");
208}
The list class represents a linked list.
Definition list.hpp:48
void string_eq(string left, string right)
string to_hex(int32_t i)
Definition numeral.cpp:189
string binary_to_hexadecimal(string bin)
Converts a binary stream to its hexadecimal represention.
Definition numeral.cpp:313
string uint32_to_Hex(uint32_t i)
Converts an unsigned integer to a hexadecimal string.
Definition numeral.cpp:306
string to_hanzi(int32_t nr)
Generates a Chinese numeral for a given integer.
Definition numeral.cpp:130
string to_roman(int32_t nr)
Generates a Roman numeral string for an integer.
Definition numeral.cpp:34
string as_hexadecimal(int i, int len)
Converts an unsigned integer to a hexadecimal string with a fixed length.
Definition numeral.cpp:247
string to_padded_hex(uint8_t i)
Converts an 8-bit unsigned integer to a fixed-length (2) hex string.
Definition numeral.cpp:153
string to_Hex(int32_t i)
Definition numeral.cpp:174
TEST_CASE("test for operator+= and advance()")
string as_string(int16_t i)
Definition string.cpp:272