Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding unit tests.
[simgrid.git] / src / mc / snapshot / unitTest / mc_snapshot_unit_BOOST.cpp
1 /*************************************************
2 TODO: comment
3 *************************************************/
4 #define BOOST_TEST_MODULE snapshots
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 180 "mc/mc_snapshot.cpp" 
19
20 #include <cstdlib>
21 #include <cstring>
22
23 #include <sys/mman.h>
24
25 #include "simgrid/src/mc/mc_config.hpp"
26 #include "simgrid/src/mc/mc_mmu.hpp"
27 #include "simgrid/src/mc/mc_private.hpp"
28 #include "simgrid/src/mc/mc_snapshot.hpp"
29
30
31 static inline void init_memory(void* mem, size_t size)
32 {
33   char* dest = (char*) mem;
34   for (size_t i = 0; i < size; ++i) {
35     dest[i] = rand() & 255;
36   }
37 }
38
39 static int test_snapshot(bool sparse_checkpoint);
40
41 BOOST_AUTO_TEST_SUITE(Snapshots)
42 BOOST_AUTO_TEST_CASE(flat_snapshots) {
43   BOOST_TEST(test_snapshot(0) == 1);
44 }
45 BOOST_AUTO_TEST_CASE(page_snapshots) {
46   BOOST_TEST(test_snapshot(1) == 1);
47 }
48 BOOST_AUTO_TEST_SUITE_END()
49
50 static int test_snapshot(bool sparse_checkpoint) {
51
52   // xbt_test_add("Initialization");
53   _sg_mc_sparse_checkpoint = sparse_checkpoint;
54   BOOST_TEST(xbt_pagesize == getpagesize());
55   BOOST_TEST(1 << xbt_pagebits == xbt_pagesize);
56
57   std::unique_ptr<simgrid::mc::RemoteClient> process(new simgrid::mc::RemoteClient(getpid(), -1));
58   process->init();
59   mc_model_checker = new ::simgrid::mc::ModelChecker(std::move(process));
60
61   for(int n=1; n!=256; ++n) {
62
63     // Store region page(s):
64     size_t byte_size = n * xbt_pagesize;
65     void* source = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
66     BOOST_TEST(source!=MAP_FAILED, "Could not allocate source memory");
67
68     // Init memory and take snapshots:
69     init_memory(source, byte_size);
70     simgrid::mc::RegionSnapshot region0 = simgrid::mc::sparse_region(
71       simgrid::mc::RegionType::Unknown, source, source, byte_size);
72     for(int i=0; i<n; i+=2) {
73       init_memory((char*) source + i*xbt_pagesize, xbt_pagesize);
74     }
75     simgrid::mc::RegionSnapshot region = simgrid::mc::sparse_region(
76       simgrid::mc::RegionType::Unknown, source, source, byte_size);
77
78     void* destination = mmap(nullptr, byte_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
79     BOOST_TEST(source!=MAP_FAILED, "Could not allocate destination memory");
80
81     // xbt_test_add("Reading whole region data for %i page(s)", n);
82     const void* read = MC_region_read(&region, destination, source, byte_size);
83     BOOST_TEST(not memcmp(source, read, byte_size), "Mismatch in MC_region_read()");
84
85     // xbt_test_add("Reading parts of region data for %i page(s)", n);
86     for(int j=0; j!=100; ++j) {
87       size_t offset = rand() % byte_size;
88       size_t size = rand() % (byte_size - offset);
89       const void* read = MC_region_read(&region, destination, (const char*) source+offset, size);
90       BOOST_TEST(not memcmp((char*)source + offset, read, size), "Mismatch in MC_region_read()");
91     }
92
93     // xbt_test_add("Compare whole region data for %i page(s)", n);
94
95     BOOST_TEST(MC_snapshot_region_memcmp(source, &region0, source, &region, byte_size),
96       "Unexpected match in MC_snapshot_region_memcmp() with previous snapshot");
97
98     // xbt_test_add("Compare parts of region data for %i page(s) with itself", n);
99     for(int j=0; j!=100; ++j) {
100       size_t offset = rand() % byte_size;
101       size_t size = rand() % (byte_size - offset);
102       BOOST_TEST(
103           not MC_snapshot_region_memcmp((char*)source + offset, &region, (char*)source + offset, &region, size),
104           "Mismatch in MC_snapshot_region_memcmp()");
105     }
106
107     if (n==1) {
108       // xbt_test_add("Read pointer for %i page(s)", n);
109       memcpy(source, &mc_model_checker, sizeof(void*));
110       simgrid::mc::RegionSnapshot region2 = simgrid::mc::sparse_region(
111         simgrid::mc::RegionType::Unknown, source, source, byte_size);
112       BOOST_TEST(MC_region_read_pointer(&region2, source) == mc_model_checker,
113         "Mismtach in MC_region_read_pointer()");
114     }
115
116     munmap(destination, byte_size);
117     munmap(source, byte_size);
118   }
119
120   delete mc_model_checker;
121   mc_model_checker = nullptr;
122
123   return 1; // dummy value, for BOOST unit test
124 }
125
126 /*******************************/
127 /* GENERATED FILE, DO NOT EDIT */
128 /*******************************/
129