Home | History | Annotate | Download | only in facet.num.get.members
      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, bool& v) const;
     16 
     17 #include <locale>
     18 #include <ios>
     19 #include <cassert>
     20 #include <streambuf>
     21 #include "test_iterators.h"
     22 
     23 typedef std::num_get<char, input_iterator<const char*> > F;
     24 
     25 class my_facet
     26     : public F
     27 {
     28 public:
     29     explicit my_facet(std::size_t refs = 0)
     30         : F(refs) {}
     31 };
     32 
     33 class p1
     34     : public std::numpunct<char>
     35 {
     36 public:
     37     p1() : std::numpunct<char>() {}
     38 
     39 protected:
     40     virtual string_type do_truename() const {return "a";}
     41     virtual string_type do_falsename() const {return "abb";}
     42 };
     43 
     44 class p2
     45     : public std::numpunct<char>
     46 {
     47 public:
     48     p2() : std::numpunct<char>() {}
     49 
     50 protected:
     51     virtual string_type do_truename() const {return "a";}
     52     virtual string_type do_falsename() const {return "ab";}
     53 };
     54 
     55 int main()
     56 {
     57     const my_facet f(1);
     58     std::ios ios(0);
     59     {
     60         const char str[] = "1";
     61         std::ios_base::iostate err = ios.goodbit;
     62         bool b;
     63         input_iterator<const char*> iter =
     64             f.get(input_iterator<const char*>(str),
     65                   input_iterator<const char*>(str+sizeof(str)),
     66                   ios, err, b);
     67         assert(iter.base() == str+sizeof(str)-1);
     68         assert(err == ios.goodbit);
     69         assert(b == true);
     70     }
     71     {
     72         const char str[] = "0";
     73         std::ios_base::iostate err = ios.goodbit;
     74         bool b;
     75         input_iterator<const char*> iter =
     76             f.get(input_iterator<const char*>(str),
     77                   input_iterator<const char*>(str+sizeof(str)),
     78                   ios, err, b);
     79         assert(iter.base() == str+sizeof(str)-1);
     80         assert(err == ios.goodbit);
     81         assert(b == false);
     82     }
     83     {
     84         const char str[] = "12";
     85         std::ios_base::iostate err = ios.goodbit;
     86         bool b;
     87         input_iterator<const char*> iter =
     88             f.get(input_iterator<const char*>(str),
     89                   input_iterator<const char*>(str+sizeof(str)),
     90                   ios, err, b);
     91         assert(iter.base() == str+sizeof(str)-1);
     92         assert(err == ios.failbit);
     93         assert(b == true);
     94     }
     95     {
     96         const char str[] = "*12";
     97         std::ios_base::iostate err = ios.goodbit;
     98         bool b;
     99         input_iterator<const char*> iter =
    100             f.get(input_iterator<const char*>(str),
    101                   input_iterator<const char*>(str+sizeof(str)),
    102                   ios, err, b);
    103         assert(iter.base() == str+0);
    104         assert(err == ios.failbit);
    105         assert(b == false);
    106     }
    107     boolalpha(ios);
    108     {
    109         const char str[] = "1";
    110         std::ios_base::iostate err = ios.goodbit;
    111         bool b;
    112         input_iterator<const char*> iter =
    113             f.get(input_iterator<const char*>(str),
    114                   input_iterator<const char*>(str+sizeof(str)),
    115                   ios, err, b);
    116         assert(iter.base() == str+0);
    117         assert(err == ios.failbit);
    118         assert(b == false);
    119     }
    120     {
    121         const char str[] = "true";
    122         std::ios_base::iostate err = ios.goodbit;
    123         bool b;
    124         input_iterator<const char*> iter =
    125             f.get(input_iterator<const char*>(str),
    126                   input_iterator<const char*>(str+sizeof(str)),
    127                   ios, err, b);
    128         assert(iter.base() == str+sizeof(str)-1);
    129         assert(err == ios.goodbit);
    130         assert(b == true);
    131     }
    132     {
    133         const char str[] = "false";
    134         std::ios_base::iostate err = ios.goodbit;
    135         bool b;
    136         input_iterator<const char*> iter =
    137             f.get(input_iterator<const char*>(str),
    138                   input_iterator<const char*>(str+sizeof(str)),
    139                   ios, err, b);
    140         assert(iter.base() == str+sizeof(str)-1);
    141         assert(err == ios.goodbit);
    142         assert(b == false);
    143     }
    144     ios.imbue(std::locale(ios.getloc(), new p1));
    145     {
    146         const char str[] = "a";
    147         std::ios_base::iostate err = ios.goodbit;
    148         bool b;
    149         input_iterator<const char*> iter =
    150             f.get(input_iterator<const char*>(str),
    151                   input_iterator<const char*>(str+1),
    152                   ios, err, b);
    153         assert(iter.base() == str+1);
    154         assert(err == ios.eofbit);
    155         assert(b == true);
    156     }
    157     {
    158         const char str[] = "abc";
    159         std::ios_base::iostate err = ios.goodbit;
    160         bool b;
    161         input_iterator<const char*> iter =
    162             f.get(input_iterator<const char*>(str),
    163                   input_iterator<const char*>(str+3),
    164                   ios, err, b);
    165         assert(iter.base() == str+2);
    166         assert(err == ios.failbit);
    167         assert(b == false);
    168     }
    169     {
    170         const char str[] = "acc";
    171         std::ios_base::iostate err = ios.goodbit;
    172         bool b;
    173         input_iterator<const char*> iter =
    174             f.get(input_iterator<const char*>(str),
    175                   input_iterator<const char*>(str+3),
    176                   ios, err, b);
    177         assert(iter.base() == str+1);
    178         assert(err == ios.goodbit);
    179         assert(b == true);
    180     }
    181     ios.imbue(std::locale(ios.getloc(), new p2));
    182     {
    183         const char str[] = "a";
    184         std::ios_base::iostate err = ios.goodbit;
    185         bool b;
    186         input_iterator<const char*> iter =
    187             f.get(input_iterator<const char*>(str),
    188                   input_iterator<const char*>(str+1),
    189                   ios, err, b);
    190         assert(iter.base() == str+1);
    191         assert(err == ios.eofbit);
    192         assert(b == true);
    193     }
    194     {
    195         const char str[] = "ab";
    196         std::ios_base::iostate err = ios.goodbit;
    197         bool b;
    198         input_iterator<const char*> iter =
    199             f.get(input_iterator<const char*>(str),
    200                   input_iterator<const char*>(str+2),
    201                   ios, err, b);
    202         assert(iter.base() == str+2);
    203         assert(err == ios.eofbit);
    204         assert(b == false);
    205     }
    206     {
    207         const char str[] = "abc";
    208         std::ios_base::iostate err = ios.goodbit;
    209         bool b;
    210         input_iterator<const char*> iter =
    211             f.get(input_iterator<const char*>(str),
    212                   input_iterator<const char*>(str+3),
    213                   ios, err, b);
    214         assert(iter.base() == str+2);
    215         assert(err == ios.goodbit);
    216         assert(b == false);
    217     }
    218     {
    219         const char str[] = "ac";
    220         std::ios_base::iostate err = ios.goodbit;
    221         bool b;
    222         input_iterator<const char*> iter =
    223             f.get(input_iterator<const char*>(str),
    224                   input_iterator<const char*>(str+2),
    225                   ios, err, b);
    226         assert(iter.base() == str+1);
    227         assert(err == ios.goodbit);
    228         assert(b == true);
    229     }
    230 }
    231