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, long double& 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: 31 explicit my_facet(std::size_t refs = 0) 32 : F(refs) {} 33 }; 34 35 int main() 36 { 37 const my_facet f(1); 38 std::ios ios(0); 39 long double 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<long double>(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 const char str[] = "1.189731495357231765021264e+49321"; 173 std::ios_base::iostate err = ios.goodbit; 174 v = 0; 175 input_iterator<const char*> iter = 176 f.get(input_iterator<const char*>(str), 177 input_iterator<const char*>(str+sizeof(str)), 178 ios, err, v); 179 assert(iter.base() == str+sizeof(str)-1); 180 assert(err == ios.failbit); 181 assert(v == INFINITY); 182 } 183 { 184 const char str[] = "1.189731495357231765021264e+49329"; 185 std::ios_base::iostate err = ios.goodbit; 186 v = 0; 187 input_iterator<const char*> iter = 188 f.get(input_iterator<const char*>(str), 189 input_iterator<const char*>(str+sizeof(str)), 190 ios, err, v); 191 assert(iter.base() == str+sizeof(str)-1); 192 assert(err == ios.failbit); 193 assert(v == INFINITY); 194 } 195 { 196 const char str[] = "11.189731495357231765021264e+4932"; 197 std::ios_base::iostate err = ios.goodbit; 198 v = 0; 199 input_iterator<const char*> iter = 200 f.get(input_iterator<const char*>(str), 201 input_iterator<const char*>(str+sizeof(str)), 202 ios, err, v); 203 assert(iter.base() == str+sizeof(str)-1); 204 assert(err == ios.failbit); 205 assert(v == INFINITY); 206 } 207 { 208 const char str[] = "91.189731495357231765021264e+4932"; 209 std::ios_base::iostate err = ios.goodbit; 210 v = 0; 211 input_iterator<const char*> iter = 212 f.get(input_iterator<const char*>(str), 213 input_iterator<const char*>(str+sizeof(str)), 214 ios, err, v); 215 assert(iter.base() == str+sizeof(str)-1); 216 assert(err == ios.failbit); 217 assert(v == INFINITY); 218 } 219 { 220 const char str[] = "304888344611713860501504000000"; 221 std::ios_base::iostate err = ios.goodbit; 222 v = 0; 223 input_iterator<const char*> iter = 224 f.get(input_iterator<const char*>(str), 225 input_iterator<const char*>(str+sizeof(str)), 226 ios, err, v); 227 assert(iter.base() == str+sizeof(str)-1); 228 assert(err != ios.failbit); 229 assert(v == 304888344611713860501504000000.0L); 230 } 231 } 232