| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <__bits/test/tests.hpp>
|
|---|
| 8 | #include <bitset>
|
|---|
| 9 | #include <initializer_list>
|
|---|
| 10 | #include <sstream>
|
|---|
| 11 | #include <string>
|
|---|
| 12 |
|
|---|
| 13 | namespace std::test
|
|---|
| 14 | {
|
|---|
| 15 | bool bitset_test::run(bool report)
|
|---|
| 16 | {
|
|---|
| 17 | report_ = report;
|
|---|
| 18 | start();
|
|---|
| 19 |
|
|---|
| 20 | test_constructors_and_assignment();
|
|---|
| 21 | test_strings();
|
|---|
| 22 | test_operations();
|
|---|
| 23 |
|
|---|
| 24 | return end();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | const char* bitset_test::name()
|
|---|
| 28 | {
|
|---|
| 29 | return "bitset";
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | void bitset_test::test_constructors_and_assignment()
|
|---|
| 33 | {
|
|---|
| 34 | // 00101010bin == 42dec (note that the bits are reversed in bitset)
|
|---|
| 35 | auto check1 = {false, true, false, true, false, true, false, false};
|
|---|
| 36 | std::bitset<8> b1{42};
|
|---|
| 37 | test_eq(
|
|---|
| 38 | "from number to number equivalence",
|
|---|
| 39 | b1.to_ulong(), 42UL
|
|---|
| 40 | );
|
|---|
| 41 |
|
|---|
| 42 | auto it = check1.begin();
|
|---|
| 43 | size_t i{};
|
|---|
| 44 | for (; i < 8U; ++i)
|
|---|
| 45 | {
|
|---|
| 46 | if (*it++ != b1[i])
|
|---|
| 47 | break;
|
|---|
| 48 | }
|
|---|
| 49 | test_eq("from number iterating over bits", i, 8U);
|
|---|
| 50 |
|
|---|
| 51 | std::bitset<8> b2{"00101010"};
|
|---|
| 52 | test_eq(
|
|---|
| 53 | "from string to number equivalence",
|
|---|
| 54 | b2.to_ulong(), 42UL
|
|---|
| 55 | );
|
|---|
| 56 |
|
|---|
| 57 | it = check1.begin();
|
|---|
| 58 | i = size_t{};
|
|---|
| 59 | for (; i < 8U; ++i)
|
|---|
| 60 | {
|
|---|
| 61 | if (*it++ != b2[i])
|
|---|
| 62 | break;
|
|---|
| 63 | }
|
|---|
| 64 | test_eq("from string iterating over bits", i, 8U);
|
|---|
| 65 |
|
|---|
| 66 | std::bitset<16> b3{0b1111'1101'1011'1010};
|
|---|
| 67 | test_eq(
|
|---|
| 68 | "from binary to number equivalence",
|
|---|
| 69 | b3.to_ulong(), 0b1111'1101'1011'1010UL
|
|---|
| 70 | );
|
|---|
| 71 |
|
|---|
| 72 | std::bitset<64> b4{0xABCD'DCBA'DEAD'BEEFULL};
|
|---|
| 73 | test_eq(
|
|---|
| 74 | "from hex to number equivalence",
|
|---|
| 75 | b4.to_ulong(), static_cast<unsigned long>(0xABCD'DCBA'DEAD'BEEFULL)
|
|---|
| 76 | );
|
|---|
| 77 |
|
|---|
| 78 | std::bitset<8> b5{"XXYXYXYX", 8U, 'X', 'Y'};
|
|---|
| 79 | test_eq(
|
|---|
| 80 | "from string with different 0/1 values equivalence",
|
|---|
| 81 | b5.to_ulong(), 42U
|
|---|
| 82 | );
|
|---|
| 83 |
|
|---|
| 84 | std::bitset<8> b6{"XXYXYXYXXXX IGNORED", 8U, 'X', 'Y'};
|
|---|
| 85 | test_eq(
|
|---|
| 86 | "from prefix string with different 0/1 values equivalence",
|
|---|
| 87 | b6.to_ulong(), 42U
|
|---|
| 88 | );
|
|---|
| 89 |
|
|---|
| 90 | std::bitset<8> b7{std::string{"XXXXYXYXYX"}, 2U, 8U, 'X', 'Y'};
|
|---|
| 91 | test_eq(
|
|---|
| 92 | "from substring with different 0/1 values equivalence",
|
|---|
| 93 | b7.to_ulong(), 42U
|
|---|
| 94 | );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | void bitset_test::test_strings()
|
|---|
| 98 | {
|
|---|
| 99 | std::bitset<8> b1{42};
|
|---|
| 100 | auto s1 = b1.to_string();
|
|---|
| 101 | test_eq(
|
|---|
| 102 | "to string",
|
|---|
| 103 | s1, "00101010"
|
|---|
| 104 | );
|
|---|
| 105 |
|
|---|
| 106 | auto s2 = b1.to_string('X', 'Y');
|
|---|
| 107 | test_eq(
|
|---|
| 108 | "to string string with different 0/1 values",
|
|---|
| 109 | s2, "XXYXYXYX"
|
|---|
| 110 | );
|
|---|
| 111 |
|
|---|
| 112 | std::istringstream iss{"00101010"};
|
|---|
| 113 | std::bitset<8> b2{};
|
|---|
| 114 | iss >> b2;
|
|---|
| 115 | test_eq(
|
|---|
| 116 | "istream operator>>",
|
|---|
| 117 | b2.to_ulong(), 42UL
|
|---|
| 118 | );
|
|---|
| 119 |
|
|---|
| 120 | std::ostringstream oss{};
|
|---|
| 121 | oss << b2;
|
|---|
| 122 | std::string s3{oss.str()};
|
|---|
| 123 | std::string s4{"00101010"};
|
|---|
| 124 | test_eq(
|
|---|
| 125 | "ostream operator<<",
|
|---|
| 126 | s3, s4
|
|---|
| 127 | );
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void bitset_test::test_operations()
|
|---|
| 131 | {
|
|---|
| 132 | std::bitset<8> b1{};
|
|---|
| 133 |
|
|---|
| 134 | b1.set(3);
|
|---|
| 135 | test_eq("set", b1[3], true);
|
|---|
| 136 |
|
|---|
| 137 | b1.reset(3);
|
|---|
| 138 | test_eq("reset", b1[3], false);
|
|---|
| 139 |
|
|---|
| 140 | b1.flip(3);
|
|---|
| 141 | test_eq("flip", b1[3], true);
|
|---|
| 142 |
|
|---|
| 143 | b1 >>= 1;
|
|---|
| 144 | test_eq("lshift new", b1[2], true);
|
|---|
| 145 | test_eq("lshift old", b1[3], false);
|
|---|
| 146 |
|
|---|
| 147 | b1 <<= 1;
|
|---|
| 148 | test_eq("rshift new", b1[2], false);
|
|---|
| 149 | test_eq("rshift old", b1[3], true);
|
|---|
| 150 |
|
|---|
| 151 | test_eq("any1", b1.any(), true);
|
|---|
| 152 | test_eq("none1", b1.none(), false);
|
|---|
| 153 | test_eq("all1", b1.all(), false);
|
|---|
| 154 |
|
|---|
| 155 | /* b1 <<= 7; */
|
|---|
| 156 | /* test_eq("any2", b1.any(), false); */
|
|---|
| 157 | /* test_eq("none2", b1.none(), true); */
|
|---|
| 158 | /* test_eq("all2", b1.all(), false); */
|
|---|
| 159 |
|
|---|
| 160 | b1.set();
|
|---|
| 161 | test_eq("set + all", b1.all(), true);
|
|---|
| 162 |
|
|---|
| 163 | b1.reset();
|
|---|
| 164 | test_eq("reset + none", b1.none(), true);
|
|---|
| 165 |
|
|---|
| 166 | std::bitset<8> b2{0b0101'0101};
|
|---|
| 167 | std::bitset<8> b3{0b1010'1010};
|
|---|
| 168 | b2.flip();
|
|---|
| 169 | test_eq("flip all", b2, b3);
|
|---|
| 170 |
|
|---|
| 171 | std::bitset<8> b4{0b0011'0101};
|
|---|
| 172 | std::bitset<8> b5{0b1010'1100};
|
|---|
| 173 | test_eq("and", (b4 & b5), std::bitset<8>{0b0010'0100});
|
|---|
| 174 | test_eq("or", (b4 | b5), std::bitset<8>{0b1011'1101});
|
|---|
| 175 | test_eq("count", b4.count(), 4U);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|