test.hpp 4.2 KB

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