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  /*
13   *   LOCATION:    see http://www.boost.org for most recent version.
14   *   FILE         object_cache_test.cpp
15   *   VERSION      see <boost/version.hpp>
16   *   DESCRIPTION: Test code for a generic object cache.
17   */
18 #include <boost/regex/config.hpp>
19 #ifdef BOOST_REGEX_CXX03
20 #include <boost/regex/v4/object_cache.hpp>
21 #define SP_NS boost
22 #else
23 #include <boost/regex/v5/object_cache.hpp>
24 #define SP_NS std
25 #endif
26 #include <boost/detail/lightweight_main.hpp>
27 #include "../test_macros.hpp"
28 
29 class test_object
30 {
31 public:
test_object(int i)32    test_object(int i)
33       : m_value(i)
34    {
35       ++s_count;
36    }
value() const37    int value()const
38    {
39       return m_value;
40    }
count()41    static int count()
42    {
43       return s_count;
44    }
45 private:
46    int m_value;
47    static int s_count;
48 };
49 
50 int test_object::s_count = 0;
51 
52 static const int max_cache_size = 5;
53 
cpp_main(int,char * [])54 int cpp_main(int /*argc*/, char * /*argv*/[])
55 {
56    int i;
57    for(i = 0; i < 20; ++i)
58    {
59       SP_NS::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
60       BOOST_CHECK(p->value() == i);
61       p = boost::object_cache<int, test_object>::get(i, max_cache_size);
62       BOOST_CHECK(p->value() == i);
63       if(i)
64       {
65          p = boost::object_cache<int, test_object>::get(i-1, max_cache_size);
66          BOOST_CHECK(p->value() == i-1);
67       }
68    }
69    int current_count = test_object::count();
70    for(int j = 0; j < 10; ++j)
71    {
72       for(i = 20 - max_cache_size; i < 20; ++i)
73       {
74          SP_NS::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
75          BOOST_CHECK(p->value() == i);
76          p = boost::object_cache<int, test_object>::get(i, max_cache_size);
77          BOOST_CHECK(p->value() == i);
78       }
79    }
80    BOOST_CHECK(current_count == test_object::count());
81    return 0;
82 }
83 
84 
85