1 // file      : tests/cxx/parser/validation/built-in/float/driver.cxx
2 // copyright : Copyright (c) 2006-2017 Code Synthesis Tools CC
3 // license   : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 
5 // Test the built-in float, double, and decimal types validation.
6 //
7 #include <math.h>
8 #include <cassert>
9 
10 #include <xsd/cxx/parser/validating/exceptions.hxx>
11 #include <xsd/cxx/parser/validating/xml-schema-pimpl.hxx>
12 
13 using namespace xsd::cxx::parser::validating;
14 
15 template <typename T>
16 bool
test_post_fail(T & p)17 test_post_fail (T& p)
18 {
19   try
20   {
21     p._post ();
22   }
23   catch (invalid_value<char> const&)
24   {
25     return true;
26   }
27 
28   return false;
29 }
30 
31 int
main()32 main ()
33 {
34   // Good.
35   //
36 
37   // float
38   //
39   {
40     float_pimpl<char> p;
41     p.pre ();
42     p._pre ();
43     p._characters (" 0000123.456 ");
44     p._post ();
45     assert (p.post_float () == 123.456F);
46   }
47 
48   {
49     float_pimpl<char> p;
50     p.pre ();
51     p._pre ();
52     p._characters ("-12.345E2");
53     p._post ();
54     assert (p.post_float () == -12.345E2F);
55   }
56 
57   {
58     float_pimpl<char> p;
59     p.pre ();
60     p._pre ();
61     p._characters ("0");
62     p._post ();
63     assert (p.post_float () == 0.0F);
64   }
65 
66   {
67     float_pimpl<char> p;
68     p.pre ();
69     p._pre ();
70     p._characters ("-0");
71     p._post ();
72     assert (p.post_float () == -0.0F);
73   }
74 
75   {
76     float_pimpl<char> p;
77     p.pre ();
78     p._pre ();
79     p._characters ("INF");
80     p._post ();
81     assert (isinf (p.post_float ()));
82   }
83 
84   {
85     float_pimpl<char> p;
86     p.pre ();
87     p._pre ();
88     p._characters ("-INF");
89     p._post ();
90     assert (isinf (p.post_float ()));
91   }
92 
93   {
94     float_pimpl<char> p;
95     p.pre ();
96     p._pre ();
97     p._characters ("NaN");
98     p._post ();
99     assert (isnan (p.post_float ()));
100   }
101 
102   // double
103   //
104   {
105     double_pimpl<char> p;
106     p.pre ();
107     p._pre ();
108     p._characters (" 0000123.456789 ");
109     p._post ();
110     assert (p.post_double () == 123.456789);
111   }
112 
113   {
114     double_pimpl<char> p;
115     p.pre ();
116     p._pre ();
117     p._characters ("-12.3456789E2");
118     p._post ();
119     assert (p.post_double () == -12.3456789E2);
120   }
121 
122   {
123     double_pimpl<char> p;
124     p.pre ();
125     p._pre ();
126     p._characters ("0");
127     p._post ();
128     assert (p.post_double () == 0.0);
129   }
130 
131   {
132     double_pimpl<char> p;
133     p.pre ();
134     p._pre ();
135     p._characters ("-0");
136     p._post ();
137     assert (p.post_double () == -0.0);
138   }
139 
140   {
141     double_pimpl<char> p;
142     p.pre ();
143     p._pre ();
144     p._characters ("INF");
145     p._post ();
146     assert (isinf (p.post_double ()));
147   }
148 
149   {
150     double_pimpl<char> p;
151     p.pre ();
152     p._pre ();
153     p._characters ("-INF");
154     p._post ();
155     assert (isinf (p.post_double ()));
156   }
157 
158   {
159     double_pimpl<char> p;
160     p.pre ();
161     p._pre ();
162     p._characters ("NaN");
163     p._post ();
164     assert (isnan (p.post_double ()));
165   }
166 
167   // decimal
168   //
169   {
170     decimal_pimpl<char> p;
171     p.pre ();
172     p._pre ();
173     p._characters (" 0000123.456789 ");
174     p._post ();
175     assert (p.post_decimal () == 123.456789);
176   }
177 
178   {
179     decimal_pimpl<char> p;
180     p.pre ();
181     p._pre ();
182     p._characters ("-123.45678912345");
183     p._post ();
184     assert (p.post_decimal () == -123.45678912345);
185   }
186 
187   {
188     decimal_pimpl<char> p;
189     p.pre ();
190     p._pre ();
191     p._characters ("0");
192     p._post ();
193     assert (p.post_decimal () == 0.0);
194   }
195 
196   {
197     decimal_pimpl<char> p;
198     p.pre ();
199     p._pre ();
200     p._characters ("-0");
201     p._post ();
202     assert (p.post_decimal () == -0.0);
203   }
204 
205 
206   // Bad
207   //
208 
209   // float
210   //
211   {
212     float_pimpl<char> p;
213     p.pre ();
214     p._pre ();
215     p._characters ("+INF");
216     assert (test_post_fail (p));
217   }
218 
219   {
220     float_pimpl<char> p;
221     p.pre ();
222     p._pre ();
223     p._characters ("1.45 E2");
224     assert (test_post_fail (p));
225   }
226 
227   // double
228   //
229   {
230     double_pimpl<char> p;
231     p.pre ();
232     p._pre ();
233     p._characters ("+INF");
234     assert (test_post_fail (p));
235   }
236 
237   {
238     double_pimpl<char> p;
239     p.pre ();
240     p._pre ();
241     p._characters ("1.45 E2");
242     assert (test_post_fail (p));
243   }
244 
245   // decimal
246   //
247   {
248     decimal_pimpl<char> p;
249     p.pre ();
250     p._pre ();
251     p._characters ("INF");
252     assert (test_post_fail (p));
253   }
254 
255   {
256     decimal_pimpl<char> p;
257     p.pre ();
258     p._pre ();
259     p._characters ("+INF");
260     assert (test_post_fail (p));
261   }
262 
263   {
264     decimal_pimpl<char> p;
265     p.pre ();
266     p._pre ();
267     p._characters ("-INF");
268     assert (test_post_fail (p));
269   }
270 
271   {
272     decimal_pimpl<char> p;
273     p.pre ();
274     p._pre ();
275     p._characters ("NaN");
276     assert (test_post_fail (p));
277   }
278 
279   {
280     decimal_pimpl<char> p;
281     p.pre ();
282     p._pre ();
283     p._characters ("1.45 2");
284     assert (test_post_fail (p));
285   }
286 }
287