Classes | Namespaces | Functions
sparse_vector_test.cc File Reference

Implementation tests for a sparse_vector object. More...

#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <sstream>
#include <stdexcept>
#include "lardataobj/Utilities/sparse_vector.h"

Go to the source code of this file.

Classes

class  actions::BaseAction< T >
 Base class for testing actions. More...
 
class  TestManagerClass< T >
 Test manager class. More...
 
struct  TestManagerClass< T >::FailureInfo_t
 Data structure to document failure of a single action. More...
 
class  actions::BaseAction< T >
 Base class for testing actions. More...
 
class  actions::Print< T >
 
class  actions::PrintSparseVector< T >
 
class  actions::PrintRange< T >
 
class  actions::PrintNonVoid< T >
 
class  actions::Clear< T >
 
class  actions::Resize< T >
 
class  actions::Truncate< T >
 
class  actions::ResizeWith< T >
 
class  actions::Assign< T >
 
class  actions::AssignMove< T >
 
class  actions::Insert< T >
 
class  actions::Add< T >
 
class  actions::Erase< T >
 
class  actions::EraseRangeAt< T >
 
class  actions::Scale< T >
 
class  actions::ScaleAll< T >
 
class  actions::SetElement< T >
 
class  actions::UnsetElement< T >
 
class  actions::PushBack< T >
 
class  actions::SetValue< T >
 
class  actions::FlipSign< T >
 
class  actions::Optimize< T >
 
class  actions::FailTest< T >
 

Namespaces

 actions
 Namespace containing all the test actions.
 

Functions

template<typename T >
abs (T value)
 
template<typename CONT >
std::ostream & PrintVector (const CONT &sv, std::ostream &out=std::cout)
 Prints a container into a stream. More...
 
template<typename CONT >
std::string VectorToString (const CONT &sv)
 Converts a container into a string. More...
 
template<typename T >
std::ostream & PrintVectorRanges (const lar::sparse_vector< T > &sv, std::ostream &out=std::cout)
 Prints a sparse vector highlighting the ranges. More...
 
template<typename T >
 actions::Scale (size_t pos, T factor) -> Scale< T >
 
template<typename T >
 actions::ScaleAll (T factor) -> ScaleAll< T >
 
int main ()
 A simple test suite. More...
 

Detailed Description

Implementation tests for a sparse_vector object.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.fnal..nosp@m.gov)
Date
April 22, 2014
Version
1.0

The test program is made of some infrastucture (mostly the TestManagerClass class) and some test operations.

The test operations are derived by actions::BaseAction class; while they are not documented, their content is a good showcase of features of the sparse_vector class.

Definition in file sparse_vector_test.cc.

Function Documentation

template<typename T >
T abs ( value)

Definition at line 30 of file sparse_vector_test.cc.

int main ( void  )

A simple test suite.

Definition at line 1103 of file sparse_vector_test.cc.

