1 | // Header file for the test of the circular buffer library. |
2 | |
3 | // Copyright (c) 2003-2008 Jan Gaspar |
4 | |
5 | // Use, modification, and distribution is subject to the Boost Software |
6 | // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
7 | // http://www.boost.org/LICENSE_1_0.txt) |
8 | |
9 | #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP) |
10 | #define BOOST_CIRCULAR_BUFFER_TEST_HPP |
11 | |
12 | #if defined(_MSC_VER) && _MSC_VER >= 1200 |
13 | #pragma once |
14 | #endif |
15 | |
16 | #define BOOST_CB_TEST |
17 | |
18 | #include <boost/circular_buffer.hpp> |
19 | #include <boost/test/included/unit_test.hpp> |
20 | #include <boost/iterator.hpp> |
21 | #include <iterator> |
22 | #include <numeric> |
23 | #include <vector> |
24 | #if !defined(BOOST_NO_EXCEPTIONS) |
25 | #include <exception> |
26 | #endif |
27 | |
28 | // Integer (substitute for int) - more appropriate for testing |
29 | class MyInteger { |
30 | private: |
31 | int* m_pValue; |
32 | static int ms_exception_trigger; |
33 | void check_exception() { |
34 | if (ms_exception_trigger > 0) { |
35 | if (--ms_exception_trigger == 0) { |
36 | delete m_pValue; |
37 | m_pValue = 0; |
38 | #if !defined(BOOST_NO_EXCEPTIONS) |
39 | throw std::exception(); |
40 | #endif |
41 | } |
42 | } |
43 | } |
44 | public: |
45 | MyInteger() : m_pValue(new int(0)) { check_exception(); } |
46 | MyInteger(int i) : m_pValue(new int(i)) { check_exception(); } |
47 | MyInteger(const MyInteger& src) : m_pValue(new int(src)) { check_exception(); } |
48 | ~MyInteger() { delete m_pValue; } |
49 | MyInteger& operator = (const MyInteger& src) { |
50 | if (this == &src) |
51 | return *this; |
52 | check_exception(); |
53 | delete m_pValue; |
54 | m_pValue = new int(src); |
55 | return *this; |
56 | } |
57 | operator int () const { return *m_pValue; } |
58 | static void set_exception_trigger(int n) { ms_exception_trigger = n; } |
59 | }; |
60 | |
61 | // default constructible class |
62 | class MyDefaultConstructible |
63 | { |
64 | public: |
65 | MyDefaultConstructible() : m_n(1) {} |
66 | MyDefaultConstructible(int n) : m_n(n) {} |
67 | int m_n; |
68 | }; |
69 | |
70 | // class counting instances of self |
71 | class InstanceCounter { |
72 | public: |
73 | InstanceCounter() { increment(); } |
74 | InstanceCounter(const InstanceCounter& y) { y.increment(); } |
75 | ~InstanceCounter() { decrement(); } |
76 | static int count() { return ms_count; } |
77 | private: |
78 | void increment() const { ++ms_count; } |
79 | void decrement() const { --ms_count; } |
80 | static int ms_count; |
81 | }; |
82 | |
83 | // dummy class suitable for iterator referencing test |
84 | class Dummy { |
85 | public: |
86 | enum DummyEnum { |
87 | eVar, |
88 | eFnc, |
89 | eConst, |
90 | eVirtual |
91 | }; |
92 | Dummy() : m_n(eVar) {} |
93 | DummyEnum fnc() { return eFnc; } |
94 | DummyEnum const_fnc() const { return eConst; } |
95 | virtual DummyEnum virtual_fnc() { return eVirtual; } |
96 | DummyEnum m_n; |
97 | }; |
98 | |
99 | // simulator of an input iterator |
100 | struct MyInputIterator |
101 | : boost::iterator<std::input_iterator_tag, int, ptrdiff_t, int*, int&> { |
102 | typedef std::vector<int>::iterator vector_iterator; |
103 | typedef int value_type; |
104 | typedef int* pointer; |
105 | typedef int& reference; |
106 | typedef size_t size_type; |
107 | typedef ptrdiff_t difference_type; |
108 | explicit MyInputIterator(const vector_iterator& it) : m_it(it) {} |
109 | MyInputIterator& operator = (const MyInputIterator& it) { |
110 | if (this == &it) |
111 | return *this; |
112 | m_it = it.m_it; |
113 | return *this; |
114 | } |
115 | reference operator * () const { return *m_it; } |
116 | pointer operator -> () const { return &(operator*()); } |
117 | MyInputIterator& operator ++ () { |
118 | ++m_it; |
119 | return *this; |
120 | } |
121 | MyInputIterator operator ++ (int) { |
122 | MyInputIterator tmp = *this; |
123 | ++*this; |
124 | return tmp; |
125 | } |
126 | bool operator == (const MyInputIterator& it) const { return m_it == it.m_it; } |
127 | bool operator != (const MyInputIterator& it) const { return m_it != it.m_it; } |
128 | private: |
129 | vector_iterator m_it; |
130 | }; |
131 | |
132 | #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) |
133 | |
134 | inline std::input_iterator_tag iterator_category(const MyInputIterator&) { |
135 | return std::input_iterator_tag(); |
136 | } |
137 | inline int* value_type(const MyInputIterator&) { return 0; } |
138 | inline ptrdiff_t* distance_type(const MyInputIterator&) { return 0; } |
139 | |
140 | #endif // #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) |
141 | |
142 | using boost::unit_test::test_suite; |
143 | using namespace boost; |
144 | using namespace std; |
145 | |
146 | #endif // #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP) |
147 | |