Home | History | Annotate | Download | only in string.capacity
      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 // void shrink_to_fit();
     13 
     14 #include <string>
     15 #include <cassert>
     16 
     17 template <class S>
     18 void
     19 test(S s)
     20 {
     21     typename S::size_type old_cap = s.capacity();
     22     S s0 = s;
     23     s.shrink_to_fit();
     24     assert(s.__invariants());
     25     assert(s == s0);
     26     assert(s.capacity() <= old_cap);
     27     assert(s.capacity() >= s.size());
     28 }
     29 
     30 int main()
     31 {
     32     typedef std::string S;
     33     S s;
     34     test(s);
     35 
     36     s.assign(10, 'a');
     37     s.erase(5);
     38     test(s);
     39 
     40     s.assign(100, 'a');
     41     s.erase(50);
     42     test(s);
     43 }
     44