Merge #9911: Wshadow: various gcc fixes
[bitcoinplatinum.git] / src / test / raii_event_tests.cpp
blob0f40874f55c10494ce4766848e1a58ee68bda6aa
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.
5 #include <event2/event.h>
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)
10 #include <map>
11 #include <stdlib.h>
13 #include "support/events.h"
15 #include "test/test_bitcoin.h"
17 #include <vector>
19 #include <boost/test/unit_test.hpp>
21 static std::map<void*, short> tags;
22 static std::map<void*, uint16_t> orders;
23 static uint16_t tagSequence = 0;
25 static void* tag_malloc(size_t sz) {
26 void* mem = malloc(sz);
27 if (!mem) return mem;
28 tags[mem]++;
29 orders[mem] = tagSequence++;
30 return mem;
33 static void tag_free(void* mem) {
34 tags[mem]--;
35 orders[mem] = tagSequence++;
36 free(mem);
39 BOOST_FIXTURE_TEST_SUITE(raii_event_tests, BasicTestingSetup)
41 BOOST_AUTO_TEST_CASE(raii_event_creation)
43 event_set_mem_functions(tag_malloc, realloc, tag_free);
45 void* base_ptr = NULL;
47 auto base = obtain_event_base();
48 base_ptr = (void*)base.get();
49 BOOST_CHECK(tags[base_ptr] == 1);
51 BOOST_CHECK(tags[base_ptr] == 0);
53 void* event_ptr = NULL;
55 auto base = obtain_event_base();
56 auto event = obtain_event(base.get(), -1, 0, NULL, NULL);
58 base_ptr = (void*)base.get();
59 event_ptr = (void*)event.get();
61 BOOST_CHECK(tags[base_ptr] == 1);
62 BOOST_CHECK(tags[event_ptr] == 1);
64 BOOST_CHECK(tags[base_ptr] == 0);
65 BOOST_CHECK(tags[event_ptr] == 0);
67 event_set_mem_functions(malloc, realloc, free);
70 BOOST_AUTO_TEST_CASE(raii_event_order)
72 event_set_mem_functions(tag_malloc, realloc, tag_free);
74 void* base_ptr = NULL;
75 void* event_ptr = NULL;
77 auto base = obtain_event_base();
78 auto event = obtain_event(base.get(), -1, 0, NULL, NULL);
80 base_ptr = (void*)base.get();
81 event_ptr = (void*)event.get();
83 // base should have allocated before event
84 BOOST_CHECK(orders[base_ptr] < orders[event_ptr]);
86 // base should be freed after event
87 BOOST_CHECK(orders[base_ptr] > orders[event_ptr]);
89 event_set_mem_functions(malloc, realloc, free);
92 BOOST_AUTO_TEST_SUITE_END()
94 #endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED