// These helper functions are only used by the test, // so they are not in the main BitString class.
std::string Stringify(BitString bit_string) {
std::stringstream ss;
ss << bit_string; return ss.str();
}
// Make max bitstring, e.g. BitString[4095,15,2047] for {12,4,11} template <size_t kCount = BitString::kCapacity>
BitString MakeBitStringMax() {
BitString bs{};
for (size_t i = 0; i < kCount; ++i) {
bs.SetAt(i,
MakeBitStringChar(i, MaxInt<BitStringChar::StorageType>(BitString::kBitSizeAtPosition[i])));
}
// TODO: Consider removing this test, it's kind of replicating the logic in GetLsbForPosition().
TEST(InstanceOfBitString, GetLsbForPosition) {
ASSERT_LE(3u, BitString::kCapacity); // Test will fail if kCapacity is not at least 3. Update it.
EXPECT_EQ(0u, BitString::GetLsbForPosition(0u));
EXPECT_EQ(BitString::kBitSizeAtPosition[0u], BitString::GetLsbForPosition(1u));
EXPECT_EQ(BitString::kBitSizeAtPosition[0u] + BitString::kBitSizeAtPosition[1u],
BitString::GetLsbForPosition(2u));
}
// There should be at least "kCapacity" # of checks here, 1 for each unique position.
EXPECT_EQ(MakeBitStringChar(/*idx=*/0, /*val=*/1u), bs[0]);
EXPECT_EQ(MakeBitStringChar(/*idx=*/1, /*val=*/2u), bs[1]);
EXPECT_EQ(MakeBitStringChar(/*idx=*/2, /*val=*/3u), bs[2]);
// Each maximal value should be tested here for each position.
uint32_t max_bitstring_ints[] = {
MaxInt<uint32_t>(12),
MaxInt<uint32_t>(4),
MaxInt<uint32_t>(11),
};
// Update tests if changing the tuning values above. for (size_t i = 0; i < arraysize(max_bitstring_ints); ++i) {
ASSERT_EQ(MinimumBitsToStore(max_bitstring_ints[i]), BitString::kBitSizeAtPosition[i]) << i;
}
BitString bs_max = MakeBitStringMax();
for (size_t i = 0; i < arraysize(max_bitstring_ints); ++i) {
ASSERT_EQ(max_bitstring_ints[i], static_cast<uint32_t>(bs_max[i])) << i;
}
template <size_t kPos>
constexpr auto MaxForPos() { return MaxInt<BitString::StorageType>(BitString::kBitSizeAtPosition[kPos]);
}
TEST(InstanceOfBitString, MemoryRepresentation) { // Verify that the lower positions are stored in less significant bits.
BitString bs = MakeBitString({MaxForPos<0>(), MaxForPos<1>()});
BitString::StorageType as_int = static_cast<BitString::StorageType>(bs);
// Below tests assumes the capacity is at least 3.
ASSERT_LE(3u, BitString::kCapacity);
EXPECT_EQ((MaxForPos<0>() << 0) | (MaxForPos<1>() << BitString::kBitSizeAtPosition[0]),
as_int);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.