Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove redundant access specifier.
[simgrid.git] / src / mc / sosp / PageStore_test.cpp
1 /* Copyright (c) 2015-2018. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #define BOOST_TEST_MODULE PAGESTORE
7 #define BOOST_TEST_DYN_LINK
8
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>
11
12 #include <cstdint>
13 #include <cstring>
14 #include <iostream>
15
16 #include <sys/mman.h>
17 #include <unistd.h>
18
19 #include <memory>
20
21 #include "src/mc/sosp/PageStore.hpp"
22
23 using simgrid::mc::PageStore;
24
25 /***********************************/
26 // a class to hold the variable used in the test cases
27 class BOOST_tests {
28 public:
29   static std::size_t pagesize;
30   static std::unique_ptr<PageStore> store;
31   static void* data;
32   static size_t pageno[4];
33   static int value;
34
35   // member functions used by the test suite(s)
36   static void Init();
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();
42
43   static void new_content(void* data, std::size_t size);
44   static void* getpage();
45 };
46
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;
53
54 void BOOST_tests::Init()
55 {
56   pagesize = (size_t)getpagesize();
57   store    = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
58   data     = getpage();
59   BOOST_CHECK_MESSAGE(store->size() == 0, "Bad size");
60 }
61
62 void BOOST_tests::store_page_once()
63 {
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");
70 }
71
72 void BOOST_tests::store_same_page()
73 {
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");
78 }
79
80 void BOOST_tests::store_new_page()
81 {
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");
86 }
87
88 void BOOST_tests::unref_pages()
89 {
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");
95 }
96
97 void BOOST_tests::reallocate_page()
98 {
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");
104 }
105
106 void BOOST_tests::new_content(void* data, std::size_t size)
107 {
108   ::memset(data, ++value, size);
109 }
110
111 void* BOOST_tests::getpage()
112 {
113   return mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
114 }
115
116 namespace utf = boost::unit_test; // for test case dependence
117
118 BOOST_AUTO_TEST_SUITE(PAGESTORE)
119 BOOST_AUTO_TEST_CASE(Init)
120 {
121   BOOST_tests::Init();
122 }
123
124 BOOST_AUTO_TEST_CASE(store_page_once, *utf::depends_on("PAGESTORE/Init"))
125 {
126   BOOST_tests::store_page_once();
127 }
128
129 BOOST_AUTO_TEST_CASE(store_same_page, *utf::depends_on("PAGESTORE/store_page_once"))
130 {
131   BOOST_tests::store_same_page();
132 }
133
134 BOOST_AUTO_TEST_CASE(store_new_page, *utf::depends_on("PAGESTORE/store_same_page"))
135 {
136   BOOST_tests::store_new_page();
137 }
138
139 BOOST_AUTO_TEST_CASE(unref_pages, *utf::depends_on("PAGESTORE/store_new_page"))
140 {
141   BOOST_tests::unref_pages();
142 }
143
144 BOOST_AUTO_TEST_CASE(reallocate_page, *utf::depends_on("PAGESTORE/unref_pages"))
145 {
146   BOOST_tests::reallocate_page();
147 }
148 BOOST_AUTO_TEST_SUITE_END()