testReadStream.cc
Go to the documentation of this file.
1 
11 #include <utl/Test.h>
12 #include <utl/Math.h>
13 #include <tst/Verify.h>
14 #include <utl/ReadStream.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <cstdio>
17 
18 using namespace tst;
19 using namespace utl;
20 using namespace std;
21 
22 
23 struct Data {
24  int fTime;
25  double fPressure;
26  bool fFlag;
27 };
28 
29 
30 inline
31 istream&
32 operator>>(istream& is, Data& d)
33 {
34  return is >> d.fTime >> ws >> d.fPressure >> ws >> d.fFlag;
35 }
36 
37 
38 const string file =
39  "1 13.9 0\n"
40  " 2 18 1 \t\n"
41  "#2 19 1\n"
42  "3 123/* foo */ 0\n"
43  "4 3 1 //comment \n"
44  " \t \t \n"
45  "5 22 0 # another\n"
46  "6 11 \r 1 # /* bar \n"
47  "7 12 0 // what # fool\n"
48  "\n"
49  "8 14 1 # baz // some \n"
50  "/* cc */ 9 15 1 /**//**/ \n"
51  "/* crap *//* more crap */ \n"
52  "///* an abundance of crap \n"
53  "\t 10 /*foo*/16 /* *//**/ 0 \n";
54 
55 
56 const Data result[] = {
57  { 1, 13.9, false },
58  { 2, 18, true },
59  { 3, 123, false },
60  { 4, 3, true },
61  { 5, 22, false },
62  { 6, 11, true },
63  { 7, 12, false },
64  { 8, 14, true },
65  { 9, 15, true },
66  { 10, 16, false }
67 };
68 
69 
70 const string stringResult[] = {
71  "1 13.9 0",
72  "2 18 1",
73  "3 123 0",
74  "4 3 1",
75  "5 22 0",
76  "6 11 1",
77  "7 12 0",
78  "8 14 1",
79  "9 15 1",
80  "10 16 0"
81 };
82 
83 
84 const string stringResultNoStripping[] = {
85  "1 13.9 0",
86  " 2 18 1 \t",
87  "#2 19 1",
88  "3 123/* foo */ 0",
89  "4 3 1 //comment ",
90  " \t \t ",
91  "5 22 0 # another",
92  "6 11 \r 1 # /* bar ",
93  "7 12 0 // what # fool",
94  "8 14 1 # baz // some ",
95  "/* cc */ 9 15 1 /**//**/ ",
96  "/* crap *//* more crap */ ",
97  "///* an abundance of crap ",
98  "\t 10 /*foo*/16 /* *//**/ 0 "
99 };
100 
101 
105 class testReadStream : public CppUnit::TestFixture {
106 
107  CPPUNIT_TEST_SUITE(testReadStream);
108  CPPUNIT_TEST(TestVectorData);
109  CPPUNIT_TEST(TestVectorString);
110  CPPUNIT_TEST(TestVectorStringNoStripping);
111  CPPUNIT_TEST_SUITE_END();
112 
113 public:
114  void setUp() { }
115 
116  void tearDown() { }
117 
118  void
120  {
121  istringstream is(file);
122  vector<Data> v;
123  ReadStream(is).GetLines(v);
124  CPPUNIT_ASSERT(v.size() == Length(result));
125  for (unsigned int i = 0, n = v.size(); i < n; ++i) {
126  CPPUNIT_ASSERT(Verify<Equal>(v[i].fTime, result[i].fTime));
127  CPPUNIT_ASSERT(Verify<Equal>(v[i].fPressure, result[i].fPressure));
128  CPPUNIT_ASSERT(Verify<Equal>(v[i].fFlag, result[i].fFlag));
129  }
130  }
131 
132  void
134  {
135  istringstream is(file);
136  vector<string> v;
137  ReadStream(is).GetLines(v);
138  CPPUNIT_ASSERT(v.size() == Length(stringResult));
139  for (unsigned int i = 0, n = v.size(); i < n; ++i)
140  CPPUNIT_ASSERT(Verify<Equal>(v[i], stringResult[i]));
141  }
142 
143  void
145  {
146  istringstream is(file);
147  vector<string> v;
148  ReadStream(is).GetLines(v, false);
149  CPPUNIT_ASSERT(v.size() == Length(stringResultNoStripping));
150  for (unsigned int i = 0, n = v.size(); i < n; ++i)
151  CPPUNIT_ASSERT(Verify<Equal>(v[i], stringResultNoStripping[i]));
152  }
153 
154 };
155 
156 
void operator>>(const Event &theEvent, IoSdEvent &rawSEvent)
int fTime
const string stringResult[]
bool is(const double a, const double b)
Definition: testlib.cc:113
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
const Data result[]
const string file
void TestVectorStringNoStripping()
bool fFlag
void GetLines(std::vector< T > &v, const bool filterComments=true, const bool trim=false)
Definition: ReadStream.h:101
double fPressure
const string stringResultNoStripping[]

, generated on Tue Sep 26 2023.