summaryrefslogtreecommitdiffstats
path: root/lib/libUPnP/Neptune/Source/Tests/Lists1/ListsTest1.cpp
blob: 22a8808dbf242261c8306ed41e3f51bb056c95b8 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*****************************************************************
|
|      Lists Test Program 1
|
|      (c) 2005-2006 Gilles Boccon-Gibod
|      Author: Gilles Boccon-Gibod (bok@bok.net)
|
****************************************************************/

/*----------------------------------------------------------------------
|       includes
+---------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Neptune.h"
#include "NptDebug.h"

/*----------------------------------------------------------------------
|       globals
+---------------------------------------------------------------------*/
static unsigned int A_Count = 0;

/*----------------------------------------------------------------------
|       types
+---------------------------------------------------------------------*/
class A {
public:
    A() : _a(0), _b(0), _c(&_a) {
        printf("A::A()\n");
        A_Count++;
    }
    A(int a, char b) : _a(a), _b(b), _c(&_a) {
        printf("A::A(%d, %d)\n", a, b);
        A_Count++;
    }
    A(const A& other) : _a(other._a), _b(other._b), _c(&_a) {
        printf("A::A(copy: a=%d, b=%d)\n", _a, _b);
        A_Count++;
    }
    ~A() {
        printf("A::~A(), a=%d, b=%d\n", _a, _b);
        A_Count--;
    }
    bool Check() { return _c == &_a; }
    bool operator==(const A& other) const {
        return _a == other._a && _b == other._b;
    }
    int _a;
    char _b;
    int* _c;
};

static int ApplyCounter = 0;
class Test1 {
public: 
    NPT_Result operator()(const A& a) const {
        ApplyCounter++;
        A aa(3,4);
        if (a == aa) return NPT_ERROR_OUT_OF_MEMORY;
        return NPT_SUCCESS;
    }
};

#define CHECK(x) {                                  \
    if (!(x)) {                                     \
        printf("TEST FAILED line %d\n", __LINE__);  \
        return 1;                                   \
    }                                               \
}

/*----------------------------------------------------------------------
|       compare
+---------------------------------------------------------------------*/
static int
compare(NPT_UInt32 a, NPT_UInt32 b)
{
    return (a==b)?0:(a<b)?-1:1;
}

/*----------------------------------------------------------------------
|       SortTest
+---------------------------------------------------------------------*/
static int
SortTest()
{
    for (unsigned int i=0; i<10000; i++) {
        unsigned int list_size = 1+NPT_System::GetRandomInteger()%100;
        NPT_List<NPT_UInt32> list;
        for (unsigned int j=0; j<list_size; j++) {
            list.Add(NPT_System::GetRandomInteger()%(2*list_size));
        }
        CHECK(NPT_SUCCEEDED(list.Sort(compare)));
        NPT_UInt32 value = 0;
        for (unsigned int j=0; j<list_size; j++) {
            NPT_UInt32 cursor = 0;
            list.Get(j, cursor);
            CHECK(cursor >= value);
            value = cursor;
        }
    }
    
    return 0;
}

