PIVX Core  5.6.99
P2P Digital Currency
raii_event_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <event2/event.h>
6 
7 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
8 // It would probably be ideal to define dummy test(s) that report skipped, but boost::test doesn't seem to make that practical (at least not in versions available with common distros)
9 
10 #include <map>
11 #include <stdlib.h>
12 
13 #include "support/events.h"
14 
15 #include "test/test_pivx.h"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 static std::map<void*, short> tags;
20 static std::map<void*, uint16_t> orders;
21 static uint16_t tagSequence = 0;
22 
23 static void* tag_malloc(size_t sz) {
24  void* mem = malloc(sz);
25  if (!mem) return mem;
26  tags[mem]++;
27  orders[mem] = tagSequence++;
28  return mem;
29 }
30 
31 static void tag_free(void* mem) {
32  tags[mem]--;
33  orders[mem] = tagSequence++;
34  free(mem);
35 }
36 
38 
39 BOOST_AUTO_TEST_CASE(raii_event_creation)
40 {
41  event_set_mem_functions(tag_malloc, realloc, tag_free);
42 
43  void* base_ptr = nullptr;
44  {
45  auto base = obtain_event_base();
46  base_ptr = (void*)base.get();
47  BOOST_CHECK(tags[base_ptr] == 1);
48  }
49  BOOST_CHECK(tags[base_ptr] == 0);
50 
51  void* event_ptr = nullptr;
52  {
53  auto base = obtain_event_base();
54  auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
55 
56  base_ptr = (void*)base.get();
57  event_ptr = (void*)event.get();
58 
59  BOOST_CHECK(tags[base_ptr] == 1);
60  BOOST_CHECK(tags[event_ptr] == 1);
61  }
62  BOOST_CHECK(tags[base_ptr] == 0);
63  BOOST_CHECK(tags[event_ptr] == 0);
64 
65  event_set_mem_functions(malloc, realloc, free);
66 }
67 
68 BOOST_AUTO_TEST_CASE(raii_event_order)
69 {
70  event_set_mem_functions(tag_malloc, realloc, tag_free);
71 
72  void* base_ptr = nullptr;
73  void* event_ptr = nullptr;
74  {
75  auto base = obtain_event_base();
76  auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
77 
78  base_ptr = (void*)base.get();
79  event_ptr = (void*)event.get();
80 
81  // base should have allocated before event
82  BOOST_CHECK(orders[base_ptr] < orders[event_ptr]);
83  }
84  // base should be freed after event
85  BOOST_CHECK(orders[base_ptr] > orders[event_ptr]);
86 
87  event_set_mem_functions(malloc, realloc, free);
88 }
89 
91 
92 #endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
BOOST_AUTO_TEST_SUITE_END()
raii_event obtain_event(struct event_base *base, evutil_socket_t s, short events, event_callback_fn cb, void *arg)
Definition: events.h:37
raii_event_base obtain_event_base()
Definition: events.h:30
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_AUTO_TEST_CASE(funcName)
Definition: object.cpp:15
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Basic testing setup.
Definition: test_pivx.h:51