FFBinaryStream_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 "FFBinaryStream.hpp"
40 #include "TestUtil.hpp"
41 
42 using namespace std;
43 using namespace gnsstk;
44 
45 // generic little-endian binary stream
47 {
48 public:
49  FFBinaryStreamLE(const char* fn,
50  std::ios::openmode mode=std::ios::in|std::ios::binary)
51  : gnsstk::FFBinaryStream(fn, mode)
52  {}
53 
54  virtual bool isStreamLittleEndian() const noexcept
55  { return true; }
56 };
57 
58 // generic big-endian binary stream
60 {
61 public:
62  FFBinaryStreamBE(const char* fn,
63  std::ios::openmode mode=std::ios::in|std::ios::binary)
64  : gnsstk::FFBinaryStream(fn, mode)
65  {}
66 
67  virtual bool isStreamLittleEndian() const noexcept
68  { return false; }
69 };
70 
72 {
73 public:
74 
75  // constructor
77  {
78  init();
79  }
80 
81  // initialize tests
82  void init();
83 
84  int testTypes();
85 
86  template <class StreamType>
87  void testStream(const std::string& outfn, const std::string& reffn,
88  TestUtil& testFramework);
89 
90  string beFile;
91  string leFile;
92  string beRef;
93  string leRef;
94 }; // class FFBinaryStream_T
95 
96 
97 //============================================================
98 // Initialize Test Data Filenames and Values
99 //============================================================
100 
103 {
104 
105  TestUtil testUtil;
106  string dp = gnsstk::getPathData() + gnsstk::getFileSep();
107  string op = gnsstk::getPathTestTemp() + gnsstk::getFileSep();
108 
109  //----------------------------------------
110  // Full file paths
111  //----------------------------------------
112  beFile = op + "test_output_FFBinaryStream_BE.dat";
113  leFile = op + "test_output_FFBinaryStream_LE.dat";
114  beRef = dp + "test_output_FFBinaryStream_BE.exp";
115  leRef = dp + "test_output_FFBinaryStream_LE.exp";
116 }
117 
118 
121 {
122  TUDEF("FFBinaryStream", "writeData");
123  testFramework.changeSourceMethod("getData/writeData (little-endian)");
124  testStream<FFBinaryStreamLE>(leFile, leRef, testFramework);
125  testFramework.changeSourceMethod("getData/writeData (big-endian)");
126  testStream<FFBinaryStreamBE>(beFile, beRef, testFramework);
127  return testFramework.countFails();
128 }
129 
130 
131 template <class StreamType>
133 testStream(const std::string& outfn, const std::string& reffn,
134  TestUtil& testFramework)
135 {
136  StreamType testStrm(outfn.c_str(), ios::out);
137  static const uint8_t uint8Arr[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W',
138  'o', 'r', 'l', 'd', '!' };
139  // test edges as well as "random" non-edge numbers
140  static const uint16_t uint16Arr[] = { 0xffff, 0x0000, 0xdead, 0xbeef,
141  0x7ff3 };
142  static const uint32_t uint32Arr[] = { 0, 2147483647, 4294967295, 6456568 };
143  static const uint64_t uint64Arr[] = { 0, 9223372036854775807,
144  18446744073709551615lu, 2904954569522 };
145 
146  static const int8_t int8Arr[] = { 0, 127, -127, -1 };
147  static const int16_t int16Arr[] = { 0, -1, -32767, 32767, 98, -1553 };
148  static const int32_t int32Arr[] = { 0, -1, -2147483647, 2147483647, -123456,
149  63454567 };
150  static const int64_t int64Arr[] = { 0, -1, -9223372036854775807,
151  9223372036854775807, 104993356393,
152  -544342456764 };
153 
154  // http://www.h-schmidt.net/FloatConverter/IEEE754.html
155  // 3a00f07f
156  static const float fltArr[] = { 4.918649210594594E-4 };
157  // http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
158  // 01 23 45 67 89 ab cd ef
159  // 3d 85 9d 6a a8 91 f3 7f
160  static const double dblArr[] = { 3.5127005640885040e-303,
161  2.4573306210644260e-12 };
162 
163  static const streampos fileSize =
164  sizeof(uint8Arr) +
165  sizeof(uint16Arr) +
166  sizeof(uint32Arr) +
167  sizeof(uint64Arr) +
168  sizeof(int8Arr) +
169  sizeof(int16Arr) +
170  sizeof(int32Arr) +
171  sizeof(int64Arr) +
172  sizeof(fltArr) +
173  sizeof(dblArr);
174 
175  // Note that this puts 17 bytes into the file which means that
176  // the longer quantities (2-, 4-, and 8-bytes) will not be
177  // aligned. This is in itself a useful test.
178  unsigned i;
179  uint8_t tu8;
180  uint16_t tu16;
181  uint32_t tu32;
182  uint64_t tu64;
183  uint8_t ts8;
184  int16_t ts16;
185  int32_t ts32;
186  int64_t ts64;
187  float tf;
188  double td;
189 
190  // writeData makes sure that the output file grows by the
191  // expected number of bytes for the data type.
192 
193  // unsigned types
194  streampos expectedPos = 0;
195  for (i = 0; i < sizeof(uint8Arr); i++)
196  {
197  testStrm.writeData(uint8Arr[i]);
198  expectedPos += 1;
199  TUASSERTE(streampos, expectedPos, testStrm.tellg());
200  }
201  // sizeof is in bytes, divide by 2 to get array size, repeat as necessary
202  for (i = 0; i < (sizeof(uint16Arr) >> 1); i++)
203  {
204  testStrm.writeData(uint16Arr[i]);
205  expectedPos += 2;
206  TUASSERTE(streampos, expectedPos, testStrm.tellg());
207  }
208  for (i = 0; i < (sizeof(uint32Arr) >> 2); i++)
209  {
210  testStrm.writeData(uint32Arr[i]);
211  expectedPos += 4;
212  TUASSERTE(streampos, expectedPos, testStrm.tellg());
213  }
214  for (i = 0; i < (sizeof(uint64Arr) >> 3); i++)
215  {
216  testStrm.writeData(uint64Arr[i]);
217  expectedPos += 8;
218  TUASSERTE(streampos, expectedPos, testStrm.tellg());
219  }
220 
221  // signed types
222  for (i = 0; i < sizeof(int8Arr); i++)
223  {
224  testStrm.writeData(int8Arr[i]);
225  expectedPos += 1;
226  TUASSERTE(streampos, expectedPos, testStrm.tellg());
227  }
228  for (i = 0; i < (sizeof(int16Arr) >> 1); i++)
229  {
230  testStrm.writeData(int16Arr[i]);
231  expectedPos += 2;
232  TUASSERTE(streampos, expectedPos, testStrm.tellg());
233  }
234  for (i = 0; i < (sizeof(int32Arr) >> 2); i++)
235  {
236  testStrm.writeData(int32Arr[i]);
237  expectedPos += 4;
238  TUASSERTE(streampos, expectedPos, testStrm.tellg());
239  }
240  for (i = 0; i < (sizeof(int64Arr) >> 3); i++)
241  {
242  testStrm.writeData(int64Arr[i]);
243  expectedPos += 8;
244  TUASSERTE(streampos, expectedPos, testStrm.tellg());
245  }
246  for (i = 0; i < (sizeof(fltArr) >> 2); i++)
247  {
248  testStrm.writeData(fltArr[i]);
249  expectedPos += 4;
250  TUASSERTE(streampos, expectedPos, testStrm.tellg());
251  }
252  for (i = 0; i < (sizeof(dblArr) >> 3); i++)
253  {
254  testStrm.writeData(dblArr[i]);
255  expectedPos += 8;
256  TUASSERTE(streampos, expectedPos, testStrm.tellg());
257  }
258 
259  testStrm.close();
260 
261  // getData tests make sure that the data read back in from the
262  // file matches what was written out in the first place.
263 
264 
265  StreamType testStrmIn(outfn.c_str(), ios::in);
266 
267  #ifdef WIN32
268  testFramework.assert(bool(testStrmIn), "Couldn't open " + outfn + " for input", __LINE__);
269  #else
270  testFramework.assert(static_cast<bool>(testStrmIn), "Couldn't open " + outfn + " for input", __LINE__);
271  #endif
272 
273  // check file size
274  testStrmIn.seekg(0, testStrmIn.end);
275  TUASSERTE(streampos, fileSize, testStrmIn.tellg());
276  testStrmIn.seekg(0, testStrmIn.beg);
277 
278  // unsigned types
279  for (i = 0; i < sizeof(uint8Arr); i++)
280  {
281  testStrmIn.getData(tu8);
282  TUASSERTE(uint8_t, uint8Arr[i], tu8);
283  }
284  // sizeof is in bytes, divide by 2 to get array size, repeat as necessary
285  for (i = 0; i < (sizeof(uint16Arr) >> 1); i++)
286  {
287  testStrmIn.getData(tu16);
288  TUASSERTE(uint16_t, uint16Arr[i], tu16);
289  }
290  for (i = 0; i < (sizeof(uint32Arr) >> 2); i++)
291  {
292  testStrmIn.getData(tu32);
293  TUASSERTE(uint32_t, uint32Arr[i], tu32);
294  }
295  for (i = 0; i < (sizeof(uint64Arr) >> 3); i++)
296  {
297  testStrmIn.getData(tu64);
298  TUASSERTE(uint64_t, uint64Arr[i], tu64);
299  }
300 
301  // signed types
302  for (i = 0; i < sizeof(int8Arr); i++)
303  {
304  testStrmIn.getData(ts8);
305  TUASSERTE(int8_t, int8Arr[i], ts8);
306  }
307  for (i = 0; i < (sizeof(int16Arr) >> 1); i++)
308  {
309  testStrmIn.getData(ts16);
310  TUASSERTE(int16_t, int16Arr[i], ts16);
311  }
312  for (i = 0; i < (sizeof(int32Arr) >> 2); i++)
313  {
314  testStrmIn.getData(ts32);
315  TUASSERTE(int32_t, int32Arr[i], ts32);
316  }
317  for (i = 0; i < (sizeof(int64Arr) >> 3); i++)
318  {
319  testStrmIn.getData(ts64);
320  TUASSERTE(int64_t, int64Arr[i], ts64);
321  }
322  for (i = 0; i < (sizeof(fltArr) >> 2); i++)
323  {
324  testStrmIn.getData(tf);
325  TUASSERTFE(fltArr[i], tf);
326  }
327  for (i = 0; i < (sizeof(dblArr) >> 3); i++)
328  {
329  testStrmIn.getData(td);
330  TUASSERTFE(dblArr[i], td);
331  }
332 
333  testStrmIn.close();
334 
335  testFramework.assert_binary_files_equal
336  (__LINE__, reffn, outfn, "Encoded output file does not match reference");
337 }
338 
339 
344 int main(int argc, char *argv[])
345 {
346  int errorTotal = 0;
347 
348  FFBinaryStream_T testClass; // test data is loaded here
349 
350  errorTotal += testClass.testTypes();
351 
352  cout << "Total Failures for " << __FILE__ << ": " << errorTotal << endl;
353 
354  return( errorTotal );
355 
356 } // main()
FFBinaryStreamBE::isStreamLittleEndian
virtual bool isStreamLittleEndian() const noexcept
Definition: FFBinaryStream_T.cpp:67
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
FFBinaryStream_T::testTypes
int testTypes()
Definition: FFBinaryStream_T.cpp:120
const
#define const
Definition: getopt.c:43
FFBinaryStream_T::leFile
string leFile
Little-endian output file.
Definition: FFBinaryStream_T.cpp:91
gnsstk::FFBinaryStream
Definition: FFBinaryStream.hpp:60
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
FFBinaryStream_T::leRef
string leRef
Little-endian output reference file.
Definition: FFBinaryStream_T.cpp:93
FFBinaryStreamLE
Definition: FFBinaryStream_T.cpp:46
FFBinaryStreamBE::FFBinaryStreamBE
FFBinaryStreamBE(const char *fn, std::ios::openmode mode=std::ios::in|std::ios::binary)
Definition: FFBinaryStream_T.cpp:62
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
FFBinaryStream_T::init
void init()
Definition: FFBinaryStream_T.cpp:102
FFBinaryStreamLE::isStreamLittleEndian
virtual bool isStreamLittleEndian() const noexcept
Definition: FFBinaryStream_T.cpp:54
TestUtil.hpp
FFBinaryStream_T::FFBinaryStream_T
FFBinaryStream_T()
Definition: FFBinaryStream_T.cpp:76
FFBinaryStreamBE
Definition: FFBinaryStream_T.cpp:59
FFBinaryStream_T::testStream
void testStream(const std::string &outfn, const std::string &reffn, TestUtil &testFramework)
Definition: FFBinaryStream_T.cpp:133
FFBinaryStream.hpp
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
FFBinaryStream_T::beRef
string beRef
Big-endian output reference file.
Definition: FFBinaryStream_T.cpp:92
gnsstk::TestUtil::assert_binary_files_equal
void assert_binary_files_equal(int lineNumber, const std::string &file1Name, const std::string &file2Name, const std::string &testMsg, unsigned long long from=0, unsigned long long to=-1)
Definition: TestUtil.hpp:758
std
Definition: Angle.hpp:142
FFBinaryStreamLE::FFBinaryStreamLE
FFBinaryStreamLE(const char *fn, std::ios::openmode mode=std::ios::in|std::ios::binary)
Definition: FFBinaryStream_T.cpp:49
FFBinaryStream_T::beFile
string beFile
Big-endian output file.
Definition: FFBinaryStream_T.cpp:90
TUASSERTFE
#define TUASSERTFE(EXP, GOT)
Definition: TestUtil.hpp:103
example5.fn
string fn
Definition: example5.py:10
main
int main(int argc, char *argv[])
Definition: FFBinaryStream_T.cpp:344
gnsstk::TestUtil
Definition: TestUtil.hpp:265
FFBinaryStream_T
Definition: FFBinaryStream_T.cpp:71


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