/*----------------------------------------------------------------------
|       main
+---------------------------------------------------------------------*/
int
main(int /*argc*/, char** /*argv*/)
{
    if (SortTest()) return 1;
    
    NPT_List<A> a_list;
    a_list.Add(A(1,2));
    a_list.Add(A(2,3));
    a_list.Add(A(3,4));
    CHECK(a_list.GetItemCount() == 3);
    CHECK(a_list.Contains(A(2,3)));
    CHECK(!a_list.Contains(A(7,8)));
    A a;
    CHECK(NPT_SUCCEEDED(a_list.PopHead(a)));
    CHECK(a == A(1,2));
    CHECK(a_list.GetItemCount() == 2);
    CHECK(NPT_SUCCEEDED(a_list.Get(0, a)));
    CHECK(a == A(2,3));
    A* pa = NULL;
    CHECK(NPT_SUCCEEDED(a_list.Get(0,pa)));
    CHECK(pa != NULL);
    CHECK(*pa == A(2,3));
    CHECK(a_list.GetItem(1) == ++a_list.GetFirstItem());

    a_list.Clear();
    CHECK(a_list.GetItemCount() == 0);
    a_list.Insert(a_list.GetFirstItem(), A(7,9));
    CHECK(a_list.GetItemCount() == 1);
    CHECK(*a_list.GetFirstItem() == A(7,9));

    a_list.Add(A(1, 2));
    CHECK(a_list.GetItemCount() == 2);
    CHECK(A_Count == 3);
    CHECK(*a_list.GetFirstItem() == A(7,9));
    CHECK(*a_list.GetLastItem()  == A(1,2));
    
    a_list.Insert(a_list.GetLastItem(), A(3,4));
    CHECK(a_list.GetItemCount() == 3);
    CHECK(*a_list.GetLastItem() == A(1,2));

    // test ApplyUntil 
    ApplyCounter = 0;
    bool applied;
    NPT_Result result = a_list.ApplyUntil(Test1(), NPT_UntilResultEquals(NPT_ERROR_OUT_OF_MEMORY), &applied);
    CHECK(applied == true);
    CHECK(result == NPT_SUCCESS);
    CHECK(ApplyCounter == 2);

    ApplyCounter = 0;
    result = a_list.ApplyUntil(Test1(), NPT_UntilResultNotEquals(NPT_SUCCESS), &applied);
    CHECK(applied == true);
    CHECK(result == NPT_ERROR_OUT_OF_MEMORY);
    CHECK(ApplyCounter == 2);

    ApplyCounter = 0;
    result = a_list.ApplyUntil(Test1(), NPT_UntilResultEquals(NPT_FAILURE), &applied);
    CHECK(applied == false);
    CHECK(result == NPT_SUCCESS);
    CHECK(ApplyCounter == 3);

    a_list.Insert(NPT_List<A>::Iterator(NULL), A(3,4));
    CHECK(a_list.GetItemCount() == 4);
    CHECK(*a_list.GetLastItem() == A(3,4));

    a_list.Insert(a_list.GetFirstItem(), A(7,8));
    CHECK(a_list.GetItemCount() == 5);
    CHECK(*a_list.GetFirstItem() == A(7,8));

    a_list.Insert(a_list.GetItem(2), A(9,10));
    CHECK(a_list.GetItemCount() == 6);
    CHECK(*a_list.GetItem(2) == A(9,10));

    a_list.Erase(a_list.GetItem(1));
    CHECK(a_list.GetItemCount() == 5);
    CHECK(*a_list.GetItem(1) == A(9,10));
    CHECK(A_Count == 1+a_list.GetItemCount());

    NPT_List<int> i1_list;
    NPT_List<int> i2_list;
    CHECK(i1_list == i2_list);
    i1_list.Add(3);
    CHECK(i1_list != i2_list);
    CHECK(!(i1_list == i2_list));
    i2_list.Add(3);
    CHECK(i1_list == i2_list);
    i2_list.Add(4);
    CHECK(i1_list != i2_list);
    i1_list.Add(4);
    i1_list.Add(5);
    i2_list.Add(6);
    CHECK(i1_list != i2_list);
  

    // NPT_Stack test
    NPT_Stack<int> i_stack;
    int i=0;
    CHECK(NPT_FAILED(i_stack.Pop(i)));
    CHECK(NPT_FAILED(i_stack.Peek(i)));
    CHECK(NPT_SUCCEEDED(i_stack.Push(4)));
    CHECK(NPT_SUCCEEDED(i_stack.Push(5)));
    CHECK(NPT_SUCCEEDED(i_stack.Push(6)));
    CHECK(NPT_SUCCEEDED(i_stack.Pop(i)));
    CHECK(i == 6);
    CHECK(NPT_SUCCEEDED(i_stack.Peek(i)));
    CHECK(i == 5);
    CHECK(NPT_SUCCEEDED(i_stack.Pop(i)));
    CHECK(i == 5);
    CHECK(NPT_SUCCEEDED(i_stack.Pop(i)));
    CHECK(i == 4);

    return 0;
}