Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/test/test_misc.cpp
16337 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
#include "test_precomp.hpp"
5
6
namespace opencv_test { namespace {
7
8
TEST(Core_OutputArrayCreate, _1997)
9
{
10
struct local {
11
static void create(OutputArray arr, Size submatSize, int type)
12
{
13
int sizes[] = {submatSize.width, submatSize.height};
14
arr.create(sizeof(sizes)/sizeof(sizes[0]), sizes, type);
15
}
16
};
17
18
Mat mat(Size(512, 512), CV_8U);
19
Size submatSize = Size(256, 256);
20
21
ASSERT_NO_THROW(local::create( mat(Rect(Point(), submatSize)), submatSize, mat.type() ));
22
}
23
24
TEST(Core_SaturateCast, NegativeNotClipped)
25
{
26
double d = -1.0;
27
unsigned int val = cv::saturate_cast<unsigned int>(d);
28
29
ASSERT_EQ(0xffffffff, val);
30
}
31
32
template<typename T, typename U>
33
static double maxAbsDiff(const T &t, const U &u)
34
{
35
Mat_<double> d;
36
absdiff(t, u, d);
37
double ret;
38
minMaxLoc(d, NULL, &ret);
39
return ret;
40
}
41
42
TEST(Core_OutputArrayAssign, _Matxd_Matd)
43
{
44
Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
45
Matx23d actualx;
46
47
{
48
OutputArray oa(actualx);
49
oa.assign(expected);
50
}
51
52
Mat actual = (Mat) actualx;
53
54
EXPECT_LE(maxAbsDiff(expected, actual), 0.0);
55
}
56
57
TEST(Core_OutputArrayAssign, _Matxd_Matf)
58
{
59
Mat expected = (Mat_<float>(2,3) << 1, 2, 3, .1, .2, .3);
60
Matx23d actualx;
61
62
{
63
OutputArray oa(actualx);
64
oa.assign(expected);
65
}
66
67
Mat actual = (Mat) actualx;
68
69
EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
70
}
71
72
TEST(Core_OutputArrayAssign, _Matxf_Matd)
73
{
74
Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
75
Matx23f actualx;
76
77
{
78
OutputArray oa(actualx);
79
oa.assign(expected);
80
}
81
82
Mat actual = (Mat) actualx;
83
84
EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
85
}
86
87
TEST(Core_OutputArrayAssign, _Matxd_UMatd)
88
{
89
Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
90
UMat uexpected = expected.getUMat(ACCESS_READ);
91
Matx23d actualx;
92
93
{
94
OutputArray oa(actualx);
95
oa.assign(uexpected);
96
}
97
98
Mat actual = (Mat) actualx;
99
100
EXPECT_LE(maxAbsDiff(expected, actual), 0.0);
101
}
102
103
TEST(Core_OutputArrayAssign, _Matxd_UMatf)
104
{
105
Mat expected = (Mat_<float>(2,3) << 1, 2, 3, .1, .2, .3);
106
UMat uexpected = expected.getUMat(ACCESS_READ);
107
Matx23d actualx;
108
109
{
110
OutputArray oa(actualx);
111
oa.assign(uexpected);
112
}
113
114
Mat actual = (Mat) actualx;
115
116
EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
117
}
118
119
TEST(Core_OutputArrayAssign, _Matxf_UMatd)
120
{
121
Mat expected = (Mat_<double>(2,3) << 1, 2, 3, .1, .2, .3);
122
UMat uexpected = expected.getUMat(ACCESS_READ);
123
Matx23f actualx;
124
125
{
126
OutputArray oa(actualx);
127
oa.assign(uexpected);
128
}
129
130
Mat actual = (Mat) actualx;
131
132
EXPECT_LE(maxAbsDiff(expected, actual), FLT_EPSILON);
133
}
134
135
136
137
int fixedType_handler(OutputArray dst)
138
{
139
int type = CV_32FC2; // return points only {x, y}
140
if (dst.fixedType())
141
{
142
type = dst.type();
143
CV_Assert(type == CV_32FC2 || type == CV_32FC3); // allow points + confidence level: {x, y, confidence}
144
}
145
const int N = 100;
146
dst.create(Size(1, N), type);
147
Mat m = dst.getMat();
148
if (m.type() == CV_32FC2)
149
{
150
for (int i = 0; i < N; i++)
151
m.at<Vec2f>(i) = Vec2f((float)i, (float)(i*2));
152
}
153
else if (m.type() == CV_32FC3)
154
{
155
for (int i = 0; i < N; i++)
156
m.at<Vec3f>(i) = Vec3f((float)i, (float)(i*2), 1.0f / (i + 1));
157
}
158
else
159
{
160
CV_Assert(0 && "Internal error");
161
}
162
return CV_MAT_CN(type);
163
}
164
165
TEST(Core_OutputArray, FixedType)
166
{
167
Mat_<Vec2f> pointsOnly;
168
int num_pointsOnly = fixedType_handler(pointsOnly);
169
EXPECT_EQ(2, num_pointsOnly);
170
171
Mat_<Vec3f> pointsWithConfidence;
172
int num_pointsWithConfidence = fixedType_handler(pointsWithConfidence);
173
EXPECT_EQ(3, num_pointsWithConfidence);
174
175
Mat defaultResult;
176
int num_defaultResult = fixedType_handler(defaultResult);
177
EXPECT_EQ(2, num_defaultResult);
178
}
179
180
181
182
TEST(Core_String, find_last_of__with__empty_string)
183
{
184
cv::String s;
185
size_t p = s.find_last_of("q", 0);
186
// npos is not exported: EXPECT_EQ(cv::String::npos, p);
187
EXPECT_EQ(std::string::npos, p);
188
}
189
190
TEST(Core_String, end_method_regression)
191
{
192
cv::String old_string = "012345";
193
cv::String new_string(old_string.begin(), old_string.end());
194
EXPECT_EQ(6u, new_string.size());
195
}
196
197
TEST(Core_Copy, repeat_regression_8972)
198
{
199
Mat src = (Mat_<int>(1, 4) << 1, 2, 3, 4);
200
201
ASSERT_ANY_THROW({
202
repeat(src, 5, 1, src);
203
});
204
}
205
206
207
class ThrowErrorParallelLoopBody : public cv::ParallelLoopBody
208
{
209
public:
210
ThrowErrorParallelLoopBody(cv::Mat& dst, int i) : dst_(dst), i_(i) {}
211
~ThrowErrorParallelLoopBody() {}
212
void operator()(const cv::Range& r) const
213
{
214
for (int i = r.start; i < r.end; i++)
215
{
216
CV_Assert(i != i_);
217
dst_.row(i).setTo(1);
218
}
219
}
220
protected:
221
Mat dst_;
222
int i_;
223
};
224
225
TEST(Core_Parallel, propagate_exceptions)
226
{
227
Mat dst1(1000, 100, CV_8SC1, Scalar::all(0));
228
ASSERT_NO_THROW({
229
parallel_for_(cv::Range(0, dst1.rows), ThrowErrorParallelLoopBody(dst1, -1));
230
});
231
232
Mat dst2(1000, 100, CV_8SC1, Scalar::all(0));
233
ASSERT_THROW({
234
parallel_for_(cv::Range(0, dst2.rows), ThrowErrorParallelLoopBody(dst2, dst2.rows / 2));
235
}, cv::Exception);
236
}
237
238
TEST(Core_Version, consistency)
239
{
240
// this test verifies that OpenCV version loaded in runtime
241
// is the same this test has been built with
242
EXPECT_EQ(CV_VERSION_MAJOR, cv::getVersionMajor());
243
EXPECT_EQ(CV_VERSION_MINOR, cv::getVersionMinor());
244
EXPECT_EQ(CV_VERSION_REVISION, cv::getVersionRevision());
245
EXPECT_EQ(String(CV_VERSION), cv::getVersionString());
246
}
247
248
249
250
//
251
// Test core/check.hpp macros
252
//
253
254
void test_check_eq_1(int value_1, int value_2)
255
{
256
CV_CheckEQ(value_1, value_2, "Validation check failed");
257
}
258
TEST(Core_Check, testEQ_int_fail)
259
{
260
try
261
{
262
test_check_eq_1(123, 5678);
263
FAIL() << "Unreachable code called";
264
}
265
catch (const cv::Exception& e)
266
{
267
EXPECT_STREQ(e.err.c_str(),
268
"> Validation check failed (expected: 'value_1 == value_2'), where\n"
269
"> 'value_1' is 123\n"
270
"> must be equal to\n"
271
"> 'value_2' is 5678\n"
272
);
273
}
274
catch (const std::exception& e)
275
{
276
FAIL() << "Unexpected C++ exception: " << e.what();
277
}
278
catch (...)
279
{
280
FAIL() << "Unexpected unknown exception";
281
}
282
}
283
TEST(Core_Check, testEQ_int_pass)
284
{
285
EXPECT_NO_THROW(
286
{
287
test_check_eq_1(1234, 1234);
288
});
289
}
290
291
292
void test_check_eq_2(float value_1, float value_2)
293
{
294
CV_CheckEQ(value_1, value_2, "Validation check failed (float)");
295
}
296
TEST(Core_Check, testEQ_float_fail)
297
{
298
try
299
{
300
test_check_eq_2(1234.5f, 1234.55f);
301
FAIL() << "Unreachable code called";
302
}
303
catch (const cv::Exception& e)
304
{
305
EXPECT_STREQ(e.err.c_str(),
306
"> Validation check failed (float) (expected: 'value_1 == value_2'), where\n"
307
"> 'value_1' is 1234.5\n" // TODO Locale handling (use LC_ALL=C on Linux)
308
"> must be equal to\n"
309
"> 'value_2' is 1234.55\n"
310
);
311
}
312
catch (const std::exception& e)
313
{
314
FAIL() << "Unexpected C++ exception: " << e.what();
315
}
316
catch (...)
317
{
318
FAIL() << "Unexpected unknown exception";
319
}
320
}
321
TEST(Core_Check, testEQ_float_pass)
322
{
323
EXPECT_NO_THROW(
324
{
325
test_check_eq_2(1234.6f, 1234.6f);
326
});
327
}
328
329
330
void test_check_eq_3(double value_1, double value_2)
331
{
332
CV_CheckEQ(value_1, value_2, "Validation check failed (double)");
333
}
334
TEST(Core_Check, testEQ_double_fail)
335
{
336
try
337
{
338
test_check_eq_3(1234.5, 1234.56);
339
FAIL() << "Unreachable code called";
340
}
341
catch (const cv::Exception& e)
342
{
343
EXPECT_STREQ(e.err.c_str(),
344
"> Validation check failed (double) (expected: 'value_1 == value_2'), where\n"
345
"> 'value_1' is 1234.5\n" // TODO Locale handling (use LC_ALL=C on Linux)
346
"> must be equal to\n"
347
"> 'value_2' is 1234.56\n"
348
);
349
}
350
catch (const std::exception& e)
351
{
352
FAIL() << "Unexpected C++ exception: " << e.what();
353
}
354
catch (...)
355
{
356
FAIL() << "Unexpected unknown exception";
357
}
358
}
359
TEST(Core_Check, testEQ_double_pass)
360
{
361
EXPECT_NO_THROW(
362
{
363
test_check_eq_3(1234.0f, 1234.0f);
364
});
365
}
366
367
368
void test_check_ne_1(int value_1, int value_2)
369
{
370
CV_CheckNE(value_1, value_2, "Validation NE check failed");
371
}
372
TEST(Core_Check, testNE_int_fail)
373
{
374
try
375
{
376
test_check_ne_1(123, 123);
377
FAIL() << "Unreachable code called";
378
}
379
catch (const cv::Exception& e)
380
{
381
EXPECT_STREQ(e.err.c_str(),
382
"> Validation NE check failed (expected: 'value_1 != value_2'), where\n"
383
"> 'value_1' is 123\n"
384
"> must be not equal to\n"
385
"> 'value_2' is 123\n"
386
);
387
}
388
catch (const std::exception& e)
389
{
390
FAIL() << "Unexpected C++ exception: " << e.what();
391
}
392
catch (...)
393
{
394
FAIL() << "Unexpected unknown exception";
395
}
396
}
397
TEST(Core_Check, testNE_int_pass)
398
{
399
EXPECT_NO_THROW(
400
{
401
test_check_ne_1(123, 1234);
402
});
403
}
404
405
406
void test_check_le_1(int value_1, int value_2)
407
{
408
CV_CheckLE(value_1, value_2, "Validation LE check failed");
409
}
410
TEST(Core_Check, testLE_int_fail)
411
{
412
try
413
{
414
test_check_le_1(1234, 123);
415
FAIL() << "Unreachable code called";
416
}
417
catch (const cv::Exception& e)
418
{
419
EXPECT_STREQ(e.err.c_str(),
420
"> Validation LE check failed (expected: 'value_1 <= value_2'), where\n"
421
"> 'value_1' is 1234\n"
422
"> must be less than or equal to\n"
423
"> 'value_2' is 123\n"
424
);
425
}
426
catch (const std::exception& e)
427
{
428
FAIL() << "Unexpected C++ exception: " << e.what();
429
}
430
catch (...)
431
{
432
FAIL() << "Unexpected unknown exception";
433
}
434
}
435
TEST(Core_Check, testLE_int_pass)
436
{
437
EXPECT_NO_THROW(
438
{
439
test_check_le_1(1234, 1234);
440
});
441
EXPECT_NO_THROW(
442
{
443
test_check_le_1(123, 1234);
444
});
445
}
446
447
void test_check_lt_1(int value_1, int value_2)
448
{
449
CV_CheckLT(value_1, value_2, "Validation LT check failed");
450
}
451
TEST(Core_Check, testLT_int_fail)
452
{
453
try
454
{
455
test_check_lt_1(1234, 123);
456
FAIL() << "Unreachable code called";
457
}
458
catch (const cv::Exception& e)
459
{
460
EXPECT_STREQ(e.err.c_str(),
461
"> Validation LT check failed (expected: 'value_1 < value_2'), where\n"
462
"> 'value_1' is 1234\n"
463
"> must be less than\n"
464
"> 'value_2' is 123\n"
465
);
466
}
467
catch (const std::exception& e)
468
{
469
FAIL() << "Unexpected C++ exception: " << e.what();
470
}
471
catch (...)
472
{
473
FAIL() << "Unexpected unknown exception";
474
}
475
}
476
TEST(Core_Check, testLT_int_fail_eq)
477
{
478
try
479
{
480
test_check_lt_1(123, 123);
481
FAIL() << "Unreachable code called";
482
}
483
catch (const cv::Exception& e)
484
{
485
EXPECT_STREQ(e.err.c_str(),
486
"> Validation LT check failed (expected: 'value_1 < value_2'), where\n"
487
"> 'value_1' is 123\n"
488
"> must be less than\n"
489
"> 'value_2' is 123\n"
490
);
491
}
492
catch (const std::exception& e)
493
{
494
FAIL() << "Unexpected C++ exception: " << e.what();
495
}
496
catch (...)
497
{
498
FAIL() << "Unexpected unknown exception";
499
}
500
}
501
TEST(Core_Check, testLT_int_pass)
502
{
503
EXPECT_NO_THROW(
504
{
505
test_check_lt_1(123, 1234);
506
});
507
}
508
509
510
void test_check_ge_1(int value_1, int value_2)
511
{
512
CV_CheckGE(value_1, value_2, "Validation GE check failed");
513
}
514
TEST(Core_Check, testGE_int_fail)
515
{
516
try
517
{
518
test_check_ge_1(123, 1234);
519
FAIL() << "Unreachable code called";
520
}
521
catch (const cv::Exception& e)
522
{
523
EXPECT_STREQ(e.err.c_str(),
524
"> Validation GE check failed (expected: 'value_1 >= value_2'), where\n"
525
"> 'value_1' is 123\n"
526
"> must be greater than or equal to\n"
527
"> 'value_2' is 1234\n"
528
);
529
}
530
catch (const std::exception& e)
531
{
532
FAIL() << "Unexpected C++ exception: " << e.what();
533
}
534
catch (...)
535
{
536
FAIL() << "Unexpected unknown exception";
537
}
538
}
539
TEST(Core_Check, testGE_int_pass)
540
{
541
EXPECT_NO_THROW(
542
{
543
test_check_ge_1(1234, 1234);
544
});
545
EXPECT_NO_THROW(
546
{
547
test_check_ge_1(1234, 123);
548
});
549
}
550
551
void test_check_gt_1(int value_1, int value_2)
552
{
553
CV_CheckGT(value_1, value_2, "Validation GT check failed");
554
}
555
TEST(Core_Check, testGT_int_fail)
556
{
557
try
558
{
559
test_check_gt_1(123, 1234);
560
FAIL() << "Unreachable code called";
561
}
562
catch (const cv::Exception& e)
563
{
564
EXPECT_STREQ(e.err.c_str(),
565
"> Validation GT check failed (expected: 'value_1 > value_2'), where\n"
566
"> 'value_1' is 123\n"
567
"> must be greater than\n"
568
"> 'value_2' is 1234\n"
569
);
570
}
571
catch (const std::exception& e)
572
{
573
FAIL() << "Unexpected C++ exception: " << e.what();
574
}
575
catch (...)
576
{
577
FAIL() << "Unexpected unknown exception";
578
}
579
}
580
TEST(Core_Check, testGT_int_fail_eq)
581
{
582
try
583
{
584
test_check_gt_1(123, 123);
585
FAIL() << "Unreachable code called";
586
}
587
catch (const cv::Exception& e)
588
{
589
EXPECT_STREQ(e.err.c_str(),
590
"> Validation GT check failed (expected: 'value_1 > value_2'), where\n"
591
"> 'value_1' is 123\n"
592
"> must be greater than\n"
593
"> 'value_2' is 123\n"
594
);
595
}
596
catch (const std::exception& e)
597
{
598
FAIL() << "Unexpected C++ exception: " << e.what();
599
}
600
catch (...)
601
{
602
FAIL() << "Unexpected unknown exception";
603
}
604
}
605
TEST(Core_Check, testGT_int_pass)
606
{
607
EXPECT_NO_THROW(
608
{
609
test_check_gt_1(1234, 123);
610
});
611
}
612
613
614
void test_check_MatType_1(int src_type)
615
{
616
CV_CheckTypeEQ(src_type, CV_32FC1, "Unsupported source type");
617
}
618
TEST(Core_Check, testMatType_pass)
619
{
620
EXPECT_NO_THROW(
621
{
622
test_check_MatType_1(CV_MAKE_TYPE(CV_32F, 1));
623
});
624
}
625
TEST(Core_Check, testMatType_fail_1)
626
{
627
try
628
{
629
test_check_MatType_1(CV_8UC1);
630
FAIL() << "Unreachable code called";
631
}
632
catch (const cv::Exception& e)
633
{
634
EXPECT_STREQ(e.err.c_str(),
635
"> Unsupported source type (expected: 'src_type == CV_32FC1'), where\n"
636
"> 'src_type' is 0 (CV_8UC1)\n"
637
"> must be equal to\n"
638
"> 'CV_32FC1' is 5 (CV_32FC1)\n"
639
);
640
}
641
catch (const std::exception& e)
642
{
643
FAIL() << "Unexpected C++ exception: " << e.what();
644
}
645
catch (...)
646
{
647
FAIL() << "Unexpected unknown exception";
648
}
649
}
650
651
void test_check_MatType_2(int src_type)
652
{
653
CV_CheckType(src_type, src_type == CV_32FC1 || src_type == CV_32FC3, "Unsupported src");
654
}
655
TEST(Core_Check, testMatType_fail_2)
656
{
657
try
658
{
659
test_check_MatType_2(CV_8UC1);
660
FAIL() << "Unreachable code called";
661
}
662
catch (const cv::Exception& e)
663
{
664
EXPECT_STREQ(e.err.c_str(),
665
"> Unsupported src:\n"
666
"> 'src_type == CV_32FC1 || src_type == CV_32FC3'\n"
667
"> where\n> 'src_type' is 0 (CV_8UC1)\n"
668
);
669
}
670
catch (const std::exception& e)
671
{
672
FAIL() << "Unexpected C++ exception: " << e.what();
673
}
674
catch (...)
675
{
676
FAIL() << "Unexpected unknown exception";
677
}
678
}
679
680
void test_check_MatDepth_1(int src_depth)
681
{
682
CV_CheckDepthEQ(src_depth, CV_32F, "Unsupported source depth");
683
}
684
TEST(Core_Check, testMatDepth_pass)
685
{
686
EXPECT_NO_THROW(
687
{
688
test_check_MatDepth_1(CV_MAKE_TYPE(CV_32F, 1));
689
});
690
}
691
TEST(Core_Check, testMatDepth_fail_1)
692
{
693
try
694
{
695
test_check_MatDepth_1(CV_8U);
696
FAIL() << "Unreachable code called";
697
}
698
catch (const cv::Exception& e)
699
{
700
EXPECT_STREQ(e.err.c_str(),
701
"> Unsupported source depth (expected: 'src_depth == CV_32F'), where\n"
702
"> 'src_depth' is 0 (CV_8U)\n"
703
"> must be equal to\n"
704
"> 'CV_32F' is 5 (CV_32F)\n"
705
);
706
}
707
catch (const std::exception& e)
708
{
709
FAIL() << "Unexpected C++ exception: " << e.what();
710
}
711
catch (...)
712
{
713
FAIL() << "Unexpected unknown exception";
714
}
715
}
716
717
void test_check_MatDepth_2(int src_depth)
718
{
719
CV_CheckDepth(src_depth, src_depth == CV_32F || src_depth == CV_64F, "Unsupported src");
720
}
721
TEST(Core_Check, testMatDepth_fail_2)
722
{
723
try
724
{
725
test_check_MatDepth_2(CV_8U);
726
FAIL() << "Unreachable code called";
727
}
728
catch (const cv::Exception& e)
729
{
730
EXPECT_STREQ(e.err.c_str(),
731
"> Unsupported src:\n"
732
"> 'src_depth == CV_32F || src_depth == CV_64F'\n"
733
"> where\n> 'src_depth' is 0 (CV_8U)\n"
734
);
735
}
736
catch (const std::exception& e)
737
{
738
FAIL() << "Unexpected C++ exception: " << e.what();
739
}
740
catch (...)
741
{
742
FAIL() << "Unexpected unknown exception";
743
}
744
}
745
746
747
}} // namespace
748
749