-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestboost.cpp
78 lines (56 loc) · 1.81 KB
/
testboost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Unit-tests for timers using boost::posix_time
*/
// (C)Copyright 2017-2024, RW Penney
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <boost/test/unit_test.hpp>
#include <boost/thread.hpp>
#include <cmath>
#include <cstdlib>
#include "testdefns.hpp"
#include "rtimers/boost.hpp"
namespace BoostUT = boost::unit_test;
namespace rtimers {
namespace testing {
typedef Timer<boostpt::ThreadManager<boostpt::HiResClock, MeanBoundStats>,
NullLogger> QuietThreadedTimer;
static void keepBusy(QuietThreadedTimer* timer, unsigned iterations)
{
occupyTimer(*timer, iterations);
}
TestBoost::TestBoost()
: BoostUT::test_suite("Boost timer variants")
{
add(BOOST_TEST_CASE(threaded));
}
void TestBoost::threaded()
{ std::vector<boost::thread> threads;
std::vector<unsigned> expected;
std::vector<QuietThreadedTimer*> timers;
const unsigned ntimers = 11, nthreads = 200;
for (unsigned i=0; i<ntimers; ++i) {
std::stringstream ident;
ident << "Boost threads [" << i << "]";
timers.push_back(new QuietThreadedTimer(ident.str()));
expected.push_back(0);
}
for (unsigned i=0; i<nthreads; ++i) {
unsigned slot = i % ntimers;
QuietThreadedTimer *tmr = timers[slot];
unsigned iterations = 200 + (std::rand() % 6101);
threads.push_back(boost::thread(keepBusy, tmr, iterations));
expected[slot] += iterations;
}
while (!threads.empty()) {
threads.back().join();
threads.pop_back();
}
for (unsigned i=0; i<ntimers; ++i) {
BOOST_CHECK_EQUAL(expected[i], timers[i]->getStats().count);
delete timers[i];
}
}
} // namespace testing
} // namespace rtimers