1// { dg-options "-std=gnu++11" }
2
3// Copyright (C) 2005-2015 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library.  This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING3.  If not see
18// <http://www.gnu.org/licenses/>.
19
20
21#include <deque>
22#include <testsuite_hooks.h>
23#include <testsuite_rvalref.h>
24
25using namespace __gnu_test;
26
27// Test deque::push_back makes no unneeded copies.
28void
29test01()
30{
31  bool test __attribute__((unused)) = true;
32
33  std::deque<copycounter> a;
34  copycounter c(1);
35  copycounter::copycount = 0;
36  for(int i = 0; i < 1000; ++i)
37    a.push_back(c);
38  VERIFY(copycounter::copycount == 1000);
39}
40
41// Test deque::push_front makes no unneeded copies.
42void
43test02()
44{
45  bool test __attribute__((unused)) = true;
46
47  std::deque<copycounter> a;
48  copycounter c(1);
49  copycounter::copycount = 0;
50  for(int i = 0; i < 1000; ++i)
51    a.push_front(c);
52  VERIFY(copycounter::copycount == 1000);
53}
54
55// Test deque::insert makes no unneeded copies.
56void
57test03()
58{
59  bool test __attribute__((unused)) = true;
60
61  std::deque<copycounter> a(1000);
62  copycounter c(1);
63  copycounter::copycount = 0;
64  a.insert(a.begin(),c);
65  a.insert(a.end(),c);
66  for(int i = 0; i < 500; ++i)
67    a.insert(a.begin() + i, c);
68  VERIFY(copycounter::copycount == 502);
69}
70
71// Test deque::insert(iterator, count, value) makes no unneeded copies
72// when it has to also reallocate the deque's internal buffer.
73void
74test04()
75{
76  bool test __attribute__((unused)) = true;
77
78  copycounter c(1);
79  std::deque<copycounter> a(10, c);
80  copycounter::copycount = 0;
81  a.insert(a.begin(), 20, c);
82  VERIFY(copycounter::copycount == 20);
83  a.insert(a.end(), 50, c);
84  VERIFY(copycounter::copycount == 70);
85  // NOTE : These values are each one higher than might be expected, as
86  // deque::insert(iterator, count, value) copies the value it is given
87  // when it has to move elements in the deque in case that value is
88  // in the deque.
89
90  // Near the start
91  a.insert(a.begin() + 10, 100, c);
92  VERIFY(copycounter::copycount == 170 + 1);
93  // Near the end
94  a.insert(a.end() - 10, 1000, c);
95  VERIFY(copycounter::copycount == 1170 + 2);
96}
97
98// Test deque::insert(iterator, count, value) makes no unneeded copies
99// when it doesn't have to reallocate the deque's internal buffer.
100void
101test05()
102{
103  bool test __attribute__((unused)) = true;
104
105  copycounter c(1);
106  std::deque<copycounter> a(10, c);
107  copycounter::copycount = 0;
108  //a.reserve(1000);
109  a.insert(a.begin(), 20, c);
110  VERIFY(copycounter::copycount == 20 );
111  a.insert(a.end(), 50, c);
112  VERIFY(copycounter::copycount == 70 );
113
114  // NOTE : These values are each one higher than might be expected, as
115  // deque::insert(iterator, count, value) copies the value it is given
116  // when it has to move elements in the deque in case that value is
117  // in the deque.
118  // Near the start
119  a.insert(a.begin() + 10, 100, c);
120  VERIFY(copycounter::copycount == 170 + 1);
121  // Near the end
122  a.insert(a.end() - 10, 200, c);
123  VERIFY(copycounter::copycount == 370 + 2);
124}
125
126int main()
127{
128  test01();
129  test02();
130  test03();
131  test04();
132  test05();
133  return 0;
134}
135