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
44using namespace cv;
45using namespace std;
46
47class Core_ConcatenationTest : public cvtest::BaseTest
48{
49public:
50    Core_ConcatenationTest(bool horizontal, bool firstEmpty, bool secondEmpty);
51protected:
52    int prepare_test_case( int );
53    void run_func();
54    int validate_test_results( int );
55
56    Mat mat0x5;
57    Mat mat10x5;
58    Mat mat20x5;
59
60    Mat mat5x0;
61    Mat mat5x10;
62    Mat mat5x20;
63
64    Mat result;
65
66    bool horizontal;
67    bool firstEmpty;
68    bool secondEmpty;
69
70private:
71    static bool areEqual(const Mat& m1, const Mat& m2);
72
73};
74
75Core_ConcatenationTest::Core_ConcatenationTest(bool horizontal_, bool firstEmpty_, bool secondEmpty_)
76    : horizontal(horizontal_)
77    , firstEmpty(firstEmpty_)
78    , secondEmpty(secondEmpty_)
79{
80    test_case_count = 1;
81
82    mat0x5 = Mat::ones(0,5, CV_8U);
83    mat10x5 = Mat::ones(10,5, CV_8U);
84    mat20x5 = Mat::ones(20,5, CV_8U);
85
86    mat5x0 = Mat::ones(5,0, CV_8U);
87    mat5x10 = Mat::ones(5,10, CV_8U);
88    mat5x20 = Mat::ones(5,20, CV_8U);
89}
90
91int Core_ConcatenationTest::prepare_test_case( int test_case_idx )
92{
93    cvtest::BaseTest::prepare_test_case( test_case_idx );
94    return 1;
95}
96
97void Core_ConcatenationTest::run_func()
98{
99    if (horizontal)
100    {
101        cv::hconcat((firstEmpty ? mat5x0 : mat5x10),
102                    (secondEmpty ? mat5x0 : mat5x10),
103                    result);
104    } else {
105        cv::vconcat((firstEmpty ? mat0x5 : mat10x5),
106                    (secondEmpty ? mat0x5 : mat10x5),
107                    result);
108    }
109}
110
111int Core_ConcatenationTest::validate_test_results( int )
112{
113    Mat expected;
114
115    if (firstEmpty && secondEmpty)
116        expected = (horizontal ? mat5x0 : mat0x5);
117    else if ((firstEmpty && !secondEmpty) || (!firstEmpty && secondEmpty))
118        expected = (horizontal ? mat5x10 : mat10x5);
119    else
120        expected = (horizontal ? mat5x20 : mat20x5);
121
122    if (areEqual(expected, result))
123    {
124        return cvtest::TS::OK;
125    } else
126    {
127        ts->printf( cvtest::TS::LOG, "Concatenation failed");
128        ts->set_failed_test_info( cvtest::TS::FAIL_MISMATCH );
129    }
130
131    return cvtest::TS::OK;
132}
133
134bool Core_ConcatenationTest::areEqual(const Mat &m1, const Mat &m2)
135{
136    return m1.size() == m2.size()
137            && m1.type() == m2.type()
138            && countNonZero(m1 != m2) == 0;
139}
140
141TEST(Core_Concatenation, hconcat_empty_nonempty) { Core_ConcatenationTest test(true, true, false); test.safe_run(); }
142TEST(Core_Concatenation, hconcat_nonempty_empty) { Core_ConcatenationTest test(true, false, true); test.safe_run(); }
143TEST(Core_Concatenation, hconcat_empty_empty) { Core_ConcatenationTest test(true, true, true); test.safe_run(); }
144
145TEST(Core_Concatenation, vconcat_empty_nonempty) { Core_ConcatenationTest test(false, true, false); test.safe_run(); }
146TEST(Core_Concatenation, vconcat_nonempty_empty) { Core_ConcatenationTest test(false, false, true); test.safe_run(); }
147TEST(Core_Concatenation, vconcat_empty_empty) { Core_ConcatenationTest test(false, true, true); test.safe_run(); }
148