Repository navigation
Expand file tree
/
Copy pathbitformat_test.cpp
More file actions
104 lines (87 loc) · 3.29 KB
/
Copy pathbitformat_test.cpp
File metadata and controls
104 lines (87 loc) · 3.29 KB
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
/*
Copyright 2013-2026 Mike Bergmann
This file is part of Registerer.
Registerer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Registerer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Registerer. If not, see <http://www.gnu.org/licenses/>.
*/
#include "bitformat.h"
#include <gtest/gtest.h>
using namespace registerer;
TEST(Bitformat, Parse)
{
EXPECT_TRUE(parse("0b1010") == (Value{ .number = 10, .base = Base::Bin }));
EXPECT_TRUE(parse("0xFF") == (Value{ .number = 255, .base = Base::Hex }));
EXPECT_TRUE(parse(" 42 ") == (Value{ .number = 42, .base = Base::Dec }));
EXPECT_TRUE(parse("0B11") == (Value{ .number = 3, .base = Base::Bin }));
}
TEST(Bitformat, ParseRejectsInvalid)
{
EXPECT_FALSE(parse(""));
EXPECT_FALSE(parse(" "));
EXPECT_FALSE(parse("0b12")); // 2 is not a binary digit
EXPECT_FALSE(parse("12ab")); // trailing garbage
EXPECT_FALSE(parse("-1")); // unsigned only
EXPECT_FALSE(parse("0x1ffffffffffffffff")); // 65 bits: refused, not truncated
}
TEST(Bitformat, FullRangeRoundTrips)
{
const Value max{ .number = UINT64_MAX, .base = Base::Hex };
EXPECT_EQ(format(max), "0xffffffffffffffff");
EXPECT_TRUE(parse(format(max)) == max);
}
TEST(Bitformat, BaseCycle)
{
EXPECT_EQ(next(Base::Dec), Base::Hex);
EXPECT_EQ(next(Base::Hex), Base::Bin);
EXPECT_EQ(next(Base::Bin), Base::Dec);
}
TEST(Bitformat, Toggle)
{
EXPECT_EQ(format({ .number = toggle(0, 31), .base = Base::Hex }), "0x80000000");
EXPECT_EQ(format({ .number = toggle(0, 15), .base = Base::Bin }), "0b1000000000000000");
EXPECT_EQ(toggle(toggle(0xF0, 3), 3), 0xF0u);
EXPECT_EQ(mask(63), std::uint64_t{ 1 } << 63);
EXPECT_EQ(mask(64), 0u);
EXPECT_EQ(mask(-1), 0u);
}
TEST(Bitformat, Layout)
{
const Rows r = layout(0xAA, 3);
EXPECT_EQ(r.bin, "1010 1010 ");
EXPECT_EQ(r.pos, " ^ ");
EXPECT_EQ(r.hex, " a a ");
EXPECT_EQ(r.dec, " 170");
EXPECT_EQ(r.dec.size(), r.bin.size() - 1);
}
TEST(Bitformat, LayoutEdgeCases)
{
EXPECT_EQ(layout(0, 0).bin, "0000 ");
EXPECT_EQ(layout(0, 0).pos, " ^ ");
EXPECT_EQ(layout(0x1FF, 8).bin, "0001 1111 1111 ");
EXPECT_EQ(layout(0x1FF, 8).pos, " ^ ");
EXPECT_EQ(layout(1, 40).pos, " ");
}
TEST(Bitformat, LayoutWidest)
{
// Widest case: every column stays aligned.
const Rows wide = layout(UINT64_MAX, 63);
EXPECT_EQ(wide.bin.size(), 16u * 5);
EXPECT_EQ(wide.pos.size(), wide.bin.size());
EXPECT_EQ(wide.hex.size(), wide.bin.size());
EXPECT_EQ(wide.pos.front(), '^');
EXPECT_EQ(wide.hex,
std::string(3, ' ')
+ "f f f f f f f f"
" f f f f f f f"
" f ");
EXPECT_EQ(wide.dec.size(), wide.bin.size() - 1);
EXPECT_TRUE(wide.dec.ends_with("18446744073709551615"));
}