34 template <
typename CONT>
35 std::ostream&
PrintVector(
const CONT& sv, std::ostream& out = std::cout) {
36 out <<
"(" << sv.size() <<
") {";
43 template <
typename CONT>
45 std::ostringstream sstr;
56 out <<
"(" << sv.
size() <<
") [" << sv.
get_ranges().size() <<
"] {";
59 while (i++ < range.begin_index())
64 i += range.size() - 1;
66 while (i++ < sv.
size())
91 unsigned int nAction{0};
97 (This_t& tc,
const Action_t&
action,
unsigned int errors):
105 unsigned int nAction{0};
133 while (iNext != rend) {
134 auto iRange = iNext++;
135 if (iRange->empty()) {
136 out <<
"[range " << i <<
" is empty]" <<
std::endl;
140 if (!(*iRange < *iNext)) {
141 out <<
"[range " << i <<
" comes after range " << (i+1) <<
"]" 145 if (!iRange->separate(*iNext)) {
146 out <<
"[range " << i <<
" is overlapping or adjacent to " 154 out <<
"[size is " << sv.
size() <<
", it should be at least " 162 bool match()
const {
return std::equal(v.begin(), v.end(), sv.
begin()); }
166 unsigned int local_errors = 0;
179 void run(
const Action_t& action)
192 unsigned int perform(
const Action_t& action,
bool autoCorrect =
true)
194 out <<
"[" << ++nAction <<
"] ";
198 if (quietness <= 0) {
202 unsigned int errors =
check();
210 failures.emplace_back(*
this, action, actual_errors);
214 out <<
" *** recovery from error failed ***" <<
std::endl;
221 int quiet(
int nq = 0) {
int q = quietness; quietness = nq;
return q; }
224 unsigned int operator() (
const Action_t& action) {
return perform(action); }
236 out << nAction <<
" tests performed";
241 out <<
", " <<
nErrors <<
" unexpected errors occurred in " 242 << failures.size() <<
" of them:" <<
std::endl;
244 out <<
" [" <<
info.nAction <<
"] " <<
info.description
273 template <
typename T>
283 int expected_errors{0};
291 { expected_errors = exp_err;
return *
this; }
294 void describe(TestClass_t& tc, std::ostream& out)
const 295 { doDescribe(tc, out); }
298 void describe(TestClass_t& tc)
const {
return doDescribe(tc, tc.
out); }
302 { std::ostringstream sstr;
describe(tc, sstr);
return sstr.str(); }
305 void operator() (Vector_t& v)
const { actionOnVector(v); }
308 void operator() (SparseVector_t& v)
const { actionOnSparseVector(v); }
313 virtual void doDescribe(TestClass_t&, std::ostream& out)
const 314 { out <<
"no action"; }
319 template <
typename ITER>
320 static ITER skipRange(ITER start, ITER
const end);
321 template <
typename ITER>
322 static ITER skipVoid(ITER start, ITER
const end);
323 template <
typename ITER>
324 static ITER rangeStart(ITER
const begin, ITER last);
325 template <
typename ITER>
326 static ITER voidStart(ITER
const begin, ITER last);
327 template <
typename Vector>
330 if ((pos>= v.
size()) || isVoid(v[pos]))
331 return std::make_pair(v.end(), v.end());
332 auto rbegin = rangeStart(v.begin(), v.begin() +
pos);
333 auto rend = skipRange(rbegin, v.end());
334 return std::make_pair(rbegin, rend);
339 template <
typename ITER>
340 static ITER findRangeStart
341 (Vector_t
const& v, std::size_t i, ITER start);
342 template <
typename ITER>
344 {
return findRangeStart(v, i, v.cbegin()); }
348 template <
typename ITER>
349 static ITER findVoidStart(Vector_t
const& v, std::size_t i, ITER start);
351 static bool isVoid(Data_t
value) {
return SparseVector_t::is_zero(value); }
356 template <
typename T>
367 { out <<
"print the vectors"; }
370 { doPrintVector(v); }
372 { doPrintVector(v); }
374 template <
typename Vector>
379 template <
typename T>
390 { out <<
"print the sparse vector"; }
398 template <
typename T>
413 { out <<
"print the range at position " <<
position; }
419 std::cout <<
" position " << position <<
" (value: " 420 << v[
position] <<
" is within range " << range_str
423 catch (std::out_of_range
const&) {
424 std::cout <<
" no range at position " << position <<
std::endl;
430 template <
typename T>
441 { out <<
"print the elements which are not in the void"; }
445 typedef typename Base_t::SparseVector_t::value_type (*cmp_t)
446 (
typename Base_t::SparseVector_t::value_type);
447 std::cout <<
"Non-zero elements in vector: " 448 << (v.size() - std::count_if
449 (v.begin(), v.end(), cmp_t(&Base_t::SparseVector_t::is_zero)))
454 std::cout <<
"Non-void elements in sparse vector: " 461 template <
typename T>
473 { out <<
"clear the vectors"; }
476 { doClearVector(v); }
478 { doClearVector(v); }
480 template <
typename Vector>
486 template <
typename T>
498 Resize(
size_t new_size = 0): size(new_size) {}
502 { out <<
"resize to " << size <<
" elements"; }
505 { doResizeVector(v); }
507 { doResizeVector(v); }
509 template <
typename Vector>
515 template <
typename T>
525 { out <<
"truncate to " <<
Base_t::size <<
" elements"; }
530 template <
typename T>
546 { out <<
"resize to " << size <<
" elements, filling with " <<
value; }
549 { doResizeVector(v); }
551 { doResizeVector(v); }
553 template <
typename Vector>
559 template <
typename T>
571 Assign(Vector_t new_data): data(new_data) {}
581 { doAssignVector(v); }
583 { doAssignVector(v); }
585 template <
typename Vector>
591 template <
typename T>
608 Vector_t local(data);
613 SparseVector_t local(data);
621 out <<
" into the vector";
627 template <
typename T>
639 Insert(
size_t pos, Vector_t new_data): position(pos), data(new_data) {}
644 size_t max_size =
std::max(v.size(), position + data.size());
645 v.resize(max_size, 0);
653 out <<
"add data vector ";
655 out <<
" starting at position " <<
position;
661 template <
typename T>
672 Data_t baseline = Data_t(0);
674 Add(
size_t pos, Vector_t new_data, Data_t baseline = Data_t(0))
675 : position(pos), data(new_data), baseline(baseline) {}
680 size_t max_size =
std::max(v.size(), position + data.size());
681 v.resize(max_size, 0);
683 std::transform(data.cbegin(), data.cend(), v.cbegin() +
position,
685 [
this](
auto a,
auto b){
return a + ((
b == 0)? baseline: b); }
689 { v.
combine_range(position, data, std::plus<Data_t>(), baseline); }
693 out <<
"increment by data in ";
695 out <<
" with baseline " << baseline
696 <<
" starting at position " <<
position;
702 template <
typename T>
713 Erase(
size_t from,
size_t to): first(from), last(to) {}
719 v.begin() +
std::min(first, v.size()),
720 v.begin() +
std::min(last, v.size()),
721 Base_t::SparseVector_t::value_zero
729 out <<
"render elements from " << first <<
" to " << last
736 template <
typename T>
758 auto&& [rstart, rend] = Base_t::findRangeBorders(v, position);
759 std::fill(rstart, rend, SparseVector_t::value_zero);
766 { out <<
"void range containing position " <<
position; }
771 template <
typename T>
783 Scale(
size_t pos, Data_t factor): position(pos), factor(factor) {}
788 auto&& [rstart, rend] = Base_t::findRangeBorders(v, position);
789 while (rstart < rend) *(rstart++) *= factor;
800 out <<
"scale data by a factor " << factor
801 <<
" starting at position " <<
position;
807 template <
typename T>
811 template <
typename T>
836 { out <<
"scale all data by a factor " << factor; }
843 template <
typename T>
855 SetElement(
size_t pos, Data_t new_value): position(pos), value(new_value)
862 { v.
set_at(position, value); }
865 { out <<
"set the element " << position <<
" to " <<
value; }
870 template <
typename T>
885 { v[
position] = Base_t::SparseVector_t::value_zero; }
890 { out <<
"turn the element " << position <<
" into the void"; }
895 template <
typename T>
910 { v.push_back(value); }
915 { out <<
"add element " << value <<
" (if non-zero)"; }
920 template <
typename T>
936 { doAssignValue(v); }
938 { doAssignValue(v); }
941 { out <<
"set element " << position <<
" to " <<
value; }
943 template <
typename Vector>
949 template <
typename T>
964 { out <<
"change the sign of element " <<
position; }
971 template <
typename Vector>
977 template <
typename T>
999 out <<
"optimize the sparse vector (";
1000 if (opt_param < 0) out <<
"default settings";
1001 else out <<
"optimization parameter: " << opt_param;
1008 template <
typename T>
1019 { v.push_back(Data_t(v.size())); }
1022 { out <<
"designed failure: changes only vector"; }
1034 template <
typename T>
1035 template <
typename ITER>
1037 for (; start !=
end; ++start)
if (isVoid(*start))
break;
1041 template <
typename T>
1042 template <
typename ITER>
1044 for (; start !=
end; ++start)
if (!isVoid(*start))
break;
1049 template <
typename T>
1050 template <
typename ITER>
1053 if (last == begin)
return begin;
1054 while (--last != begin)
if (isVoid(*last))
break;
1055 return std::next(last);
1058 template <
typename T>
1059 template <
typename ITER>
1061 if (last == begin)
return begin;
1062 while (--last != begin)
if (!isVoid(*last))
break;
1063 return std::next(last);
1067 template <
typename T>
1068 template <
typename ITER>
1070 (Vector_t
const& v, std::size_t i, ITER start)
1072 auto const end = v.cend();
1074 start = skipVoid(start,
end);
1075 while (start !=
end) {
1076 if (i-- == 0)
break;
1077 start = skipRange(start,
end);
1078 start = skipVoid(start,
end);
1083 template <
typename T>
1084 template <
typename ITER>
1086 (Vector_t
const& v, std::size_t i, ITER start)
1088 auto const end = v.cend();
1090 start = skipRange(start,
end);
1091 while (start !=
end) {
1092 if (i-- == 0)
break;
1093 start = skipVoid(start,
end);
1094 start = skipRange(start,
end);
1106 typedef float Data_t;
1137 new_size++, new_size++ * 0, new_size++,
1138 new_size++, new_size++ * 0, new_size++
1181 for (
size_t i = 13; i < 16; ++i)
1224 (5, { 20, 20, 20, 20, 8, 7, 8, 7, 8, 7, 8, 7 }, 30));
1285 #ifdef SPARSE_VECTOR_TEST_FAIL 1289 #endif // SPARSE_VECTOR_TEST_FAIL
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
Base class for testing actions.
virtual void actionOnVector(Vector_t &v) const override
std::string VectorToString(const CONT &sv)
Converts a container into a string.
std::ostream & out
output stream
unsigned int nAction
number of actions taken so far
virtual void actionOnSparseVector(SparseVector_t &v) const override
std::vector< Data_t > Vector_t
virtual void actionOnVector(Vector_t &v) const override
size_type n_ranges() const
Returns the internal list of non-void ranges.
void describe(TestClass_t &tc) const
Print a description of this action in the test class stream.
static ITER findRangeStart(Vector_t const &v, std::size_t i, ITER start)
Returns iterator to the first element of the i-th range after start.
Data structure to document failure of a single action.
virtual void actionOnVector(Vector_t &v) const override
size_type size() const
Returns the size of the vector.
bool validate() const
Checks if the sparse vector is valid.
TestClass_t::Data_t Data_t
type stored
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
std::ostream & PrintVector(const CONT &sv, std::ostream &out=std::cout)
Prints a container into a stream.
virtual void actionOnVector(Vector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
void recover()
Set the sparse vector as a copy of the vector (lossy!)
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void actionOnVector(Vector_t &v) const override
const datarange_t & add_range(size_type offset, ITER first, ITER last)
Adds a sequence of elements as a range with specified offset.
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
virtual void actionOnVector(Vector_t &v) const override
unsigned int perform(const Action_t &action, bool autoCorrect=true)
Perform the specified action and evaluate the result.
virtual void actionOnSparseVector(SparseVector_t &v) const override
const_iterator cend() const
TestManagerClass(std::ostream &output_stream=std::cout)
Constructor, specify an output stream.
auto range_data(std::size_t i)
Provides direct access to data of i-th non-void range (zero-based)
std::ostream & PrintVectorRanges(const lar::sparse_vector< T > &sv, std::ostream &out=std::cout)
Prints a sparse vector highlighting the ranges.
Scale(size_t pos, T factor) -> Scale< T >
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
static ITER skipRange(ITER start, ITER const end)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
static void doClearVector(Vector &v)
virtual void doDescribe(TestClass_t &, std::ostream &out) const
value_type & set_at(size_type index, value_type value)
Writes into an element (creating or expanding a range if needed)
virtual void actionOnVector(Vector_t &v) const override
This_t & set_expected_errors(int exp_err)
auto iterate_ranges() -> decltype(auto)
bool check(const std::vector< std::vector< float > > &outputs)
TestClass_t::SparseVector_t SparseVector_t
type of sparse vector
ScaleAll(T factor) -> ScaleAll< T >
TestManagerClass< T > TestClass_t
lar::sparse_vector< Data_t > SparseVector_t
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
void mirror()
Set the vector as a copy of the sparse vector.
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
Insert(size_t pos, Vector_t new_data)
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void actionOnVector(Vector_t &v) const override
SetValue(size_t pos, Data_t val)
static bool isVoid(Data_t value)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
std::string describe(cet::exempt_ptr< fhicl::ConfigurationTable const > pb, std::string const &prefix)
static ITER rangeStart(ITER const begin, ITER last)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
Add(size_t pos, Vector_t new_data, Data_t baseline=Data_t(0))
virtual void actionOnVector(Vector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
static ITER voidStart(ITER const begin, ITER last)
virtual void actionOnVector(Vector_t &v) const override
QTextStream & flush(QTextStream &s)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
void make_void(iterator first, iterator last)
Makes all the elements from first and before last void.
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
int summary() const
Print a summary of the failures.
virtual void actionOnVector(Vector_t &v) const override
SetElement(size_t pos, Data_t new_value)
int main()
A simple test suite.
void describe(TestClass_t &tc, std::ostream &out) const
Print a description of this action into the specified steam.
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void actionOnVector(Vector_t &v) const override
virtual void actionOnVector(Vector_t &v) const override
static int max(int a, int b)
static ITER findRangeStart(Vector_t const &v, std::size_t i)
virtual void actionOnVector(Vector_t &v) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
const datarange_t & combine_range(size_type offset, ITER first, ITER last, OP &&op, value_type void_value=value_zero)
Combines a sequence of elements as a range with data at offset.
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
AssignMove(Vector_t new_data)
void doAssignValue(Vector &v) const
def validate(nxgraph, desc)
virtual void actionOnSparseVector(SparseVector_t &v) const override
void doResizeVector(Vector &v) const
std::vector< FailureInfo_t > failures
documentation of the failures
std::string description(TestClass_t &tc) const
Returns a string with a description of this action.
size_t current_vector_size() const
Returns the current size of the STL vector.
datarange_t make_void_around(size_type index)
Casts the whole range with the specified item into the void.
virtual void actionOnVector(Vector_t &v) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
void doResizeVector(Vector &v) const
virtual void actionOnVector(Vector_t &v) const override
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
int expected_errors
number of errors expected for this action
const datarange_t & find_range(size_type index) const
Returns the range containing the specified index.
Erase(size_t from, size_t to)
static ITER skipVoid(ITER start, ITER const end)
TestClass_t::Vector_t Vector_t
type of STL vector
actions::BaseAction< Data_t > Action_t
void unset_at(size_type index)
Casts the element with the specified index into the void.
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
std::size_t find_range_number(size_type index) const
Returns the number (0-based) of range containing index.
virtual void actionOnSparseVector(SparseVector_t &v) const override
iterator begin()
Standard iterators interface.
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
static void doPrintVector(Vector const &v)
TestManagerClass< Data_t > This_t
virtual void actionOnSparseVector(SparseVector_t &v) const override
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Scale(size_t pos, Data_t factor)
void push_back(value_type value)
const_iterator cbegin() const
Namespace containing all the test actions.
virtual void actionOnSparseVector(SparseVector_t &v) const override
virtual void actionOnVector(Vector_t &v) const override
void doFlipValue(Vector &v) const
Class defining a sparse vector (holes are zeroes)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
Assign(Vector_t new_data)
virtual void actionOnVector(Vector_t &) const
void doAssignVector(Vector &v) const
Truncate(size_t new_size=0)
virtual void doDescribe(TestClass_t &, std::ostream &out) const override
bool optimize()
Performs internal optimization, returns whether the object was changed.
void assign(ITER first, ITER last)
Copies data from a sequence between two iterators.
virtual void actionOnSparseVector(SparseVector_t &v) const override
recob::tracking::Vector_t Vector_t
void run(const Action_t &action)
static ITER findVoidStart(Vector_t const &v, std::size_t i, ITER start)
virtual void actionOnSparseVector(SparseVector_t &) const
std::string description
description of the failed action
typename Vector_t::const_iterator v_citer_t
virtual void actionOnVector(Vector_t &v) const override
static auto findRangeBorders(Vector &v, std::size_t pos)
virtual void actionOnVector(Vector_t &v) const override
QTextStream & endl(QTextStream &s)
virtual void actionOnSparseVector(SparseVector_t &v) const override
Resize(size_t new_size=0)
ResizeWith(size_t new_size, Data_t val)
size_type count() const
Returns the number of non-void cells.