mirror of
https://github.com/boostorg/dynamic_bitset.git
synced 2026-01-19 04:12:09 +00:00
Add test_set() (#6164).
This commit is contained in:
@@ -960,6 +960,25 @@ struct bitset_test {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_set_bit(const Bitset& b, std::size_t pos, bool value)
|
||||
{
|
||||
Bitset lhs(b);
|
||||
std::size_t N = lhs.size();
|
||||
if (pos < N) {
|
||||
Bitset prev(lhs);
|
||||
// Stores a new value in the bit at position pos in lhs.
|
||||
BOOST_CHECK(lhs.test_set(pos, value) == prev[pos]);
|
||||
BOOST_CHECK(lhs[pos] == value);
|
||||
|
||||
// All other values of lhs remain unchanged
|
||||
for (std::size_t I = 0; I < N; ++I)
|
||||
if (I != pos)
|
||||
BOOST_CHECK(lhs[I] == prev[I]);
|
||||
} else {
|
||||
// Not in range, doesn't satisfy precondition.
|
||||
}
|
||||
}
|
||||
|
||||
static void operator_shift_left(const Bitset& lhs, std::size_t pos)
|
||||
{
|
||||
Bitset x(lhs);
|
||||
|
||||
@@ -611,6 +611,23 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
|
||||
Tests::test_bit(b, long_string.size()/2);
|
||||
}
|
||||
//=====================================================================
|
||||
// Test b.test_set(pos)
|
||||
{ // case pos >= b.size()
|
||||
boost::dynamic_bitset<Block> b;
|
||||
Tests::test_set_bit(b, 0, true);
|
||||
Tests::test_set_bit(b, 0, false);
|
||||
}
|
||||
{ // case pos < b.size()
|
||||
boost::dynamic_bitset<Block> b(std::string("0"));
|
||||
Tests::test_set_bit(b, 0, true);
|
||||
Tests::test_set_bit(b, 0, false);
|
||||
}
|
||||
{ // case pos == b.size() / 2
|
||||
boost::dynamic_bitset<Block> b(long_string);
|
||||
Tests::test_set_bit(b, long_string.size() / 2, true);
|
||||
Tests::test_set_bit(b, long_string.size() / 2, false);
|
||||
}
|
||||
//=====================================================================
|
||||
// Test b << pos
|
||||
{ // case pos == 0
|
||||
std::size_t pos = 0;
|
||||
|
||||
@@ -202,6 +202,7 @@ public:
|
||||
dynamic_bitset& <a href="#flip2">flip</a>(size_type n);
|
||||
dynamic_bitset& <a href="#flip1">flip</a>();
|
||||
bool <a href="#test">test</a>(size_type n) const;
|
||||
bool <a href="#test">test_set</a>(size_type n, bool val = true);
|
||||
bool <a href="#all">all</a>() const;
|
||||
bool <a href="#any">any</a>() const;
|
||||
bool <a href="#none">none</a>() const;
|
||||
@@ -1159,6 +1160,18 @@ bool <a id="test">test</a>(size_type n) const
|
||||
<b>Returns:</b> <tt>true</tt> if bit <tt>n</tt> is set and
|
||||
<tt>false</tt> is bit <tt>n</tt> is 0.
|
||||
|
||||
<hr />
|
||||
<pre>
|
||||
bool <a id="test">test_set</a>(size_type n, bool val = true)
|
||||
</pre>
|
||||
|
||||
<b>Precondition:</b> <tt>n < this->size()</tt>.<br />
|
||||
<b>Effects:</b> Sets bit <tt>n</tt> if <tt>val</tt> is
|
||||
<tt>true</tt>, and clears bit <tt>n</tt> if <tt>val</tt> is
|
||||
<tt>false</tt>. <br />
|
||||
<b>Returns:</b> <tt>true</tt> if the previous state of bit
|
||||
<tt>n</tt> was set and <tt>false</tt> is bit <tt>n</tt> is 0.
|
||||
|
||||
<hr />
|
||||
<pre>
|
||||
reference <a id="bracket">operator[]</a>(size_type n)
|
||||
|
||||
@@ -278,6 +278,7 @@ public:
|
||||
dynamic_bitset& flip(size_type n);
|
||||
dynamic_bitset& flip();
|
||||
bool test(size_type n) const;
|
||||
bool test_set(size_type n, bool val = true);
|
||||
bool all() const;
|
||||
bool any() const;
|
||||
bool none() const;
|
||||
@@ -1002,6 +1003,16 @@ bool dynamic_bitset<Block, Allocator>::test(size_type pos) const
|
||||
return m_unchecked_test(pos);
|
||||
}
|
||||
|
||||
template <typename Block, typename Allocator>
|
||||
bool dynamic_bitset<Block, Allocator>::test_set(size_type pos, bool val)
|
||||
{
|
||||
bool const b = test(pos);
|
||||
if (b != val) {
|
||||
set(pos, val);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
template <typename Block, typename Allocator>
|
||||
bool dynamic_bitset<Block, Allocator>::all() const
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user