3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file map_debug_base.hpp
44 * Contains a debug-mode base for all maps.
47 #ifndef PB_DS_MAP_DEBUG_BASE_HPP
48 #define PB_DS_MAP_DEBUG_BASE_HPP
50 #ifdef PB_DS_USE_MAP_DEBUG_BASE
55 #include <pb_ds/testsuite/regression/res_mng/dbg_ex_allocator.hpp>
63 #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
64 #define PB_DS_DBG_ASSERT(X) assert(X)
65 #define PB_DS_DBG_VERIFY(X) assert(X)
66 #define PB_DS_DBG_ONLY(X) X
67 #else // #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
68 #define PB_DS_DBG_ASSERT(X)
69 #define PB_DS_DBG_VERIFY(X) {if((X)==0);}
70 #define PB_DS_DBG_ONLY(X) ;
71 #endif // #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
73 #define PB_DS_CLASS_T_DEC \
74 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
76 #define PB_DS_CLASS_C_DEC \
82 template<typename Key, class Eq_Fn, typename Const_Key_Reference>
86 typedef typename std::allocator< Key> key_allocator;
88 typedef typename key_allocator::size_type size_type;
90 typedef Const_Key_Reference const_key_reference;
95 map_debug_base(const PB_DS_CLASS_C_DEC& other);
100 insert_new(const_key_reference r_key);
103 erase_existing(const_key_reference r_key);
109 check_key_exists(const_key_reference r_key) const;
112 check_key_does_not_exist(const_key_reference r_key) const;
115 check_size(size_type size) const;
118 swap(PB_DS_CLASS_C_DEC& other);
120 template<typename Cmp_Fn>
122 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other);
125 join(PB_DS_CLASS_C_DEC& other);
128 typedef std::list< Key> key_set;
130 typedef typename key_set::iterator key_set_iterator;
132 typedef typename key_set::const_iterator const_key_set_iterator;
135 #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
137 assert_valid() const;
138 #endif // #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
140 const_key_set_iterator
141 find(const_key_reference r_key) const;
144 find(const_key_reference r_key);
156 PB_DS_DBG_ONLY(assert_valid();)
161 map_debug_base(const PB_DS_CLASS_C_DEC& other) :
162 m_key_set(other.m_key_set)
164 PB_DS_DBG_ONLY(assert_valid();)
171 PB_DS_DBG_ONLY(assert_valid();)
177 insert_new(const_key_reference r_key)
179 PB_DS_DBG_ONLY(assert_valid();)
181 pb_ds::test::dbg_ex_allocator<char> alloc;
183 const double orig_throw_prob = alloc.get_throw_prob();
185 alloc.set_throw_prob(0);
187 if (find(r_key) != m_key_set.end())
189 std::cerr << "insert_new " << r_key << std::endl;
196 m_key_set.push_back(r_key);
200 std::cerr << "insert_new 1" << r_key << std::endl;
205 alloc.set_throw_prob(orig_throw_prob);
207 PB_DS_DBG_ONLY(assert_valid();)
213 erase_existing(const_key_reference r_key)
215 PB_DS_DBG_ONLY(assert_valid();)
217 key_set_iterator it = find(r_key);
219 if (it == m_key_set.end())
221 std::cerr << "erase_existing " << r_key << std::endl;
228 PB_DS_DBG_ONLY(assert_valid();)
236 PB_DS_DBG_ONLY(assert_valid();)
240 PB_DS_DBG_ONLY(assert_valid();)
246 check_key_exists(const_key_reference r_key) const
248 PB_DS_DBG_ONLY(assert_valid();)
250 if (find(r_key) == m_key_set.end())
252 std::cerr << "check_key_exists " << r_key << std::endl;
257 PB_DS_DBG_ONLY(assert_valid();)
263 check_key_does_not_exist(const_key_reference r_key) const
265 PB_DS_DBG_ONLY(assert_valid();)
267 if (find(r_key) != m_key_set.end())
269 std::cerr << "check_key_does_not_exist " << r_key << std::endl;
278 check_size(size_type size) const
280 PB_DS_DBG_ONLY(assert_valid();)
282 const size_type key_set_size = m_key_set.size();
284 if (size != key_set_size)
286 std::cerr << "check_size " << size << " " << key_set_size << std::endl;
291 PB_DS_DBG_ONLY(assert_valid();)
297 swap(PB_DS_CLASS_C_DEC& other)
299 PB_DS_DBG_ONLY(assert_valid();)
301 m_key_set.swap(other.m_key_set);
303 PB_DS_DBG_ONLY(assert_valid();)
307 typename PB_DS_CLASS_C_DEC::const_key_set_iterator
309 find(const_key_reference r_key) const
311 PB_DS_DBG_ONLY(assert_valid();)
313 for (const_key_set_iterator it = m_key_set.begin(); it != m_key_set.end();
315 if (m_eq(*it, r_key))
318 return (m_key_set.end());
322 typename PB_DS_CLASS_C_DEC::key_set_iterator
324 find(const_key_reference r_key)
326 PB_DS_DBG_ONLY(assert_valid();)
328 key_set_iterator it = m_key_set.begin();
330 while (it != m_key_set.end())
332 if (m_eq(*it, r_key))
340 PB_DS_DBG_ONLY(assert_valid();)
343 #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
349 const_key_set_iterator prime_it = m_key_set.begin();
351 while (prime_it != m_key_set.end())
353 const_key_set_iterator sec_it = prime_it;
357 while (sec_it != m_key_set.end())
359 assert(!m_eq(*sec_it, * prime_it));
360 assert(!m_eq(*prime_it, * sec_it));
368 #endif // #ifdef PB_DS_MAP_DEBUG_BASE_DEBUG
371 template<typename Cmp_Fn>
374 split(const_key_reference r_key, Cmp_Fn cmp_fn, PB_DS_CLASS_C_DEC& other)
376 pb_ds::test::dbg_ex_allocator<char> alloc;
378 const double orig_throw_prob = alloc.get_throw_prob();
380 alloc.set_throw_prob(0);
384 key_set_iterator it = m_key_set.begin();
386 while (it != m_key_set.end())
387 if (cmp_fn(r_key, * it))
389 other.insert_new(*it);
391 it = m_key_set.erase(it);
396 alloc.set_throw_prob(orig_throw_prob);
402 join(PB_DS_CLASS_C_DEC& other)
404 pb_ds::test::dbg_ex_allocator<char> alloc;
406 const double orig_throw_prob = alloc.get_throw_prob();
408 alloc.set_throw_prob(0);
410 key_set_iterator it = other.m_key_set.begin();
412 while (it != other.m_key_set.end())
416 it = other.m_key_set.erase(it);
419 PB_DS_DBG_ASSERT(other.m_key_set.empty());
421 alloc.set_throw_prob(orig_throw_prob);
424 #undef PB_DS_CLASS_T_DEC
425 #undef PB_DS_CLASS_C_DEC
427 #undef PB_DS_DBG_ASSERT
428 #undef PB_DS_DBG_VERIFY
429 #undef PB_DS_DBG_ONLY
431 } // namespace detail
435 #endif // #ifdef PB_DS_USE_MAP_DEBUG_BASE
437 #endif // #ifndef PB_DS_MAP_DEBUG_BASE_HPP