1103  {
1104 
1105  // let's test on:
1106  typedef float Data_t;
1107 
1108 // typedef std::vector<Data_t> Vector_t;
1109 // typedef lar::sparse_vector<Data_t> SparseVector_t;
1110 
1111  // first test: instanciate
1113 
1115 
1116  Test(actions::Resize<Data_t>(10));
1117 
1118  Test(actions::Assign<Data_t>({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }));
1119 
1120  Test(actions::Clear<Data_t>());
1121 
1122  Test(actions::AssignMove<Data_t>({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }));
1123 
1124  Test(actions::Clear<Data_t>());
1125 
1126  Test(actions::Insert<Data_t>(5, { 5, 6, 7 }));
1127 
1128  Test(actions::Insert<Data_t>(15, { 15, 16, 17 }));
1129 
1130  Test(actions::Insert<Data_t>(11, { 11, 12, 13 }));
1131 
1132  Test(actions::Insert<Data_t>(3, { 3 }));
1133 
1134  Data_t new_size = Data_t(Test.current_vector_size());
1135  for (Data_t value:
1136  {
1137  new_size++, new_size++ * 0, new_size++,
1138  new_size++, new_size++ * 0, new_size++
1139  })
1141 
1142  Test(actions::ResizeWith<Data_t>(new_size += 2, -1));
1143 
1144  Test(actions::Resize<Data_t>(new_size += 5));
1145 
1146  Test(actions::Insert<Data_t>(7, { 7, 8 }));
1147 
1148  Test(actions::Insert<Data_t>(18, { 18, 19, 20 }));
1149 
1150  Test(actions::Insert<Data_t>(4, { 4, 5 }));
1151 
1152  Test(actions::Insert<Data_t>(22, { 22 }));
1153 
1154  Test(actions::Insert<Data_t>(19, { 19 }));
1155 
1156  Test(actions::Erase<Data_t>(7, 17));
1157 
1158  Test(actions::Erase<Data_t>(21, 23));
1159 
1161 
1162  for (size_t i = 0; i < Test.current_vector_size(); ++i)
1164 
1165  Test(actions::Insert<Data_t>(15, { 15, 16, 17, 18, 19, 20, 21, 22, 23 }));
1166 
1167  Test(actions::Insert<Data_t>(9, { 9, 10 }));
1168 
1169  Test(actions::Erase<Data_t>(9, 11));
1170 
1171  Test(actions::Insert<Data_t>(9, { 9, 10 }));
1172 
1174 
1176 
1177  Test(actions::SetValue<Data_t>(15, -15.));
1178 
1179  Test(actions::FlipSign<Data_t>(15));
1180 
1181  for (size_t i = 13; i < 16; ++i)
1182  Test(actions::SetElement<Data_t>(i, i));
1183 
1184  Test(actions::Scale(15U, 4.0F));
1185 
1186  Test(actions::Scale(15U, 0.25F));
1187 
1188  Test(actions::ScaleAll(2.0F));
1189 
1190  Test(actions::ScaleAll(0.5F));
1191 
1193 
1195 
1197 
1199 
1201 
1203 
1204  Test(actions::Optimize<Data_t>(-1));
1205 
1206  // at this point:
1207  // (31) [2] {
1208  // 0 0 0 [ 3 4 5 6 ] 0 0 0
1209  // 0 0 0 0 0 0 [ 16 17 18 19
1210  // 20 21 22 23 -1 ] 0 0 0 0 0
1211  // 0
1212  // }
1213 
1214  Test(actions::Add<Data_t>(5, { 7, 8, 7, 8 }, 10));
1215  // at this point:
1216  // (31) [2] {
1217  // 0 0 0 [ 3 4 12 13 17 18 ] 0
1218  // 0 0 0 0 0 0 [ 16 17 18 19
1219  // 20 21 22 23 -1 ] 0 0 0 0 0
1220  // 0
1221  // }
1222 
1224  (5, { 20, 20, 20, 20, 8, 7, 8, 7, 8, 7, 8, 7 }, 30));
1225  // at this point:
1226  // (31) [2] {
1227  // 0 0 0 [ 3 4 32 33 37 38 38
1228  // 37 38 37 38 37 38 23 17 18 19
1229  // 20 21 22 23 -1 ] 0 0 0 0 0
1230  // 0
1231  // }
1232 
1233  Test(actions::Add<Data_t>(27, { 7, 8, }, 20));
1234  // at this point:
1235  // (31) [2] {
1236  // 0 0 0 [ 3 4 32 33 37 38 38
1237  // 37 38 37 38 37 38 23 17 18 19
1238  // 20 21 22 23 -1 ] 0 0 [ 27 28 ] 0
1239  // 0
1240  // }
1241 
1243  (4, TestManagerClass<Data_t>::Vector_t(22, 10.0), 15.0));
1244  // at this point:
1245  // (31) [2] {
1246  // 0 0 0 [ 3 14 42 43 47 48 48
1247  // 47 48 47 48 47 48 33 27 28 29
1248  // 30 31 32 33 9 25 ] 0 [ 27 28 ] 0
1249  // 0
1250  // }
1251 
1253  (2, TestManagerClass<Data_t>::Vector_t(22, -10.0), 12.0));
1254  // at this point:
1255  // (31) [2] {
1256  // 0 0 [ 2 3 4 32 33 37 38 38
1257  // 37 38 37 38 37 38 23 17 18 19
1258  // 20 21 22 23 -1 25 ] 0 [ 27 28 ] 0
1259  // 0
1260  // }
1261 
1262  Test(actions::Add<Data_t>(26, { 16, 0 }, 10.0));
1263  // at this point:
1264  // (31) [2] {
1265  // 0 0 [ 2 3 4 32 33 37 38 38
1266  // 37 38 37 38 37 38 23 17 18 19
1267  // 20 21 22 23 -1 25 26 27 28 ] 0
1268  // 0
1269  // }
1270 
1271  Test(actions::Truncate<Data_t>(new_size -= 3));
1272 
1273  Test(actions::Truncate<Data_t>(16));
1274 
1275  Test(actions::Truncate<Data_t>(15));
1276 
1277  Test(actions::Truncate<Data_t>(14));
1278 
1279  Test(actions::Truncate<Data_t>(9));
1280 
1281  Test(actions::Truncate<Data_t>(3));
1282 
1283  Test(actions::Truncate<Data_t>(2));
1284 
1285 #ifdef SPARSE_VECTOR_TEST_FAIL
1286  // enable this to verify that the error detection works
1287  Test(actions::FailTest<Data_t>());
1288  Test.recover();
1289 #endif // SPARSE_VECTOR_TEST_FAIL
1290 
1291  return Test.summary();
1292 } // main()
Base class for testing actions.
std::vector< Data_t > Vector_t
void recover()
Set the sparse vector as a copy of the vector (lossy!)
A Test class.
Definition: class.h:3
int summary() const
Print a summary of the failures.
size_t current_vector_size() const
Returns the current size of the STL vector.
void run(const Action_t &action)
Test manager class.
template<typename CONT >
std::ostream& PrintVector ( const CONT &  sv,
std::ostream &  out = std::cout 
)

