OSDN Git Service

2010-09-03 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / debug / construct_neg.h
1 // Copyright (C) 2010 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 //
18
19 #include <vector>
20 #include <list>
21 #include <testsuite_hooks.h>
22
23 // Check that invalid range of pointers is detected
24 template<template<typename> class ContTraits>
25   void
26   debug_check1()
27   {
28     bool test __attribute__((unused)) = true;
29
30     typedef ContTraits<int> Traits;
31     typedef typename Traits::cont_type cont_type;
32     typedef typename Traits::val_type val_type;
33     typedef std::vector<val_type> vector_type;
34
35     vector_type v;
36     for (int i = 0; i != 5; ++i)
37       v.push_back(Traits::make_val(i));
38     VERIFY(v.size() == 5);
39
40     val_type *first = &v.front() + 1;
41     val_type *last = first + 2;
42     cont_type c1(first, last);
43     VERIFY(c1.size() == 2);
44
45     cont_type c2(last, first); // Expected failure
46   }
47
48 template<template<typename> class ContTraits>
49   void
50   check1()
51   {
52 #ifdef _GLIBCXX_DEBUG
53     debug_check1<ContTraits>();
54 #else
55     __builtin_abort();
56 #endif
57   }
58
59 // Check that invalid range of debug random iterators is detected
60 template<template<typename> class ContTraits>
61   void
62   debug_check2()
63   {
64     bool test __attribute__((unused)) = true;
65
66     typedef ContTraits<int> Traits;
67     typedef typename Traits::cont_type cont_type;
68     typedef typename Traits::val_type val_type;
69     typedef std::vector<val_type> vector_type;
70
71     vector_type v;
72     for (int i = 0; i != 5; ++i)
73       v.push_back(Traits::make_val(i));
74     VERIFY(v.size() == 5);
75
76     typename vector_type::iterator first = v.begin() + 1;
77     typename vector_type::iterator last = first + 2;
78     cont_type c1(first, last);
79     VERIFY(c1.size() == 2);
80
81     cont_type c2(last, first); // Expected failure
82   }
83
84 template<template<typename> class ContTraits>
85   void
86   check2()
87   {
88 #ifdef _GLIBCXX_DEBUG
89     debug_check2<ContTraits>();
90 #else
91     __builtin_abort();
92 #endif
93   }
94
95 // Check that invalid range of debug not random iterators is detected
96 template<template<typename> class ContTraits>
97   void
98   debug_check3()
99   {
100     bool test __attribute__((unused)) = true;
101
102     typedef ContTraits<int> Traits;
103     typedef typename Traits::cont_type cont_type;
104     typedef typename Traits::val_type val_type;
105     typedef std::list<val_type> list_type;
106     list_type l;
107     for (int i = 0; i != 5; ++i)
108       l.push_back(Traits::make_val(i));
109     VERIFY(l.size() == 5);
110
111     typename list_type::iterator first = l.begin(); ++first;
112     typename list_type::iterator last = first; ++last; ++last;
113     cont_type c1(first, last);
114     VERIFY(c1.size() == 2);
115
116     cont_type c2(last, first); // Expected failure
117   }
118
119 template<template<typename> class ContTraits>
120   void
121   check3()
122   {
123 #ifdef _GLIBCXX_DEBUG
124     debug_check3<ContTraits>();
125 #else
126     __builtin_abort();
127 #endif
128   }