SILICON
Loading...
Searching...
No Matches
num_formatting.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "tests.hpp"
19
20#include <core/wireUtils.hpp>
22
23#include <string>
24#include <tuple>
25#include <vector>
26
27using namespace SILICON::core;
28
29TEST(NumFormattingTest, FormatsKnownValues)
30{
31 const auto value = busValueFromBits("1010");
32
33 EXPECT_EQ(formatValue(value, BusValueFormat::Raw), "1010");
34 EXPECT_EQ(formatValue(value, BusValueFormat::Unsigned), "10");
35 EXPECT_EQ(formatValue(value, BusValueFormat::Signed), "-6");
36 EXPECT_EQ(formatValue(value, BusValueFormat::Hex), "0xA");
37 EXPECT_EQ(formatValue(value, BusValueFormat::Oct), "0o12");
38 EXPECT_EQ(formatValue(value, BusValueFormat::Bin), "0b1010");
39 EXPECT_EQ(formatValue(busValueFromInteger(10, 8), BusValueFormat::Hex, 2), "0x0A");
40}
41
42TEST(NumFormattingTest, MaxValueHasNoMachineIntegerWidthLimit)
43{
44 EXPECT_EQ(maxValueForBusWidth(4), busValueFromBits("1111"));
45 EXPECT_EQ(maxValueForBusWidth(128), BusValue(128, State::HIGH));
46}
47
48TEST(NumFormattingTest, ParsesSupportedNumericFormats)
49{
50 for (const auto& [text, expectedFormat, expectedWidth] :
51 std::vector<std::tuple<std::string, BusValueFormat, std::size_t>>{
52 {"42", BusValueFormat::Unsigned, 6},
53 {" 0x2a ", BusValueFormat::Hex, 8},
54 {"0o52", BusValueFormat::Oct, 6},
55 {"0b101010", BusValueFormat::Bin, 6}}) {
56 const auto [value, format] = valueFromStr(text);
57 EXPECT_EQ(format, expectedFormat);
58 EXPECT_EQ(value.size(), expectedWidth);
59 EXPECT_EQ(formatValue(value, BusValueFormat::Unsigned), "42");
60 }
61
62 const auto [hexWithE, format] = valueFromStr("0xFE");
63 EXPECT_EQ(format, BusValueFormat::Hex);
64 EXPECT_EQ(hexWithE, busValueFromBits("11111110"));
65}
66
67TEST(NumFormattingTest, ParsesFourStateRawValuesAsMsbFirst)
68{
69 const auto [binaryValue, binaryFormat] = valueFromStr("1010");
70 EXPECT_EQ(binaryFormat, BusValueFormat::Raw);
71 EXPECT_EQ(binaryValue, busValueFromBits("1010"));
72
73 const auto [value, format] = valueFromStr("10xE");
74 EXPECT_EQ(format, BusValueFormat::Raw);
76 EXPECT_EQ(formatValue(value, BusValueFormat::Raw), "10XE");
77 EXPECT_EQ(formatValue(value, BusValueFormat::Hex), "10XE");
78}
79
80TEST(NumFormattingTest, RejectsInvalidInput)
81{
82 EXPECT_EQ(valueFromStr("").format, BusValueFormat::Unknown);
83 EXPECT_EQ(valueFromStr("0b102").format, BusValueFormat::Unknown);
84 EXPECT_EQ(valueFromStr("10Z1").format, BusValueFormat::Unknown);
85 EXPECT_EQ(valueFromStr("-0x2a").format, BusValueFormat::Unknown);
86 EXPECT_THROW((void)busValueFromBits("10Z1"), std::invalid_argument);
87}
88
89TEST(NumFormattingTest, SignedUsesTwosComplementWidth)
90{
91 EXPECT_EQ(formatValue(busValueFromBits("1111"), BusValueFormat::Signed), "-1");
92 EXPECT_EQ(formatValue(busValueFromBits("1000"), BusValueFormat::Signed), "-8");
93 EXPECT_EQ(formatValue(busValueFromBits("0111"), BusValueFormat::Signed), "7");
94
95 const auto [negative, format] = valueFromStr("-42");
96 EXPECT_EQ(format, BusValueFormat::Signed);
97 EXPECT_EQ(formatValue(negative, BusValueFormat::Signed), "-42");
98}
99
100TEST(NumFormattingTest, ResizesParsedSignedValuesWithTwosComplementSemantics)
101{
102 const auto expectResize = [](const std::string_view text, const std::size_t width,
103 const std::string_view expected) {
104 const auto resized = resizeParsedValue(valueFromStr(text), width);
105 ASSERT_TRUE(resized) << text << " at " << width << " bits";
106 EXPECT_EQ(formatValue(*resized, BusValueFormat::Raw), expected);
107 };
108
109 expectResize("-1", 1, "1");
110 expectResize("-1", 4, "1111");
111 expectResize("-1", 8, "11111111");
112 expectResize("-8", 4, "1000");
113 expectResize("-8", 8, "11111000");
114 expectResize("127", 8, "01111111");
115
116 EXPECT_FALSE(resizeParsedValue(valueFromStr("-9"), 4));
117 EXPECT_FALSE(resizeParsedValue(valueFromStr("+128"), 8));
118}
119
120TEST(NumFormattingTest, ResizesUnsignedAndRawValuesWithZeroExtension)
121{
122 EXPECT_EQ(resizeParsedValue(valueFromStr("255"), 8), busValueFromBits("11111111"));
123 EXPECT_FALSE(resizeParsedValue(valueFromStr("256"), 8));
124 EXPECT_EQ(resizeParsedValue(valueFromStr("0b101"), 8), busValueFromBits("00000101"));
125 EXPECT_EQ(resizeParsedValue(valueFromStr("X"), 8), BusValue(8, State::UNKNOWN));
126
127 const auto wide = valueFromStr("36893488147419103231");
128 EXPECT_TRUE(resizeParsedValue(wide, 65));
129 EXPECT_FALSE(resizeParsedValue(wide, 64));
130}
131
132TEST(NumFormattingTest, NumericFitChecksAreSeparateFromBitVectorNormalization)
133{
134 EXPECT_TRUE(SILICON::wireUtils::fitsUnsigned(busValueFromBits("000001"), 4));
135 EXPECT_FALSE(SILICON::wireUtils::fitsUnsigned(busValueFromBits("100001"), 4));
136 EXPECT_TRUE(SILICON::wireUtils::fitsSigned(busValueFromBits("11111"), 1));
137 EXPECT_TRUE(SILICON::wireUtils::fitsSigned(busValueFromBits("11000"), 4));
138 EXPECT_FALSE(SILICON::wireUtils::fitsSigned(busValueFromBits("10111"), 4));
139
141 busValueFromBits("0001"));
143 busValueFromBits("1111"));
144}
145
146TEST(NumFormattingTest, SupportsValuesWiderThanMachineIntegers)
147{
149 "36893488147419103231");
150 EXPECT_EQ(
151 formatValue(busValueFromBits("1" + std::string(64, '0')), BusValueFormat::Signed),
152 "-18446744073709551616");
153
154 const auto [wide, format] = valueFromStr("36893488147419103231");
155 EXPECT_EQ(format, BusValueFormat::Unsigned);
156 EXPECT_EQ(wide, BusValue(65, State::HIGH));
157}
158
159TEST(NumFormattingTest, RoundTripsArbitraryPrecisionValuesAcrossNumericBases)
160{
161 const std::string decimal =
162 "115792089237316195423570985008687907853269984665640564039457584007913129639935";
163 const auto value = BusValue(256, State::HIGH);
164
165 EXPECT_EQ(formatValue(value, BusValueFormat::Unsigned), decimal);
166 EXPECT_EQ(valueFromStr(decimal).value, value);
167 EXPECT_EQ(valueFromStr("000" + decimal).value, value);
168
169 const auto hex = valueFromStr("0x" + std::string(64, 'F'));
170 EXPECT_EQ(hex.format, BusValueFormat::Hex);
171 EXPECT_EQ(hex.value, value);
172 EXPECT_EQ(formatValue(hex.value, BusValueFormat::Hex), "0x" + std::string(64, 'F'));
173
174 const auto octal = valueFromStr("0o" + std::string(86, '7'));
175 EXPECT_EQ(octal.format, BusValueFormat::Oct);
176 EXPECT_EQ(octal.value.size(), 258);
177 EXPECT_EQ(formatValue(octal.value, BusValueFormat::Oct), "0o" + std::string(86, '7'));
178}
179
180TEST(NumFormattingTest, PreservesExplicitBaseWidthsWhenParsingLeadingZeroes)
181{
182 EXPECT_EQ(valueFromStr("0b0001").value, busValueFromBits("0001"));
183 EXPECT_EQ(valueFromStr("0x01").value, busValueFromBits("00000001"));
184 EXPECT_EQ(valueFromStr("0o01").value, busValueFromBits("000001"));
185}
186
187TEST(NumFormattingTest, FormatsWidthLimitedIntegers)
188{
189 EXPECT_EQ(formatInteger(0xf3, BusValueFormat::Signed, 8), "-13");
190 EXPECT_EQ(formatInteger(0xf3, BusValueFormat::Unsigned, 8), "243");
191 EXPECT_EQ(formatInteger(0xf3, BusValueFormat::Bin, 8), "11110011");
192 EXPECT_EQ(formatInteger(0xf3, BusValueFormat::Oct, 8), "363");
193 EXPECT_EQ(formatInteger(0x0ff3, BusValueFormat::Signed, 16), "4083");
194 EXPECT_EQ(formatInteger(0xffffffffffffffffULL, BusValueFormat::Signed, 64), "-1");
195}
196
197TEST(NumFormattingTest, ParsesWidthLimitedIntegers)
198{
199 EXPECT_EQ(parseInteger("-128", BusValueFormat::Signed, 8), 0x80);
200 EXPECT_EQ(parseInteger("127", BusValueFormat::Signed, 8), 0x7f);
201 EXPECT_EQ(parseInteger("255", BusValueFormat::Unsigned, 8), 0xff);
202 EXPECT_EQ(parseInteger("0b11110011", BusValueFormat::Bin, 8), 0xf3);
203 EXPECT_EQ(parseInteger("0o363", BusValueFormat::Oct, 8), 0xf3);
204 EXPECT_EQ(parseInteger("0xF3", BusValueFormat::Hex, 8), 0xf3);
205 EXPECT_EQ(parseInteger("18446744073709551615", BusValueFormat::Unsigned, 64),
206 BigInt("18446744073709551615"));
207 EXPECT_FALSE(parseInteger("-129", BusValueFormat::Signed, 8));
208 EXPECT_FALSE(parseInteger("128", BusValueFormat::Signed, 8));
209 EXPECT_FALSE(parseInteger("256", BusValueFormat::Unsigned, 8));
210 EXPECT_FALSE(parseInteger("0b102", BusValueFormat::Bin, 8));
211 EXPECT_FALSE(parseInteger("1", BusValueFormat::Raw, 8));
212 EXPECT_FALSE(parseInteger("1", BusValueFormat::Unknown, 8));
213}
214
215TEST(NumFormattingTest, FormatsArbitraryPrecisionIntegers)
216{
217 const BigInt value("340282366920938463463374607431768211455");
218 EXPECT_EQ(formatInteger(value, BusValueFormat::Unsigned, 128), value.str());
219 EXPECT_EQ(formatInteger(value, BusValueFormat::Signed, 128), "-1");
220 EXPECT_EQ(formatInteger(value, BusValueFormat::Hex, 128), std::string(32, 'F'));
221 EXPECT_EQ(formatInteger(value, BusValueFormat::Oct, 128), "3" + std::string(42, '7'));
222 EXPECT_EQ(formatInteger(value, BusValueFormat::Bin, 128), std::string(128, '1'));
223 EXPECT_EQ(formatInteger(value, BusValueFormat::Unsigned, 65), "36893488147419103231");
224 EXPECT_EQ(formatInteger(value, BusValueFormat::Unsigned, 8), "255");
225 EXPECT_EQ(formatInteger(value, BusValueFormat::Bin, 129), "0" + std::string(128, '1'));
226 EXPECT_EQ(formatInteger(-value, BusValueFormat::Unsigned, 128), "1");
227 EXPECT_EQ(formatInteger(-1, BusValueFormat::Hex, 128), std::string(32, 'F'));
228
229 const BigInt minimum = -(BigInt{1} << 127);
230 EXPECT_EQ(formatInteger(minimum, BusValueFormat::Signed, 128), minimum.str());
231}
232
233TEST(NumFormattingTest, ParsesArbitraryPrecisionIntegersAcrossNumericBases)
234{
235 const BigInt maximum("340282366920938463463374607431768211455");
236 EXPECT_EQ(parseInteger(maximum.str(), BusValueFormat::Unsigned, 128), maximum);
237 EXPECT_EQ(parseInteger(" +" + maximum.str() + " ", BusValueFormat::Unsigned, 128),
238 maximum);
239 EXPECT_EQ(parseInteger("0x" + std::string(32, 'F'), BusValueFormat::Hex, 128), maximum);
240 EXPECT_EQ(parseInteger("0o3" + std::string(42, '7'), BusValueFormat::Oct, 128),
241 maximum);
242 EXPECT_EQ(parseInteger("0b" + std::string(128, '1'), BusValueFormat::Bin, 128),
243 maximum);
244
245 const BigInt overflow = maximum + 1;
246 EXPECT_FALSE(parseInteger(overflow.str(), BusValueFormat::Unsigned, 128));
247 EXPECT_FALSE(parseInteger("0x1" + std::string(32, '0'), BusValueFormat::Hex, 128));
248 EXPECT_FALSE(parseInteger("0o4" + std::string(42, '0'), BusValueFormat::Oct, 128));
249 EXPECT_FALSE(parseInteger("0b1" + std::string(128, '0'), BusValueFormat::Bin, 128));
250}
251
252TEST(NumFormattingTest, ParsesArbitraryPrecisionSignedBounds)
253{
254 const BigInt signBit = BigInt{1} << 127;
255 const BigInt minimum = -signBit;
256 const BigInt maximum = signBit - 1;
257 EXPECT_EQ(parseInteger(minimum.str(), BusValueFormat::Signed, 128), signBit);
258 EXPECT_EQ(parseInteger(maximum.str(), BusValueFormat::Signed, 128), maximum);
259 EXPECT_EQ(parseInteger("-1", BusValueFormat::Signed, 128), (BigInt{1} << 128) - 1);
260 EXPECT_EQ(parseInteger("-0", BusValueFormat::Signed, 128), 0);
261
262 const BigInt underflow = minimum - 1;
263 EXPECT_FALSE(parseInteger(underflow.str(), BusValueFormat::Signed, 128));
264 EXPECT_FALSE(parseInteger(signBit.str(), BusValueFormat::Signed, 128));
265}
266
267TEST(NumFormattingTest, ConvertsArbitraryPrecisionIntegersToFixedWidthBusValues)
268{
269 const BigInt value = (BigInt{1} << 128) + 5;
270 EXPECT_EQ(busValueFromInteger(value, 129),
271 busValueFromBits("1" + std::string(125, '0') + "101"));
272 EXPECT_EQ(busValueFromInteger(value, 8), busValueFromBits("00000101"));
273 EXPECT_EQ(busValueFromInteger(0, 128), BusValue(128, State::LOW));
274 EXPECT_TRUE(busValueFromInteger(value, 0).empty());
275}
276
277TEST(NumFormattingTest, ConvertsBusValuesToUnsignedArbitraryPrecisionIntegers)
278{
279 const BigInt value = (BigInt{1} << 128) + 5;
280 EXPECT_EQ(integerFromBusValue(busValueFromInteger(value, 256)), value);
281 EXPECT_EQ(integerFromBusValue(busValueFromBits("1111")), 15);
282 EXPECT_EQ(integerFromBusValue(BusValue(128, State::LOW)), 0);
283 EXPECT_EQ(integerFromBusValue({}), 0);
284}
285
286TEST(NumFormattingTest, RejectsNonbinaryBusValuesWhenConvertingToIntegers)
287{
288 EXPECT_THROW((void)integerFromBusValue(busValueFromBits("10X1")),
289 std::invalid_argument);
290 EXPECT_THROW((void)integerFromBusValue(busValueFromBits("10E1")),
291 std::invalid_argument);
292}
293
294TEST(NumFormattingTest, ConvertsNegativeIntegersToTwosComplementBusValues)
295{
296 EXPECT_EQ(busValueFromInteger(-1, 1), busValueFromBits("1"));
297 EXPECT_EQ(busValueFromInteger(-1, 128), BusValue(128, State::HIGH));
298 EXPECT_EQ(busValueFromInteger(-8, 4), busValueFromBits("1000"));
299 EXPECT_EQ(busValueFromInteger(-8, 8), busValueFromBits("11111000"));
300 EXPECT_EQ(busValueFromInteger(-129, 8), busValueFromBits("01111111"));
301 EXPECT_TRUE(busValueFromInteger(-1, 0).empty());
302
303 const BigInt minimum = -(BigInt{1} << 127);
304 EXPECT_EQ(busValueFromInteger(minimum, 128),
305 busValueFromBits("1" + std::string(127, '0')));
306 EXPECT_EQ(busValueFromInteger(minimum + 5, 128),
307 busValueFromBits("1" + std::string(124, '0') + "101"));
308 EXPECT_EQ(busValueFromInteger(minimum - 1, 128),
309 busValueFromBits("0" + std::string(127, '1')));
310 EXPECT_EQ(busValueFromInteger(minimum - 5, 8), busValueFromBits("11111011"));
311}
312
313TEST(NumFormattingTest, EnforcesIntegerWidthsAndFormats)
314{
315 EXPECT_THROW((void)formatInteger(1, BusValueFormat::Unsigned, 0),
316 std::invalid_argument);
317 EXPECT_THROW((void)formatInteger(1, BusValueFormat::Raw, 128), std::invalid_argument);
318 EXPECT_THROW((void)formatInteger(1, BusValueFormat::Unknown, 128),
319 std::invalid_argument);
320 EXPECT_FALSE(parseInteger("1", BusValueFormat::Unsigned, 0));
321 EXPECT_FALSE(parseInteger("", BusValueFormat::Unsigned, 128));
322 EXPECT_FALSE(parseInteger("-1", BusValueFormat::Unsigned, 128));
323 EXPECT_FALSE(parseInteger("+", BusValueFormat::Signed, 128));
324 EXPECT_EQ(parseInteger("-1", BusValueFormat::Signed, 1), 1);
325 EXPECT_EQ(parseInteger("0", BusValueFormat::Signed, 1), 0);
326 EXPECT_FALSE(parseInteger("1", BusValueFormat::Signed, 1));
327}
ParsedBusValue valueFromStr(const std::string_view value)
Parse user-facing decimal, hexadecimal, octal, binary, or four-state text.
boost::multiprecision::cpp_int BigInt
std::optional< BigInt > parseInteger(std::string_view text, const BusValueFormat format, const std::size_t bitWidth)
std::optional< BusValue > resizeParsedValue(const ParsedBusValue &parsed, const std::size_t width)
Adapts a parsed textual value to an exact destination width.
BigInt integerFromBusValue(const BusValue &value, const Signedness signedness)
Decodes an LSB-first BusValue as an unsigned arbitrary-precision integer. Empty values yield zero....
BusValue maxValueForBusWidth(const std::size_t width)
BusValue busValueFromBits(const std::string_view bits)
Strictly decodes an MSB-first raw string containing 0, 1, X, or E.
std::string formatInteger(const BigInt &value, const BusValueFormat format, const std::size_t bitWidth)
BusValue busValueFromInteger(const BigInt &value, const std::size_t width)
Builds an LSB-first BusValue from the low width bits of an integer. Negative values use two's complem...
std::string formatValue(const BusValue &value, const BusValueFormat format, const std::size_t fixedWidth)
Format an LSB-first BusValue for display.
std::vector< State > BusValue
Definition wire.hpp:120
BusValue normalizeBusValue(const BusValue &value, const std::size_t width, const State extension)
Resize an LSB-first value to an exact bus width.
Definition wire.cpp:121
bool fitsUnsigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first value is representable as an unsigned value.
Definition wire.cpp:97
bool fitsSigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first two's-complement value fits a signed width.
Definition wire.cpp:105
TEST(NumFormattingTest, FormatsKnownValues)