Prints a container into a stream.

Definition at line 35 of file sparse_vector_test.cc.

35  {
36  out << "(" << sv.size() << ") {";
37  for (auto value: sv) out << " " << value;
38  return out << " }" << std::flush;
39 } // PrintVector()
QTextStream & flush(QTextStream &s)
template<typename T >
std::ostream& PrintVectorRanges ( const lar::sparse_vector< T > &  sv,
std::ostream &  out = std::cout 
)

Prints a sparse vector highlighting the ranges.

Definition at line 54 of file sparse_vector_test.cc.

55 {
56  out << "(" << sv.size() << ") [" << sv.get_ranges().size() << "] {";
57  size_t i = 0;
58  for (auto range: sv.get_ranges()) {
59  while (i++ < range.begin_index())
61  out << " [";
62  for (auto value: range) out << " " << value;
63  out << " ]";
64  i += range.size() - 1;
65  }
66  while (i++ < sv.size())
68  return out << " }" << std::flush;
69 } // PrintVectorRanges()
size_type size() const
Returns the size of the vector.
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
QTextStream & flush(QTextStream &s)
A sparse vector.
template<typename CONT >
std::string VectorToString ( const CONT &  sv)

Converts a container into a string.

Definition at line 44 of file sparse_vector_test.cc.

44  {
45  std::ostringstream sstr;
46  PrintVector(sv, sstr);
47  return sstr.str();
48 } // VectorToString()
std::ostream & PrintVector(const CONT &sv, std::ostream &out=std::cout)
Prints a container into a stream.