array2d.cpp 16.9 KB
Newer Older
1
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
2
3
4
5
6
7
8
9
10
11
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/interfaces/enumerable.h>
#include <dlib/array2d.h>
#include "tester.h"
12
13
#include <dlib/pixel.h>
#include <dlib/image_transforms.h>
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

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

    logger dlog("test.array2d");

    template <
        typename array2d
        >
    void array2d_kernel_test (
    )
    /*!
        requires
            - array2d is an implementation of array2d/array2d_kernel_abstract.h 
              is instantiated with unsigned long 
        ensures
            - runs tests on array2d for compliance with the specs
    !*/
    {        
        srand(static_cast<unsigned int>(time(0)));

        array2d test,test2;

        long nc, nr;


43
        DLIB_TEST(get_rect(test).is_empty());
44
45

        enumerable<unsigned long>& e = test;
46
        DLIB_TEST(e.at_start() == true);
47
48


49
50
51
        DLIB_TEST(e.size() == 0);
        DLIB_TEST(e.at_start() == true);
        DLIB_TEST(e.current_element_valid() == false);
52

53
54
55
56
57
58
        DLIB_TEST (e.move_next() == false);
        DLIB_TEST (e.move_next() == false);
        DLIB_TEST (e.move_next() == false);
        DLIB_TEST (e.move_next() == false);
        DLIB_TEST (e.move_next() == false);
        DLIB_TEST (e.move_next() == false);
59
60


61
62
63
        DLIB_TEST(e.size() == 0);
        DLIB_TEST(e.at_start() == false);
        DLIB_TEST(e.current_element_valid() == false);
64
65
66
67


        e.reset();

68
69
70
        DLIB_TEST(e.size() == 0);
        DLIB_TEST(e.at_start() == true);
        DLIB_TEST(e.current_element_valid() == false);
71
72


73
        DLIB_TEST(get_rect(test).is_empty());
74
75
76



77
        DLIB_TEST(test.at_start() == true);
78
79


80
81
82
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
83

84
85
86
87
88
89
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
90
91


92
93
94
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == false);
        DLIB_TEST(test.current_element_valid() == false);
95
96
97
98


        test.reset();

99
100
101
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
102
103
104
105

        test.clear();


106
        DLIB_TEST(test.at_start() == true);
107
108


109
110
111
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
112

113
114
115
116
117
118
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
119
120
121
122


        test.set_size(0,0);

123
        DLIB_TEST(get_rect(test).is_empty());
124

125
        DLIB_TEST(test.at_start() == true);
126
127


128
129
130
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
131

132
133
134
135
136
137
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
        DLIB_TEST (test.move_next() == false);
138
139

        swap(test,test2);
140
141
142
143
        DLIB_TEST (test2.at_start() == false);
        DLIB_TEST (test2.current_element_valid() == false);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
144
        swap(test,test2);
145
146
        DLIB_TEST(test2.at_start() == true);
        DLIB_TEST(test2.current_element_valid() == false);
147
148


149
150
151
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == false);
        DLIB_TEST(test.current_element_valid() == false);
152
153
154
155


        test.reset();

