Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
try to fix the build on centos
[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 pageno1, pageno2, pageno3, pageno4;
33   static int value;
34
35 public: // 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::pageno1                   = 0;
52 size_t BOOST_tests::pageno2                   = 0;
53 size_t BOOST_tests::pageno3                   = 0;
54 size_t BOOST_tests::pageno4                   = 0;
55 int BOOST_tests::value                        = 0;
56
57 void BOOST_tests::Init()
58 {
59   pagesize = (size_t)getpagesize();
60   store    = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
61   data     = getpage();
62   BOOST_CHECK_MESSAGE(store->size() == 0, "Bad size");
63 }
64
65 void BOOST_tests::store_page_once()
66 {
67   new_content(data, pagesize);
68   pageno1 = store->store_page(data);
69   BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 1, "Bad refcount");
70   const void* copy = store->get_page(pageno1);
71   BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize) == 0, "Page data should be the same");
72   BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
73 }
74
75 void BOOST_tests::store_same_page()
76 {
77   pageno2 = store->store_page(data);
78   BOOST_CHECK_MESSAGE(pageno1 == pageno2, "Page should be the same");
79   BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 2, "Bad refcount");
80   BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
81 }
82
83 void BOOST_tests::store_new_page()
84 {
85   new_content(data, pagesize);
86   pageno3 = store->store_page(data);
87   BOOST_CHECK_MESSAGE(pageno1 != pageno3, "New page should be different");
88   BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
89 }
90
91 void BOOST_tests::unref_pages()
92 {
93   store->unref_page(pageno1);
94   BOOST_CHECK_MESSAGE(store->get_ref(pageno1) == 1, "Bad refcount");
95   BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
96   store->unref_page(pageno2);
97   BOOST_CHECK_MESSAGE(store->size() == 1, "Bad size");
98 }
99
100 void BOOST_tests::reallocate_page()
101 {
102   new_content(data, pagesize);
103   pageno4 = store->store_page(data);
104   BOOST_CHECK_MESSAGE(pageno1 == pageno4, "Page was not reused");
105   BOOST_CHECK_MESSAGE(store->get_ref(pageno4) == 1, "Bad refcount");
106   BOOST_CHECK_MESSAGE(store->size() == 2, "Bad size");
107 }
108
109 void BOOST_tests::new_content(void* data, std::size_t size)
110 {
111   ::memset(data, ++value, size);
112 }
113
114 void* BOOST_tests::getpage()
115 {
116   return mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
117 }
118
119 namespace utf = boost::unit_test; // for test case dependence
120
121 BOOST_AUTO_TEST_SUITE(PAGESTORE)
122 BOOST_AUTO_TEST_CASE(Init)
123 {
124   BOOST_tests::Init();
125 }
126
127 BOOST_AUTO_TEST_CASE(store_page_once, *utf::depends_on("PAGESTORE/Init"))
128 {
129   BOOST_tests::store_page_once();
130 }
131
132 BOOST_AUTO_TEST_CASE(store_same_page, *utf::depends_on("PAGESTORE/store_page_once"))
133 {
134   BOOST_tests::store_same_page();
135 }
136
137 BOOST_AUTO_TEST_CASE(store_new_page, *utf::depends_on("PAGESTORE/store_same_page"))
138 {
139   BOOST_tests::store_new_page();
140 }
141
142 BOOST_AUTO_TEST_CASE(unref_pages, *utf::depends_on("PAGESTORE/store_new_page"))
143 {
144   BOOST_tests::unref_pages();
145 }
146
147 BOOST_AUTO_TEST_CASE(reallocate_page, *utf::depends_on("PAGESTORE/unref_pages"))
148 {
149   BOOST_tests::reallocate_page();
150 }
151 BOOST_AUTO_TEST_SUITE_END()