1470094fd0f1fa43891f2f6acb74dae96916a029
[platform/upstream/opencv.git] / modules / core / test / test_concatenation.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "test_precomp.hpp"
43
44 namespace opencv_test { namespace {
45
46 class Core_ConcatenationTest : public cvtest::BaseTest
47 {
48 public:
49     Core_ConcatenationTest(bool horizontal, bool firstEmpty, bool secondEmpty);
50 protected:
51     int prepare_test_case( int );
52     void run_func();
53     int validate_test_results( int );
54
55     Mat mat0x5;
56     Mat mat10x5;
57     Mat mat20x5;
58
59     Mat mat5x0;
60     Mat mat5x10;
61     Mat mat5x20;
62
63     Mat result;
64
65     bool horizontal;
66     bool firstEmpty;
67     bool secondEmpty;
68
69 private:
70     static bool areEqual(const Mat& m1, const Mat& m2);
71
72 };
73
74 Core_ConcatenationTest::Core_ConcatenationTest(bool horizontal_, bool firstEmpty_, bool secondEmpty_)
75     : horizontal(horizontal_)
76     , firstEmpty(firstEmpty_)
77     , secondEmpty(secondEmpty_)
78 {
79     test_case_count = 1;
80
81     mat0x5 = Mat::ones(0,5, CV_8U);
82     mat10x5 = Mat::ones(10,5, CV_8U);
83     mat20x5 = Mat::ones(20,5, CV_8U);
84
85     mat5x0 = Mat::ones(5,0, CV_8U);
86     mat5x10 = Mat::ones(5,10, CV_8U);
87     mat5x20 = Mat::ones(5,20, CV_8U);
88 }
89
90 int Core_ConcatenationTest::prepare_test_case( int test_case_idx )
91 {
92     cvtest::BaseTest::prepare_test_case( test_case_idx );
93     return 1;
94 }
95
96 void Core_ConcatenationTest::run_func()
97 {
98     if (horizontal)
99     {
100         cv::hconcat((firstEmpty ? mat5x0 : mat5x10),
101                     (secondEmpty ? mat5x0 : mat5x10),
102                     result);
103     } else {
104         cv::vconcat((firstEmpty ? mat0x5 : mat10x5),
105                     (secondEmpty ? mat0x5 : mat10x5),
106                     result);
107     }
108 }
109
110 int Core_ConcatenationTest::validate_test_results( int )
111 {
112     Mat expected;
113
114     if (firstEmpty && secondEmpty)
115         expected = (horizontal ? mat5x0 : mat0x5);
116     else if ((firstEmpty && !secondEmpty) || (!firstEmpty && secondEmpty))
117         expected = (horizontal ? mat5x10 : mat10x5);
118     else
119         expected = (horizontal ? mat5x20 : mat20x5);
120
121     if (areEqual(expected, result))
122     {
123         return cvtest::TS::OK;
124     } else
125     {
126         ts->printf( cvtest::TS::LOG, "Concatenation failed");
127         ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
128     }
129
130     return cvtest::TS::OK;
131 }
132
133 bool Core_ConcatenationTest::areEqual(const Mat &m1, const Mat &m2)
134 {
135     return m1.size() == m2.size()
136             && m1.type() == m2.type()
137             && countNonZero(m1 != m2) == 0;
138 }
139
140 TEST(Core_Concatenation, hconcat_empty_nonempty) { Core_ConcatenationTest test(true, true, false); test.safe_run(); }
141 TEST(Core_Concatenation, hconcat_nonempty_empty) { Core_ConcatenationTest test(true, false, true); test.safe_run(); }
142 TEST(Core_Concatenation, hconcat_empty_empty) { Core_ConcatenationTest test(true, true, true); test.safe_run(); }
143
144 TEST(Core_Concatenation, vconcat_empty_nonempty) { Core_ConcatenationTest test(false, true, false); test.safe_run(); }
145 TEST(Core_Concatenation, vconcat_nonempty_empty) { Core_ConcatenationTest test(false, false, true); test.safe_run(); }
146 TEST(Core_Concatenation, vconcat_empty_empty) { Core_ConcatenationTest test(false, true, true); test.safe_run(); }
147
148 }} // namespace