diff --git a/dynamic_bitset.html b/dynamic_bitset.html index b7dbd79..a707627 100644 --- a/dynamic_bitset.html +++ b/dynamic_bitset.html @@ -1250,6 +1250,24 @@ bool none() const returns false.
Throws: nothing. +
+
+bool at(size_type n) const
+
+ +Precondition: n < this->size().
+Returns: The same as operator[](n).
+Throws: std::out_of_range if that n is not within the range of the bitset. + +
+
+reference at(size_type n)
+
+ +Precondition: n < this->size().
+Returns: The same as operator[](n).
+Throws: std::out_of_range if that n is not within the range of the bitset. +
 bool test(size_type n) const
diff --git a/test/bitset_test.hpp b/test/bitset_test.hpp
index 0b42069..feb2c2a 100644
--- a/test/bitset_test.hpp
+++ b/test/bitset_test.hpp
@@ -1158,6 +1158,65 @@ struct bitset_test {
     BOOST_TEST((lhs >> pos) == (x >>= pos));
   }
 
+  static void at(const Bitset& lhs, const std::vector& 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();
+      }
+  }
+
   // operator|
   static
   void operator_or(const Bitset& lhs, const Bitset& rhs)
diff --git a/test/dyn_bitset_unit_tests1.cpp b/test/dyn_bitset_unit_tests1.cpp
index c722701..4f0a7aa 100644
--- a/test/dyn_bitset_unit_tests1.cpp
+++ b/test/dyn_bitset_unit_tests1.cpp
@@ -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 b1;
+      std::vector bitvec1;
+      Tests::at(b1, bitvec1);
+  }
+  {
+      boost::dynamic_bitset b(std::string("1"));
+      std::vector bit_vec(1, true);
+      Tests::at(b, bit_vec);
+  }
+  {
+      boost::dynamic_bitset b(long_string);
+      std::size_t n = long_string.size();
+      std::vector 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