File: test_safe_mode.cpp

package info (click to toggle)
boost 1.32.0-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 93,952 kB
  • ctags: 128,458
  • sloc: cpp: 492,477; xml: 52,125; python: 13,519; ansic: 13,013; sh: 1,773; yacc: 853; makefile: 526; perl: 418; lex: 110; csh: 6
file content (211 lines) | stat: -rw-r--r-- 5,664 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* Boost.MultiIndex test for safe_mode.
 *
 * Copyright 2003-2004 Joaqun M Lpez Muoz.
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 * See http://www.boost.org/libs/multi_index for library home page.
 */

#include "test_safe_mode.hpp"

#include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
#include "pre_multi_index.hpp"
#include "employee.hpp"
#include "pair_of_ints.hpp"
#include <stdexcept>
#include <boost/test/test_tools.hpp>

using namespace boost::multi_index;

#define TRY_SAFE_MODE \
try{

#define CATCH_SAFE_MODE(err) \
  throw std::runtime_error("safe mode violation not detected");\
}catch(const safe_mode_exception& e){\
  if(e.error_code!=(err))throw std::runtime_error(\
    "safe mode violation not expected");\
}

struct change_id
{
  change_id(int new_id_):new_id(new_id_){}
  void operator()(employee& e){e.id=new_id;}

private:
  int new_id;
};

typedef multi_index_container<
  pair_of_ints,
  indexed_by<
    ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,first)>,
    ordered_unique<BOOST_MULTI_INDEX_MEMBER(pair_of_ints,int,second)> > >
int_int_set;

void test_safe_mode()
{
  employee_set es,es2;
  employee_set_as_inserted& i=get<as_inserted>(es);
  es.insert(employee(0,"Joe",31));

  TRY_SAFE_MODE
    employee_set::iterator it;
    employee_set::iterator it2=es.begin();
    it2=it;
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it;
    employee e=*it;
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)
  
  TRY_SAFE_MODE
    employee_set::iterator it=es.end();
    employee e=*it;
  CATCH_SAFE_MODE(safe_mode::not_dereferenceable_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.end();
    ++it;
  CATCH_SAFE_MODE(safe_mode::not_incrementable_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.begin();
    --it;
  CATCH_SAFE_MODE(safe_mode::not_decrementable_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it;
    employee_set::iterator it2;
    bool b=(it==it2);
    b=true; /* avoid warning about unused var */
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.begin();
    employee_set::iterator it2;
    bool b=(it==it2);
    b=true; /* avoid warning about unused var */
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.begin();
    employee_set::iterator it2=es2.begin();
    bool b=(it==it2);
    b=true; /* avoid warning about unused var */
  CATCH_SAFE_MODE(safe_mode::not_same_owner)

  TRY_SAFE_MODE
    es.erase(es.end(),es.begin());
  CATCH_SAFE_MODE(safe_mode::invalid_range)

  TRY_SAFE_MODE
    employee_set::iterator it;
    es.insert(it,employee(0,"Joe",31));
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    es.erase(es.end());
  CATCH_SAFE_MODE(safe_mode::not_dereferenceable_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.begin();
    es2.insert(it,employee(0,"Joe",31));
  CATCH_SAFE_MODE(safe_mode::not_owner)

  TRY_SAFE_MODE
    employee_set::iterator it=es.begin();
    employee_set::iterator it2=es2.end();
    es2.erase(it,it2);
  CATCH_SAFE_MODE(safe_mode::not_owner)

  TRY_SAFE_MODE
    employee_set::iterator it=es.insert(employee(1,"Robert",27)).first;
    es.erase(it);
    es.erase(it);
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it;
    {
      employee_set es3;
      it=es3.insert(employee(0,"Joe",31)).first;
    }
    employee e=*it;
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it;
    {
      employee_set es3;
      it=es3.insert(employee(0,"Joe",31)).first;
    }
    employee_set::iterator it2;
    it2=it;
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set es3(es);
    employee_set es4;
    employee_set::iterator it=es3.begin();
    es3.swap(es4);
    es3.erase(it);
  CATCH_SAFE_MODE(safe_mode::not_owner)

  /* this, unlike the previous case, is indeed correct, test safe mode
   * gets it right
   */
  { 
    employee_set es3(es);
    employee_set es4;
    employee_set::iterator it=es3.begin();
    es3.swap(es4);
    es4.erase(it);
  }

  TRY_SAFE_MODE
    employee_set::iterator it=es.insert(employee(1,"Robert",27)).first;
    employee_set_by_name::iterator it2=project<name>(es,it);
    es.modify_key(it,change_id(0));
    employee e=*it2;
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    int_int_set iis;
    int_int_set::iterator it=iis.insert(pair_of_ints(0,0)).first;
    iis.insert(pair_of_ints(1,1));
    iis.modify(it,increment_first);
    pair_of_ints p=*it;
    p.first=0; /* avoid warning about unused var */
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    int_int_set iis;
    int_int_set::iterator it=iis.insert(pair_of_ints(0,0)).first;
    iis.insert(pair_of_ints(1,1));
    iis.modify(it,increment_second);
    pair_of_ints p=*it;
    p.first=0; /* avoid warning about unused var */
  CATCH_SAFE_MODE(safe_mode::invalid_iterator)

  TRY_SAFE_MODE
    employee_set::iterator it=es.end();
    employee_set_by_name::iterator it2=project<name>(es2,it);
  CATCH_SAFE_MODE(safe_mode::not_owner)

  TRY_SAFE_MODE
    employee_set_by_name::iterator it=get<name>(es).end();
    employee_set::iterator it2=project<0>(es2,it);
  CATCH_SAFE_MODE(safe_mode::not_owner)

  TRY_SAFE_MODE
    i.splice(i.begin(),i,i.begin(),i.end());
  CATCH_SAFE_MODE(safe_mode::inside_range)

  TRY_SAFE_MODE
    i.splice(i.begin(),i);
  CATCH_SAFE_MODE(safe_mode::same_container)
}