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;
check_exception()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:
MyInteger()45     MyInteger() : m_pValue(new int(0)) { check_exception(); }
MyInteger(int i)46     MyInteger(int i) : m_pValue(new int(i)) { check_exception(); }
MyInteger(const MyInteger & src)47     MyInteger(const MyInteger& src) : m_pValue(new int(src)) { check_exception(); }
~MyInteger()48     ~MyInteger() { delete m_pValue; }
operator =(const MyInteger & src)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     }
operator int() const57     operator int () const { return *m_pValue; }
set_exception_trigger(int n)58     static void set_exception_trigger(int n) { ms_exception_trigger = n; }
59 };
60 
61 // default constructible class
62 class MyDefaultConstructible
63 {
64 public:
MyDefaultConstructible()65     MyDefaultConstructible() : m_n(1) {}
MyDefaultConstructible(int n)66     MyDefaultConstructible(int n) : m_n(n) {}
67     int m_n;
68 };
69 
70 // class counting instances of self
71 class InstanceCounter {
72 public:
InstanceCounter()73     InstanceCounter() { increment(); }
InstanceCounter(const InstanceCounter & y)74     InstanceCounter(const InstanceCounter& y) { y.increment(); }
~InstanceCounter()75     ~InstanceCounter() { decrement(); }
count()76     static int count() { return ms_count; }
77 private:
increment() const78     void increment() const { ++ms_count; }
decrement() const79     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     };
Dummy()92     Dummy() : m_n(eVar) {}
fnc()93     DummyEnum fnc() { return eFnc; }
const_fnc() const94     DummyEnum const_fnc() const { return eConst; }
virtual_fnc()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;
MyInputIteratorMyInputIterator108     explicit MyInputIterator(const vector_iterator& it) : m_it(it) {}
operator =MyInputIterator109     MyInputIterator& operator = (const MyInputIterator& it) {
110         if (this == &it)
111             return *this;
112         m_it = it.m_it;
113         return *this;
114     }
operator *MyInputIterator115     reference operator * () const { return *m_it; }
operator ->MyInputIterator116     pointer operator -> () const { return &(operator*()); }
operator ++MyInputIterator117     MyInputIterator& operator ++ () {
118         ++m_it;
119         return *this;
120     }
operator ++MyInputIterator121     MyInputIterator operator ++ (int) {
122         MyInputIterator tmp = *this;
123         ++*this;
124         return tmp;
125     }
operator ==MyInputIterator126     bool operator == (const MyInputIterator& it) const { return m_it == it.m_it; }
operator !=MyInputIterator127     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 
iterator_category(const MyInputIterator &)134 inline std::input_iterator_tag iterator_category(const MyInputIterator&) {
135     return std::input_iterator_tag();
136 }
value_type(const MyInputIterator &)137 inline int* value_type(const MyInputIterator&) { return 0; }
distance_type(const MyInputIterator &)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