File: atomic.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (172 lines) | stat: -rw-r--r-- 4,438 bytes parent folder | download | duplicates (4)
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
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/thread/atomic.cpp
// Purpose:     wxAtomic??? unit test
// Author:      Armel Asselin
// Created:     2006-12-14
// Copyright:   (c) 2006 Armel Asselin
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"


#ifndef WX_PRECOMP
#endif // WX_PRECOMP

#include "wx/atomic.h"
#include "wx/thread.h"
#include "wx/dynarray.h"
#include "wx/log.h"
#include "wx/vector.h"

typedef wxVector<wxThread*> wxArrayThread;

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

static const wxInt32 ITERATIONS_NUM = 10000;

// ----------------------------------------------------------------------------
// test helper thread
// ----------------------------------------------------------------------------

namespace
{
    enum ETestType
    {
        IncAndDecMixed,
        IncOnly,
        DecOnly
    };

    class MyThread : public wxThread
    {
    public:
        MyThread(wxAtomicInt &operateOn, ETestType testType) : wxThread(wxTHREAD_JOINABLE),
            m_operateOn(operateOn), m_testType(testType) {}

        // thread execution starts here
        virtual void *Entry() wxOVERRIDE;

    public:
        wxAtomicInt &m_operateOn;
        ETestType m_testType;
    };
} // anonymous namespace

// ----------------------------------------------------------------------------
// the tests themselves
// ----------------------------------------------------------------------------

TEST_CASE("Atomic::NoThread", "[atomic]")
{
    wxAtomicInt int1 = 0,
                int2 = 0;

    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
    {
        wxAtomicInc(int1);
        wxAtomicDec(int2);
    }

    CHECK( int1 == ITERATIONS_NUM );
    CHECK( int2 == -ITERATIONS_NUM );
}

TEST_CASE("Atomic::ReturnValue", "[atomic]")
{
    wxAtomicInt i(0);
    REQUIRE( wxAtomicInc(i) == 1 );
    REQUIRE( wxAtomicInc(i) == 2 );

    REQUIRE( wxAtomicDec(i) == 1 );
    REQUIRE( wxAtomicDec(i) == 0 );
}

TEST_CASE("Atomic::WithThreads", "[atomic]")
{
    int count wxDUMMY_INITIALIZE(0);
    ETestType testType wxDUMMY_INITIALIZE(DecOnly);

    SECTION( "2 threads using inc and dec") { count =  2; testType = IncAndDecMixed; }
    SECTION("10 threads using inc and dec") { count = 10; testType = IncAndDecMixed; }
    SECTION( "2 threads using inc or dec" ) { count =  2; testType = IncOnly; }
    SECTION("10 threads using inc or dec" ) { count = 10; testType = IncOnly; }

    wxAtomicInt    int1=0;

    wxArrayThread  threads;

    int i;
    for ( i = 0; i < count; ++i )
    {
        ETestType actualThreadType;
        switch(testType)
        {
        default:
            actualThreadType = testType;
            break;
        case IncOnly:
            actualThreadType =  (i&1)==0 ? IncOnly : DecOnly;
            break;
        }

        MyThread *thread = new MyThread(int1, actualThreadType);

        if ( thread->Create() != wxTHREAD_NO_ERROR )
        {
            wxLogError(wxT("Can't create thread!"));
            delete thread;
        }
        else
            threads.push_back(thread);
    }

    for ( i = 0; i < count; ++i )
    {
        threads[i]->Run();
    }


    for ( i = 0; i < count; ++i )
    {
        // each thread should return 0, else it detected some problem
        CHECK (threads[i]->Wait() == (wxThread::ExitCode)0);
        delete threads[i];
    }

    CHECK( int1 == 0 );
}

// ----------------------------------------------------------------------------

void *MyThread::Entry()
{
    wxInt32 negativeValuesSeen = 0;

    for ( wxInt32 i = 0; i < ITERATIONS_NUM; ++i )
    {
        switch ( m_testType )
        {
            case IncAndDecMixed:
                wxAtomicInc(m_operateOn);
                if ( wxAtomicDec(m_operateOn) < 0 )
                    ++negativeValuesSeen;
                break;

            case IncOnly:
                wxAtomicInc(m_operateOn);
                break;

            case DecOnly:
                wxAtomicDec(m_operateOn);
                break;
        }
    }

    return wxUIntToPtr(negativeValuesSeen);
}