1// { dg-options " -std=gnu++11 " } 2 3// 2014-04-14 Rüdiger Sonderfeld <ruediger@c-plusplus.de> 4 5// Copyright (C) 2014-2015 Free Software Foundation, Inc. 6// 7// This file is part of the GNU ISO C++ Library. This library is free 8// software; you can redistribute it and/or modify it under the 9// terms of the GNU General Public License as published by the 10// Free Software Foundation; either version 3, or (at your option) 11// any later version. 12 13// This library is distributed in the hope that it will be useful, 14// but WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16// GNU General Public License for more details. 17 18// You should have received a copy of the GNU General Public License along 19// with this library; see the file COPYING3. If not see 20// <http://www.gnu.org/licenses/>. 21 22// 22.4.5.1.1 (C++11) time_get members [locale.time.get.members] 23 24#include <locale> 25#include <sstream> 26#include <iterator> 27#include <testsuite_hooks.h> 28 29#ifndef _GLIBCXX_ASSERT 30# include <iostream> 31# define PRINT(x) cout << #x << ": " << x << endl 32# define TESTHEAD(x) cout << x << endl 33#else 34# define PRINT(x) do {} while(false) 35# define TESTHEAD(x) do {} while(false) 36#endif 37 38void test01() 39{ 40 using namespace std; 41 bool test __attribute__((unused)) = true; 42 43 locale loc_c = locale::classic(); 44 45 istringstream iss; 46 iss.imbue(loc_c); 47 const time_get<char>& tget = use_facet<time_get<char>>(iss.getloc()); 48 typedef istreambuf_iterator<char> iter; 49 const iter end; 50 51 tm time; 52 ios_base::iostate err = ios_base::badbit; 53 54 // check regular operations with format string 55 TESTHEAD("regular operations"); 56 iss.str("d 2014-04-14 01:09:35"); 57 string format = "d %Y-%m-%d %H:%M:%S"; 58 auto ret = tget.get(iter(iss), end, iss, err, &time, 59 format.data(), format.data()+format.size()); 60 PRINT(err); 61 VERIFY(err == ios_base::eofbit); 62 VERIFY(ret == end); 63 PRINT(time.tm_year); 64 VERIFY(time.tm_year == 114); 65 PRINT(time.tm_mon); 66 VERIFY(time.tm_mon == 3); 67 PRINT(time.tm_mday); 68 VERIFY(time.tm_mday == 14); 69 PRINT(time.tm_hour); 70 VERIFY(time.tm_hour == 1); 71 PRINT(time.tm_min); 72 VERIFY(time.tm_min == 9); 73 PRINT(time.tm_sec); 74 VERIFY(time.tm_sec == 35); 75 76 TESTHEAD("check eof"); 77 iss.str("2020 "); 78 format = "%Y"; 79 ret = tget.get(iter(iss), end, iss, err, &time, 80 format.data(), format.data()+format.size()); 81 VERIFY(err != ios_base::eofbit); 82 VERIFY(time.tm_year == 120); 83 VERIFY(ret != end); 84 85 TESTHEAD("check broken format"); 86 iss.str("2014-04-14 01:09:35"); 87 format = "%"; 88 ret = tget.get(iter(iss), end, iss, err, &time, 89 format.data(), format.data()+format.size()); 90 VERIFY(err == ios_base::failbit); 91 92 TESTHEAD("check single format letter version"); 93 iss.str("2020"); 94 ret = tget.get(iter(iss), end, iss, err, &time, 'Y'); 95 VERIFY(err == ios_base::eofbit); 96 VERIFY(time.tm_year == 120); 97 VERIFY(ret == end); 98 99 TESTHEAD("check skipping of space"); 100 iss.str("2010 07 01"); 101 format = "%Y %m %d"; 102 ret = tget.get(iter(iss), end, iss, err, &time, 103 format.data(), format.data()+format.size()); 104 VERIFY(err == ios_base::eofbit); 105 VERIFY(time.tm_year == 110); 106 VERIFY(time.tm_mon == 6); 107 VERIFY(time.tm_mday == 1); 108 VERIFY(ret == end); 109 110 TESTHEAD("check mismatch"); 111 iss.str("year: 1970"); 112 format = "jahr: %Y"; 113 ret = tget.get(iter(iss), end, iss, err, &time, 114 format.data(), format.data()+format.size()); 115 VERIFY(err == ios_base::failbit); 116 VERIFY(ret == iter(iss)); 117 118 TESTHEAD("check case insensitive match"); 119 iss.str("yEaR: 1980"); 120 format = "YeAR: %Y"; 121 ret = tget.get(iter(iss), end, iss, err, &time, 122 format.data(), format.data()+format.size()); 123 VERIFY(err == ios_base::eofbit); 124 VERIFY(ret == end); 125 VERIFY(time.tm_year == 80); 126} 127 128int main() 129{ 130 test01(); 131 return 0; 132} 133