Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
replaced BOOST_TEST with BOOST_CHECK_EQUAL/BOOS_CHECK_MESSAGE.
[simgrid.git] / src / mc / snapshot / unitTest / PageStore_unit_BOOST.cpp
1 /****************************************************
2 TODO: comment
3 ****************************************************/
4 #define BOOST_TEST_MODULE PAGESTORE
5 #define BOOST_TEST_DYN_LINK
6 #include <boost/test/unit_test.hpp>
7
8 // /*******************************/
9 // /* GENERATED FILE, DO NOT EDIT */
10 // /*******************************/
11 // 
12 // #include <stdio.h>
13 // #include "xbt.h"
14 // /*******************************/
15 // /* GENERATED FILE, DO NOT EDIT */
16 // /*******************************/
17 // 
18 // #line 191 "mc/PageStore.cpp" 
19
20 #include <iostream>
21 #include <cstring>
22 #include <cstdint>
23
24 #include <unistd.h>
25 #include <sys/mman.h>
26
27 #include <memory>
28
29 #include "src/mc/PageStore.hpp"
30
31 static int value = 0;
32
33 static void new_content(void* data, std::size_t size)
34 {
35   ::memset(data, ++value, size);
36 }
37
38 static void* getpage()
39 {
40   return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
41 }
42
43 BOOST_AUTO_TEST_CASE(pageStore) {
44 // XBT_TEST_UNIT("base", test_mc_page_store, "Test adding/removing pages in the store")
45 // {
46
47   using simgrid::mc::PageStore;
48
49   std::cout << "Test adding/removing pages in the store" << std::endl;
50
51   // xbt_test_add("Init");
52   std::size_t pagesize = (size_t) getpagesize();
53   std::unique_ptr<PageStore> store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
54   void* data = getpage();
55   BOOST_CHECK_MESSAGE(store->size()==0, "Bad size");
56
57   // xbt_test_add("Store the page once");
58   new_content(data, pagesize);
59   size_t pageno1 = store->store_page(data);
60   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
61   const void* copy = store->get_page(pageno1);
62   BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
63   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
64
65   // xbt_test_add("Store the same page again");
66   size_t pageno2 = store->store_page(data);
67   BOOST_CHECK_MESSAGE(pageno1==pageno2, "Page should be the same");
68   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==2, "Bad refcount");
69   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
70
71   // xbt_test_add("Store a new page");
72   new_content(data, pagesize);
73   size_t pageno3 = store->store_page(data);
74   BOOST_CHECK_MESSAGE(pageno1 != pageno3, "New page should be different");
75   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
76
77   // xbt_test_add("Unref pages");
78   store->unref_page(pageno1);
79   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
80   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
81   store->unref_page(pageno2);
82   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
83
84   // xbt_test_add("Reallocate page");
85   new_content(data, pagesize);
86   size_t pageno4 = store->store_page(data);
87   BOOST_CHECK_MESSAGE(pageno1 == pageno4, "Page was not reused");
88   BOOST_CHECK_MESSAGE(store->get_ref(pageno4)==1, "Bad refcount");
89   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
90 }
91
92 /*******************************/
93 /* GENERATED FILE, DO NOT EDIT */
94 /*******************************/