• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // <deque>
11 
12 // template <class T, class A>
13 //   void swap(deque<T, A>& x, deque<T, A>& y);
14 
15 #include <deque>
16 #include <cassert>
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 
20 template <class C>
21 C
make(int size,int start=0)22 make(int size, int start = 0 )
23 {
24     const int b = 4096 / sizeof(int);
25     int init = 0;
26     if (start > 0)
27     {
28         init = (start+1) / b + ((start+1) % b != 0);
29         init *= b;
30         --init;
31     }
32     C c(init, 0);
33     for (int i = 0; i < init-start; ++i)
34         c.pop_back();
35     for (int i = 0; i < size; ++i)
36         c.push_back(i);
37     for (int i = 0; i < start; ++i)
38         c.pop_front();
39     return c;
40 };
41 
42 template <class C>
testN(int start,int N,int M)43 void testN(int start, int N, int M)
44 {
45     C c1 = make<C>(N, start);
46     C c2 = make<C>(M);
47     C c1_save = c1;
48     C c2_save = c2;
49     swap(c1, c2);
50     assert(c1 == c2_save);
51     assert(c2 == c1_save);
52 }
53 
main()54 int main()
55 {
56     {
57         int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
58         const int N = sizeof(rng)/sizeof(rng[0]);
59         for (int i = 0; i < N; ++i)
60             for (int j = 0; j < N; ++j)
61                 for (int k = 0; k < N; ++k)
62                     testN<std::deque<int> >(rng[i], rng[j], rng[k]);
63     }
64     {
65         int a1[] = {1, 3, 7, 9, 10};
66         int a2[] = {0, 2, 4, 5, 6, 8, 11};
67         typedef test_allocator<int> A;
68         std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
69         std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
70         swap(c1, c2);
71         assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
72         assert(c1.get_allocator() == A(1));
73         assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
74         assert(c2.get_allocator() == A(2));
75     }
76     {
77         int a1[] = {1, 3, 7, 9, 10};
78         int a2[] = {0, 2, 4, 5, 6, 8, 11};
79         typedef other_allocator<int> A;
80         std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A(1));
81         std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A(2));
82         swap(c1, c2);
83         assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
84         assert(c1.get_allocator() == A(2));
85         assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
86         assert(c2.get_allocator() == A(1));
87     }
88 #if __cplusplus >= 201103L
89     {
90         int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
91         const int N = sizeof(rng)/sizeof(rng[0]);
92         for (int i = 0; i < N; ++i)
93             for (int j = 0; j < N; ++j)
94                 for (int k = 0; k < N; ++k)
95                     testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]);
96     }
97     {
98         int a1[] = {1, 3, 7, 9, 10};
99         int a2[] = {0, 2, 4, 5, 6, 8, 11};
100         typedef min_allocator<int> A;
101         std::deque<int, A> c1(a1, a1+sizeof(a1)/sizeof(a1[0]), A());
102         std::deque<int, A> c2(a2, a2+sizeof(a2)/sizeof(a2[0]), A());
103         swap(c1, c2);
104         assert((c1 == std::deque<int, A>(a2, a2+sizeof(a2)/sizeof(a2[0]))));
105         assert(c1.get_allocator() == A());
106         assert((c2 == std::deque<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
107         assert(c2.get_allocator() == A());
108     }
109 #endif
110 }
111