summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/23_containers/deque/modifiers/moveable.cc
blob: 7c95f892f09dd2c87ce34d55900d1c3cf08648e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// { dg-do run { target c++11 } }

// Copyright (C) 2005-2019 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.


#include <deque>
#include <testsuite_hooks.h>
#include <testsuite_rvalref.h>

using namespace __gnu_test;

// Test deque::push_back makes no unneeded copies.
void
test01()
{
  std::deque<copycounter> a;
  copycounter c(1);
  copycounter::copycount = 0;
  for(int i = 0; i < 1000; ++i)
    a.push_back(c);
  VERIFY(copycounter::copycount == 1000);
}

// Test deque::push_front makes no unneeded copies.
void
test02()
{
  std::deque<copycounter> a;
  copycounter c(1);
  copycounter::copycount = 0;
  for(int i = 0; i < 1000; ++i)
    a.push_front(c);
  VERIFY(copycounter::copycount == 1000);
}

// Test deque::insert makes no unneeded copies.
void
test03()
{
  std::deque<copycounter> a(1000);
  copycounter c(1);
  copycounter::copycount = 0;
  a.insert(a.begin(),c);
  a.insert(a.end(),c);
  for(int i = 0; i < 500; ++i)
    a.insert(a.begin() + i, c);
  VERIFY(copycounter::copycount == 502);
}

// Test deque::insert(iterator, count, value) makes no unneeded copies
// when it has to also reallocate the deque's internal buffer.
void
test04()
{
  copycounter c(1);
  std::deque<copycounter> a(10, c);
  copycounter::copycount = 0;
  a.insert(a.begin(), 20, c);
  VERIFY(copycounter::copycount == 20);
  a.insert(a.end(), 50, c);
  VERIFY(copycounter::copycount == 70);
  // NOTE : These values are each one higher than might be expected, as
  // deque::insert(iterator, count, value) copies the value it is given
  // when it has to move elements in the deque in case that value is
  // in the deque.
  
  // Near the start
  a.insert(a.begin() + 10, 100, c);
  VERIFY(copycounter::copycount == 170 + 1);
  // Near the end
  a.insert(a.end() - 10, 1000, c);
  VERIFY(copycounter::copycount == 1170 + 2);
}

// Test deque::insert(iterator, count, value) makes no unneeded copies
// when it doesn't have to reallocate the deque's internal buffer.
void
test05()
{
  copycounter c(1);
  std::deque<copycounter> a(10, c);
  copycounter::copycount = 0;
  //a.reserve(1000);
  a.insert(a.begin(), 20, c);
  VERIFY(copycounter::copycount == 20 );
  a.insert(a.end(), 50, c);
  VERIFY(copycounter::copycount == 70 );
  
  // NOTE : These values are each one higher than might be expected, as
  // deque::insert(iterator, count, value) copies the value it is given
  // when it has to move elements in the deque in case that value is
  // in the deque.
  // Near the start
  a.insert(a.begin() + 10, 100, c);
  VERIFY(copycounter::copycount == 170 + 1);
  // Near the end
  a.insert(a.end() - 10, 200, c);
  VERIFY(copycounter::copycount == 370 + 2);
}

int main()
{
  test01();
  test02();
  test03();
  test04();
  test05();
  return 0;
}