~s-cecilio/lomse/master

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//--------------------------------------------------------------------------------------
//  This file is part of the Lomse library.
//  Copyright (c) 2010 Lomse project
//
//  Lomse is free software; you can redistribute it and/or modify it under the
//  terms of the GNU General Public License as published by the Free Software Foundation,
//  either version 3 of the License, or (at your option) any later version.
//
//  Lomse is distributed in the hope that it will be useful, but WITHOUT ANY
//  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
//  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with Lomse; if not, see <http://www.gnu.org/licenses/>.
//  
//  For any comment, suggestion or feature request, please contact the manager of
//  the project at cecilios@users.sourceforge.net
//
//-------------------------------------------------------------------------------------

#ifdef _LOMSE_DEBUG

#include <UnitTest++.h>
#include <sstream>
#include "lomse_config.h"

//classes related to these tests
#include "lomse_stack.h"


using namespace UnitTest;
using namespace std;
using namespace lomse;

typedef Stack<int*>  StackInt;

class StackTestFixture
{
public:

    StackTestFixture()     //SetUp fixture
    {
    }

    ~StackTestFixture()    //TearDown fixture
    {
    }
};

SUITE(StackTest)
{
    TEST_FIXTURE(StackTestFixture, StackIsEmpty)
    {
        StackInt stack;
        CHECK( stack.size() == 0 );
    }

    TEST_FIXTURE(StackTestFixture, StackPush)
    {
        StackInt stack;
        stack.push( new int(1) );
        CHECK( stack.size() == 1 );
        stack.push( new int(2) );
        CHECK( stack.size() == 2 );
    }

    TEST_FIXTURE(StackTestFixture, StackPop)
    {
        StackInt stack;
        stack.push( new int(1) );
        stack.push( new int(2) );
        CHECK( stack.size() == 2 );
        int* p = stack.pop();
        CHECK( *p == 2 );
        delete p;
        p = stack.pop();
        CHECK( *p == 1 );
        delete p;
        CHECK( stack.size() == 0 );
    }

    TEST_FIXTURE(StackTestFixture, StackGetItem)
    {
        StackInt stack;
        stack.push( new int(1) );
        stack.push( new int(2) );
        stack.push( new int(3) );
        stack.push( new int(4) );
        CHECK( stack.size() == 4 );
        CHECK( *(stack.get_item(2)) == 3 );
    }

    TEST_FIXTURE(StackTestFixture, StackPopWhenEmpty)
    {
        StackInt stack;
        int* p = stack.pop();
        CHECK( p == NULL );
    }

}

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

typedef UndoableStack<int*>  UndoStackInt;

class UndoableStackTestFixture
{
public:

    UndoableStackTestFixture()     //SetUp fixture
    {
    }

    ~UndoableStackTestFixture()    //TearDown fixture
    {
    }
};

SUITE(UndoableStackTest)
{
    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackIsEmpty)
    {
        UndoStackInt stack;
        CHECK( stack.size() == 0 );
    }

    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackPush)
    {
        UndoStackInt stack;
        stack.push( new int(1) );
        CHECK( stack.size() == 1 );
        stack.push( new int(2) );
        CHECK( stack.size() == 2 );
    }

    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackPop)
    {
        UndoStackInt stack;
        stack.push( new int(1) );
        stack.push( new int(2) );
        CHECK( stack.size() == 2 );
        int* p = stack.pop();
        CHECK( *p == 2 );
        p = stack.pop();
        CHECK( *p == 1 );
        CHECK( stack.size() == 0 );
    }

    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackGetItem)
    {
        UndoStackInt stack;
        stack.push( new int(1) );
        stack.push( new int(2) );
        stack.push( new int(3) );
        stack.push( new int(4) );
        CHECK( stack.size() == 4 );
        CHECK( *(stack.get_item(2)) == 3 );
    }

    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackPopWhenEmpty)
    {
        UndoStackInt stack;
        int* p = stack.pop();
        CHECK( p == NULL );
    }

    TEST_FIXTURE(UndoableStackTestFixture, UndoableStackUndoPop)
    {
        UndoStackInt stack;
        stack.push( new int(1) );
        stack.push( new int(2) );
        stack.push( new int(3) );
        stack.push( new int(4) );
        CHECK( stack.size() == 4 );
        CHECK( *(stack.get_item(2)) == 3 );
        CHECK( stack.history_size() == 0 );
        stack.pop();    //4
        CHECK( stack.history_size() == 1 );
        stack.pop();    //3
        CHECK( stack.history_size() == 2 );
        stack.undo_pop();   //adds 3
        CHECK( stack.history_size() == 1 );
        CHECK( stack.size() == 3 );
        CHECK( *(stack.get_item(2)) == 3 );
        stack.undo_pop();   //adds 4
        CHECK( stack.history_size() == 0 );
        CHECK( stack.size() == 4 );
        CHECK( *(stack.get_item(3)) == 4 );
    }

}

#endif  // _LOMSE_DEBUG