Binex_ReadWrite_T.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #include "BinexData.hpp"
40 #include "BinexStream.hpp"
41 #include "TestUtil.hpp"
42 
43 using namespace std;
44 using namespace gnsstk;
45 
46 //=============================================================================
47 // Class declarations
48 //=============================================================================
50 {
51 public:
52 
53  // constructor
54  BinexReadWrite_T(int v = 0) : verboseLevel(v)
55  {
56  init();
57  };
58 
59  // destructor
60  virtual ~BinexReadWrite_T() {};
61 
62  // initialize tests
63  void init();
64 
65  // test methods
66  // @return number of failures, i.e., 0=PASS, !0=FAIL
67  int doForwardTests();
68  int doReverseTests();
69 
70  unsigned verboseLevel; // amount to display during tests, 0 = least
71 
72 private:
73 
75  {
76  CharType = 'c',
77  ShortType = 's',
78  LongType = 'l',
79  UbnxiType = 'U',
80  MgfziType = 'M'
81  };
82 
83  struct TestData
84  {
86  long long value;
87  };
88 
89  typedef vector<TestData> TestDataList;
90  typedef vector<TestDataList> TestDataListList;
91  typedef vector<BinexData> RecordList;
92 
93  // @param[in] c character to convert
94  // @param[in/out] dtype data type derived from c
95  // @return true if the character is a valid data type,
96  // false otherwise
97  bool charToType(char c, TestDataType& t);
98 
99  // read a list of numbers (one per line) from the specified file
100  // @return true on success, false on failure
101  bool readNums(const string& filename);
102 
103  // generate BINEX records from the contents of numList
104  // @return true if successful, false otherwise
105  bool createRecs();
106 
110  void dumpBuffer(const unsigned char* buffer, size_t size);
111 
113 
115 
117 
118 }; // class BinexReadWrite_T
119 
120 
121 //============================================================
122 // Initialize Test Data Filenames and Values
123 //============================================================
124 
126 {
127 
128  TestUtil testUtil;
129  string dataFilePath = gnsstk::getPathData();
130 
131  //----------------------------------------
132  // Full file paths
133  //----------------------------------------
134  string inputFile = dataFilePath + gnsstk::getFileSep()
135  + "test_input_binex_readwrite.txt";
136 
137  if (verboseLevel > 0)
138  {
139  cout << " Reading test input . . ." << endl;
140  }
141  readNums(inputFile);
142 
143  if (verboseLevel > 0)
144  {
145  cout << " " << numList.size() << " numbers" << endl;
146  cout << " Creating BINEX records . . ." << endl;
147  }
148  createRecs();
149  if (verboseLevel > 0)
150  {
151  cout << " " << testRecords.size() << " records" << endl;
152  }
153 }
154 
155 
157 {
158  switch (c)
159  {
160  case CharType:
161  t = CharType;
162  break;
163  case ShortType:
164  t = ShortType;
165  break;
166  case LongType:
167  t = LongType;
168  break;
169  case UbnxiType:
170  t = UbnxiType;
171  break;
172  case MgfziType:
173  t = MgfziType;
174  break;
175 
176  default:
177  return false;
178  }
179  return true;
180 }
181 
182 
183 //---------------------------------------------------------------------------
184 bool BinexReadWrite_T :: readNums(const string& filename)
185 {
186  ifstream ifs(filename.c_str());
187 
188  if (!ifs.good())
189  return false;
190 
191  while (ifs.good())
192  {
193  string line;
194  getline(ifs, line);
195 
196  // ignore comments
197  string::size_type hashPos = line.find('#');
198  if (hashPos != string::npos)
199  {
200  line.erase(hashPos);
201  }
202  // ignore empty lines
203  string::size_type nonWhitePos = line.find_first_not_of(" \t");
204  if (nonWhitePos != string::npos)
205  {
206  istringstream iss(line);
207  TestData num;
208  char c;
209  iss >> c;
210 
211  // make sure the type is valid
212  if (charToType(c, num.dtype))
213  {
214  iss >> num.value;
215  numList.push_back(num);
216  }
217  else
218  {
219  if (verboseLevel > 0)
220  {
221  cout << " Warning: Unrecognized data type: " << c << endl;
222  }
223  }
224  }
225  }
226  return (numList.size() > 0);
227 }
228 
229 
231 {
232  TestDataList::const_iterator tdIter = numList.begin();
233  while (tdIter != numList.end() )
234  {
235  BinexData record(1);
236  TestDataList recordData;
237  size_t offset = 0;
238 
239  // create multiple records with 9 fields each
240  short dataNum = 0;
241  for ( ; (dataNum < 9) && (tdIter != numList.end()); ++dataNum, ++tdIter)
242  {
243  switch (tdIter->dtype)
244  {
245  case CharType:
246  {
247  char c = tdIter->value;
248  record.updateMessageData(offset, c, sizeof(c) );
249  break;
250  }
251  case ShortType:
252  {
253  short s = tdIter->value;
254  record.updateMessageData(offset, s, sizeof(s) );
255  break;
256  }
257  case LongType:
258  {
259  long l = tdIter->value;
260  record.updateMessageData(offset, l, sizeof(l) );
261  break;
262  }
263  case UbnxiType:
264  {
265  BinexData::UBNXI u(tdIter->value);
266  record.updateMessageData(offset, u);
267  break;
268  }
269  case MgfziType:
270  {
271  BinexData::MGFZI m(tdIter->value);
272  record.updateMessageData(offset, m);
273  break;
274  }
275  default:
276  // Internal error
277  cout << " Internal error during record creation" << std::endl;
278  return false;
279  }
280  recordData.push_back(*tdIter);
281  }
282  testData.push_back(recordData);
283  testRecords.push_back(record);
284  }
285  return true;
286 }
287 
288 
290 {
291  TUDEF("BinexData", "Read/Write (Fwd)");
292 
293  string tempFilePath = gnsstk::getPathTestTemp();
294  string tempFileName = tempFilePath + gnsstk::getFileSep() +
295  "test_output_binex_readwrite.binex";
296  BinexStream outStream(tempFileName.c_str(),
297  std::ios::out | std::ios::binary);
298 
299  TUASSERT(outStream.good());
300 
301  outStream.exceptions(ios_base::failbit | ios_base::badbit);
302  RecordList::iterator recordIter = testRecords.begin();
303  for ( ; recordIter != testRecords.end(); ++recordIter)
304  {
305  try
306  {
307  std::streampos posBefore = outStream.tellp();
308  (*recordIter).putRecord(outStream);
309  std::streampos posAfter = outStream.tellp();
310  TUASSERTE(long long, (*recordIter).getRecordSize(), (posAfter - posBefore));
311  }
312  catch (Exception& e)
313  {
314  ostringstream oss;
315  oss << "exception writing record: " << e;
316  TUFAIL(oss.str());
317  }
318  catch (...)
319  {
320  TUFAIL("unknown exception writing record");
321  }
322  }
323  outStream.close();
324 
325  BinexStream inStream(tempFileName.c_str(),
326  std::ios::in | std::ios::binary);
327  inStream.exceptions(ios_base::failbit);
328 
329  TUASSERT(inStream.good());
330 
331  recordIter = testRecords.begin();
332  while (inStream.good() && (EOF != inStream.peek() ) )
333  {
334  if (recordIter == testRecords.end() )
335  {
336  TUFAIL("stored records exhausted before file records");
337  break;
338  }
339  BinexData record;
340  try
341  {
342  record.getRecord(inStream);
343  if (record == *recordIter)
344  {
345  TUPASS("gotten record matches");
346  }
347  else
348  {
349  ostringstream oss;
350  oss << "Actual record:" << endl;
351  (*recordIter).dump(oss);
352  oss << "Expected record:" << endl;
353  record.dump(oss);
354 
355  TUFAIL(oss.str());
356  }
357  }
358  catch (Exception& e)
359  {
360  ostringstream oss;
361  oss << "stream exception reading record: " << e;
362  TUFAIL(oss.str());
363  }
364  catch (...)
365  {
366  TUFAIL("unknown exception reading record");
367  }
368 
369  recordIter++;
370  }
371  inStream.close();
372 
373  TURETURN();
374 }
375 
376 
378 {
379  TUDEF("BinexData", "Read/Write (Rev)");
380 
381  // @todo
382 
383  TURETURN();
384 }
385 
386 
391 int main(int argc, char *argv[])
392 {
393  int errorTotal = 0;
394 
395  BinexReadWrite_T testClass; // test data is loaded here
396 
397  errorTotal += testClass.doForwardTests();
398 
399  //errorTotal += testClass.doReverseTests();
400 
401  return( errorTotal );
402 
403 } // main()
BinexReadWrite_T::testData
TestDataListList testData
Definition: Binex_ReadWrite_T.cpp:114
BinexReadWrite_T::doForwardTests
int doForwardTests()
Definition: Binex_ReadWrite_T.cpp:289
BinexReadWrite_T::numList
TestDataList numList
Definition: Binex_ReadWrite_T.cpp:112
BinexReadWrite_T::TestDataList
vector< TestData > TestDataList
Definition: Binex_ReadWrite_T.cpp:89
main
int main(int argc, char *argv[])
Definition: Binex_ReadWrite_T.cpp:391
BinexReadWrite_T::~BinexReadWrite_T
virtual ~BinexReadWrite_T()
Definition: Binex_ReadWrite_T.cpp:60
dataFilePath
string dataFilePath
Definition: Rinex3Obs_FromScratch_T.cpp:51
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
BinexReadWrite_T::TestData::value
long long value
Definition: Binex_ReadWrite_T.cpp:86
BinexReadWrite_T
Definition: Binex_ReadWrite_T.cpp:49
BinexReadWrite_T::TestDataListList
vector< TestDataList > TestDataListList
Definition: Binex_ReadWrite_T.cpp:90
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
BinexReadWrite_T::TestData
Definition: Binex_ReadWrite_T.cpp:83
BinexReadWrite_T::TestDataType
TestDataType
Definition: Binex_ReadWrite_T.cpp:74
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::BinexStream
Definition: BinexStream.hpp:63
TUASSERT
#define TUASSERT(EXPR)
Definition: TestUtil.hpp:63
gnsstk::BinexData
Definition: BinexData.hpp:65
gnsstk::BinexData::dump
virtual void dump(std::ostream &s) const
Definition: BinexData.cpp:1712
TestUtil.hpp
BinexStream.hpp
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
TUPASS
#define TUPASS(MSG)
Definition: TestUtil.hpp:230
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
BinexReadWrite_T::readNums
bool readNums(const string &filename)
Definition: Binex_ReadWrite_T.cpp:184
gnsstk::BinexData::UBNXI
Definition: BinexData.hpp:96
BinexReadWrite_T::doReverseTests
int doReverseTests()
Definition: Binex_ReadWrite_T.cpp:377
gnsstk::BinexData::updateMessageData
BinexData & updateMessageData(size_t &offset, const UBNXI &data)
Definition: BinexData.cpp:1051
std
Definition: Angle.hpp:142
BinexReadWrite_T::testRecords
RecordList testRecords
Definition: Binex_ReadWrite_T.cpp:116
BinexReadWrite_T::charToType
bool charToType(char c, TestDataType &t)
Definition: Binex_ReadWrite_T.cpp:156
gnsstk::BinexData::getRecord
virtual size_t getRecord(std::istream &s)
Definition: BinexData.cpp:1267
BinexReadWrite_T::createRecs
bool createRecs()
Definition: Binex_ReadWrite_T.cpp:230
BinexReadWrite_T::RecordList
vector< BinexData > RecordList
Definition: Binex_ReadWrite_T.cpp:91
gnsstk::BinexData::MGFZI
Definition: BinexData.hpp:293
gnsstk::TestUtil
Definition: TestUtil.hpp:265
BinexReadWrite_T::verboseLevel
unsigned verboseLevel
Definition: Binex_ReadWrite_T.cpp:70
BinexData.hpp
BinexReadWrite_T::TestData::dtype
TestDataType dtype
Definition: Binex_ReadWrite_T.cpp:85
BinexReadWrite_T::init
void init()
Definition: Binex_ReadWrite_T.cpp:125
tempFilePath
string tempFilePath
Definition: Rinex3Obs_FromScratch_T.cpp:50
BinexReadWrite_T::BinexReadWrite_T
BinexReadWrite_T(int v=0)
Definition: Binex_ReadWrite_T.cpp:54


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38