BinUtils_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 "TestUtil.hpp"
40 #include "BinUtils.hpp"
41 #include "Exception.hpp"
42 #include <iostream>
43 #include <cmath>
44 
45 using namespace std;
46 
47 // tests for
48 // 1) integer quantities with no offset
49 // 2) integer quantities with an offset into the buffer
50 // 3) decimal quantities with no offset
51 // 4) decimal quantities with an offset into the buffer
52 #define TOHOSTTEST(TYPE,STR,EXP,FN) \
53  { \
54  testFramework.changeSourceMethod(#FN); \
55  TYPE val; \
56  gnsstk::BinUtils::FN(STR,val); \
57  TUASSERTE(TYPE,EXP,val); \
58  }
59 #define TOHOSTTESTPOS(TYPE,STR,EXP,FN,POS) \
60  { \
61  testFramework.changeSourceMethod(#FN); \
62  TYPE val; \
63  gnsstk::BinUtils::FN(STR,val,POS); \
64  TUASSERTE(TYPE,EXP,val); \
65  }
66 #define TOHOSTTESTF(TYPE,STR,EXP,FN) \
67  { \
68  testFramework.changeSourceMethod(#FN); \
69  TYPE val; \
70  gnsstk::BinUtils::FN(STR,val); \
71  TUASSERTFE(EXP,val); \
72  }
73 #define TOHOSTTESTFPOS(TYPE,STR,EXP,FN,POS) \
74  { \
75  testFramework.changeSourceMethod(#FN); \
76  TYPE val; \
77  gnsstk::BinUtils::FN(STR,val,POS); \
78  TUASSERTFE(EXP,val); \
79  }
80 
81 #define HOSTTOTEST(TYPE,STR,VAL,FN) \
82  testFramework.changeSourceMethod(#FN); \
83  gnsstk::BinUtils::FN(buffer,VAL); \
84  TUASSERTE(int,0,memcmp(buffer,STR,sizeof(TYPE)));
85 #define HOSTTOTESTPOS(TYPE,STR,VAL,FN,POS) \
86  testFramework.changeSourceMethod(#FN); \
87  gnsstk::BinUtils::FN(buffer,VAL,POS); \
88  TUASSERTE(int,0,memcmp(&buffer[POS],STR,sizeof(TYPE)));
89 
91 {
92 public:
93 
95  {
96  TUDEF("BinUtils", "buitoh*");
97 
98  TOHOSTTEST(uint16_t,"\x34\x96",0x9634,buitohs);
99  TOHOSTTEST(uint32_t,"\xde\xad\xbe\xef",0xefbeadde,buitohl);
100  TOHOSTTEST(uint64_t,"\x01\x02\x03\x04\x05\x06\x07\x08",0x0807060504030201,buitohll);
101  // With signed quantities, we try to exacerbate possible sign
102  // extension problems.
103  TOHOSTTEST(int16_t,"\xf7\x03",0x03f7,buitohss);
104  TOHOSTTEST(int32_t,"\xff\x0c\x0b\x0a",0x0a0b0cff,buitohsl);
105  TOHOSTTEST(int64_t,"\xff\xff\x56\x34\x12\x0f\x0e\x0d",0x0d0e0f123456ffff,buitohsll);
106  // using http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
107  // fiddled around until I found a number that seemed to be
108  // able to reasonably survive a round-trip, i.e. looked for a
109  // number that was representable consistently.
110  TOHOSTTESTF(float,"\xe2\x3b\x5d\x40",(float)3.45678,buitohf);
111  TOHOSTTESTF(double,"\x2c\xbc\xcb\x45\x7c\xa7\x0b\x40",(double)3.45678,buitohd);
112  // Make sure we aren't doing things wrong where byte-swapped
113  // data could result in an inappropriate NaN interpretation.
114  uint32_t fbits = 0x0100807f;
115  float *fptr = (float*)&fbits;
116  TOHOSTTESTF(float,"\x7f\x80\x00\x01",*fptr,buitohf);
117  // if we're doing things wrong, the f37f will get changed to fb7f
118  uint64_t dbits = 0x3d859d6aa891f37f;
119  double *dptr = (double*)&dbits;
120  TOHOSTTESTF(double,"\x7f\xf3\x91\xa8\x6a\x9d\x85\x3d",*dptr,buitohd);
121 
122  // same as above with buffer offsets
123 
124  TOHOSTTESTPOS(uint16_t,"#%!\x34\x96",0x9634,buitohs,3);
125  TOHOSTTESTPOS(uint32_t,"#%!\xde\xad\xbe\xef",0xefbeadde,buitohl,3);
126  TOHOSTTESTPOS(uint64_t,"#%!\x01\x02\x03\x04\x05\x06\x07\x08",0x0807060504030201,buitohll,3);
127  TOHOSTTESTPOS(int16_t,"#%!\xf7\x03",0x03f7,buitohss,3);
128  TOHOSTTESTPOS(int32_t,"#%!\xff\x0c\x0b\x0a",0x0a0b0cff,buitohsl,3);
129  TOHOSTTESTPOS(int64_t,"#%!\xff\xff\x56\x34\x12\x0f\x0e\x0d",0x0d0e0f123456ffff,buitohsll,3);
130  TOHOSTTESTFPOS(float,"#%!\xe2\x3b\x5d\x40",(float)3.45678,buitohf,3);
131  TOHOSTTESTFPOS(double,"#%!\x2c\xbc\xcb\x45\x7c\xa7\x0b\x40",(double)3.45678,buitohd,3);
132  TOHOSTTESTFPOS(float,"#%!\x7f\x80\x00\x01",*fptr,buitohf,3);
133  TOHOSTTESTFPOS(double,"#%!\x7f\xf3\x91\xa8\x6a\x9d\x85\x3d",*dptr,buitohd,3);
134 
135  return testFramework.countFails();
136  }
137 
138 
140  {
141  TUDEF("BinUtils", "buntoh*");
142 
143  TOHOSTTEST(uint16_t,"\x34\x96",0x3496,buntohs);
144  TOHOSTTEST(uint32_t,"\xde\xad\xbe\xef",0xdeadbeef,buntohl);
145  TOHOSTTEST(uint64_t,"\x01\x02\x03\x04\x05\x06\x07\x08",0x0102030405060708,buntohll);
146  // With signed quantities, we try to exacerbate possible sign
147  // extension problems.
148  TOHOSTTEST(int16_t,"\xf7\x03",(int16_t)0xf703,buntohss);
149  TOHOSTTEST(int32_t,"\xff\x0c\x0b\x0a",0xff0c0b0a,buntohsl);
150  TOHOSTTEST(int64_t,"\xff\xff\x56\x34\x12\x0f\x0e\x0d",0xffff5634120f0e0d,buntohsll);
151  // using http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html
152  // fiddled around until I found a number that seemed to be
153  // able to reasonably survive a round-trip, i.e. looked for a
154  // number that was representable consistently.
155  TOHOSTTESTF(float,"\x40\x5d\x3b\xe2",(float)3.45678,buntohf);
156  TOHOSTTESTF(double,"\x40\x0b\xa7\x7c\x45\xcb\xbc\x2c",(double)3.45678,buntohd);
157  // Make sure we aren't doing things wrong where byte-swapped
158  // data could result in an inappropriate NaN interpretation.
159  uint32_t fbits = 0x0100807f;
160  float *fptr = (float*)&fbits;
161  TOHOSTTESTF(float,"\x01\x00\x80\x7f",*fptr,buntohf);
162  // if we're doing things wrong, the f37f will get changed to fb7f
163  uint64_t dbits = 0x3d859d6aa891f37f;
164  double *dptr = (double*)&dbits;
165  TOHOSTTESTF(double,"\x3d\x85\x9d\x6a\xa8\x91\xf3\x7f",*dptr,buntohd);
166 
167  // same as above with offsets
168 
169  TOHOSTTESTPOS(uint16_t,"abc\x34\x96",0x3496,buntohs,3);
170  TOHOSTTESTPOS(uint32_t,"abc\xde\xad\xbe\xef",0xdeadbeef,buntohl,3);
171  TOHOSTTESTPOS(uint64_t,"abc\x01\x02\x03\x04\x05\x06\x07\x08",0x0102030405060708,buntohll,3);
172  TOHOSTTESTPOS(int16_t,"abc\xf7\x03",(int16_t)0xf703,buntohss,3);
173  TOHOSTTESTPOS(int32_t,"abc\xff\x0c\x0b\x0a",0xff0c0b0a,buntohsl,3);
174  TOHOSTTESTPOS(int64_t,"abc\xff\xff\x56\x34\x12\x0f\x0e\x0d",0xffff5634120f0e0d,buntohsll,3);
175  TOHOSTTESTFPOS(float,"abc\x40\x5d\x3b\xe2",(float)3.45678,buntohf,3);
176  TOHOSTTESTFPOS(double,"abc\x40\x0b\xa7\x7c\x45\xcb\xbc\x2c",(double)3.45678,buntohd,3);
177  TOHOSTTESTFPOS(float,"abc\x01\x00\x80\x7f",*fptr,buntohf,3);
178  TOHOSTTESTFPOS(double,"abc\x3d\x85\x9d\x6a\xa8\x91\xf3\x7f",*dptr,buntohd,3);
179 
180  return testFramework.countFails();
181  }
182 
183 
185  {
186  TUDEF("BinUtils", "buhtoi*");
187 
188  char buffer[20];
189  HOSTTOTEST(uint16_t,"\x34\x96",0x9634,buhtois);
190  HOSTTOTEST(uint32_t,"\xde\xad\xbe\xef",0xefbeadde,buhtoil);
191  HOSTTOTEST(uint64_t,"\x01\x02\x03\x04\x05\x06\x07\x08",0x0807060504030201,buhtoill);
192  HOSTTOTEST(int16_t,"\xf7\x03",0x03f7,buhtoiss);
193  HOSTTOTEST(int32_t,"\xff\x0c\x0b\x0a",0x0a0b0cff,buhtoisl);
194  HOSTTOTEST(int64_t,"\xff\xff\x56\x34\x12\x0f\x0e\x0d",0x0d0e0f123456ffff,buhtoisll);
195  HOSTTOTEST(float,"\xe2\x3b\x5d\x40",3.45678f,buhtoif);
196  HOSTTOTEST(double,"\x2c\xbc\xcb\x45\x7c\xa7\x0b\x40",3.45678,buhtoid);
197  uint32_t fbits = 0x0100807f;
198  float *fptr = (float*)&fbits;
199  HOSTTOTEST(float,"\x7f\x80\x00\x01",*fptr,buhtoif);
200  uint64_t dbits = 0x3d859d6aa891f37f;
201  double *dptr = (double*)&dbits;
202  HOSTTOTEST(double,"\x7f\xf3\x91\xa8\x6a\x9d\x85\x3d",*dptr,buhtoid);
203 
204  // same as above with offsets
205 
206  HOSTTOTESTPOS(uint16_t,"\x34\x96",0x9634,buhtois,3);
207  HOSTTOTESTPOS(uint32_t,"\xde\xad\xbe\xef",0xefbeadde,buhtoil,3);
208  HOSTTOTESTPOS(uint64_t,"\x01\x02\x03\x04\x05\x06\x07\x08",0x0807060504030201,buhtoill,3);
209  HOSTTOTESTPOS(int16_t,"\xf7\x03",0x03f7,buhtoiss,3);
210  HOSTTOTESTPOS(int32_t,"\xff\x0c\x0b\x0a",0x0a0b0cff,buhtoisl,3);
211  HOSTTOTESTPOS(int64_t,"\xff\xff\x56\x34\x12\x0f\x0e\x0d",0x0d0e0f123456ffff,buhtoisll,3);
212  HOSTTOTESTPOS(float,"\xe2\x3b\x5d\x40",3.45678f,buhtoif,3);
213  HOSTTOTESTPOS(double,"\x2c\xbc\xcb\x45\x7c\xa7\x0b\x40",3.45678,buhtoid,3);
214  HOSTTOTESTPOS(float,"\x7f\x80\x00\x01",*fptr,buhtoif,3);
215  HOSTTOTESTPOS(double,"\x7f\xf3\x91\xa8\x6a\x9d\x85\x3d",*dptr,buhtoid,3);
216 
217  return testFramework.countFails();
218  }
219 
220 
222  {
223  TUDEF("BinUtils", "buhton*");
224 
225  char buffer[20];
226  HOSTTOTEST(uint16_t,"\x96\x34",0x9634,buhtons);
227  HOSTTOTEST(uint32_t,"\xef\xbe\xad\xde",0xefbeadde,buhtonl);
228  HOSTTOTEST(uint64_t,"\x08\x07\x06\x05\x04\x03\x02\x01",0x0807060504030201,buhtonll);
229  HOSTTOTEST(int16_t,"\x03\xf7",0x03f7,buhtonss);
230  HOSTTOTEST(int32_t,"\x0a\x0b\x0c\xff",0x0a0b0cff,buhtonsl);
231  HOSTTOTEST(int64_t,"\x0d\x0e\x0f\x12\x34\x56\xff\xff",0x0d0e0f123456ffff,buhtonsll);
232  HOSTTOTEST(float,"\x40\x5d\x3b\xe2",3.45678f,buhtonf);
233  HOSTTOTEST(double,"\x40\x0b\xa7\x7c\x45\xcb\xbc\x2c",3.45678,buhtond);
234  uint32_t fbits = 0x0100807f;
235  float *fptr = (float*)&fbits;
236  HOSTTOTEST(float,"\x01\x00\x80\x7f",*fptr,buhtonf);
237  uint64_t dbits = 0x3d859d6aa891f37f;
238  double *dptr = (double*)&dbits;
239  HOSTTOTEST(double,"\x3d\x85\x9d\x6a\xa8\x91\xf3\x7f",*dptr,buhtond);
240 
241  // same as above with offsets
242 
243  HOSTTOTESTPOS(uint16_t,"\x96\x34",0x9634,buhtons,3);
244  HOSTTOTESTPOS(uint32_t,"\xef\xbe\xad\xde",0xefbeadde,buhtonl,3);
245  HOSTTOTESTPOS(uint64_t,"\x08\x07\x06\x05\x04\x03\x02\x01",0x0807060504030201,buhtonll,3);
246  HOSTTOTESTPOS(int16_t,"\x03\xf7",0x03f7,buhtonss,3);
247  HOSTTOTESTPOS(int32_t,"\x0a\x0b\x0c\xff",0x0a0b0cff,buhtonsl,3);
248  HOSTTOTESTPOS(int64_t,"\x0d\x0e\x0f\x12\x34\x56\xff\xff",0x0d0e0f123456ffff,buhtonsll,3);
249  HOSTTOTESTPOS(float,"\x40\x5d\x3b\xe2",3.45678f,buhtonf,3);
250  HOSTTOTESTPOS(double,"\x40\x0b\xa7\x7c\x45\xcb\xbc\x2c",3.45678,buhtond,3);
251  HOSTTOTESTPOS(float,"\x01\x00\x80\x7f",*fptr,buhtonf,3);
252  HOSTTOTESTPOS(double,"\x3d\x85\x9d\x6a\xa8\x91\xf3\x7f",*dptr,buhtond,3);
253 
254  return testFramework.countFails();
255  }
256 
257  //==========================================================
258  // Test Suite: decodeVarTest()
259  //==========================================================
260  //
261  // Tests if item was removed from string and output bytes
262  // are in host byte order
263  //
264  //==========================================================
265  int decodeVarTest(void)
266  {
267  TUDEF("BinUtils", "decodeVar");
268 
269  //std::cout.setf(std::ios_base::hex,std::ios_base::basefield);
270 
271  std::string stringTest = "Random";
272  // should remove first character of stringTest
273  gnsstk::BinUtils::decodeVar<char>(stringTest);
274  TUASSERTE(std::string,"andom",stringTest);
275 
276  std::string stringTest0 = "Random";
277  char out0 = gnsstk::BinUtils::decodeVar<char>(stringTest0, 0);
278  TUASSERTE(char,'R',out0);
279 
280  // test possibility of corruption with numbers that would be
281  // NaN if byte swapped.
282  uint64_t dbits = 0x3d859d6aa891f37f;
283  double *dptr = (double*)&dbits;
284  static const unsigned char test4Arr[] = { 0x3d, 0x85, 0x9d, 0x6a,
285  0xa8, 0x91, 0xf3, 0x7f };
286  std::string encBE((char*)test4Arr, sizeof(test4Arr));
287  TUASSERTFE(*dptr,gnsstk::BinUtils::decodeVar<double>(encBE));
288  TUASSERTE(size_t,0,encBE.size());
289 
290  // These tests are strange to look at. Do not make the
291  // mistake of thinking that the expected value being decoded
292  // is 5000; it is not. The expected value being decoded is
293  // 0x35303030 or 892350512.
294  //
295  // Ordinarily the decodeVar/encodeVar methods would not be
296  // used in this fashion. They would instead be used to
297  // encode/decode binary values, so the string wouldn't be
298  // "5000", it would be 0x1388 which is <CR>X in text.
299 
300  std::string stringTest1 = "I am 5000.";
301  uint32_t out1 = gnsstk::BinUtils::decodeVar<uint32_t>(stringTest1, 5);
302  TUASSERTE(uint32_t,0x35303030,out1);
303 
304  std::string stringTest2 = "The word 'this' should be read";
305  float out2 = gnsstk::BinUtils::decodeVar<float>(stringTest2, 10);
306  uint32_t bytes = 0x74686973; // 'this' as ascii bytes
307  float fexpected = *(float*)&bytes; // interpret bytes as a float
308  TUASSERTFE(fexpected,out2);
309 
310  return testFramework.countFails();
311  }
312 
313 
314  int decodeVarLETest(void)
315  {
316  TUDEF("BinUtils", "decodeVarLE");
317 
318  //std::cout.setf(std::ios_base::hex,std::ios_base::basefield);
319 
320  std::string stringTest = "Random";
321  // should remove first character of stringTest
322  gnsstk::BinUtils::decodeVarLE<char>(stringTest);
323  TUASSERTE(std::string,"andom",stringTest);
324 
325  std::string stringTest0 = "Random";
326  char out0 = gnsstk::BinUtils::decodeVarLE<char>(stringTest0, 0);
327  TUASSERTE(char,'R',out0);
328 
329  // test possibility of corruption with numbers that would be
330  // NaN if byte swapped.
331  uint64_t dbits = 0x3d859d6aa891f37f;
332  double *dptr = (double*)&dbits;
333  static const unsigned char test4Arr[] = { 0x7f, 0xf3, 0x91, 0xa8,
334  0x6a, 0x9d, 0x85, 0x3d };
335  std::string encLE((char*)test4Arr, sizeof(test4Arr));
336  TUASSERTFE(*dptr,gnsstk::BinUtils::decodeVarLE<double>(encLE));
337  TUASSERTE(size_t,0,encLE.size());
338 
339  // These tests are strange to look at. Do not make the
340  // mistake of thinking that the expected value being decoded
341  // is 5000; it is not. The expected value being decoded is
342  // 0x35303030 or 892350512.
343  //
344  // Ordinarily the decodeVarLE/encodeVarLE methods would not be
345  // used in this fashion. They would instead be used to
346  // encode/decode binary values, so the string wouldn't be
347  // "5000", it would be 0x1388 which is <CR>X in text.
348 
349  std::string stringTest1 = "I am 5012.";
350  uint32_t out1 = gnsstk::BinUtils::decodeVarLE<uint32_t>(stringTest1, 5);
351  TUASSERTE(uint32_t,0x32313035,out1);
352 
353  std::string stringTest2 = "The word 'this' should be read";
354  float out2 = gnsstk::BinUtils::decodeVarLE<float>(stringTest2, 10);
355  uint32_t bytes = 0x73696874; // 'this' as ascii bytes
356  float fexpected = *(float*)&bytes; // interpret bytes as a float
357  TUASSERTFE(fexpected,out2);
358 
359  return testFramework.countFails();
360  }
361 
362 
363  //==========================================================
364  // Test Suite: encodeVarTest()
365  //==========================================================
366  //
367  // Tests if bytes are in network byte order
368  //
369  //==========================================================
370  int encodeVarTest(void)
371  {
372  TUDEF("BinUtils", "encodeVar");
373 
374  char test1 = 'H';
375  std::string stringTest1 = gnsstk::BinUtils::encodeVar<char>(test1);
376  TUASSERTE(std::string,"H",stringTest1);
377 
378  uint32_t test2 = 0x41424344; // "ABCD"
379  std::string stringTest2 = gnsstk::BinUtils::encodeVar<uint32_t>(test2);
380  TUASSERTE(std::string,"ABCD",stringTest2);
381 
382  // This is weird. Why do it this way? Converting from hex
383  // to 331575210351 decimal and storing it in a float...
384  float test3 = 0x4D336C316F; // "M3l10"
385  std::string stringTest3 = gnsstk::BinUtils::encodeVar<float>(test3);
386  TUASSERTE(std::string,"\x52\x9A\x66\xD8",stringTest3);
387 
388  // odd value that has been causing issues with MDP tests
389  static const double test4 = 2.4573306210644260e-12;
390  // big-endian, converted using
391  // http://babbage.cs.qc.edu/IEEE-754.old/64bit.html
392  static const unsigned char test4Arr[] = { 0x3d, 0x85, 0x9d, 0x6a,
393  0xa8, 0x91, 0xf3, 0x7f };
394  static const std::string test4Str((char*)test4Arr, sizeof(test4Arr));
395  std::string encBE(gnsstk::BinUtils::encodeVar<double>(test4));
396  TUASSERTE(std::string, test4Str, encBE);
397 
398  return testFramework.countFails();
399  }
400 
401 
402  int encodeVarLETest(void)
403  {
404  TUDEF("BinUtils", "encodeVarLE");
405 
406  char test1 = 'H';
407  std::string stringTest1 = gnsstk::BinUtils::encodeVarLE<char>(test1);
408  TUASSERTE(std::string,"H",stringTest1);
409 
410  uint32_t test2 = 0x41424344; // "ABCD"
411  std::string stringTest2 = gnsstk::BinUtils::encodeVarLE<uint32_t>(test2);
412  TUASSERTE(std::string,"DCBA",stringTest2);
413 
414  // This is weird. Why do it this way? Converting from hex
415  // to 331575210351 decimal and storing it in a float...
416  float test3 = 0x4D336C316F; // "M3l10"
417  std::string stringTest3 = gnsstk::BinUtils::encodeVarLE<float>(test3);
418  TUASSERTE(std::string,"\xD8\x66\x9A\x52",stringTest3);
419 
420  // odd value that has been causing issues with MDP tests
421  static const double test4 = 2.4573306210644260e-12;
422  // big-endian, converted using
423  // http://babbage.cs.qc.edu/IEEE-754.old/64bit.html
424  static const unsigned char test4Arr[] = { 0x7f, 0xf3, 0x91, 0xa8,
425  0x6a, 0x9d, 0x85, 0x3d };
426  static const std::string test4Str((char*)test4Arr, sizeof(test4Arr));
427  std::string encLE(gnsstk::BinUtils::encodeVarLE<double>(test4));
428  TUASSERTE(std::string, test4Str, encLE);
429 
430  return testFramework.countFails();
431  }
432 
433  //====================================================================
434  // Test Suite: computeCRCTest()
435  //====================================================================
436  //
437  // Tests if computeCRC is generating valid Cyclic
438  // Redundancy Checks
439  //
440  // Using http://www.zorc.breitbandkatze.de/crc.html for
441  // generating comparison for CRCs, in addition to writing out
442  // the calculations by hand.
443  //
444  //=====================================================================
445  int computeCRCTest(void)
446  {
449  TUDEF("BinUtils", "computeCRC");
450  uint32_t crc;
451  // Use printable characters for the convenience of checking
452  // against web calculators that only allow you to enter
453  // printable characters.
454  unsigned char data1[] = "This is a Test!@#$^...";
455  // -1 ignores the NULL at the end
456  unsigned long len1 = sizeof(data1)-1;
457 
458  // these tests exercise computeCRC with:
459  // 1) simple and complex polynomials
460  // 2) bit-wide (e.g. 24-bit) and byte-wide (e.g. 16-, 32-bit) orders
461  // 3) zero and non-zero initial and final XOR values
462  // 4) direct and non-direct computation
463  // 5) data bytes reversed and non-reversed
464  // 6) reverse and do not reverse CRC before final XOR
465 
466  // Test a standard CRC-32
467  crc = computeCRC(data1, len1, gnsstk::BinUtils::CRC32);
468  TUASSERTE(unsigned long, 0xeaa96e4d, crc);
469 
470  // Test a standard CRC-16
471  crc = computeCRC(data1, len1, gnsstk::BinUtils::CRC16);
472  TUASSERTE(unsigned long, 0x2c74, crc);
473 
474  // Test a standard CRC-CCITT
475  crc = computeCRC(data1, len1, gnsstk::BinUtils::CRCCCITT);
476  TUASSERTE(unsigned long, 0x3bcc, crc);
477 
478  // Test a standard CRC-24Q
479  crc = computeCRC(data1, len1, gnsstk::BinUtils::CRC24Q);
480  TUASSERTE(unsigned long, 0x6fa2f6, crc);
481 
482  // non-direct test
483  CRCParam nonDirect(24, 0x823ba9, 0xffffff, 0xffffff, false, false,false);
484  crc = computeCRC(data1, len1, nonDirect);
485  TUASSERTE(unsigned long, 0x982748, crc);
486 
487  // Parity bit.
488  CRCParam parity(1, 1, 0, 0, true, false, false);
489  crc = computeCRC(data1, len1, parity);
490  TUASSERTE(unsigned long, 1, crc);
491 
492  //test crc computation on 1 ASCII char
493  unsigned char data2[] = { 0x72 };
494  unsigned long len2 = 1;
495  crc = computeCRC(data2, len2, gnsstk::BinUtils::CRC32);
496  TUASSERTE(unsigned long, 0x6c09ff9d, crc);
497  crc = computeCRC(data2, len2, gnsstk::BinUtils::CRC16);
498  TUASSERTE(unsigned long, 0x2580, crc);
499  crc = computeCRC(data2, len2, gnsstk::BinUtils::CRCCCITT);
500  TUASSERTE(unsigned long, 0xbf25, crc);
501 
502  return testFramework.countFails();
503  }
504 
505  //==========================================================
506  // Test Suite: xorChecksumTest()
507  //==========================================================
508  //
509  // Computes xorChecksums with 1, 2 and 3 words for chars,
510  // shorts, and ints
511  //
512  //==========================================================
513  int xorChecksumTest(void)
514  {
515  TUDEF("BinUtils", "xorChecksum");
516 
517  try
518  {
519  gnsstk::BinUtils::xorChecksum("Hello", 2);
520  TUFAIL("xorChecksum should have failed on uneven input string");
521  }
522  catch(gnsstk::InvalidParameter e)
523  {
524  TUPASS("xorChecksum");
525  }
526  catch(...)
527  {
528  TUFAIL("xorChecksum threw the wrong exception type");
529  }
530 
531  std::string cksum;
532 
533  cksum = gnsstk::BinUtils::xorChecksum("7", 1);
534  TUASSERTE(std::string,std::string("7"),cksum);
535 
536  cksum = gnsstk::BinUtils::xorChecksum("Bc", 1);
537  TUASSERTE(std::string,std::string("!"),cksum);
538 
539  cksum = gnsstk::BinUtils::xorChecksum("P/Q", 1);
540  TUASSERTE(std::string,std::string("."),cksum);
541 
542  cksum = gnsstk::BinUtils::xorChecksum("mn", 2);
543  TUASSERTE(std::string,std::string("mn"),cksum);
544 
545  cksum = gnsstk::BinUtils::xorChecksum("59WZ", 2);
546  TUASSERTE(std::string,std::string("bc"),cksum);
547 
548  cksum = gnsstk::BinUtils::xorChecksum("am+*09", 2);
549  TUASSERTE(std::string,std::string("z~"),cksum);
550 
551  cksum = gnsstk::BinUtils::xorChecksum("97Bg", 4);
552  TUASSERTE(std::string,std::string("97Bg"),cksum);
553 
554  cksum = gnsstk::BinUtils::xorChecksum("ABCD!#$%", 4);
555  TUASSERTE(std::string,std::string("`aga"),cksum);
556 
557  cksum = gnsstk::BinUtils::xorChecksum("+a0.ehZ64xYN", 4);
558  TUASSERTE(std::string,std::string("zq3V"),cksum);
559 
560  return testFramework.countFails();
561 
562  }
563 
564  //==========================================================
565  // Test Suite: countBitsTest()
566  //==========================================================
567  //
568  // Counts the number of set bits in 32 bit unsigned int
569  //
570  //==========================================================
571  int countBitsTest(void)
572  {
573  TUDEF("BinUtils", "countBits");
574  std::string failMesg;
575 
576  TUASSERTE(unsigned short,2,gnsstk::BinUtils::countBits(5));
577 
578  // testing if bit count is constant in a left-shift operation
579  TUASSERTE(unsigned short,2,gnsstk::BinUtils::countBits(10));
580  TUASSERTE(unsigned short,2,gnsstk::BinUtils::countBits(20));
581 
582  // same but for right bit
583  TUASSERTE(unsigned short,1,gnsstk::BinUtils::countBits(16));
584  TUASSERTE(unsigned short,1,gnsstk::BinUtils::countBits(8));
585 
586  // random case
587  TUASSERTE(unsigned short,4,gnsstk::BinUtils::countBits(15));
588 
589  return testFramework.countFails();
590  }
591 
592 };
593 
594 
595 int main (void)
596 {
597  int errorTotal = 0;
598  BinUtils_T testClass;
599 
600  errorTotal += testClass.intelToHostTest();
601  errorTotal += testClass.netToHostTest();
602  errorTotal += testClass.hostToIntelTest();
603  errorTotal += testClass.hostToNetTest();
604  errorTotal += testClass.decodeVarTest();
605  errorTotal += testClass.decodeVarLETest();
606  errorTotal += testClass.encodeVarTest();
607  errorTotal += testClass.encodeVarLETest();
608  errorTotal += testClass.computeCRCTest();
609  errorTotal += testClass.xorChecksumTest();
610  errorTotal += testClass.countBitsTest();
611 
612  cout << "Total Failures for " << __FILE__ << ": " << errorTotal << endl;
613 
614  return errorTotal; //Return the total number of errors
615 }
BinUtils_T::countBitsTest
int countBitsTest(void)
Definition: BinUtils_T.cpp:571
gnsstk::BinUtils::CRC24Q
const CRCParam CRC24Q(24, 0x823ba9, 0, 0xffffff, true, false, false)
CRC-24Q parameters.
Definition: BinUtils.hpp:311
gnsstk::BinUtils::buitohsl
void buitohsl(const void *p, int32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:635
BinUtils_T::decodeVarTest
int decodeVarTest(void)
Definition: BinUtils_T.cpp:265
gnsstk::BinUtils::buitohs
void buitohs(const void *p, uint16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:574
gnsstk::BinUtils::buntohsll
void buntohsll(const void *p, int64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:783
gnsstk::BinUtils::buntohd
void buntohd(const void *p, double &v, unsigned pos=0)
Definition: BinUtils.hpp:818
gnsstk::BinUtils::buhtonl
void buhtonl(void *p, uint32_t v, unsigned pos=0)
Definition: BinUtils.hpp:983
gnsstk::BinUtils::buhtoid
void buhtoid(void *p, double v, unsigned pos=0)
Definition: BinUtils.hpp:950
BinUtils_T::encodeVarLETest
int encodeVarLETest(void)
Definition: BinUtils_T.cpp:402
TOHOSTTEST
#define TOHOSTTEST(TYPE, STR, EXP, FN)
Definition: BinUtils_T.cpp:52
gnsstk::BinUtils::buhtoiss
void buhtoiss(void *p, int16_t v, unsigned pos=0)
Definition: BinUtils.hpp:885
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
TUFAIL
#define TUFAIL(MSG)
Definition: TestUtil.hpp:228
gnsstk::BinUtils::buhtond
void buhtond(void *p, double v, unsigned pos=0)
Definition: BinUtils.hpp:1082
BinUtils_T::hostToNetTest
int hostToNetTest()
Definition: BinUtils_T.cpp:221
TOHOSTTESTF
#define TOHOSTTESTF(TYPE, STR, EXP, FN)
Definition: BinUtils_T.cpp:66
gnsstk::BinUtils::buhtonll
void buhtonll(void *p, uint64_t v, unsigned pos=0)
Definition: BinUtils.hpp:998
gnsstk::BinUtils::buitohss
void buitohss(const void *p, int16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:621
gnsstk::BinUtils::buhtonss
void buhtonss(void *p, int16_t v, unsigned pos=0)
Definition: BinUtils.hpp:1017
gnsstk::BinUtils::computeCRC
uint32_t computeCRC(const unsigned char *data, unsigned long len, const CRCParam &params)
Definition: BinUtils.hpp:477
gnsstk::BinUtils::buntohl
void buntohl(const void *p, uint32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:719
gnsstk::BinUtils::buhtonsl
void buhtonsl(void *p, int32_t v, unsigned pos=0)
Definition: BinUtils.hpp:1031
gnsstk::BinUtils::buntohss
void buntohss(const void *p, int16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:753
gnsstk::BinUtils::CRCParam
Encapsulate parameters for CRC computation.
Definition: BinUtils.hpp:280
TestUtil.hpp
gnsstk::BinUtils::buitohsll
void buitohsll(const void *p, int64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:651
HOSTTOTESTPOS
#define HOSTTOTESTPOS(TYPE, STR, VAL, FN, POS)
Definition: BinUtils_T.cpp:85
gnsstk::BinUtils::buhtois
void buhtois(void *p, uint16_t v, unsigned pos=0)
Definition: BinUtils.hpp:838
BinUtils_T::intelToHostTest
int intelToHostTest()
Definition: BinUtils_T.cpp:94
BinUtils_T::netToHostTest
int netToHostTest()
Definition: BinUtils_T.cpp:139
gnsstk::BinUtils::xorChecksum
std::string xorChecksum(const std::string &str, unsigned wordSize)
Definition: BinUtils.hpp:551
gnsstk::BinUtils::buntohll
void buntohll(const void *p, uint64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:734
TUPASS
#define TUPASS(MSG)
Definition: TestUtil.hpp:230
BinUtils_T::xorChecksumTest
int xorChecksumTest(void)
Definition: BinUtils_T.cpp:513
BinUtils_T::decodeVarLETest
int decodeVarLETest(void)
Definition: BinUtils_T.cpp:314
TOHOSTTESTFPOS
#define TOHOSTTESTFPOS(TYPE, STR, EXP, FN, POS)
Definition: BinUtils_T.cpp:73
gnsstk::BinUtils::buhtoil
void buhtoil(void *p, uint32_t v, unsigned pos=0)
Definition: BinUtils.hpp:851
main
int main(void)
Definition: BinUtils_T.cpp:595
gnsstk::BinUtils::buntohf
void buntohf(const void *p, float &v, unsigned pos=0)
Definition: BinUtils.hpp:803
gnsstk::BinUtils::buntohs
void buntohs(const void *p, uint16_t &v, unsigned pos=0)
Definition: BinUtils.hpp:706
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
gnsstk::BinUtils::buitohl
void buitohl(const void *p, uint32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:587
HOSTTOTEST
#define HOSTTOTEST(TYPE, STR, VAL, FN)
Definition: BinUtils_T.cpp:81
gnsstk::BinUtils::buitohf
void buitohf(const void *p, float &v, unsigned pos=0)
Definition: BinUtils.hpp:671
gnsstk::BinUtils::buitohd
void buitohd(const void *p, double &v, unsigned pos=0)
Definition: BinUtils.hpp:686
BinUtils.hpp
gnsstk::BinUtils::buhtoill
void buhtoill(void *p, uint64_t v, unsigned pos=0)
Definition: BinUtils.hpp:866
gnsstk::BinUtils::buhtonsll
void buhtonsll(void *p, int64_t v, unsigned pos=0)
Definition: BinUtils.hpp:1047
gnsstk::BinUtils::buhtoisll
void buhtoisll(void *p, int64_t v, unsigned pos=0)
Definition: BinUtils.hpp:915
BinUtils_T
Definition: BinUtils_T.cpp:90
Exception.hpp
std
Definition: Angle.hpp:142
gnsstk::BinUtils::buntohsl
void buntohsl(const void *p, int32_t &v, unsigned pos=0)
Definition: BinUtils.hpp:767
gnsstk::BinUtils::buhtonf
void buhtonf(void *p, float v, unsigned pos=0)
Definition: BinUtils.hpp:1067
gnsstk::BinUtils::countBits
unsigned short countBits(uint32_t v)
Definition: BinUtils.hpp:342
TUASSERTFE
#define TUASSERTFE(EXP, GOT)
Definition: TestUtil.hpp:103
gnsstk::BinUtils::buitohll
void buitohll(const void *p, uint64_t &v, unsigned pos=0)
Definition: BinUtils.hpp:602
TOHOSTTESTPOS
#define TOHOSTTESTPOS(TYPE, STR, EXP, FN, POS)
Definition: BinUtils_T.cpp:59
gnsstk::BinUtils::buhtoif
void buhtoif(void *p, float v, unsigned pos=0)
Definition: BinUtils.hpp:935
gnsstk::BinUtils::CRC32
const CRCParam CRC32(32, 0x4c11db7, 0xffffffff, 0xffffffff, true, true, true)
CRC-32 parameters.
Definition: BinUtils.hpp:309
BinUtils_T::computeCRCTest
int computeCRCTest(void)
Definition: BinUtils_T.cpp:445
gnsstk::BinUtils::buhtoisl
void buhtoisl(void *p, int32_t v, unsigned pos=0)
Definition: BinUtils.hpp:899
gnsstk::BinUtils::CRC16
const CRCParam CRC16(16, 0x8005, 0, 0, true, true, true)
CRC-16 parameters.
Definition: BinUtils.hpp:307
BinUtils_T::hostToIntelTest
int hostToIntelTest()
Definition: BinUtils_T.cpp:184
gnsstk::BinUtils::CRCCCITT
const CRCParam CRCCCITT(16, 0x1021, 0xffff, 0, true, false, false)
CCITT CRC parameters.
Definition: BinUtils.hpp:305
BinUtils_T::encodeVarTest
int encodeVarTest(void)
Definition: BinUtils_T.cpp:370
gnsstk::BinUtils::buhtons
void buhtons(void *p, uint16_t v, unsigned pos=0)
Definition: BinUtils.hpp:970


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