Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
convert the two other boost::unit_test suites to Catch2
[simgrid.git] / src / mc / sosp / PageStore_test.cpp
1 /* Copyright (c) 2015-2019. 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 #include "src/include/catch.hpp"
7
8 #include <cstdint>
9 #include <cstring>
10 #include <iostream>
11
12 #include <sys/mman.h>
13 #include <unistd.h>
14
15 #include <memory>
16
17 #include "src/mc/sosp/PageStore.hpp"
18
19 #define BOOST_CHECK_MESSAGE(a, b)                                                                                      \
20   INFO(b);                                                                                                             \
21   REQUIRE(a);
22
23 using simgrid::mc::PageStore;
24
25 /***********************************/
26 // a class to hold the variable used in the test cases
27 class helper_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 };
45
46 // static member datat initialization
47 std::size_t helper_tests::pagesize             = 0;
48 std::unique_ptr<PageStore> helper_tests::store = nullptr;
49 void* helper_tests::data                       = nullptr;
50 size_t helper_tests::pageno[4]                 = {0, 0, 0, 0};
51 int helper_tests::value                        = 0;
52
53 void helper_tests::Init()
54 {
55   pagesize = (size_t)getpagesize();
56   store    = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(50));
57   data     = mmap(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
58   BOOST_CHECK_MESSAGE(store->size() == 0, "Bad size");
59 }
60
61 void helper_tests::store_page_once()
62 {
63   new_content(data, pagesize);
64   pageno[0] = store->store_page(data);
65   REQUIRE(store->get_ref(pageno[0]) == 1);
66   const void* copy = store->get_page(pageno[0]);
67   BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize) == 0, "Page data should be the same");
68   REQUIRE(store->size() == 1);
69 }
70
71 void helper_tests::store_same_page()
72 {
73   pageno[1] = store->store_page(data);
74   BOOST_CHECK_MESSAGE(pageno[0] == pageno[1], "Page should be the same");
75   REQUIRE(store->get_ref(pageno[0]) == 2);
76   REQUIRE(store->size() == 1);
77 }
78
79 void helper_tests::store_new_page()
80 {
81   new_content(data, pagesize);
82   pageno[2] = store->store_page(data);
83   BOOST_CHECK_MESSAGE(pageno[0] != pageno[2], "New page should be different");
84   REQUIRE(store->size() == 2);
85 }
86
87 void helper_tests::unref_pages()
88 {
89   store->unref_page(pageno[0]);
90   BOOST_CHECK_MESSAGE(store->get_ref(pageno[0]) == 1, "Bad refcount");
91   REQUIRE(store->size() == 2);
92   store->unref_page(pageno[1]);
93   REQUIRE(store->size() == 1);
94 }
95
96 void helper_tests::reallocate_page()
97 {
98   new_content(data, pagesize);
99   pageno[3] = store->store_page(data);
100   BOOST_CHECK_MESSAGE(pageno[0] == pageno[3], "Page was not reused");
101   REQUIRE(store->get_ref(pageno[3]) == 1);
102   REQUIRE(store->size() == 2);
103 }
104
105 void helper_tests::new_content(void* data, std::size_t size)
106 {
107   value++;
108   ::memset(data, value, size);
109 }
110
111 TEST_CASE("MC page store, used during checkpoint", "MC::PageStore")
112 {
113   helper_tests::Init();
114   INFO("Store page once");
115   helper_tests::store_page_once();
116
117   INFO("Store the same page");
118   helper_tests::store_same_page();
119
120   INFO("Store a new page");
121   helper_tests::store_new_page();
122
123   INFO("Unref pages");
124   helper_tests::unref_pages();
125
126   INFO("Reallocate pages");
127   helper_tests::reallocate_page();
128 }