Skip to content
Snippets Groups Projects
Commit f1f21006 authored by Christian Engwer's avatar Christian Engwer
Browse files

Merge branch 'feature/boundschecking-for-bitsetvector' into 'master'

BitSetVector: Add and test bounds checking

See also Flyspray/FS#1030.

See merge request !34
parents 617bbb54 8136be0b
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@
#include <iostream>
#include <algorithm>
#include <dune/common/boundschecking.hh>
#include <dune/common/genericiterator.hh>
#include <dune/common/exceptions.hh>
......@@ -41,7 +42,9 @@ namespace Dune {
BitSetVectorConstReference(const BitSetVector& blockBitField_, int block_number_) :
blockBitField(blockBitField_),
block_number(block_number_)
{}
{
DUNE_ASSERT_BOUNDS(blockBitField_.size() > block_number_);
}
//! hide assignment operator
BitSetVectorConstReference& operator=(const BitSetVectorConstReference & b);
......@@ -588,10 +591,14 @@ namespace Dune {
}
typename std::vector<bool>::reference getBit(size_type i, size_type j) {
DUNE_ASSERT_BOUNDS(j < block_size);
DUNE_ASSERT_BOUNDS(i < size());
return BlocklessBaseClass::operator[](i*block_size+j);
}
typename std::vector<bool>::const_reference getBit(size_type i, size_type j) const {
DUNE_ASSERT_BOUNDS(j < block_size);
DUNE_ASSERT_BOUNDS(i < size());
return BlocklessBaseClass::operator[](i*block_size+j);
}
......
#include <config.h>
#include <dune/common/bitsetvector.hh>
#include <dune/common/diagonalmatrix.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
......@@ -199,6 +200,53 @@ int main() try {
<< ") All good: Exception thrown as expected." << std::endl;
}
// Read beyond end of bitsetvector
try {
Dune::BitSetVector<3> const b(10);
DUNE_UNUSED auto const x = b[10];
std::cout << "(line " << __LINE__ << ") Error: No exception thrown."
<< std::endl;
passed = false;
} catch (Dune::RangeError) {
std::cout << "(line " << __LINE__
<< ") All good: Exception thrown as expected." << std::endl;
}
// Write beyond end of bitsetvector
try {
Dune::BitSetVector<3> b(10);
b[10] = true;
std::cout << "(line " << __LINE__ << ") Error: No exception thrown."
<< std::endl;
passed = false;
} catch (Dune::RangeError) {
std::cout << "(line " << __LINE__
<< ") All good: Exception thrown as expected." << std::endl;
}
// Read beyond end of bitsetvectorreference
try {
Dune::BitSetVector<3> const b(10);
DUNE_UNUSED auto const x = b[10][3];
std::cout << "(line " << __LINE__ << ") Error: No exception thrown."
<< std::endl;
passed = false;
} catch (Dune::RangeError) {
std::cout << "(line " << __LINE__
<< ") All good: Exception thrown as expected." << std::endl;
}
// Write beyond end of bitsetvectorreference
try {
Dune::BitSetVector<3> b(10);
b[10][3] = true;
std::cout << "(line " << __LINE__ << ") Error: No exception thrown."
<< std::endl;
passed = false;
} catch (Dune::RangeError) {
std::cout << "(line " << __LINE__
<< ") All good: Exception thrown as expected." << std::endl;
}
return passed ? 0 : 1;
} catch (Dune::Exception &e) {
std::cerr << e << std::endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment