File: testfacet.cpp

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (342 lines) | stat: -rw-r--r-- 12,403 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
 * Use, modification and distribution is subject to the 
 * Boost Software License, Version 1.0. (See accompanying
 * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
 * Author: Jeff Garland, Bart Garst 
 */

#include <sstream>
#include <iostream>
#include <fstream>

#include "boost/date_time/gregorian/greg_month.hpp"
#include "boost/date_time/gregorian/greg_facet.hpp"
#include "boost/date_time/date_format_simple.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/testfrmwk.hpp"

#ifndef BOOST_DATE_TIME_NO_LOCALE

    const char* const de_short_month_names[]={"Jan","Feb","Mar","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "NAM"};

    const char* const de_long_month_names[]={"Januar","Februar","Marz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember","NichtDerMonat"};
    const char* const de_special_value_names[]={"NichtDatumzeit","-unbegrenztheit", "+unbegrenztheit"};

const char* const de_short_weekday_names[]={"Son", "Mon", "Die","Mit", "Don", "Fre", "Sam"};

    const char* const de_long_weekday_names[]={"Sonntag", "Montag", "Dienstag","Mittwoch", "Donnerstag", "Freitag", "Samstag"};

#endif 

/** Not used for now
    const char* const es_short_month_names[]={"Ene","Feb","Mar","Abr","Pue","Jun","Jul","Ago","Sep","Oct","Nov","Dic", "NAM"};

    const char* const es_long_month_names[]={"Enero","Febrero","Marcha","Abril","Pueda","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre","NoAMes"};
    const char* const es_special_value_names[]={"NoUnRatoDeLaFacha","-infinito", "+infinito"};
**/
int
main()
{
//   std::locale native("");
//   std::cout << "native: " << native.name() << std::endl;
//#ifndef BOOST_NO_STD_LOCALE
#ifndef BOOST_DATE_TIME_NO_LOCALE

  using namespace boost::gregorian;

  typedef greg_facet_config facet_config;
  typedef boost::date_time::all_date_names_put<facet_config> date_facet;
  typedef boost::date_time::date_names_put<facet_config> date_facet_base;
  typedef boost::date_time::ostream_month_formatter<date_facet_base> month_formatter;

  {
    // special_values tests
    std::stringstream ss;
    date_facet_base* f = new date_facet_base();
    std::locale loc(std::locale::classic(), f);
    ss.imbue(loc);
    date d(not_a_date_time);
    ss << d;
    check("Special value, stream out nadt" , ss.str() == std::string("not-a-date-time"));
    ss.str("");
    d = date(neg_infin);
    ss << d;
    check("Special value, stream out neg_infin" , ss.str() == std::string("-infinity"));
    ss.str("");
    d = date(pos_infin);
    ss << d;
    check("Special value, stream out pos_infin" , ss.str() == std::string("+infinity"));
  }

  date_facet gdnp(de_short_month_names, de_long_month_names, 
                  de_special_value_names, de_long_weekday_names,
                  de_long_weekday_names, 
                  '.', 
                  boost::date_time::ymd_order_dmy);

  std::stringstream ss;
  std::ostreambuf_iterator<char> coi(ss);
  gdnp.put_month_short(coi, Oct);
  check("check german short month: " + ss.str(), 
        ss.str() == std::string("Okt"));

  ss.str(""); //reset string stream 
  greg_month m(Oct);
  month_formatter::format_month(m, ss, gdnp);
  check("check german short month: " + ss.str(), 
        ss.str() == std::string("Okt"));
  ss.str(""); //reset string stream 
//   month_formatter::format_month(m, ss, gdnp);
//   check("check german long month: " + ss.str(), 
//         ss.str() == std::string("Oktober"));


  greg_year_month_day ymd(2002,Oct,1);
  typedef boost::date_time::ostream_ymd_formatter<greg_year_month_day, date_facet_base> ymd_formatter;
  ss.str(""); //reset string stream 
  ymd_formatter::ymd_put(ymd, ss, gdnp);
  check("check ymd: " + ss.str(), 
        ss.str() == std::string("01.Okt.2002"));


  typedef boost::date_time::ostream_date_formatter<date, date_facet_base> datef;

  std::stringstream os;
  date d1(2002, Oct, 1);
  datef::date_put(d1, os, gdnp);
  check("ostream low level check string:"+os.str(), 
        os.str() == std::string("01.Okt.2002"));

//   //Locale tests
  std::locale global;
  std::cout << "global: " << global.name() << std::endl;

  // put a facet into a locale
  //check for a facet p319
  check("no registered facet here",
        !std::has_facet<date_facet>(global));

  std::locale global2(global, 
                      new date_facet(de_short_month_names, 
                                     de_long_month_names,
                                     de_special_value_names,
                                     de_long_weekday_names,
                                     de_long_weekday_names));

  check("facet registered here",
        std::has_facet<boost::date_time::date_names_put<facet_config> >(global2));

  std::stringstream os2;
  os2.imbue(global2); 
  datef::date_put(d1, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("2002-Okt-01"));

  date infin(pos_infin);
  os2.str(""); //clear stream
  datef::date_put(infin, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("+unbegrenztheit"));

  os2.str(""); //clear stream
  os2 << infin;
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("+unbegrenztheit"));


  date nadt(not_a_date_time);
  os2.str(""); //clear stream
  datef::date_put(nadt, os2);
  check("check string imbued ostream: "+os2.str(), 
        os2.str() == std::string("NichtDatumzeit"));
  

  std::stringstream os3;
  os3 << d1;
  check("check any old ostream: "+os3.str(), 
        os3.str() == std::string("2002-Oct-01"));

  std::ofstream f("test_facet_file.out");
  f << d1 << std::endl;
  
//   // date formatter that takes locale and gets facet from locale
  std::locale german_dates1(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           '.',
                                           boost::date_time::ymd_order_dmy,
                                           boost::date_time::month_as_integer));

  os3.imbue(german_dates1); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("01.10.2002"));

  std::locale german_dates2(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           ' ',
                                           boost::date_time::ymd_order_iso,
                                           boost::date_time::month_as_short_string));

  os3.imbue(german_dates2); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("2002 Okt 01"));

  std::locale german_dates3(global, 
                            new date_facet(de_short_month_names, 
                                           de_long_month_names,
                                           de_special_value_names,
                                           de_short_weekday_names,
                                           de_long_weekday_names,
                                           ' ',
                                           boost::date_time::ymd_order_us,
                                           boost::date_time::month_as_long_string));

  os3.imbue(german_dates3); 
  os3.str("");
  os3 << d1;
  check("check date order: "+os3.str(), 
        os3.str() == std::string("Oktober 01 2002"));

  date_period dp(d1, date_duration(3));
  os3.str("");
  os3 << dp;
  check("check date period: "+os3.str(), 
        os3.str() == std::string("[Oktober 01 2002/Oktober 03 2002]"));


  /*******************************************************************/
  /* Streaming operations for date durations                         */
  /*******************************************************************/

  date_duration dur(26);
  std::stringstream ss2;
  ss2 << dur;
  check("date_duration stream out", ss2.str() == std::string("26"));

  dur = date_duration(boost::date_time::pos_infin);
  ss2.str("");
  ss2 << dur;
  check("date_duration stream out", ss2.str() == std::string("+infinity"));

  /*******************************************************************/
  /* Streaming operations for date generator functions               */
  /*******************************************************************/

  partial_date pd(26, Jun);
  //std::stringstream ss2;
  ss2.str("");
  ss2 << pd;
  check("partial date stream out", ss2.str() == std::string("26 Jun"));

  ss2.str("");
  nth_kday_of_month nkm(nth_kday_of_month::second, Friday, Sep);
  ss2 << nkm;
  check("nth kday of month", ss2.str() == std::string("second Fri of Sep"));

  ss2.str("");
  first_kday_of_month fkm(Saturday, May);
  ss2 << fkm;
  check("first kday of month", ss2.str() == std::string("first Sat of May"));

  ss2.str("");
  last_kday_of_month lkm(Monday, Aug);
  ss2 << lkm;
  check("last kday of month", ss2.str() == std::string("last Mon of Aug"));

  ss2.str("");
  first_kday_after  fka(Thursday);//fkb.get_date(d)
  ss2 << fka;
  check("first kday after", ss2.str() == std::string("Thu after"));

  ss2.str("");
  first_kday_before fkb(Tuesday); // same ^
  ss2 << fkb;
  check("first kday after", ss2.str() == std::string("Tue before"));

  std::cout << pd << '\n'
            << nkm << '\n'
            << fkm << '\n'
            << lkm << '\n'
            << fka << '\n'
            << fkb << '\n'
            << std::endl;
  
  /*******************************************************************/
  /* Input Streaming for greg_month                                  */
  /*******************************************************************/
  {
    std::stringstream ss1("January");
    std::stringstream ss2("dec"); // misspelled
    std::stringstream german("Okt");
    german.imbue(global2);
    greg_month m(3);
    ss1 >> m;
    check("Stream in month", m == greg_month(Jan));
#ifndef BOOST_NO_STD_WSTRING
    std::wstringstream ws1(L"Dec");
    ws1 >> m;
    check("Wide Stream in month", m == greg_month(Dec));
#else
    check("Wide Stream in not supported by this compiler", false);
#endif // BOOST_NO_STD_WSTRING
    german >> m;
    check("Stream in German month", m == greg_month(Oct));
    try{
      ss2 >> m; // misspelled
      check("Bad month exception NOT thrown (misspelled name)", false);
    }catch(bad_month){
      check("Bad month exception caught (misspelled name)", true);
    }catch(...){
      check("Bad month exception NOT caught (misspelled name)", false);
    }
  }
  /*******************************************************************/
  /* Input Streaming for greg_weekday                                */
  /*******************************************************************/
  {
    std::stringstream ss1("Sun");
    std::stringstream ss2("Wensday"); // misspelled
    std::stringstream german("Mittwoch"); // Wednesday
    german.imbue(global2);
    greg_weekday wd(Friday); //something not Sunday...
    ss1 >> wd;
    check("Stream in weekday", wd == greg_weekday(Sunday));
#ifndef BOOST_NO_STD_WSTRING
    std::wstringstream ws1(L"Saturday");
    ws1 >> wd;
    check("Wide Stream in weekday", wd == greg_weekday(Saturday));
#else
    check("Wide Stream in not supported by this compiler", false);
#endif // BOOST_NO_STD_WSTRING
    german >> wd;
    check("Stream in German weekday", wd == greg_weekday(Wednesday));
    try{
      ss2 >> wd;
      check("Bad weekday exception NOT thrown (misspelled name)", false);
    }catch(bad_weekday){
      check("Bad weekday exception caught (misspelled name)", true);
    }catch(...){
      check("Bad weekday exception NOT caught (misspelled name)", false);
    }
  }

#else
  check("No tests executed - Locales not supported by this compiler", false);

#endif //BOOST_DATE_TIME_NO_LOCALE

  return printTestStats();

}