object_cache_test.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. *
  3. * Copyright (c) 2004
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE object_cache_test.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Test code for a generic object cache.
  16. */
  17. #include <boost/regex/pending/object_cache.hpp>
  18. #include <boost/detail/lightweight_main.hpp>
  19. #include "../test_macros.hpp"
  20. class test_object
  21. {
  22. public:
  23. test_object(int i)
  24. : m_value(i)
  25. {
  26. ++s_count;
  27. }
  28. int value()const
  29. {
  30. return m_value;
  31. }
  32. static int count()
  33. {
  34. return s_count;
  35. }
  36. private:
  37. int m_value;
  38. static int s_count;
  39. };
  40. int test_object::s_count = 0;
  41. static const int max_cache_size = 5;
  42. int cpp_main(int /*argc*/, char * /*argv*/[])
  43. {
  44. int i;
  45. for(i = 0; i < 20; ++i)
  46. {
  47. boost::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
  48. BOOST_CHECK(p->value() == i);
  49. p = boost::object_cache<int, test_object>::get(i, max_cache_size);
  50. BOOST_CHECK(p->value() == i);
  51. if(i)
  52. {
  53. p = boost::object_cache<int, test_object>::get(i-1, max_cache_size);
  54. BOOST_CHECK(p->value() == i-1);
  55. }
  56. }
  57. int current_count = test_object::count();
  58. for(int j = 0; j < 10; ++j)
  59. {
  60. for(i = 20 - max_cache_size; i < 20; ++i)
  61. {
  62. boost::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
  63. BOOST_CHECK(p->value() == i);
  64. p = boost::object_cache<int, test_object>::get(i, max_cache_size);
  65. BOOST_CHECK(p->value() == i);
  66. }
  67. }
  68. BOOST_CHECK(current_count == test_object::count());
  69. return 0;
  70. }