1 /* Copyright (c) 2014-2018. The SimGrid Team. All rights reserved. */
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. */
6 #define BOOST_TEST_MODULE PAGESTORE
7 #define BOOST_TEST_DYN_LINK
8 #include <boost/test/unit_test.hpp>
10 // /*******************************/
11 // /* GENERATED FILE, DO NOT EDIT */
12 // /*******************************/
16 // /*******************************/
17 // /* GENERATED FILE, DO NOT EDIT */
18 // /*******************************/
20 // #line 191 "mc/PageStore.cpp"
31 #include "src/mc/PageStore.hpp"
35 static void new_content(void* data, std::size_t size)
37 ::memset(data, ++value, size);
40 static void* getpage()
42 return mmap(nullptr, getpagesize(), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
45 BOOST_AUTO_TEST_CASE(pageStore) {
47 using simgrid::mc::PageStore;
49 std::cout << "Test adding/removing pages in the store" << std::endl;
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");
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");
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");
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");
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");
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");