1 /* Copyright (c) 2015-2018. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #define BOOST_TEST_MODULE PAGESTORE
7 #define BOOST_TEST_DYN_LINK
8 #include <boost/test/unit_test.hpp>
19 #include "src/mc/sosp/PageStore.hpp"
21 using simgrid::mc::PageStore;
23 /***********************************/
24 // a class to hold the variable used in the test cases
27 static std::size_t pagesize;
28 static std::unique_ptr<PageStore> store;
30 static size_t pageno1, pageno2, pageno3, pageno4;
33 public: // member functions used by the test suite(s)
35 static void store_page_once();
36 static void store_same_page();
37 static void store_new_page();
38 static void unref_pages();
39 static void reallocate_page();
41 static void new_content(void* data, std::size_t size);
42 static void* getpage();
45 // static member datat initialization
46 std::size_t BOOST_tests::pagesize = 0;
47 std::unique_ptr<PageStore> BOOST_tests::store = nullptr;
48 void* BOOST_tests::data = nullptr;
49 size_t BOOST_tests::pageno1 = 0;
50 size_t BOOST_tests::pageno2 = 0;
51 size_t BOOST_tests::pageno3 = 0;
52 size_t BOOST_tests::pageno4 = 0;
53 int BOOST_tests::value = 0;
55 void BOOST_tests::Init()
57 pagesize = (size_t)getpagesize();
58 store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
60 BOOST_CHECK_MESSAGE(store->size() == 0, "Bad size");
63 void BOOST_tests::store_page_once()
65 new_content(data, pagesize);
66 pageno1 = store->store_page(data);
67 BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 1, "Bad refcount");
68 const void* copy = store->get_page(pageno1);
69 BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize) == 0, "Page data should be the same");
70 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
73 void BOOST_tests::store_same_page()
75 pageno2 = store->store_page(data);
76 BOOST_CHECK_MESSAGE(pageno1 == pageno2, "Page should be the same");
77 BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 2, "Bad refcount");
78 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
81 void BOOST_tests::store_new_page()
83 new_content(data, pagesize);
84 pageno3 = store->store_page(data);
85 BOOST_CHECK_MESSAGE(pageno1 != pageno3, "New page should be different");
86 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
89 void BOOST_tests::unref_pages()
91 store->unref_page(pageno1);
92 BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 1, "Bad refcount");
93 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
94 store->unref_page(pageno2);
95 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
98 void BOOST_tests::reallocate_page()
100 new_content(data, pagesize);
101 pageno4 = store->store_page(data);
102 BOOST_CHECK_MESSAGE(pageno1 == pageno4, "Page was not reused");
103 BOOST_CHECK_MESSAGE(store->get_ref(pageno4) == 1, "Bad refcount");
104 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
107 void BOOST_tests::new_content(void* data, std::size_t size)
109 ::memset(data, ++value, size);
112 void* BOOST_tests::getpage()
114 return mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
117 namespace utf = boost::unit_test; // for test case dependence
119 BOOST_AUTO_TEST_SUITE(PAGESTORE)
120 BOOST_AUTO_TEST_CASE(Init)
125 BOOST_AUTO_TEST_CASE(store_page_once, *utf::depends_on("PAGESTORE/Init"))
127 BOOST_tests::store_page_once();
130 BOOST_AUTO_TEST_CASE(store_same_page, *utf::depends_on("PAGESTORE/store_page_once"))
132 BOOST_tests::store_same_page();
135 BOOST_AUTO_TEST_CASE(store_new_page, *utf::depends_on("PAGESTORE/store_same_page"))
137 BOOST_tests::store_new_page();
140 BOOST_AUTO_TEST_CASE(unref_pages, *utf::depends_on("PAGESTORE/store_new_page"))
142 BOOST_tests::unref_pages();
145 BOOST_AUTO_TEST_CASE(reallocate_page, *utf::depends_on("PAGESTORE/unref_pages"))
147 BOOST_tests::reallocate_page();
149 BOOST_AUTO_TEST_SUITE_END()