blob: c61dcdaf846b073d5e08662b5fcf0281ed26b378 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
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 "iterators.h"
Howard Hinnant0a111112011-05-13 21:52:40 +000023#include "../../../../../hexfloat.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024
25typedef std::num_get<char, input_iterator<const char*> > F;
26
27class my_facet
28 : public F
29{
30public:
31 explicit my_facet(std::size_t refs = 0)
32 : F(refs) {}
33};
34
35int 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +000045 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000046 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +000056 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +000067 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000068 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +000079 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +000091 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000092 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);
Howard Hinnant0a111112011-05-13 21:52:40 +000097 assert(v == hexfloat<float>(0x125, 0, -1));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098 }
99 {
100 const char str[] = "inf";
101 hex(ios);
102 std::ios_base::iostate err = ios.goodbit;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000103 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000104 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000115 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000127 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000139 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000151 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 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;
Howard Hinnant22a74dc2010-08-22 00:39:25 +0000163 input_iterator<const char*> iter =
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 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}