1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <locale>
11
12 // class num_get<charT, InputIterator>
13
14 // iter_type get(iter_type in, iter_type end, ios_base&,
15 // ios_base::iostate& err, float& v) const;
16
17 #include <locale>
18 #include <ios>
19 #include <cassert>
20 #include <streambuf>
21 #include <cmath>
22 #include "test_iterators.h"
23 #include "hexfloat.h"
24
25 typedef std::num_get<char, input_iterator<const char*> > F;
26
27 class my_facet
28 : public F
29 {
30 public:
my_facet(std::size_t refs=0)31 explicit my_facet(std::size_t refs = 0)
32 : F(refs) {}
33 };
34
main()35 int main()
36 {
37 const my_facet f(1);
38 std::ios ios(0);
39 float v = -1;
40 {
41 const char str[] = "123";
42 assert((ios.flags() & ios.basefield) == ios.dec);
43 assert(ios.getloc().name() == "C");
44 std::ios_base::iostate err = ios.goodbit;
45 input_iterator<const char*> iter =
46 f.get(input_iterator<const char*>(str),
47 input_iterator<const char*>(str+sizeof(str)),
48 ios, err, v);
49 assert(iter.base() == str+sizeof(str)-1);
50 assert(err == ios.goodbit);
51 assert(v == 123);
52 }
53 {
54 const char str[] = "-123";
55 std::ios_base::iostate err = ios.goodbit;
56 input_iterator<const char*> iter =
57 f.get(input_iterator<const char*>(str),
58 input_iterator<const char*>(str+sizeof(str)),
59 ios, err, v);
60 assert(iter.base() == str+sizeof(str)-1);
61 assert(err == ios.goodbit);
62 assert(v == -123);
63 }
64 {
65 const char str[] = "123.5";
66 std::ios_base::iostate err = ios.goodbit;
67 input_iterator<const char*> iter =
68 f.get(input_iterator<const char*>(str),
69 input_iterator<const char*>(str+sizeof(str)),
70 ios, err, v);
71 assert(iter.base() == str+sizeof(str)-1);
72 assert(err == ios.goodbit);
73 assert(v == 123.5);
74 }
75 {
76 const char str[] = "125e-1";
77 hex(ios);
78 std::ios_base::iostate err = ios.goodbit;
79 input_iterator<const char*> iter =
80 f.get(input_iterator<const char*>(str),
81 input_iterator<const char*>(str+sizeof(str)),
82 ios, err, v);
83 assert(iter.base() == str+sizeof(str)-1);
84 assert(err == ios.goodbit);
85 assert(v == 125e-1);
86 }
87 {
88 const char str[] = "0x125p-1";
89 hex(ios);
90 std::ios_base::iostate err = ios.goodbit;
91 input_iterator<const char*> iter =
92 f.get(input_iterator<const char*>(str),
93 input_iterator<const char*>(str+sizeof(str)),
94 ios, err, v);
95 assert(iter.base() == str+sizeof(str)-1);
96 assert(err == ios.goodbit);
97 assert(v == hexfloat<float>(0x125, 0, -1));
98 }
99 {
100 const char str[] = "inf";
101 hex(ios);
102 std::ios_base::iostate err = ios.goodbit;
103 input_iterator<const char*> iter =
104 f.get(input_iterator<const char*>(str),
105 input_iterator<const char*>(str+sizeof(str)),
106 ios, err, v);
107 assert(iter.base() == str+sizeof(str)-1);
108 assert(err == ios.goodbit);
109 assert(v == INFINITY);
110 }
111 {
112 const char str[] = "INF";
113 hex(ios);
114 std::ios_base::iostate err = ios.goodbit;
115 input_iterator<const char*> iter =
116 f.get(input_iterator<const char*>(str),
117 input_iterator<const char*>(str+sizeof(str)),
118 ios, err, v);
119 assert(iter.base() == str+sizeof(str)-1);
120 assert(err == ios.goodbit);
121 assert(v == INFINITY);
122 }
123 {
124 const char str[] = "-inf";
125 hex(ios);
126 std::ios_base::iostate err = ios.goodbit;
127 input_iterator<const char*> iter =
128 f.get(input_iterator<const char*>(str),
129 input_iterator<const char*>(str+sizeof(str)),
130 ios, err, v);
131 assert(iter.base() == str+sizeof(str)-1);
132 assert(err == ios.goodbit);
133 assert(v == -INFINITY);
134 }
135 {
136 const char str[] = "-INF";
137 hex(ios);
138 std::ios_base::iostate err = ios.goodbit;
139 input_iterator<const char*> iter =
140 f.get(input_iterator<const char*>(str),
141 input_iterator<const char*>(str+sizeof(str)),
142 ios, err, v);
143 assert(iter.base() == str+sizeof(str)-1);
144 assert(err == ios.goodbit);
145 assert(v == -INFINITY);
146 }
147 {
148 const char str[] = "nan";
149 hex(ios);
150 std::ios_base::iostate err = ios.goodbit;
151 input_iterator<const char*> iter =
152 f.get(input_iterator<const char*>(str),
153 input_iterator<const char*>(str+sizeof(str)),
154 ios, err, v);
155 assert(iter.base() == str+sizeof(str)-1);
156 assert(err == ios.goodbit);
157 assert(std::isnan(v));
158 }
159 {
160 const char str[] = "NAN";
161 hex(ios);
162 std::ios_base::iostate err = ios.goodbit;
163 input_iterator<const char*> iter =
164 f.get(input_iterator<const char*>(str),
165 input_iterator<const char*>(str+sizeof(str)),
166 ios, err, v);
167 assert(iter.base() == str+sizeof(str)-1);
168 assert(err == ios.goodbit);
169 assert(std::isnan(v));
170 }
171 }
172