2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
2017-06-14 17:45:43 +10:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/array/sarray.hpp>
|
|
|
|
#include <cruft/util/debug/assert.hpp>
|
2017-06-14 17:45:43 +10:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// A simple wrapper over an integer that increments at construction time, and
|
|
|
|
// decrements at destruction time. Used to check for obvious leakages in
|
|
|
|
// sarray lifetimes.
|
|
|
|
struct counter {
|
|
|
|
explicit counter (int &_var):
|
|
|
|
var (_var)
|
|
|
|
{ ++var; }
|
|
|
|
|
|
|
|
counter (const counter &rhs):
|
|
|
|
counter (rhs.var)
|
|
|
|
{ ; }
|
|
|
|
|
2017-08-24 17:53:46 +10:00
|
|
|
counter& operator= (const counter &rhs)
|
|
|
|
{
|
|
|
|
var = rhs.var;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-14 17:45:43 +10:00
|
|
|
~counter ()
|
|
|
|
{ --var; }
|
|
|
|
|
|
|
|
int &var;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
int
|
|
|
|
main (int, char**)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2017-06-14 17:45:43 +10:00
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
std::vector<counter> objects;
|
|
|
|
std::generate_n (std::back_inserter (objects), 3, [&count] () { return counter (count); });
|
|
|
|
CHECK_GT (objects.size (), 0u);
|
|
|
|
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::sarray<8,counter> array (objects);
|
2017-06-14 17:45:43 +10:00
|
|
|
tap.expect_eq (
|
|
|
|
count,
|
|
|
|
static_cast<int> (objects.size () + array.size ()),
|
|
|
|
"array constructors executed"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
objects.size (),
|
|
|
|
array.size (),
|
|
|
|
"array construction preserves size"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
tap.expect_eq (
|
|
|
|
static_cast<int> (objects.size ()),
|
|
|
|
count,
|
|
|
|
"array destructors executed"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_throw<std::length_error> ([] () {
|
|
|
|
float data[] = { 0.f, 1.f, 2.f };
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::sarray<2, float> value { data };
|
2017-06-14 17:45:43 +10:00
|
|
|
}, "oversized array initializer throws std::length_error");
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|