• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/pending/object_cache.hpp>
19 #include <boost/detail/lightweight_main.hpp>
20 #include "../test_macros.hpp"
21 
22 class test_object
23 {
24 public:
test_object(int i)25    test_object(int i)
26       : m_value(i)
27    {
28       ++s_count;
29    }
value() const30    int value()const
31    {
32       return m_value;
33    }
count()34    static int count()
35    {
36       return s_count;
37    }
38 private:
39    int m_value;
40    static int s_count;
41 };
42 
43 int test_object::s_count = 0;
44 
45 static const int max_cache_size = 5;
46 
cpp_main(int,char * [])47 int cpp_main(int /*argc*/, char * /*argv*/[])
48 {
49    int i;
50    for(i = 0; i < 20; ++i)
51    {
52       boost::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
53       BOOST_CHECK(p->value() == i);
54       p = boost::object_cache<int, test_object>::get(i, max_cache_size);
55       BOOST_CHECK(p->value() == i);
56       if(i)
57       {
58          p = boost::object_cache<int, test_object>::get(i-1, max_cache_size);
59          BOOST_CHECK(p->value() == i-1);
60       }
61    }
62    int current_count = test_object::count();
63    for(int j = 0; j < 10; ++j)
64    {
65       for(i = 20 - max_cache_size; i < 20; ++i)
66       {
67          boost::shared_ptr<const test_object> p = boost::object_cache<int, test_object>::get(i, max_cache_size);
68          BOOST_CHECK(p->value() == i);
69          p = boost::object_cache<int, test_object>::get(i, max_cache_size);
70          BOOST_CHECK(p->value() == i);
71       }
72    }
73    BOOST_CHECK(current_count == test_object::count());
74    return 0;
75 }
76 
77 
78