/*! * Copyright 2019 XGBoost contributors */ #include #include "../../../src/common/bitfield.h" namespace xgboost { TEST(BitField, Check) { { std::vector storage(4, 0); storage[2] = 2; auto bits = LBitField64({storage.data(), static_cast::index_type>( storage.size())}); size_t true_bit = 190; for (size_t i = true_bit + 1; i < bits.Size(); ++i) { ASSERT_FALSE(bits.Check(i)); } ASSERT_TRUE(bits.Check(true_bit)); for (size_t i = 0; i < true_bit; ++i) { ASSERT_FALSE(bits.Check(i)); } } { std::vector storage(4, 0); storage[2] = 1 << 3; auto bits = RBitField8({storage.data(), static_cast::index_type>( storage.size())}); size_t true_bit = 19; for (size_t i = 0; i < true_bit; ++i) { ASSERT_FALSE(bits.Check(i)); } ASSERT_TRUE(bits.Check(true_bit)); for (size_t i = true_bit + 1; i < bits.Size(); ++i) { ASSERT_FALSE(bits.Check(i)); } } { // regression test for correct index type. std::vector storage(33, 0); storage[32] = static_cast(1); auto bits = RBitField8({storage.data(), storage.size()}); ASSERT_TRUE(bits.Check(256)); } } template void TestBitFieldSet(typename BitFieldT::value_type res, size_t index, size_t true_bit) { using IndexT = typename common::Span::index_type; std::vector storage(4, 0); auto bits = BitFieldT({storage.data(), static_cast(storage.size())}); bits.Set(true_bit); for (size_t i = 0; i < true_bit; ++i) { ASSERT_FALSE(bits.Check(i)); } ASSERT_TRUE(bits.Check(true_bit)); for (size_t i = true_bit + 1; i < storage.size() * BitFieldT::kValueSize; ++i) { ASSERT_FALSE(bits.Check(i)); } ASSERT_EQ(storage[index], res); } TEST(BitField, Set) { { TestBitFieldSet(2, 2, 190); } { TestBitFieldSet(1 << 3, 2, 19); } } template void TestBitFieldClear(size_t clear_bit) { using IndexT = typename common::Span::index_type; std::vector storage(4, 0); auto bits = BitFieldT({storage.data(), static_cast(storage.size())}); bits.Set(clear_bit); bits.Clear(clear_bit); ASSERT_FALSE(bits.Check(clear_bit)); } TEST(BitField, Clear) { { TestBitFieldClear(190); } { TestBitFieldClear(19); } } } // namespace xgboost