156
157
158
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.at_start() == true);
        DLIB_TEST(test.current_element_valid() == false);
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




        for (int j = 0; j < 30; ++j)
        {
            test2.clear();
            switch (j)
            {
                case 0:
                    nc = 10;
                    nr = 11;
                    break;
                case 1:
                    nc = 1;
                    nr = 1;
                    break;
                case 2:
                    nc = 100;
                    nr = 1;
                    break;
                case 3:
                    nc = 1;
                    nr = 100;
                    break;
                default:
                    nc = ::rand()%100 + 1;
                    nr = ::rand()%100 + 1;
                    break;
            }

            test.set_size(nr,nc);

192
193
194
195
196
            DLIB_TEST(get_rect(test).left() == 0);
            DLIB_TEST(get_rect(test).top() == 0);
            DLIB_TEST(get_rect(test).right() == nc-1);
            DLIB_TEST(get_rect(test).bottom() == nr-1);

197
198
199
200
201
            DLIB_TEST(test.size() == static_cast<unsigned long>(nc*nr));
            DLIB_TEST(test.nr() == nr);
            DLIB_TEST(test.nc() == nc);
            DLIB_TEST(test.at_start() == true);
            DLIB_TEST(test.current_element_valid() == false);
202
203
204
205

            unsigned long i = 0;
            while (test.move_next())
            {
206
207
                DLIB_TEST(test.current_element_valid() == true);
                DLIB_TEST(test.at_start() == false);
208
                test.element() = i;
209
                DLIB_TEST(const_cast<const array2d&>(test).element() == i);
210
211
                ++i;
            }
212
213
            DLIB_TEST(i == test.size());
            DLIB_TEST(test.current_element_valid() == false);
214

215
216
217
218
            DLIB_TEST(test.nr() == nr);
            DLIB_TEST(test.nc() == nc);
            DLIB_TEST(test.at_start() == false);
            DLIB_TEST(test.size() == static_cast<unsigned long>(nc*nr));
219
220
221
222
223
224

            i = 0;
            for (long row = 0; row < test.nr(); ++row)
            {
                for (long col = 0; col < test.nc(); ++col)
                {
225
                    DLIB_TEST_MSG(test[row][col] == i,
226
227
228
229
                                 "\n\trow: " << row <<
                                 "\n\tcol: " << col <<
                                 "\n\ti:   " << i <<
                                 "\n\ttest[row][col]: " << test[row][col]);
230
231
                    DLIB_TEST(test[row].nc() == test.nc());
                    DLIB_TEST(test.current_element_valid() == false);
232

233
234
235
236
                    DLIB_TEST(test.nr() == nr);
                    DLIB_TEST(test.nc() == nc);
                    DLIB_TEST(test.at_start() == false);
                    DLIB_TEST(test.size() == static_cast<unsigned long>(nc*nr));
237
238
239
240
241
242
243
244
245
                    ++i;
                }
            }

            test.reset();

            i = 0;
            while (test.move_next())
            {
246
                DLIB_TEST(test.element() == i);
247
                ++i;
248
249
                DLIB_TEST(test.current_element_valid() == true);
                DLIB_TEST(test.at_start() == false);
250
            }
251
            DLIB_TEST(i == test.size());
252
253
254
255
256
257
258
259

            test.reset();




            swap(test,test2);

260
261
262
263
264
            DLIB_TEST(test2.size() == static_cast<unsigned long>(nc*nr));
            DLIB_TEST(test2.nr() == nr);
            DLIB_TEST(test2.nc() == nc);
            DLIB_TEST(test2.at_start() == true);
            DLIB_TEST(test2.current_element_valid() == false);
265
266
267
268

            i = 0;
            while (test2.move_next())
            {
269
270
                DLIB_TEST(test2.current_element_valid() == true);
                DLIB_TEST(test2.at_start() == false);
271
272
273
                test2.element() = i;
                ++i;
            }
274
275
            DLIB_TEST(i == test2.size());
            DLIB_TEST(test2.current_element_valid() == false);
276

277
278
279
280
281
282
            DLIB_TEST(test2.nr() == nr);
            DLIB_TEST(test2.nr() == test2.nr());
            DLIB_TEST(test2.nc() == nc);
            DLIB_TEST(test2.nc() == test2.nc());
            DLIB_TEST(test2.at_start() == false);
            DLIB_TEST(test2.size() == static_cast<unsigned long>(nc*nr));
283
284
285
286
287
288

            i = 0;
            for (long row = 0; row < test2.nr(); ++row)
            {
                for (long col = 0; col < test2.nc(); ++col)
                {
289
290
291
292
293
294
295
296
297
298
299
                    DLIB_TEST(test2[row][col] == i);
                    DLIB_TEST(const_cast<const array2d&>(test2)[row][col] == i);
                    DLIB_TEST(test2[row].nc() == test2.nc());
                    DLIB_TEST(test2.current_element_valid() == false);

                    DLIB_TEST(test2.nr() == nr);
                    DLIB_TEST(test2.nr() == test2.nr());
                    DLIB_TEST(test2.nc() == nc);
                    DLIB_TEST(test2.nc() == test2.nc());
                    DLIB_TEST(test2.at_start() == false);
                    DLIB_TEST(test2.size() == static_cast<unsigned long>(nc*nr));
300
301
302
303
304
305
306
307
308
                    ++i;
                }
            }

            test2.reset();

            i = 0;
            while (test2.move_next())
            {
309
310
                DLIB_TEST(test2.element() == i);
                DLIB_TEST(const_cast<const array2d&>(test2).element() == i);
311
                ++i;
312
313
                DLIB_TEST(test2.current_element_valid() == true);
                DLIB_TEST(test2.at_start() == false);
314
            }
315
            DLIB_TEST(i == test2.size());
316
317
318


            test2.clear();
319
320
321
322
323
            DLIB_TEST(test2.size() == 0);
            DLIB_TEST(test2.nr() == 0);
            DLIB_TEST(test2.nc() == 0);
            DLIB_TEST(test2.current_element_valid() == false);
            DLIB_TEST(test2.at_start() == true);
324

325
326
327
            DLIB_TEST(test.size() == 0);
            DLIB_TEST(test.nc() == 0);
            DLIB_TEST(test.nr() == 0);
328
329

            test.set_size(nr,nc);
330
331
332
            DLIB_TEST(test.size() == static_cast<unsigned long>(nc*nr));
            DLIB_TEST(test.nc() == nc);
            DLIB_TEST(test.nr() == nr);
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347



        }





        // test the serialization
        istringstream sin;
        ostringstream sout;
        test.clear();
        test2.clear();

