Home | History | Annotate | Download | only in string.access
      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 // <string>
     11 
     12 // const_reference at(size_type pos) const;
     13 //       reference at(size_type pos);
     14 
     15 #include <string>
     16 #include <stdexcept>
     17 #include <cassert>
     18 
     19 #include "min_allocator.h"
     20 
     21 #include "test_macros.h"
     22 
     23 template <class S>
     24 void
     25 test(S s, typename S::size_type pos)
     26 {
     27     const S& cs = s;
     28     if (pos < s.size())
     29     {
     30         assert(s.at(pos) == s[pos]);
     31         assert(cs.at(pos) == cs[pos]);
     32     }
     33 #ifndef TEST_HAS_NO_EXCEPTIONS
     34     else
     35     {
     36         try
     37         {
     38             s.at(pos);
     39             assert(false);
     40         }
     41         catch (std::out_of_range&)
     42         {
     43             assert(pos >= s.size());
     44         }
     45         try
     46         {
     47             cs.at(pos);
     48             assert(false);
     49         }
     50         catch (std::out_of_range&)
     51         {
     52             assert(pos >= s.size());
     53         }
     54     }
     55 #endif
     56 }
     57 
     58 int main()
     59 {
     60     {
     61     typedef std::string S;
     62     test(S(), 0);
     63     test(S("123"), 0);
     64     test(S("123"), 1);
     65     test(S("123"), 2);
     66     test(S("123"), 3);
     67     }
     68 #if TEST_STD_VER >= 11
     69     {
     70     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
     71     test(S(), 0);
     72     test(S("123"), 0);
     73     test(S("123"), 1);
     74     test(S("123"), 2);
     75     test(S("123"), 3);
     76     }
     77 #endif
     78 }
     79