~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/test/WConditionOneShot_test.h

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#ifndef WCONDITIONONESHOT_TEST_H
 
26
#define WCONDITIONONESHOT_TEST_H
 
27
 
 
28
#include <iostream>
 
29
 
 
30
#include <boost/thread.hpp>
 
31
#include <cxxtest/TestSuite.h>
 
32
 
 
33
#include "../WConditionOneShot.h"
 
34
 
 
35
/**
 
36
 * Helper class.
 
37
 */
 
38
class Callable
 
39
{
 
40
public:
 
41
 
 
42
    /**
 
43
     * Flag set true when the thread starts.
 
44
     */
 
45
    bool flag;
 
46
 
 
47
    /**
 
48
     * The condition to use.
 
49
     */
 
50
    WConditionOneShot* c;
 
51
 
 
52
    /**
 
53
     * The thread.
 
54
     */
 
55
    void threadMain()
 
56
    {
 
57
        flag = true;
 
58
 
 
59
        // let the test's thread reach its "wait" call first
 
60
        boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
 
61
        c->notify();
 
62
    };
 
63
};
 
64
 
 
65
/**
 
66
 * Test WConditionOneShot
 
67
 */
 
68
class WConditionOneShotTest : public CxxTest::TestSuite
 
69
{
 
70
public:
 
71
 
 
72
    /**
 
73
     * An instantiation should never throw an exception, as well as tear down.
 
74
     */
 
75
    void testInstantiation( void )
 
76
    {
 
77
        WConditionOneShot* c = 0;
 
78
 
 
79
        TS_ASSERT_THROWS_NOTHING( c = new WConditionOneShot() );
 
80
        TS_ASSERT_THROWS_NOTHING( delete c );
 
81
    }
 
82
 
 
83
    /**
 
84
     * Test whether notification is working.
 
85
     */
 
86
    void testWaitNotify()
 
87
    {
 
88
        WConditionOneShot c;
 
89
        Callable t;
 
90
        t.flag = false;
 
91
        t.c = &c;
 
92
 
 
93
        // start a thread
 
94
        boost::thread thread = boost::thread( boost::bind( &Callable::threadMain, &t ) );
 
95
 
 
96
        // wait for condition
 
97
        c.wait();
 
98
 
 
99
        // since it is a one shot condition -> another wait will not cause a freeze
 
100
        c.wait();
 
101
 
 
102
        TS_ASSERT( t.flag );
 
103
 
 
104
        // notifying twice should not cause exceptions (boost::lock_error)
 
105
        TS_ASSERT_THROWS_NOTHING( c.notify() );
 
106
    }
 
107
};
 
108
 
 
109
#endif  // WCONDITIONONESHOT_TEST_H
 
110