Commit 1468dd9e authored by Yoshitaka Aharen's avatar Yoshitaka Aharen
Browse files

[trac510] throw exceptions instead of assertion for parameter check

parent 3582ccf1
#include <vector>
#include <cassert>
#include <boost/noncopyable.hpp>
......@@ -25,21 +24,27 @@ class CounterImpl : boost::noncopyable {
CounterImpl::CounterImpl(const size_t items) :
counters_(items, InitialValue)
{
assert(items != 0);
if (items == 0) {
isc_throw(isc::InvalidParameter, "Items must not be 0");
}
}
CounterImpl::~CounterImpl() {}
void
CounterImpl::inc(const Counter::Type& type) {
assert(type < counters_.size());
if(type >= counters_.size()) {
isc_throw(isc::OutOfRange, "Counter type is out of range");
}
++counters_.at(type);
return;
}
const Counter::Value&
CounterImpl::get(const Counter::Type& type) const {
assert(type < counters_.size());
if(type >= counters_.size()) {
isc_throw(isc::OutOfRange, "Counter type is out of range");
}
return (counters_.at(type));
}
......
......@@ -4,6 +4,8 @@
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <exceptions/exceptions.h>
namespace isc {
namespace statistics {
......@@ -21,11 +23,10 @@ class Counter : boost::noncopyable {
///
/// This constructor is mostly exception free. But it may still throw
/// a standard exception if memory allocation fails inside the method.
///
/// Note: \a items must be greater than 0. Otherwise this constructor
/// causes an assertion failure.
///
/// \param items A number of counter items to hold (greater than 0)
///
/// \throw isc::InvalidParameter \a items is 0
Counter(const size_t items);
/// The destructor.
......@@ -34,16 +35,17 @@ class Counter : boost::noncopyable {
~Counter();
/// \brief Increment a counter item specified with \a type.
///
/// Note: \a type must be valid counter type. Otherwise this method
/// causes an assertion failure.
///
/// \param type %Counter item to increment
///
/// \throw isc::OutOfRange \a type is invalid
void inc(const Type& type);
/// \brief Get the value of a counter item specified with \a type.
///
/// \param type %Counter item to get the value of
///
/// \throw isc::OutOfRange \a type is invalid
const Value& get(const Type& type) const;
};
......
......@@ -41,8 +41,10 @@ class CounterDictionaryImpl : boost::noncopyable {
CounterDictionaryImpl::CounterDictionaryImpl(const size_t items) :
items_(items)
{
// The number of the items must not be 0.
assert(items != 0);
// The number of items must not be 0
if (items == 0) {
isc_throw(isc::InvalidParameter, "Items must not be 0");
}
}
// Destructor
......@@ -55,6 +57,7 @@ CounterDictionaryImpl::addElement(const std::string& name) {
isc_throw(isc::InvalidParameter,
"Element " << name << " already exists");
}
assert(items_ != 0);
// Create a new Counter and add to the map
dictionary_.insert(
DictionaryMap::value_type(name, CounterPtr(new Counter(items_))));
......
......@@ -27,11 +27,10 @@ class CounterDictionary : boost::noncopyable {
/// The constructor.
/// This constructor is mostly exception free. But it may still throw
/// a standard exception if memory allocation fails inside the method.
///
/// Note: \a items must be greater than 0; otherwise this constructor
/// causes assertion failure.
///
/// \param items A number of counter items to hold (greater than 0)
///
/// \throw isc::InvalidParameter \a items is 0
CounterDictionary(const size_t items);
/// The destructor.
......
......@@ -100,8 +100,9 @@ TEST_F(CounterDictionaryTest, deleteElement) {
}
TEST_F(CounterDictionaryTest, invalidCounterSize) {
// Creating counter with 0 elements will cause assertion failure
EXPECT_DEATH(CounterDictionary counters(0), "Assertion.+failed");
// Creating counter with 0 elements will cause an isc::InvalidParameter
// exception
EXPECT_THROW(CounterDictionary counters(0), isc::InvalidParameter);
}
TEST_F(CounterDictionaryTest, invalidCounterItem) {
......@@ -109,8 +110,9 @@ TEST_F(CounterDictionaryTest, invalidCounterItem) {
CounterDictionary counters(NUMBER_OF_ITEMS);
// Add an element for this test
counters.addElement("test");
// Incrementing out-of-bound counter will cause assertion failure
EXPECT_DEATH(counters["test"].inc(NUMBER_OF_ITEMS), "Assertion.+failed");
// Incrementing out-of-bound counter will cause an isc::OutOfRange
// exception
EXPECT_THROW(counters["test"].inc(NUMBER_OF_ITEMS), isc::OutOfRange);
}
TEST_F(CounterDictionaryTest, uniqueCheck) {
......
......@@ -61,13 +61,18 @@ TEST_F(CounterTest, incrementCounterItem) {
}
TEST_F(CounterTest, invalidCounterSize) {
// Creating counter with 0 elements will cause assertion failure
EXPECT_DEATH(Counter counter(0), "Assertion.+failed");
// Creating counter with 0 elements will cause an isc::InvalidParameter
// exception
EXPECT_THROW(Counter counter(0), isc::InvalidParameter);
}
TEST_F(CounterTest, invalidCounterItem) {
// Create counter
Counter counter(NUMBER_OF_ITEMS);
// Incrementing out-of-bound counter will cause assertion failure
EXPECT_DEATH(counter.inc(NUMBER_OF_ITEMS), "Assertion.+failed");
// Incrementing out-of-bound counter will cause an isc::OutOfRange
// exception
EXPECT_THROW(counter.inc(NUMBER_OF_ITEMS), isc::OutOfRange);
// Trying to get out-of-bound counter will cause an isc::OutOfRange
// exception
EXPECT_THROW(counter.get(NUMBER_OF_ITEMS), isc::OutOfRange);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment