FormattedDouble_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 "FormattedDouble.hpp"
41 
42 namespace std
43 {
44  std::ostream& operator<<(std::ostream& s, gnsstk::StringUtils::FFLead v)
45  {
46  s << static_cast<int>(v);
47  return s;
48  }
49  std::ostream& operator<<(std::ostream& s, gnsstk::StringUtils::FFSign v)
50  {
51  s << static_cast<int>(v);
52  return s;
53  }
54  std::ostream& operator<<(std::ostream& s, gnsstk::StringUtils::FFAlign v)
55  {
56  s << static_cast<int>(v);
57  return s;
58  }
59 }
60 
61 // EXP = expected value
62 // FD = FormattedDouble object
63 // VAL = double precision value expected after reading (precision is
64 // being thrown away deliberately in the formatting tests, so we
65 // need this).
66 #define STRMASSERT(EXP, FD, VAL) \
67  { \
68  std::ostringstream oss; \
69  oss << FD; \
70  std::string formatted = oss.str(); \
71  TUCSM("operator<<"); \
72  TUASSERTE(std::string, EXP, formatted); \
73  std::istringstream iss(formatted); \
74  gnsstk::FormattedDouble fdin(FD.totalLen, FD.exponentChar); \
75  iss >> fdin; \
76  TUCSM("operator>>"); \
77  TUASSERTFE(VAL, fdin.val); \
78  gnsstk::FormattedDouble fdin2(FD); \
79  fdin2 = formatted; \
80  TUCSM("operator=(string)"); \
81  TUASSERTFE(VAL, fdin2.val); \
82  }
83 
85 {
86 public:
87  unsigned constructorTest();
88  unsigned streamTest();
89  unsigned castTest();
90  unsigned scaleTest();
91 };
92 
93 
94 unsigned FormattedDouble_T ::
96 {
97  TUDEF("FormattedDouble", "");
98  // please don't mess with defaults...
99  double d(1.234);
105 
106  TUCSM("FormattedDouble(double,...)");
107  TUASSERTFE(d, t1.val);
110  TUASSERTE(unsigned, 0, t1.mantissaLen);
111  TUASSERTE(unsigned, 2, t1.exponentLen);
112  TUASSERTE(unsigned, 0, t1.totalLen);
113  TUASSERTE(char, 'e', t1.exponentChar);
118 
119  TUASSERTFE(d, t2.val);
122  TUASSERTE(unsigned, 99, t2.mantissaLen);
123  TUASSERTE(unsigned, 12, t2.exponentLen);
124  TUASSERTE(unsigned, 128, t2.totalLen);
125  TUASSERTE(char, 'F', t2.exponentChar);
130 
131  TUCSM("FormattedDouble(unsigned,char)");
132  TUASSERTFE(0., t3.val);
135  TUASSERTE(unsigned, 0, t3.mantissaLen);
136  TUASSERTE(unsigned, 2, t3.exponentLen);
137  TUASSERTE(unsigned, 0, t3.totalLen);
138  TUASSERTE(char, 'e', t3.exponentChar);
143  TURETURN();
144 }
145 
146 
147 unsigned FormattedDouble_T ::
149 {
150  TUDEF("FormattedDouble", "operator<<");
151 
152  double p1 = 1.2345; // positive test value
153  double n1 = -1.2345; // negative test value
182 
183  STRMASSERT("0.1234e+01", t1, 0.1234e+01);
184  STRMASSERT(".12345e+01", t2, .12345e+01);
185  STRMASSERT("1.2345e+00", t3, 1.2345e+00);
186  STRMASSERT("0.1234d+01", t4, 0.1234e+01);
187  STRMASSERT("0.1234D+01", t5, 0.1234e+01);
188  STRMASSERT("0.1234x+01", t6, 0.1234e+01);
189  STRMASSERT("0.1234E+01", t7, 0.1234E+01);
190  STRMASSERT(" 0.1234E+01", t8, 0.1234E+01);
191  STRMASSERT("+0.1234E+01", t9, +0.1234E+01);
192  STRMASSERT("+0.1234E+0001", t10, +0.1234E+0001);
193 
194  STRMASSERT("-0.1234e+01", t11, -0.1234e+01);
195  STRMASSERT("-.12345e+01", t12, -.12345e+01);
196  STRMASSERT("-1.2345e+00", t13, -1.2345e+00);
197  STRMASSERT("-0.1234d+01", t14, -0.1234e+01);
198  STRMASSERT("-0.1234D+01", t15, -0.1234e+01);
199  STRMASSERT("-0.1234x+01", t16, -0.1234e+01);
200  STRMASSERT("-0.1234E+01", t17, -0.1234E+01);
201  STRMASSERT("-0.1234E+01", t18, -0.1234E+01);
202  STRMASSERT("-0.1234E+01", t19, -0.1234E+01);
203  STRMASSERT("-0.1234E+0001", t20, -0.1234E+0001);
204 
205  TURETURN();
206 }
207 
208 
209 unsigned FormattedDouble_T ::
211 {
212  TUDEF("FormattedDouble", "operator=(double)");
213 
215  gnsstk::FormattedDouble &t1ref(t1 = 9.0);
216  TUASSERTFE(9.0, t1.val);
217  TUASSERTE(gnsstk::FormattedDouble*, &t1, &t1ref);
218  double foo = t1 * 123.0;
219  TUCSM("operator double()");
220  TUASSERTFE(1107.0, foo);
221  double bar = t1;
222  TUASSERTFE(9.0, bar);
223 
224  TUCSM("operator=(double)");
227  t2 = 9.0;
228  TUASSERTFE(9.0, t2.val);
229  // make sure the assignment didn't reset any existing data fields
232  TUASSERTE(unsigned, 10, t2.mantissaLen);
233  TUASSERTE(unsigned, 3, t2.exponentLen);
234  TUASSERTE(char, 'x', t2.exponentChar);
237 
238  // implicit conversions
239  unsigned u = 9;
241  t3 = u;
242  TUASSERTFE(9.0, t3.val);
243  int i = 9;
245  t4 = i;
246  TUASSERTFE(9.0, t4.val);
247 
248  TURETURN();
249 }
250 
251 
252 unsigned FormattedDouble_T ::
254 {
255  TUDEF("FormattedDouble", "");
256  double d(100.0);
260  gnsstk::FormattedDouble t2 = t1 / 2.0;
261  gnsstk::FormattedDouble t3 = t1 * 0.5;
262 
263  TUCSM("operator/(double)");
264  TUASSERTFE(50.0, t2.val);
267  TUASSERTE(unsigned, 99, t2.mantissaLen);
268  TUASSERTE(unsigned, 12, t2.exponentLen);
269  TUASSERTE(unsigned, 128, t2.totalLen);
270  TUASSERTE(char, 'F', t2.exponentChar);
275 
276  TUCSM("operator*(double)");
277  TUASSERTFE(50.0, t3.val);
280  TUASSERTE(unsigned, 99, t3.mantissaLen);
281  TUASSERTE(unsigned, 12, t3.exponentLen);
282  TUASSERTE(unsigned, 128, t3.totalLen);
283  TUASSERTE(char, 'F', t3.exponentChar);
288 
289  TURETURN();
290 }
291 
292 
293 int main()
294 {
295  unsigned errorTotal = 0;
296  FormattedDouble_T testClass;
297  errorTotal += testClass.constructorTest();
298  errorTotal += testClass.streamTest();
299  errorTotal += testClass.castTest();
300  errorTotal += testClass.scaleTest();
301  std::cout << "Total Failures for " << __FILE__ << ": " << errorTotal
302  << std::endl;
303  return errorTotal;
304 }
FormattedDouble_T::streamTest
unsigned streamTest()
Definition: FormattedDouble_T.cpp:148
FormattedDouble_T::constructorTest
unsigned constructorTest()
Definition: FormattedDouble_T.cpp:95
TUCSM
#define TUCSM(METHOD)
Definition: TestUtil.hpp:59
gnsstk::FormattedDouble::exponentLen
unsigned exponentLen
How many digits of exponent.
Definition: FormattedDouble.hpp:184
gnsstk::StringUtils::FFAlign::Left
@ Left
Formatted output will be left-aligned.
TUASSERTE
#define TUASSERTE(TYPE, EXP, GOT)
Definition: TestUtil.hpp:81
gnsstk::FormattedDouble::exponentChar
char exponentChar
What character delimits the exponent.
Definition: FormattedDouble.hpp:186
gnsstk::StringUtils::FFSign::NegPos
@ NegPos
Prefix output with a minus sign (neg) or plus sign (pos)
gnsstk::StringUtils::FFAlign::Right
@ Right
Formatted output will be right-aligned.
gnsstk::StringUtils::FFLead
FFLead
Leading character for floatFormat(), after any whitespace or sign.
Definition: StringUtils.hpp:109
gnsstk::FormattedDouble
Definition: FormattedDouble.hpp:70
gnsstk::StringUtils::FFSign::NegOnly
@ NegOnly
Prefix output with a minus sign (neg) or nothing (pos)
TestUtil.hpp
gnsstk::StringUtils::FFLead::Zero
@ Zero
Start with zero, e.g. 0.12345.
gnsstk::FormattedDouble::alignment
StringUtils::FFAlign alignment
Alignment when padding with space.
Definition: FormattedDouble.hpp:188
gnsstk::StringUtils::FFSign::NegSpace
@ NegSpace
Prefix output with a minus sign (neg) or space (pos)
gnsstk::FormattedDouble::leadSign
StringUtils::FFSign leadSign
How to handle signs for positive val.
Definition: FormattedDouble.hpp:187
TURETURN
#define TURETURN()
Definition: TestUtil.hpp:232
gnsstk::StringUtils::FFLead::Decimal
@ Decimal
Start with decimal, e.g. .12345.
main
int main()
Definition: FormattedDouble_T.cpp:293
gnsstk::StringUtils::FFLead::NonZero
@ NonZero
Start with the first non-zero digit, e.g. 1.2345.
FormattedDouble_T::scaleTest
unsigned scaleTest()
Definition: FormattedDouble_T.cpp:253
FormattedDouble.hpp
gnsstk::FormattedDouble::mantissaLen
unsigned mantissaLen
How many digits of mantissa.
Definition: FormattedDouble.hpp:183
TUDEF
#define TUDEF(CLASS, METHOD)
Definition: TestUtil.hpp:56
STRMASSERT
#define STRMASSERT(EXP, FD, VAL)
Definition: FormattedDouble_T.cpp:66
FormattedDouble_T
Definition: FormattedDouble_T.cpp:84
std::operator<<
std::ostream & operator<<(std::ostream &s, gnsstk::StringUtils::FFLead v)
Definition: FormattedDouble_T.cpp:44
gnsstk::FormattedDouble::totalLen
unsigned totalLen
Total width of space-padded value.
Definition: FormattedDouble.hpp:185
std
Definition: Angle.hpp:142
gnsstk::FormattedDouble::leadChar
StringUtils::FFLead leadChar
Leading non-space character.
Definition: FormattedDouble.hpp:182
gnsstk::StringUtils::FFSign
FFSign
How to handle sign in floatFormat()
Definition: StringUtils.hpp:117
TUASSERTFE
#define TUASSERTFE(EXP, GOT)
Definition: TestUtil.hpp:103
gnsstk::StringUtils::FFAlign
FFAlign
Alignment of data for floatFormat()
Definition: StringUtils.hpp:125
FormattedDouble_T::castTest
unsigned castTest()
Definition: FormattedDouble_T.cpp:210
gnsstk::FormattedDouble::val
double val
The value as read or to be formatted.
Definition: FormattedDouble.hpp:181


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