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;
32 public: // member functions used by the test suite(s)
34 static void store_page_once();
35 static void store_same_page();
36 static void store_new_page();
37 static void unref_pages();
38 static void reallocate_page();
40 static void new_content(void* data, std::size_t size);
41 static void* getpage();
44 // static member datat initialization
45 std::size_t BOOST_tests::pagesize = 0;
46 std::unique_ptr<PageStore> BOOST_tests::store = nullptr;
47 void* BOOST_tests::data = nullptr;
48 size_t BOOST_tests::pageno1 = 0;
49 size_t BOOST_tests::pageno2 = 0;
50 size_t BOOST_tests::pageno3 = 0;
51 size_t BOOST_tests::pageno4 = 0;
52 int BOOST_tests::value = 0;
54 void BOOST_tests::Init() {
55 pagesize = (size_t) getpagesize();
56 store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
58 BOOST_CHECK_MESSAGE(store->size()==0, "Bad size");
61 void BOOST_tests::store_page_once() {
62 new_content(data, pagesize);
63 pageno1 = store->store_page(data);
64 BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
65 const void* copy = store->get_page(pageno1);
66 BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
67 BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
70 void BOOST_tests::store_same_page() {
71 pageno2 = store->store_page(data);
72 BOOST_CHECK_MESSAGE(pageno1==pageno2, "Page should be the same");
73 BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==2, "Bad refcount");
74 BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
77 void BOOST_tests::store_new_page() {
78 new_content(data, pagesize);
79 pageno3 = store->store_page(data);
80 BOOST_CHECK_MESSAGE(pageno1 != pageno3, "New page should be different");
81 BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
84 void BOOST_tests::unref_pages() {
85 store->unref_page(pageno1);
86 BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
87 BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
88 store->unref_page(pageno2);
89 BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
92 void BOOST_tests::reallocate_page() {
93 new_content(data, pagesize);
94 pageno4 = store->store_page(data);
95 BOOST_CHECK_MESSAGE(pageno1 == pageno4, "Page was not reused");
96 BOOST_CHECK_MESSAGE(store->get_ref(pageno4)==1, "Bad refcount");
97 BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
100 void BOOST_tests::new_content(void* data, std::size_t size) {
101 ::memset(data, ++value, size);
104 void* BOOST_tests::getpage() {
105 return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
108 namespace utf = boost::unit_test; // for test case dependence
110 BOOST_AUTO_TEST_SUITE(PAGESTORE)
111 BOOST_AUTO_TEST_CASE(Init) {
115 BOOST_AUTO_TEST_CASE(store_page_once, * utf::depends_on("PAGESTORE/Init")) {
116 BOOST_tests::store_page_once();
119 BOOST_AUTO_TEST_CASE(store_same_page, * utf::depends_on("PAGESTORE/store_page_once")) {
120 BOOST_tests::store_same_page();
123 BOOST_AUTO_TEST_CASE(store_new_page, * utf::depends_on("PAGESTORE/store_same_page")) {
124 BOOST_tests::store_new_page();
127 BOOST_AUTO_TEST_CASE(unref_pages, * utf::depends_on("PAGESTORE/store_new_page")) {
128 BOOST_tests::unref_pages();
131 BOOST_AUTO_TEST_CASE(reallocate_page, * utf::depends_on("PAGESTORE/unref_pages")) {
132 BOOST_tests::reallocate_page();
134 BOOST_AUTO_TEST_SUITE_END()