Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge remote-tracking branch 'upstream/master'
[simgrid.git] / src / mc / snapshot / unitTest / PageStore_unit.cpp
1 /* Copyright (c) 2014-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 #include <boost/test/unit_test.hpp>
9
10 // /*******************************/
11 // /* GENERATED FILE, DO NOT EDIT */
12 // /*******************************/
13 // 
14 // #include <stdio.h>
15 // #include "xbt.h"
16 // /*******************************/
17 // /* GENERATED FILE, DO NOT EDIT */
18 // /*******************************/
19 // 
20 // #line 191 "mc/PageStore.cpp" 
21
22 #include <iostream>
23 #include <cstring>
24 #include <cstdint>
25
26 #include <unistd.h>
27 #include <sys/mman.h>
28
29 #include <memory>
30
31 #include "src/mc/PageStore.hpp"
32
33 static int value = 0;
34
35 static void new_content(void* data, std::size_t size)
36 {
37   ::memset(data, ++value, size);
38 }
39
40 static void* getpage()
41 {
42   return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
43 }
44
45 BOOST_AUTO_TEST_CASE(pageStore) {
46
47   using simgrid::mc::PageStore;
48
49   std::cout << "Test adding/removing pages in the store" << std::endl;
50
51   std::size_t pagesize = (size_t) getpagesize();
52   std::unique_ptr<PageStore> store = std::unique_ptr<PageStore>(new simgrid::mc::PageStore(500));
53   void* data = getpage();
54   BOOST_CHECK_MESSAGE(store->size()==0, "Bad size");
55
56   new_content(data, pagesize);
57   size_t pageno1 = store->store_page(data);
58   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
59   const void* copy = store->get_page(pageno1);
60   BOOST_CHECK_MESSAGE(::memcmp(data, copy, pagesize)==0, "Page data should be the same");
61   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
62
63   size_t pageno2 = store->store_page(data);
64   BOOST_CHECK_MESSAGE(pageno1==pageno2, "Page should be the same");
65   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==2, "Bad refcount");
66   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
67
68   new_content(data, pagesize);
69   size_t pageno3 = store->store_page(data);
70   BOOST_CHECK_MESSAGE(pageno1 != pageno3, "New page should be different");
71   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
72
73   store->unref_page(pageno1);
74   BOOST_CHECK_MESSAGE(store->get_ref(pageno1)==1, "Bad refcount");
75   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
76   store->unref_page(pageno2);
77   BOOST_CHECK_MESSAGE(store->size()==1, "Bad size");
78
79   new_content(data, pagesize);
80   size_t pageno4 = store->store_page(data);
81   BOOST_CHECK_MESSAGE(pageno1 == pageno4, "Page was not reused");
82   BOOST_CHECK_MESSAGE(store->get_ref(pageno4)==1, "Bad refcount");
83   BOOST_CHECK_MESSAGE(store->size()==2, "Bad size");
84 }