File: event_test.cpp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 80,296 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (177 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (C) 2013 Tim Blechmann
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/core/lightweight_test.hpp>

#include <boost/bind.hpp>
#include <boost/chrono.hpp>
#include <boost/thread/thread.hpp>
#include <boost/sync/events.hpp>
#include <boost/sync/support/boost_chrono.hpp>

#include <boost/typeof/typeof.hpp>

void test_event_post_wait()
{
    boost::sync::manual_reset_event ev;

    ev.set();
    ev.wait();

    BOOST_TEST( ev.try_wait() == true );

    ev.reset();

    BOOST_TEST( ev.try_wait() == false );
}


void test_event_post_try_wait()
{
    boost::sync::manual_reset_event ev;

    BOOST_TEST( ev.try_wait() == false );

    ev.set();

    BOOST_TEST( ev.try_wait() == true );
}

void test_event_post_wait_autoreset()
{
    boost::sync::auto_reset_event ev;

    ev.post();
    BOOST_TEST( ev.try_wait() == true );
    BOOST_TEST( ev.try_wait() == false );
}


void test_event_reset()
{
    boost::sync::manual_reset_event ev;

    BOOST_TEST( ev.try_wait() == false );
    ev.set();
    BOOST_TEST( ev.try_wait() == true );
    ev.reset();
    BOOST_TEST( ev.try_wait() == false );
}

inline void post_event(boost::sync::manual_reset_event& evt)
{
    evt.set();
}

inline void post_event(boost::sync::auto_reset_event& evt)
{
    evt.post();
}

template <typename EventType>
struct event_wait_and_post_test
{
    void run()
    {
        boost::thread post_thread(boost::bind(&event_wait_and_post_test::wait_and_post, this));
        ev_.wait();
    }

    void wait_and_post()
    {
        boost::this_thread::sleep_for(boost::chrono::seconds(1));
        (post_event)(ev_);
    }

    EventType ev_;
    boost::thread thread_;
};

void test_event_wait_and_post()
{
    event_wait_and_post_test<boost::sync::manual_reset_event> test;
    test.run();
}

void test_event_wait_and_post_autoreset()
{
    event_wait_and_post_test<boost::sync::auto_reset_event> test;
    test.run();
}

void test_event_wait_for()
{
    using namespace boost;

    boost::sync::manual_reset_event ev;

    BOOST_AUTO(start, chrono::system_clock::now());

    BOOST_TEST( ev.try_wait() == false );

    BOOST_TEST(!ev.wait_for(chrono::milliseconds(500)));

    BOOST_TEST( ev.try_wait() == false );


    BOOST_AUTO(end, chrono::system_clock::now());
    BOOST_AUTO(wait_time, end - start);

    // guessing!
    BOOST_TEST( wait_time > chrono::milliseconds(450) );
    BOOST_TEST( wait_time < chrono::milliseconds(1000) );

    ev.set();

    BOOST_TEST(ev.wait_for(chrono::milliseconds(500)));
}

void test_event_wait_until()
{
    using namespace boost;

    boost::sync::manual_reset_event ev;
    {
        BOOST_AUTO(now, chrono::system_clock::now());
        BOOST_AUTO(timeout, now + chrono::milliseconds(500));

        BOOST_TEST(!ev.wait_until(timeout));

        BOOST_AUTO(end, chrono::system_clock::now());
        BOOST_AUTO(timeout_delta, end - timeout);

        // guessing!
        BOOST_TEST( timeout_delta > chrono::milliseconds(-400) );
        BOOST_TEST( timeout_delta < chrono::milliseconds(400) );
    }

    ev.set();

    {
        BOOST_AUTO(start,   chrono::system_clock::now());
        BOOST_AUTO(timeout, start + chrono::milliseconds(500));

        BOOST_TEST(ev.wait_until(timeout));

        BOOST_AUTO(end, chrono::system_clock::now());

        // guessing!
        BOOST_TEST( (end - start) < chrono::milliseconds(100) );
    }
}

int main()
{
    test_event_post_wait();
    test_event_post_try_wait();
    test_event_post_wait_autoreset();
    test_event_reset();
    test_event_wait_and_post();
    test_event_wait_and_post_autoreset();
    test_event_wait_for();
    test_event_wait_until();

    return boost::report_errors();
}