size_char_alloc.pass.cpp revision bc8d3f97eb5c958007f2713238472e0c1c8fe02c
1//===----------------------------------------------------------------------===//
2//
3// ��������������������The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <string>
11
12// basic_string(size_type n, charT c, const Allocator& a = Allocator());
13
14#include <string>
15#include <stdexcept>
16#include <algorithm>
17#include <cassert>
18
19#include "../test_allocator.h"
20
21template <class charT>
22void
23test(unsigned n, charT c)
24{
25    typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
26    typedef typename S::traits_type T;
27    typedef typename S::allocator_type A;
28    S s2(n, c);
29    assert(s2.__invariants());
30    assert(s2.size() == n);
31    for (unsigned i = 0; i < n; ++i)
32        assert(s2[i] == c);
33    assert(s2.get_allocator() == A());
34    assert(s2.capacity() >= s2.size());
35}
36
37template <class charT>
38void
39test(unsigned n, charT c, const test_allocator<charT>& a)
40{
41    typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
42    typedef typename S::traits_type T;
43    typedef typename S::allocator_type A;
44    S s2(n, c, a);
45    assert(s2.__invariants());
46    assert(s2.size() == n);
47    for (unsigned i = 0; i < n; ++i)
48        assert(s2[i] == c);
49    assert(s2.get_allocator() == a);
50    assert(s2.capacity() >= s2.size());
51}
52
53template <class Tp>
54void
55test(Tp n, Tp c)
56{
57    typedef char charT;
58    typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
59    typedef typename S::traits_type T;
60    typedef typename S::allocator_type A;
61    S s2(n, c);
62    assert(s2.__invariants());
63    assert(s2.size() == n);
64    for (unsigned i = 0; i < n; ++i)
65        assert(s2[i] == c);
66    assert(s2.get_allocator() == A());
67    assert(s2.capacity() >= s2.size());
68}
69
70template <class Tp>
71void
72test(Tp n, Tp c, const test_allocator<char>& a)
73{
74    typedef char charT;
75    typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
76    typedef typename S::traits_type T;
77    typedef typename S::allocator_type A;
78    S s2(n, c, a);
79    assert(s2.__invariants());
80    assert(s2.size() == n);
81    for (unsigned i = 0; i < n; ++i)
82        assert(s2[i] == c);
83    assert(s2.get_allocator() == a);
84    assert(s2.capacity() >= s2.size());
85}
86
87int main()
88{
89    typedef test_allocator<char> A;
90    typedef std::basic_string<char, std::char_traits<char>, A> S;
91
92    test(0, 'a');
93    test(0, 'a', A(2));
94
95    test(1, 'a');
96    test(1, 'a', A(2));
97
98    test(10, 'a');
99    test(10, 'a', A(2));
100
101    test(100, 'a');
102    test(100, 'a', A(2));
103
104    test(100, 65);
105    test(100, 65, A(3));
106}
107