• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/lightweight_test.hpp>
20 #include <iterator>
21 #include <numeric>
22 #include <vector>
23 #if !defined(BOOST_NO_EXCEPTIONS)
24     #include <exception>
25 #endif
26 
27 // Integer (substitute for int) - more appropriate for testing
28 class MyInteger {
29 private:
30     int* m_pValue;
31     static int ms_exception_trigger;
check_exception()32     void check_exception() {
33         if (ms_exception_trigger > 0) {
34             if (--ms_exception_trigger == 0) {
35                 delete m_pValue;
36                 m_pValue = 0;
37 #if !defined(BOOST_NO_EXCEPTIONS)
38                 throw std::exception();
39 #endif
40             }
41         }
42     }
43 public:
MyInteger()44     MyInteger() : m_pValue(new int(0)) { check_exception(); }
MyInteger(int i)45     MyInteger(int i) : m_pValue(new int(i)) { check_exception(); }
MyInteger(const MyInteger & src)46     MyInteger(const MyInteger& src) : m_pValue(new int(src)) { check_exception(); }
~MyInteger()47     ~MyInteger() { delete m_pValue; }
operator =(const MyInteger & src)48     MyInteger& operator = (const MyInteger& src) {
49         if (this == &src)
50             return *this;
51         check_exception();
52         delete m_pValue;
53         m_pValue = new int(src);
54         return *this;
55     }
operator int() const56     operator int () const { return *m_pValue; }
set_exception_trigger(int n)57     static void set_exception_trigger(int n) { ms_exception_trigger = n; }
58 };
59 
60 // default constructible class
61 class MyDefaultConstructible
62 {
63 public:
MyDefaultConstructible()64     MyDefaultConstructible() : m_n(1) {}
MyDefaultConstructible(int n)65     MyDefaultConstructible(int n) : m_n(n) {}
66     int m_n;
67 };
68 
69 // class counting instances of self
70 class InstanceCounter {
71 public:
InstanceCounter()72     InstanceCounter() { increment(); }
InstanceCounter(const InstanceCounter & y)73     InstanceCounter(const InstanceCounter& y) { y.increment(); }
~InstanceCounter()74     ~InstanceCounter() { decrement(); }
count()75     static int count() { return ms_count; }
76 private:
increment() const77     void increment() const { ++ms_count; }
decrement() const78     void decrement() const { --ms_count; }
79     static int ms_count;
80 };
81 
82 // dummy class suitable for iterator referencing test
83 class Dummy {
84 public:
85     enum DummyEnum {
86         eVar,
87         eFnc,
88         eConst,
89         eVirtual
90     };
Dummy()91     Dummy() : m_n(eVar) {}
~Dummy()92     virtual ~Dummy() {}
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     typedef std::vector<int>::iterator vector_iterator;
102     typedef std::input_iterator_tag iterator_category;
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 namespace boost;
143 using namespace std;
144 
145 #endif // #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP)
146