summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/sequence.cpp
blob: ffa6efdb8c085a0c8e642e968d5a701da7c4fed2 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright (C) 2004  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>

#include <dlib/sequence.h>
#include "tester.h"

namespace  
{

    using namespace test;
    using namespace std;
    using namespace dlib;

    logger dlog("test.sequence");

    template <
        typename seq
        >
    void sequence_sort_test (
    )
    /*!
        requires
            - seq is an implementation of sequence/sequence_sort_aseqract.h is instantiated 
              with int
        ensures
            - runs tests on seq for compliance with the specs 
    !*/
    {        


        srand(static_cast<unsigned int>(time(0)));


        print_spinner();





        {
            // this test is to make sure that jumping around via
            // operator[] doesn't corrupt the object

            seq a;

            for (int i = 0; i < 100; ++i)
            {
                int x = i;
                a.add(a.size(),x);
            }


            int x = 0;

            for (int i = 0; i < (int)a.size(); ++i)
            {
                DLIB_TEST_MSG(a[i] >= i,"1");
                // cout << a[i] << endl;
            }

            for (unsigned long i = 0; i < a.size(); ++i)
            {
                for (unsigned long j = i+1; j < a.size(); ++j)
                {
                    if ((a[j]+a[i])%3 ==0)
                    {                    
                        a.remove(j,x);
                        --j;
                    }
                }
            }

            //cout << endl;

            for (int i = 0; i < (int)a.size(); ++i)
            {
                //   cout << a[i] << endl;
                DLIB_TEST_MSG(a[i] >= i,"2");               
            }

        }







        seq test, test2;

        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);

        enumerable<int>& e = test;

        DLIB_TEST(e.at_start() == true);
        DLIB_TEST(e.current_element_valid() == false);


        for (int g = 0; g < 5; ++g)
        {
            test.clear();
            test2.clear();
            DLIB_TEST(test.size() == 0);
            DLIB_TEST(test.at_start() == true);
            DLIB_TEST(test.current_element_valid() == false);
            DLIB_TEST(e.at_start() == true);
            DLIB_TEST(e.current_element_valid() == false);

            DLIB_TEST(e.move_next() == false);
            DLIB_TEST(e.current_element_valid() == false);
            DLIB_TEST(e.at_start() == false);
            DLIB_TEST(test.at_start() == false);
            swap(test,test2);
            DLIB_TEST(test.at_start() == true);
            test.clear();
            test2.clear();

            int a;


            for (int i = 0; i < 100; ++i)
            {
                a = i;
                test.add(i,a);
            }

            DLIB_TEST(test.size() == 100);

            for (int i = 0; i < static_cast<int>(test.size()); ++i)
            {       
                DLIB_TEST(test[i] == i);
            }   

            swap(test,test2);

            a = 0;
            DLIB_TEST(test2.at_start() == true);
            while(test2.move_next())
            {
                DLIB_TEST(test2.at_start() == false);
                DLIB_TEST(test2.current_element_valid() == true);
                DLIB_TEST(test2.element() == a);
                ++a;
            }

            DLIB_TEST(test2.at_start() == false);
            DLIB_TEST(test2.current_element_valid() == false);

            test2.reset();

            DLIB_TEST(test2.at_start() == true);
            DLIB_TEST(test2.current_element_valid() == false);

            a = 0;
            while(test2.move_next())
            {
                DLIB_TEST(test2.at_start() == false);
                DLIB_TEST(test2.current_element_valid() == true);
                DLIB_TEST(test2.element() == a);
                ++a;
            }





            for (int i = 0; i < 1000; ++i)
            {
                a = ::rand();
                test.add(0,a);
            }
            DLIB_TEST(test.size() == 1000);

            test.sort();


            for (unsigned long i = 0; i < test.size()-1; ++i)
            {
                DLIB_TEST(test[i] <= test[i+1]);    
            }

            a = 0;
            while(test.move_next())
            {
                DLIB_TEST(a <= test.element());
                a = test.element();
            }


            test.clear();
            test2.clear();

            DLIB_TEST(test.size() == 0);
            DLIB_TEST(test2.size() == 0);

            for (int i = 0; i < 100; ++i)
            {
                a = i;
                test.add(i,a);
            }

            for (int i = 100; i < 200; ++i)
            {
                a = i;
                test.add(i,a);
            }

            test.cat(test2);
            DLIB_TEST(test.size() == 200);
            DLIB_TEST(test2.size() == 0);


            // serialize the state of test, then clear test, then
            // load the state back into test.
            ostringstream sout;
            serialize(test,sout);
            DLIB_TEST(test.at_start() == true);
            istringstream sin(sout.str());
            test.clear();
            deserialize(test,sin);


            for (int i = 0; i < 200; ++i)
            {
                DLIB_TEST(test[i] == i);
            }

            a = 0;
            while (test.move_next())
            {
                DLIB_TEST(test.element() == a);
                DLIB_TEST(test[0]==0);
                ++a;
            }

            DLIB_TEST(a == 200);

            DLIB_TEST(test[9] == 9);
            test.remove(9,a);
            DLIB_TEST(a == 9);
            DLIB_TEST(test[9] == 10);
            DLIB_TEST(test.size() == 199);

            test.remove(0,a);
            DLIB_TEST(test[0] == 1);
            DLIB_TEST(test.size() == 198);
            DLIB_TEST(a == 0);
            DLIB_TEST(test[9] == 11);
            DLIB_TEST(test[20] == 22);




        }

        {
            test.clear();
            for (int i = 0; i < 100; ++i)
            {
                int a = 3;
                test.add(0,a);
            }
            DLIB_TEST(test.size() == 100);
            remover<int>& go = test;
            for (int i = 0; i < 100; ++i)
            {
                int a = 9;
                go.remove_any(a);
                DLIB_TEST(a == 3);
            }
            DLIB_TEST(go.size() == 0);
        }


    }




    class sequence_tester : public tester
    {
    public:
        sequence_tester (
        ) :
            tester ("test_sequence",
                    "Runs tests on the sequence component.")
        {}

        void perform_test (
        )
        {
            dlog << LINFO << "testing sort_1a";
            sequence_sort_test<sequence<int>::sort_1a>  ();
            dlog << LINFO << "testing sort_1a_c";
            sequence_sort_test<sequence<int>::sort_1a_c>();
            dlog << LINFO << "testing sort_2a";
            sequence_sort_test<sequence<int>::sort_2a>  ();
            dlog << LINFO << "testing sort_2a_c";
            sequence_sort_test<sequence<int>::sort_2a_c>();
        }
    } a;

}