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
9 bool init_unit_test(); // boost sometimes forget to give this prototype (NetBSD and other), which does not fit our paranoid flags
10 #include <boost/test/unit_test.hpp>
21 #include "src/mc/sosp/PageStore.hpp"
23 using simgrid::mc::PageStore;
25 /***********************************/
26 // a class to hold the variable used in the test cases
29 static std::size_t pagesize;
30 static std::unique_ptr<PageStore> store;
32 static size_t pageno[4];
35 public: // member functions used by the test suite(s)
37 static void store_page_once();
38 static void store_same_page();
39 static void store_new_page();
40 static void unref_pages();
41 static void reallocate_page();
43 static void new_content(void* data, std::size_t size);
44 static void* getpage();
47 // static member datat initialization
48 std::size_t BOOST_tests::pagesize = 0;
49 std::unique_ptr<PageStore> BOOST_tests::store = nullptr;
50 void* BOOST_tests::data = nullptr;
51 size_t BOOST_tests::pageno[4] = {0, 0, 0, 0};
52 int BOOST_tests::value = 0;
54 void BOOST_tests::Init()
56 pagesize = (size_t)getpagesize();
57 store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
59 BOOST_CHECK_MESSAGE(store->size() == 0, "Bad size");
62 void BOOST_tests::store_page_once()
64 new_content(data, pagesize);
65 pageno[0] = store->store_page(data);
66 BOOST_CHECK_MESSAGE(store->get_ref(pageno[0]) == 1, "Bad refcount");
67 const void* copy = store->get_page(pageno[0]);
68 BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize) == 0, "Page data should be the same");
69 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
72 void BOOST_tests::store_same_page()
74 pageno[1] = store->store_page(data);
75 BOOST_CHECK_MESSAGE(pageno[0] == pageno[1], "Page should be the same");
76 BOOST_CHECK_MESSAGE(store->get_ref(pageno[0]) == 2, "Bad refcount");
77 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
80 void BOOST_tests::store_new_page()
82 new_content(data, pagesize);
83 pageno[2] = store->store_page(data);
84 BOOST_CHECK_MESSAGE(pageno[0] != pageno[2], "New page should be different");
85 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
88 void BOOST_tests::unref_pages()
90 store->unref_page(pageno[0]);
91 BOOST_CHECK_MESSAGE(store->get_ref(pageno[0]) == 1, "Bad refcount");
92 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
93 store->unref_page(pageno[1]);
94 BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
97 void BOOST_tests::reallocate_page()
99 new_content(data, pagesize);
100 pageno[3] = store->store_page(data);
101 BOOST_CHECK_MESSAGE(pageno[0] == pageno[3], "Page was not reused");
102 BOOST_CHECK_MESSAGE(store->get_ref(pageno[3]) == 1, "Bad refcount");
103 BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
106 void BOOST_tests::new_content(void* data, std::size_t size)
108 ::memset(data, ++value, size);
111 void* BOOST_tests::getpage()
113 return mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
116 namespace utf = boost::unit_test; // for test case dependence
118 BOOST_AUTO_TEST_SUITE(PAGESTORE)
119 BOOST_AUTO_TEST_CASE(Init)
124 BOOST_AUTO_TEST_CASE(store_page_once, *utf::depends_on("PAGESTORE/Init"))
126 BOOST_tests::store_page_once();
129 BOOST_AUTO_TEST_CASE(store_same_page, *utf::depends_on("PAGESTORE/store_page_once"))
131 BOOST_tests::store_same_page();
134 BOOST_AUTO_TEST_CASE(store_new_page, *utf::depends_on("PAGESTORE/store_same_page"))
136 BOOST_tests::store_new_page();
139 BOOST_AUTO_TEST_CASE(unref_pages, *utf::depends_on("PAGESTORE/store_new_page"))
141 BOOST_tests::unref_pages();
144 BOOST_AUTO_TEST_CASE(reallocate_page, *utf::depends_on("PAGESTORE/unref_pages"))
146 BOOST_tests::reallocate_page();
148 BOOST_AUTO_TEST_SUITE_END()