]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/test-statbag_cc.cc
Better (actual) fix for leak reported by Coverity.
[thirdparty/pdns.git] / pdns / test-statbag_cc.cc
1 #define BOOST_TEST_DYN_LINK
2 #define BOOST_TEST_NO_MAIN
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7 #include <boost/test/unit_test.hpp>
8 #include <boost/assign/list_of.hpp>
9
10 #include <boost/tuple/tuple.hpp>
11 #include <stdint.h>
12 #include <thread>
13 #include "misc.hh"
14 #include "dns.hh"
15 #include "statbag.hh"
16
17 using std::string;
18
19 static void threadMangler(AtomicCounter* ac)
20 {
21 for(unsigned int n=0; n < 1000000; ++n)
22 (*ac)++;
23 }
24
25 static void threadMangler2(StatBag* S)
26 {
27 for(unsigned int n=0; n < 1000000; ++n)
28 S->inc("c");
29 }
30
31
32
33 BOOST_AUTO_TEST_SUITE(test_misc_hh)
34
35 BOOST_AUTO_TEST_CASE(test_StatBagBasic) {
36 StatBag s;
37 s.declare("a", "description");
38 s.declare("b", "description");
39 s.declare("c", "description");
40 s.inc("a");
41 BOOST_CHECK_EQUAL(s.read("a"), 1UL);
42
43 unsigned long n;
44 for(n=0; n < 1000000; ++n)
45 s.inc("b");
46
47 BOOST_CHECK_EQUAL(s.read("b"), n);
48
49 AtomicCounter* ac = s.getPointer("a");
50 for(n=0; n < 1000000; ++n)
51 (*ac)++;
52
53 BOOST_CHECK_EQUAL(s.read("a"), n+1);
54
55 AtomicCounter* acc = s.getPointer("c");
56 std::vector<std::thread> manglers;
57 for (int i=0; i < 4; ++i) {
58 manglers.push_back(std::thread(threadMangler, acc));
59 }
60
61 for (auto& t : manglers) {
62 t.join();
63 }
64 manglers.clear();
65
66 BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
67
68 s.set("c", 0);
69
70 for (int i=0; i < 4; ++i) {
71 manglers.push_back(std::thread(threadMangler2, &s));
72 }
73
74 for (auto& t : manglers) {
75 t.join();
76 }
77 manglers.clear();
78
79 BOOST_CHECK_EQUAL(s.read("c"), 4000000U);
80
81
82 s.set("c", 1ULL<<31);
83 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<31) );
84 s.inc("c");
85 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<31) +1 );
86
87 #ifdef UINTPTR_MAX
88 #if UINTPTR_MAX > 0xffffffffULL
89 BOOST_CHECK_EQUAL(sizeof(AtomicCounterInner), 8U);
90 s.set("c", 1ULL<<33);
91 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<33) );
92 s.inc("c");
93 BOOST_CHECK_EQUAL(s.read("c"), (1ULL<<33) +1 );
94
95 s.set("c", ~0ULL);
96 BOOST_CHECK_EQUAL(s.read("c"), 0xffffffffffffffffULL );
97 s.inc("c");
98 BOOST_CHECK_EQUAL(s.read("c"), 0UL );
99 #else
100 BOOST_CHECK_EQUAL(sizeof(AtomicCounterInner), 4U);
101 BOOST_CHECK_EQUAL(~0UL, 0xffffffffUL);
102 s.set("c", ~0UL);
103 BOOST_CHECK_EQUAL(s.read("c"), 0xffffffffUL );
104 s.inc("c");
105 BOOST_CHECK_EQUAL(s.read("c"), 0UL );
106 #endif
107 #endif
108 }
109
110
111 BOOST_AUTO_TEST_SUITE_END()
112