Compare commits

..

6 Commits

Author SHA1 Message Date
Alexander Grund
8e20aa1462 Fix ccache saving on cache hit (#70)
See boostorg/boost-ci#166
2022-06-24 08:58:29 -07:00
akr
d4be7a4fcb Changed the order of at() 2022-05-02 20:38:40 -04:00
akr
29e1d40e88 Added std::out_of_range tests 2022-05-02 20:38:40 -04:00
akr
ccca25537a Updated docs 2022-05-02 20:38:40 -04:00
akr
12be4b4464 Added tests 2022-05-02 20:38:40 -04:00
akr
b15fd0075f Added at(size_type) 2022-05-02 20:38:40 -04:00
5 changed files with 156 additions and 4 deletions

View File

@@ -126,20 +126,23 @@ jobs:
fi
git config --global pack.threads 0
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
# For coverage builds fetch the whole history, else only 1 commit using a 'fake ternary'
fetch-depth: ${{ matrix.coverage && '0' || '1' }}
- name: Cache ccache
uses: actions/cache@v2
uses: actions/cache@v3
if: env.B2_USE_CCACHE
with:
path: ~/.ccache
key: ${{matrix.os}}-${{matrix.container}}-${{matrix.compiler}}
key: ${{matrix.os}}-${{matrix.container}}-${{matrix.compiler}}-${{github.sha}}
restore-keys: |
${{matrix.os}}-${{matrix.container}}-${{matrix.compiler}}-
${{matrix.os}}-${{matrix.container}}-${{matrix.compiler}}
- name: Fetch Boost.CI
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
repository: boostorg/boost-ci
ref: master

View File

@@ -210,6 +210,8 @@ public:
dynamic_bitset&amp; <a href="#flip3">flip</a>(size_type n, size_type len);
dynamic_bitset&amp; <a href="#flip2">flip</a>(size_type n);
dynamic_bitset&amp; <a href="#flip1">flip</a>();
reference <a href="#at">at</a>(size_type n);
bool <a href="#const-at">at</a>(size_type n) const;
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;
@@ -1250,6 +1252,24 @@ bool <a id="none">none</a>() const
returns <tt>false</tt>.<br />
<b>Throws:</b> nothing.
<hr />
<pre>
reference <a id="at">at</a>(size_type n)
</pre>
<b>Precondition:</b> <tt>n &lt; this-&gt;size()</tt>.<br />
<b>Returns:</b> The same as <tt>operator[](n)</tt>.<br />
<b>Throws:</b> <tt>std::out_of_range</tt> if that <tt>n</tt> is not within the range of the bitset.
<hr />
<pre>
bool <a id="const-at">at</a>(size_type n) const
</pre>
<b>Precondition:</b> <tt>n &lt; this-&gt;size()</tt>.<br />
<b>Returns:</b> The same as <tt>operator[](n)</tt>.<br />
<b>Throws:</b> <tt>std::out_of_range</tt> if that <tt>n</tt> is not within the range of the bitset.
<hr />
<pre>
bool <a id="test">test</a>(size_type n) const

View File

@@ -293,6 +293,8 @@ public:
dynamic_bitset& flip(size_type n, size_type len);
dynamic_bitset& flip(size_type n);
dynamic_bitset& flip();
reference at(size_type n);
bool at(size_type n) const;
bool test(size_type n) const;
bool test_set(size_type n, bool val = true);
bool all() const;
@@ -1114,6 +1116,25 @@ bool dynamic_bitset<Block, Allocator>::m_unchecked_test(size_type pos) const
return (m_bits[block_index(pos)] & bit_mask(pos)) != 0;
}
template <typename Block, typename Allocator>
typename dynamic_bitset<Block, Allocator>::reference
dynamic_bitset<Block, Allocator>::at(size_type pos)
{
if (pos >= m_num_bits)
BOOST_THROW_EXCEPTION(std::out_of_range("boost::dynamic_bitset::at out_of_range"));
return (*this)[pos];
}
template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::at(size_type pos) const
{
if (pos >= m_num_bits)
BOOST_THROW_EXCEPTION(std::out_of_range("boost::dynamic_bitset::at out_of_range"));
return (*this)[pos];
}
template <typename Block, typename Allocator>
bool dynamic_bitset<Block, Allocator>::test(size_type pos) const
{

View File

@@ -1158,6 +1158,94 @@ struct bitset_test {
BOOST_TEST((lhs >> pos) == (x >>= pos));
}
static void at(const Bitset& lhs, const std::vector<bool>& bit_vec)
{
Bitset b(lhs);
std::size_t i, j, k;
// x = b.at(i)
// x = ~b.at(i)
for (i = 0; i < b.size(); ++i)
{
bool x = b.at(i);
BOOST_TEST(x == bit_vec.at(i));
x = ~b.at(i);
BOOST_TEST(x == !bit_vec.at(i));
}
Bitset prev(b);
// b.at(i) = x
for (j = 0; j < b.size(); ++j)
{
bool x = !prev.at(j);
b.at(j) = x;
for (k = 0; k < b.size(); ++k)
if (j == k)
BOOST_TEST(b.at(k) == x);
else
BOOST_TEST(b.at(k) == prev.at(k));
b.at(j) = prev.at(j);
}
b.flip();
// b.at(i) = b.at(j)
for (i = 0; i < b.size(); ++i)
{
b.at(i) = prev.at(i);
for (j = 0; j < b.size(); ++j)
{
if (i == j)
BOOST_TEST(b.at(j) == prev.at(j));
else
BOOST_TEST(b.at(j) == !prev.at(j));
}
b.at(i) = !prev.at(i);
}
// b.at(i).flip()
for (i = 0; i < b.size(); ++i)
{
b.at(i).flip();
for (j = 0; j < b.size(); ++j)
{
if (i == j)
BOOST_TEST(b.at(j) == prev.at(j));
else
BOOST_TEST(b.at(j) == !prev.at(j));
}
b.at(i).flip();
}
// b.at(b.size())
bool will_out_of_range = false;
for (i = 0; i <= b.size(); ++i)
{
if (i == b.size())
{
will_out_of_range = true;
break;
}
b.at(i);
}
if (will_out_of_range)
{
try
{
b.at(b.size());
BOOST_TEST(false); // It should have thrown an exception
}
catch (const std::out_of_range& ex)
{
// Good!
BOOST_TEST(!!ex.what());
}
catch (...)
{
BOOST_TEST(false); // threw the wrong exception
}
}
}
// operator|
static
void operator_or(const Bitset& lhs, const Bitset& rhs)

View File

@@ -509,6 +509,26 @@ void run_test_cases( BOOST_EXPLICIT_TEMPLATE_TYPE(Block) )
bit_vec[i] = long_string[n - 1 - i] == '0' ? 0 : 1;
Tests::operator_bracket(b, bit_vec);
}
//=====================================================================
// Test at
{
boost::dynamic_bitset<Block> b1;
std::vector<bool> bitvec1;
Tests::at(b1, bitvec1);
}
{
boost::dynamic_bitset<Block> b(std::string("1"));
std::vector<bool> bit_vec(1, true);
Tests::at(b, bit_vec);
}
{
boost::dynamic_bitset<Block> b(long_string);
std::size_t n = long_string.size();
std::vector<bool> bit_vec(n);
for (std::size_t i = 0; i < n; ++i)
bit_vec[i] = long_string[n - 1 - i] == '0' ? 0 : 1;
Tests::at(b, bit_vec);
}
#if !defined(BOOST_NO_CXX11_ALLOCATOR)
{
typedef boost::dynamic_bitset<Block,