put_string_en_US.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===// 2// 3// ��������������������The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10// <locale> 11 12// class money_put<charT, OutputIterator> 13 14// iter_type put(iter_type s, bool intl, ios_base& f, char_type fill, 15// const string_type& units) const; 16 17#include <locale> 18#include <ios> 19#include <streambuf> 20#include <cassert> 21#include "iterators.h" 22 23typedef std::money_put<char, output_iterator<char*> > Fn; 24 25class my_facet 26 : public Fn 27{ 28public: 29 explicit my_facet(std::size_t refs = 0) 30 : Fn(refs) {} 31}; 32 33typedef std::money_put<wchar_t, output_iterator<wchar_t*> > Fw; 34 35class my_facetw 36 : public Fw 37{ 38public: 39 explicit my_facetw(std::size_t refs = 0) 40 : Fw(refs) {} 41}; 42 43int main() 44{ 45 std::ios ios(0); 46 std::string loc_name("en_US"); 47 ios.imbue(std::locale(ios.getloc(), 48 new std::moneypunct_byname<char, false>(loc_name))); 49 ios.imbue(std::locale(ios.getloc(), 50 new std::moneypunct_byname<char, true>(loc_name))); 51 ios.imbue(std::locale(ios.getloc(), 52 new std::moneypunct_byname<wchar_t, false>(loc_name))); 53 ios.imbue(std::locale(ios.getloc(), 54 new std::moneypunct_byname<wchar_t, true>(loc_name))); 55{ 56 const my_facet f(1); 57 // char, national 58 { // zero 59 std::string v = "0"; 60 char str[100]; 61 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 62 false, ios, '*', v); 63 std::string ex(str, iter.base()); 64 assert(ex == "0.00"); 65 } 66 { // negative one 67 std::string v = "-1"; 68 char str[100]; 69 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 70 false, ios, '*', v); 71 std::string ex(str, iter.base()); 72 assert(ex == "-0.01"); 73 } 74 { // positive 75 std::string v = "123456789"; 76 char str[100]; 77 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 78 false, ios, '*', v); 79 std::string ex(str, iter.base()); 80 assert(ex == "1,234,567.89"); 81 } 82 { // negative 83 std::string v = "-123456789"; 84 char str[100]; 85 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 86 false, ios, '*', v); 87 std::string ex(str, iter.base()); 88 assert(ex == "-1,234,567.89"); 89 } 90 { // zero, showbase 91 std::string v = "0"; 92 showbase(ios); 93 char str[100]; 94 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 95 false, ios, '*', v); 96 std::string ex(str, iter.base()); 97 assert(ex == "$0.00"); 98 } 99 { // negative one, showbase 100 std::string v = "-1"; 101 showbase(ios); 102 char str[100]; 103 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 104 false, ios, '*', v); 105 std::string ex(str, iter.base()); 106 assert(ex == "-$0.01"); 107 } 108 { // positive, showbase 109 std::string v = "123456789"; 110 showbase(ios); 111 char str[100]; 112 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 113 false, ios, '*', v); 114 std::string ex(str, iter.base()); 115 assert(ex == "$1,234,567.89"); 116 } 117 { // negative, showbase 118 std::string v = "-123456789"; 119 showbase(ios); 120 char str[100]; 121 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 122 false, ios, '*', v); 123 std::string ex(str, iter.base()); 124 assert(ex == "-$1,234,567.89"); 125 } 126 { // negative, showbase, left 127 std::string v = "-123456789"; 128 showbase(ios); 129 ios.width(20); 130 left(ios); 131 char str[100]; 132 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 133 false, ios, ' ', v); 134 std::string ex(str, iter.base()); 135 assert(ex == "-$1,234,567.89 "); 136 assert(ios.width() == 0); 137 } 138 { // negative, showbase, internal 139 std::string v = "-123456789"; 140 showbase(ios); 141 ios.width(20); 142 internal(ios); 143 char str[100]; 144 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 145 false, ios, ' ', v); 146 std::string ex(str, iter.base()); 147 assert(ex == "-$ 1,234,567.89"); 148 assert(ios.width() == 0); 149 } 150 { // negative, showbase, right 151 std::string v = "-123456789"; 152 showbase(ios); 153 ios.width(20); 154 right(ios); 155 char str[100]; 156 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 157 false, ios, ' ', v); 158 std::string ex(str, iter.base()); 159 assert(ex == " -$1,234,567.89"); 160 assert(ios.width() == 0); 161 } 162 163 // char, international 164 noshowbase(ios); 165 ios.unsetf(std::ios_base::adjustfield); 166 { // zero 167 std::string v = "0"; 168 char str[100]; 169 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 170 true, ios, '*', v); 171 std::string ex(str, iter.base()); 172 assert(ex == "0.00"); 173 } 174 { // negative one 175 std::string v = "-1"; 176 char str[100]; 177 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 178 true, ios, '*', v); 179 std::string ex(str, iter.base()); 180 assert(ex == "-0.01"); 181 } 182 { // positive 183 std::string v = "123456789"; 184 char str[100]; 185 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 186 true, ios, '*', v); 187 std::string ex(str, iter.base()); 188 assert(ex == "1,234,567.89"); 189 } 190 { // negative 191 std::string v = "-123456789"; 192 char str[100]; 193 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 194 true, ios, '*', v); 195 std::string ex(str, iter.base()); 196 assert(ex == "-1,234,567.89"); 197 } 198 { // zero, showbase 199 std::string v = "0"; 200 showbase(ios); 201 char str[100]; 202 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 203 true, ios, '*', v); 204 std::string ex(str, iter.base()); 205 assert(ex == "USD 0.00"); 206 } 207 { // negative one, showbase 208 std::string v = "-1"; 209 showbase(ios); 210 char str[100]; 211 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 212 true, ios, '*', v); 213 std::string ex(str, iter.base()); 214 assert(ex == "-USD 0.01"); 215 } 216 { // positive, showbase 217 std::string v = "123456789"; 218 showbase(ios); 219 char str[100]; 220 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 221 true, ios, '*', v); 222 std::string ex(str, iter.base()); 223 assert(ex == "USD 1,234,567.89"); 224 } 225 { // negative, showbase 226 std::string v = "-123456789"; 227 showbase(ios); 228 char str[100]; 229 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 230 true, ios, '*', v); 231 std::string ex(str, iter.base()); 232 assert(ex == "-USD 1,234,567.89"); 233 } 234 { // negative, showbase, left 235 std::string v = "-123456789"; 236 showbase(ios); 237 ios.width(20); 238 left(ios); 239 char str[100]; 240 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 241 true, ios, ' ', v); 242 std::string ex(str, iter.base()); 243 assert(ex == "-USD 1,234,567.89 "); 244 assert(ios.width() == 0); 245 } 246 { // negative, showbase, internal 247 std::string v = "-123456789"; 248 showbase(ios); 249 ios.width(20); 250 internal(ios); 251 char str[100]; 252 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 253 true, ios, ' ', v); 254 std::string ex(str, iter.base()); 255 assert(ex == "-USD 1,234,567.89"); 256 assert(ios.width() == 0); 257 } 258 { // negative, showbase, right 259 std::string v = "-123456789"; 260 showbase(ios); 261 ios.width(20); 262 right(ios); 263 char str[100]; 264 output_iterator<char*> iter = f.put(output_iterator<char*>(str), 265 true, ios, ' ', v); 266 std::string ex(str, iter.base()); 267 assert(ex == " -USD 1,234,567.89"); 268 assert(ios.width() == 0); 269 } 270} 271{ 272 273 const my_facetw f(1); 274 // wchar_t, national 275 noshowbase(ios); 276 ios.unsetf(std::ios_base::adjustfield); 277 { // zero 278 std::wstring v = L"0"; 279 wchar_t str[100]; 280 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 281 false, ios, '*', v); 282 std::wstring ex(str, iter.base()); 283 assert(ex == L"0.00"); 284 } 285 { // negative one 286 std::wstring v = L"-1"; 287 wchar_t str[100]; 288 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 289 false, ios, '*', v); 290 std::wstring ex(str, iter.base()); 291 assert(ex == L"-0.01"); 292 } 293 { // positive 294 std::wstring v = L"123456789"; 295 wchar_t str[100]; 296 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 297 false, ios, '*', v); 298 std::wstring ex(str, iter.base()); 299 assert(ex == L"1,234,567.89"); 300 } 301 { // negative 302 std::wstring v = L"-123456789"; 303 wchar_t str[100]; 304 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 305 false, ios, '*', v); 306 std::wstring ex(str, iter.base()); 307 assert(ex == L"-1,234,567.89"); 308 } 309 { // zero, showbase 310 std::wstring v = L"0"; 311 showbase(ios); 312 wchar_t str[100]; 313 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 314 false, ios, '*', v); 315 std::wstring ex(str, iter.base()); 316 assert(ex == L"$0.00"); 317 } 318 { // negative one, showbase 319 std::wstring v = L"-1"; 320 showbase(ios); 321 wchar_t str[100]; 322 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 323 false, ios, '*', v); 324 std::wstring ex(str, iter.base()); 325 assert(ex == L"-$0.01"); 326 } 327 { // positive, showbase 328 std::wstring v = L"123456789"; 329 showbase(ios); 330 wchar_t str[100]; 331 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 332 false, ios, '*', v); 333 std::wstring ex(str, iter.base()); 334 assert(ex == L"$1,234,567.89"); 335 } 336 { // negative, showbase 337 std::wstring v = L"-123456789"; 338 showbase(ios); 339 wchar_t str[100]; 340 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 341 false, ios, '*', v); 342 std::wstring ex(str, iter.base()); 343 assert(ex == L"-$1,234,567.89"); 344 } 345 { // negative, showbase, left 346 std::wstring v = L"-123456789"; 347 showbase(ios); 348 ios.width(20); 349 left(ios); 350 wchar_t str[100]; 351 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 352 false, ios, ' ', v); 353 std::wstring ex(str, iter.base()); 354 assert(ex == L"-$1,234,567.89 "); 355 assert(ios.width() == 0); 356 } 357 { // negative, showbase, internal 358 std::wstring v = L"-123456789"; 359 showbase(ios); 360 ios.width(20); 361 internal(ios); 362 wchar_t str[100]; 363 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 364 false, ios, ' ', v); 365 std::wstring ex(str, iter.base()); 366 assert(ex == L"-$ 1,234,567.89"); 367 assert(ios.width() == 0); 368 } 369 { // negative, showbase, right 370 std::wstring v = L"-123456789"; 371 showbase(ios); 372 ios.width(20); 373 right(ios); 374 wchar_t str[100]; 375 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 376 false, ios, ' ', v); 377 std::wstring ex(str, iter.base()); 378 assert(ex == L" -$1,234,567.89"); 379 assert(ios.width() == 0); 380 } 381 382 // wchar_t, international 383 noshowbase(ios); 384 ios.unsetf(std::ios_base::adjustfield); 385 { // zero 386 std::wstring v = L"0"; 387 wchar_t str[100]; 388 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 389 true, ios, '*', v); 390 std::wstring ex(str, iter.base()); 391 assert(ex == L"0.00"); 392 } 393 { // negative one 394 std::wstring v = L"-1"; 395 wchar_t str[100]; 396 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 397 true, ios, '*', v); 398 std::wstring ex(str, iter.base()); 399 assert(ex == L"-0.01"); 400 } 401 { // positive 402 std::wstring v = L"123456789"; 403 wchar_t str[100]; 404 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 405 true, ios, '*', v); 406 std::wstring ex(str, iter.base()); 407 assert(ex == L"1,234,567.89"); 408 } 409 { // negative 410 std::wstring v = L"-123456789"; 411 wchar_t str[100]; 412 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 413 true, ios, '*', v); 414 std::wstring ex(str, iter.base()); 415 assert(ex == L"-1,234,567.89"); 416 } 417 { // zero, showbase 418 std::wstring v = L"0"; 419 showbase(ios); 420 wchar_t str[100]; 421 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 422 true, ios, '*', v); 423 std::wstring ex(str, iter.base()); 424 assert(ex == L"USD 0.00"); 425 } 426 { // negative one, showbase 427 std::wstring v = L"-1"; 428 showbase(ios); 429 wchar_t str[100]; 430 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 431 true, ios, '*', v); 432 std::wstring ex(str, iter.base()); 433 assert(ex == L"-USD 0.01"); 434 } 435 { // positive, showbase 436 std::wstring v = L"123456789"; 437 showbase(ios); 438 wchar_t str[100]; 439 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 440 true, ios, '*', v); 441 std::wstring ex(str, iter.base()); 442 assert(ex == L"USD 1,234,567.89"); 443 } 444 { // negative, showbase 445 std::wstring v = L"-123456789"; 446 showbase(ios); 447 wchar_t str[100]; 448 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 449 true, ios, '*', v); 450 std::wstring ex(str, iter.base()); 451 assert(ex == L"-USD 1,234,567.89"); 452 } 453 { // negative, showbase, left 454 std::wstring v = L"-123456789"; 455 showbase(ios); 456 ios.width(20); 457 left(ios); 458 wchar_t str[100]; 459 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 460 true, ios, ' ', v); 461 std::wstring ex(str, iter.base()); 462 assert(ex == L"-USD 1,234,567.89 "); 463 assert(ios.width() == 0); 464 } 465 { // negative, showbase, internal 466 std::wstring v = L"-123456789"; 467 showbase(ios); 468 ios.width(20); 469 internal(ios); 470 wchar_t str[100]; 471 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 472 true, ios, ' ', v); 473 std::wstring ex(str, iter.base()); 474 assert(ex == L"-USD 1,234,567.89"); 475 assert(ios.width() == 0); 476 } 477 { // negative, showbase, right 478 std::wstring v = L"-123456789"; 479 showbase(ios); 480 ios.width(20); 481 right(ios); 482 wchar_t str[100]; 483 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str), 484 true, ios, ' ', v); 485 std::wstring ex(str, iter.base()); 486 assert(ex == L" -USD 1,234,567.89"); 487 assert(ios.width() == 0); 488 } 489} 490} 491