1 | /*
|
---|
2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <bitset>
|
---|
30 | #include <initializer_list>
|
---|
31 | #include <internal/test/tests.hpp>
|
---|
32 | #include <sstream>
|
---|
33 | #include <string>
|
---|
34 |
|
---|
35 | namespace std::test
|
---|
36 | {
|
---|
37 | bool bitset_test::run(bool report)
|
---|
38 | {
|
---|
39 | report_ = report;
|
---|
40 | start();
|
---|
41 |
|
---|
42 | test_constructors_and_assignment();
|
---|
43 | test_strings();
|
---|
44 | test_operations();
|
---|
45 |
|
---|
46 | return end();
|
---|
47 | }
|
---|
48 |
|
---|
49 | const char* bitset_test::name()
|
---|
50 | {
|
---|
51 | return "bitset";
|
---|
52 | }
|
---|
53 |
|
---|
54 | void bitset_test::test_constructors_and_assignment()
|
---|
55 | {
|
---|
56 | // 00101010bin == 42dec (note that the bits are reversed in bitset)
|
---|
57 | auto check1 = {false, true, false, true, false, true, false, false};
|
---|
58 | std::bitset<8> b1{42};
|
---|
59 | test_eq(
|
---|
60 | "from number to number equivalence",
|
---|
61 | b1.to_ulong(), 42UL
|
---|
62 | );
|
---|
63 |
|
---|
64 | auto it = check1.begin();
|
---|
65 | size_t i{};
|
---|
66 | for (; i < 8U; ++i)
|
---|
67 | {
|
---|
68 | if (*it++ != b1[i])
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | test_eq("from number iterating over bits", i, 8U);
|
---|
72 |
|
---|
73 | std::bitset<8> b2{"00101010"};
|
---|
74 | test_eq(
|
---|
75 | "from string to number equivalence",
|
---|
76 | b2.to_ulong(), 42UL
|
---|
77 | );
|
---|
78 |
|
---|
79 | it = check1.begin();
|
---|
80 | i = size_t{};
|
---|
81 | for (; i < 8U; ++i)
|
---|
82 | {
|
---|
83 | if (*it++ != b2[i])
|
---|
84 | break;
|
---|
85 | }
|
---|
86 | test_eq("from string iterating over bits", i, 8U);
|
---|
87 |
|
---|
88 | std::bitset<16> b3{0b1111'1101'1011'1010};
|
---|
89 | test_eq(
|
---|
90 | "from binary to number equivalence",
|
---|
91 | b3.to_ulong(), 0b1111'1101'1011'1010UL
|
---|
92 | );
|
---|
93 |
|
---|
94 | std::bitset<64> b4{0xABCD'DCBA'DEAD'BEEF};
|
---|
95 | test_eq(
|
---|
96 | "from hex to number equivalence",
|
---|
97 | b4.to_ulong(), 0xABCD'DCBA'DEAD'BEEF
|
---|
98 | );
|
---|
99 |
|
---|
100 | std::bitset<8> b5{"XXYXYXYX", 8U, 'X', 'Y'};
|
---|
101 | test_eq(
|
---|
102 | "from string with different 0/1 values equivalence",
|
---|
103 | b5.to_ulong(), 42U
|
---|
104 | );
|
---|
105 |
|
---|
106 | std::bitset<8> b6{"XXYXYXYXXXX IGNORED", 8U, 'X', 'Y'};
|
---|
107 | test_eq(
|
---|
108 | "from prefix string with different 0/1 values equivalence",
|
---|
109 | b6.to_ulong(), 42U
|
---|
110 | );
|
---|
111 |
|
---|
112 | std::bitset<8> b7{std::string{"XXXXYXYXYX"}, 2U, 8U, 'X', 'Y'};
|
---|
113 | test_eq(
|
---|
114 | "from substring with different 0/1 values equivalence",
|
---|
115 | b7.to_ulong(), 42U
|
---|
116 | );
|
---|
117 | }
|
---|
118 |
|
---|
119 | void bitset_test::test_strings()
|
---|
120 | {
|
---|
121 | std::bitset<8> b1{42};
|
---|
122 | auto s1 = b1.to_string();
|
---|
123 | test_eq(
|
---|
124 | "to string",
|
---|
125 | s1, "00101010"
|
---|
126 | );
|
---|
127 |
|
---|
128 | auto s2 = b1.to_string('X', 'Y');
|
---|
129 | test_eq(
|
---|
130 | "to string string with different 0/1 values",
|
---|
131 | s2, "XXYXYXYX"
|
---|
132 | );
|
---|
133 |
|
---|
134 | std::istringstream iss{"00101010"};
|
---|
135 | std::bitset<8> b2{};
|
---|
136 | iss >> b2;
|
---|
137 | test_eq(
|
---|
138 | "istream operator>>",
|
---|
139 | b2.to_ulong(), 42UL
|
---|
140 | );
|
---|
141 |
|
---|
142 | std::ostringstream oss{};
|
---|
143 | oss << b2;
|
---|
144 | std::string s3{oss.str()};
|
---|
145 | std::string s4{"00101010"};
|
---|
146 | test_eq(
|
---|
147 | "ostream operator<<",
|
---|
148 | s3, s4
|
---|
149 | );
|
---|
150 | }
|
---|
151 |
|
---|
152 | void bitset_test::test_operations()
|
---|
153 | {
|
---|
154 | std::bitset<8> b1{};
|
---|
155 |
|
---|
156 | b1.set(3);
|
---|
157 | test_eq("set", b1[3], true);
|
---|
158 |
|
---|
159 | b1.reset(3);
|
---|
160 | test_eq("reset", b1[3], false);
|
---|
161 |
|
---|
162 | b1.flip(3);
|
---|
163 | test_eq("flip", b1[3], true);
|
---|
164 |
|
---|
165 | b1 >>= 1;
|
---|
166 | test_eq("lshift new", b1[2], true);
|
---|
167 | test_eq("lshift old", b1[3], false);
|
---|
168 |
|
---|
169 | b1 <<= 1;
|
---|
170 | test_eq("rshift new", b1[2], false);
|
---|
171 | test_eq("rshift old", b1[3], true);
|
---|
172 |
|
---|
173 | test_eq("any1", b1.any(), true);
|
---|
174 | test_eq("none1", b1.none(), false);
|
---|
175 | test_eq("all1", b1.all(), false);
|
---|
176 |
|
---|
177 | b1 <<= 7;
|
---|
178 | test_eq("any2", b1.any(), false);
|
---|
179 | test_eq("none2", b1.none(), true);
|
---|
180 | test_eq("all2", b1.all(), false);
|
---|
181 |
|
---|
182 | b1.set();
|
---|
183 | test_eq("set + all", b1.all(), true);
|
---|
184 |
|
---|
185 | b1.reset();
|
---|
186 | test_eq("reset + none", b1.none(), true);
|
---|
187 |
|
---|
188 | std::bitset<8> b2{0b0101'0101};
|
---|
189 | std::bitset<8> b3{0b1010'1010};
|
---|
190 | b2.flip();
|
---|
191 | test_eq("flip all", b2, b3);
|
---|
192 |
|
---|
193 | std::bitset<8> b4{0b0011'0101};
|
---|
194 | std::bitset<8> b5{0b1010'1100};
|
---|
195 | test_eq("and", (b4 & b5), std::bitset<8>{0b0010'0100});
|
---|
196 | test_eq("or", (b4 | b5), std::bitset<8>{0b1011'1101});
|
---|
197 | test_eq("count", b4.count(), 4U);
|
---|
198 | }
|
---|
199 | }
|
---|