348
349
350
351
352
353
        DLIB_TEST(test.size() == 0);
        DLIB_TEST(test.nc() == 0);
        DLIB_TEST(test.nr() == 0);
        DLIB_TEST(test2.size() == 0);
        DLIB_TEST(test2.nc() == 0);
        DLIB_TEST(test2.nr() == 0);
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

        test.set_size(10,10);

        for (long row = 0; row < test.nr(); ++row)
        {
            for (long col = 0; col < test.nc(); ++col)
            {
                test[row][col] = row*col;
            }
        }

        serialize(test,sout);
        sin.str(sout.str());
        deserialize(test2,sin);

369
370
371
372
373
374
        DLIB_TEST(test2.size() == test.size());
        DLIB_TEST(test2.nc() == test.nc());
        DLIB_TEST(test2.nr() == test.nr());
        DLIB_TEST(test2.size() == 100);
        DLIB_TEST(test2.nc() == 10);
        DLIB_TEST(test2.nr() == 10);
375
376
377
378
379
380


        for (long row = 0; row < test.nr(); ++row)
        {
            for (long col = 0; col < test.nc(); ++col)
            {
381
382
                DLIB_TEST(test[row][col] == static_cast<unsigned long>(row*col));
                DLIB_TEST(test2[row][col] == static_cast<unsigned long>(row*col));
383
384
385
386
387
388
389
390
391
            }
        }






        test.set_size(10,11);
392
393
        DLIB_TEST(test.nr() == 10);
        DLIB_TEST(test.nc() == 11);
394
        test.set_size(0,0);
395
396
        DLIB_TEST(test.nr() == 0);
        DLIB_TEST(test.nc() == 0);
397
398
399

    }

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
    void test_serialization()
    {
        // Do these tests because there are overloads of the serialize routines
        // specifically for these types of pixel (except for unsigned short,  
        // we do that because you can never have too many tests).
        {
            array2d<rgb_alpha_pixel> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1].red = 9;
            img[1][1].green = 8;
            img[1][1].blue = 7;
            img[1][1].alpha = 3;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c].red == img2[r][c].red);
                    DLIB_TEST(img[r][c].green == img2[r][c].green);
                    DLIB_TEST(img[r][c].blue == img2[r][c].blue);
                    DLIB_TEST(img[r][c].alpha == img2[r][c].alpha);
                }
            }
        }
        {
            array2d<hsi_pixel> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1].h = 9;
            img[1][1].s = 2;
            img[1][1].i = 3;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c].h == img2[r][c].h);
                    DLIB_TEST(img[r][c].s == img2[r][c].s);
                    DLIB_TEST(img[r][c].i == img2[r][c].i);
                }
            }
        }
        {
            array2d<bgr_pixel> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1].red = 1;
            img[1][1].green = 2;
            img[1][1].blue = 3;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c].red == img2[r][c].red);
                    DLIB_TEST(img[r][c].green == img2[r][c].green);
                    DLIB_TEST(img[r][c].blue == img2[r][c].blue);
                }
            }
        }
        {
            array2d<rgb_pixel> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1].red = 1;
            img[1][1].green = 2;
            img[1][1].blue = 3;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c].red == img2[r][c].red);
                    DLIB_TEST(img[r][c].green == img2[r][c].green);
                    DLIB_TEST(img[r][c].blue == img2[r][c].blue);
                }
            }
        }
        {
            array2d<unsigned short> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1] = 9;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c] == img2[r][c]);
                }
            }
        }
        {
            array2d<unsigned char> img, img2;
            img.set_size(3,2);
            assign_all_pixels(img, 5);
            img[1][1] = 9;
            ostringstream sout;
            serialize(img, sout);
            istringstream sin(sout.str());
            deserialize(img2, sin);

            DLIB_TEST(img2.nr() == 3);
            DLIB_TEST(img2.nc() == 2);

            for (long r = 0; r < img.nr(); ++r)
            {
                for (long c = 0; c < img.nc(); ++c)
                {
                    DLIB_TEST(img[r][c] == img2[r][c]);
                }
            }
548
549

            DLIB_TEST((char*)&img[0][0] + img.width_step() == (char*)&img[1][0]);
550
        }
Davis King's avatar
Davis King committed
551
552
553
554

        COMPILE_TIME_ASSERT(is_array2d<array2d<unsigned char> >::value == true);
        COMPILE_TIME_ASSERT(is_array2d<array2d<float> >::value == true);
        COMPILE_TIME_ASSERT(is_array2d<float>::value == false);
555
556
    }

557
558
559
560
561
562
563
564
565
566
567
568
569
570

    class array2d_tester : public tester
    {
    public:
        array2d_tester (
        ) :
            tester ("test_array2d",
                    "Runs tests on the array2d component.")
        {}

        void perform_test (
        )
        {
            dlog << LINFO << "testing kernel_1a";
571
572
573
            array2d_kernel_test<array2d<unsigned long> >();
            print_spinner();
            test_serialization();
574
575
576
577
578
579
580
            print_spinner();
        }
    } a;

}