libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
cache.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2002 by J.M.a_cacheGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
21 #define BOOST_TEST_MODULE libjmmcg_tests
22 #include <boost/test/included/unit_test.hpp>
23 
24 #include <boost/mpl/list.hpp>
25 
26 #include "core/cache.hpp"
27 
28 using namespace libjmmcg;
29 
30 struct my_factory final : public cache::factory_base<int,int> {
31  inline value_type __fastcall make(const key_type &i) const override {
32  return i<<2;
33  }
34 };
35 
36 typedef boost::mpl::list<
37  cache::ro<my_factory>,
38  cache::ro<my_factory, cache::lru<my_factory::value_type>, cache::threading<my_factory::key_type, cache::lru<my_factory::value_type> >::multi<ppd::platform_api, ppd::sequential_mode>, cache::basic_statistics>,
39  cache::ro<my_factory, cache::lru<my_factory::value_type>, cache::threading<my_factory::key_type, cache::lru<my_factory::value_type> >::multi<ppd::platform_api, ppd::heavyweight_threading>, cache::basic_statistics>
40 > test_types;
41 
42 BOOST_AUTO_TEST_SUITE(cache_tests)
43 
44 BOOST_AUTO_TEST_CASE_TEMPLATE(ctor, T, test_types) {
45  typedef T cache_t;
46 
47  cache_t cache(2,4,2,my_factory());
48  BOOST_CHECK_EQUAL(cache.empty(), true);
49  BOOST_CHECK_EQUAL(cache.size(), 0U);
50 }
51 
52 BOOST_AUTO_TEST_CASE_TEMPLATE(clear_empty, T, test_types) {
53  typedef T cache_t;
54 
55  cache_t cache(2,4,2,my_factory());
56  cache.clear();
57  BOOST_CHECK_EQUAL(cache.empty(), true);
58  BOOST_CHECK_EQUAL(cache.size(), 0U);
59 }
60 
61 BOOST_AUTO_TEST_CASE_TEMPLATE(flush_empty, T, test_types) {
62  typedef T cache_t;
63 
64  cache_t cache(2,4,2,my_factory());
65  cache.flush();
66  BOOST_CHECK_EQUAL(cache.empty(), true);
67  BOOST_CHECK_EQUAL(cache.size(), 0U);
68 }
69 
70 BOOST_AUTO_TEST_CASE_TEMPLATE(create_one_item, T, test_types) {
71  typedef T cache_t;
72 
73  cache_t cache(2,4,2,my_factory());
74  BOOST_CHECK_EQUAL(cache[1].value(), 4);
75  BOOST_CHECK_EQUAL(cache.empty(), false);
76  BOOST_CHECK_EQUAL(cache.size(), 1U);
77 }
78 
79 BOOST_AUTO_TEST_CASE_TEMPLATE(create_two_items, T, test_types) {
80  typedef T cache_t;
81 
82  cache_t cache(2,4,2,my_factory());
83  BOOST_CHECK_EQUAL(cache[1].value(), 4);
84  BOOST_CHECK_EQUAL(cache[2].value(), 8);
85  BOOST_CHECK_EQUAL(cache.empty(), false);
86  BOOST_CHECK_EQUAL(cache.size(), 2U);
87 }
88 
89 BOOST_AUTO_TEST_CASE_TEMPLATE(flush_small, T, test_types) {
90  typedef T cache_t;
91 
92  cache_t cache(2,4,2,my_factory());
93  cache[1].value();
94  cache[2].value();
95  cache[3].value();
96  cache.flush();
97  BOOST_CHECK_EQUAL(cache.empty(), false);
98  BOOST_CHECK_EQUAL(cache.size(), 3U);
99 }
100 
101 BOOST_AUTO_TEST_CASE_TEMPLATE(flush_full, T, test_types) {
102  typedef T cache_t;
103 
104  cache_t cache(2,4,2,my_factory());
105  cache[1].value();
106  cache[2].value();
107  cache[3].value();
108  cache[4].value();
109  cache[5].value();
110  cache.flush();
111  BOOST_CHECK_EQUAL(cache.empty(), false);
112  BOOST_CHECK_EQUAL(cache.size(), 2U);
113 }
114 
115 BOOST_AUTO_TEST_CASE_TEMPLATE(clear_small, T, test_types) {
116  typedef T cache_t;
117 
118  cache_t cache(2,4,2,my_factory());
119  cache[1].value();
120  cache[2].value();
121  cache.clear();
122  BOOST_CHECK_EQUAL(cache.empty(), true);
123  BOOST_CHECK_EQUAL(cache.size(), 0U);
124 }
125 
126 BOOST_AUTO_TEST_SUITE_END()