From fe29080d59cb86d4dd06b2aa6b0b44e969130cdd Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 22 Nov 2016 19:49:55 +0300 Subject: [PATCH 1/3] java: skip test in case of missed classes from opencv_contrib --- .../src/org/opencv/test/OpenCVTestCase.java | 39 ++++++++++++++++++- modules/java/pure_test/build.xml | 2 +- .../src/org/opencv/test/OpenCVTestCase.java | 39 ++++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java index 54bb79ec1e..70d0744948 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -29,6 +29,11 @@ import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; public class OpenCVTestCase extends TestCase { + + public static class TestSkipException extends RuntimeException { + public TestSkipException() {} + } + //change to 'true' to unblock fail on fail("Not yet implemented") public static final boolean passNYI = true; @@ -188,12 +193,40 @@ public class OpenCVTestCase extends TestCase { protected void runTest() throws Throwable { // Do nothing if the precondition does not hold. if (isTestCaseEnabled) { - super.runTest(); + try { + super.runTest(); + } catch (TestSkipException ex) { + Log.w(TAG, "Test case \"" + this.getClass().getName() + "\" skipped!"); + assertTrue(true); + } } else { Log.e(TAG, "Test case \"" + this.getClass().getName() + "\" disabled!"); } } + public void runBare() throws Throwable { + Throwable exception = null; + try { + setUp(); + } catch (TestSkipException ex) { + Log.w(TAG, "Test case \"" + this.getClass().getName() + "\" skipped!"); + assertTrue(true); + return; + } + try { + runTest(); + } catch (Throwable running) { + exception = running; + } finally { + try { + tearDown(); + } catch (Throwable tearingDown) { + if (exception == null) exception = tearingDown; + } + } + if (exception != null) throw exception; + } + protected Mat getMat(int type, double... vals) { return new Mat(matSize, matSize, type, new Scalar(vals)); @@ -497,6 +530,10 @@ public class OpenCVTestCase extends TestCase { } } catch(Exception ex) { + if (cname.startsWith(XFEATURES2D)) + { + throw new TestSkipException(); + } message = TAG + " :: " + "could not instantiate " + cname + "! Exception: " + ex.getMessage(); } diff --git a/modules/java/pure_test/build.xml b/modules/java/pure_test/build.xml index 4b25a3c1e3..dac4d4b455 100644 --- a/modules/java/pure_test/build.xml +++ b/modules/java/pure_test/build.xml @@ -37,7 +37,7 @@ - + diff --git a/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java index 98b1448fe3..400338ddd4 100644 --- a/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java @@ -27,6 +27,11 @@ import org.opencv.core.KeyPoint; import org.opencv.imgcodecs.Imgcodecs; public class OpenCVTestCase extends TestCase { + + public static class TestSkipException extends RuntimeException { + public TestSkipException() {} + } + //change to 'true' to unblock fail on fail("Not yet implemented") public static final boolean passNYI = true; @@ -214,12 +219,40 @@ public class OpenCVTestCase extends TestCase { protected void runTest() throws Throwable { // Do nothing if the precondition does not hold. if (isTestCaseEnabled) { - super.runTest(); + try { + super.runTest(); + } catch (TestSkipException ex) { + OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" skipped!"); + assertTrue(true); + } } else { OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" disabled!"); } } + public void runBare() throws Throwable { + Throwable exception = null; + try { + setUp(); + } catch (TestSkipException ex) { + OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" skipped!"); + assertTrue(true); + return; + } + try { + runTest(); + } catch (Throwable running) { + exception = running; + } finally { + try { + tearDown(); + } catch (Throwable tearingDown) { + if (exception == null) exception = tearingDown; + } + } + if (exception != null) throw exception; + } + protected Mat getMat(int type, double... vals) { return new Mat(matSize, matSize, type, new Scalar(vals)); @@ -523,6 +556,10 @@ public class OpenCVTestCase extends TestCase { } } catch(Exception ex) { + if (cname.startsWith(XFEATURES2D)) + { + throw new TestSkipException(); + } message = TAG + " :: " + "could not instantiate " + cname + "! Exception: " + ex.getMessage(); } From 8019498c6ed26948e72178e877c9125b26a4bfcb Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 22 Nov 2016 20:08:27 +0300 Subject: [PATCH 2/3] java tests fixes --- modules/core/misc/java/test/CoreTest.java | 2 +- modules/imgproc/misc/java/test/ImgprocTest.java | 6 +++--- .../android_test/src/org/opencv/test/OpenCVTestCase.java | 8 ++++++-- .../pure_test/src/org/opencv/test/OpenCVTestCase.java | 8 ++++++-- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/modules/core/misc/java/test/CoreTest.java b/modules/core/misc/java/test/CoreTest.java index 9c60fd5652..3187e7cd2d 100644 --- a/modules/core/misc/java/test/CoreTest.java +++ b/modules/core/misc/java/test/CoreTest.java @@ -1777,7 +1777,7 @@ public class CoreTest extends OpenCVTestCase { }; Mat roots = new Mat(); - assertEquals(0.0, Core.solvePoly(coeffs, roots)); + assertGE(1e-6, Math.abs(Core.solvePoly(coeffs, roots))); truth = new Mat(3, 1, CvType.CV_32FC2) { { diff --git a/modules/imgproc/misc/java/test/ImgprocTest.java b/modules/imgproc/misc/java/test/ImgprocTest.java index e619d016c6..8a5ce2b727 100644 --- a/modules/imgproc/misc/java/test/ImgprocTest.java +++ b/modules/imgproc/misc/java/test/ImgprocTest.java @@ -359,7 +359,7 @@ public class ImgprocTest extends OpenCVTestCase { double distance = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_CORREL); - assertEquals(1., distance); + assertEquals(1., distance, EPS); } public void testContourAreaMat() { @@ -368,7 +368,7 @@ public class ImgprocTest extends OpenCVTestCase { double area = Imgproc.contourArea(contour); - assertEquals(45., area); + assertEquals(45., area, EPS); } public void testContourAreaMatBoolean() { @@ -377,7 +377,7 @@ public class ImgprocTest extends OpenCVTestCase { double area = Imgproc.contourArea(contour, true); - assertEquals(45., area); + assertEquals(45., area, EPS); // TODO_: write better test } diff --git a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java index 70d0744948..2cd2b86155 100644 --- a/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java @@ -244,6 +244,10 @@ public class OpenCVTestCase extends TestCase { TestCase.fail(msg); } + public static void assertGE(double v1, double v2) { + assertTrue("Failed: " + v1 + " >= " + v2, v1 >= v2); + } + public static void assertListEquals(List list1, List list2) { if (list1.size() != list2.size()) { throw new UnsupportedOperationException(); @@ -458,10 +462,10 @@ public class OpenCVTestCase extends TestCase { if (isEqualityMeasured) assertTrue("Max difference between expected and actiual Mats is "+ maxDiff + ", that bigger than " + eps, - Core.checkRange(diff, true, 0.0, eps)); + maxDiff <= eps); else assertFalse("Max difference between expected and actiual Mats is "+ maxDiff + ", that less than " + eps, - Core.checkRange(diff, true, 0.0, eps)); + maxDiff <= eps); } protected static String readFile(String path) { diff --git a/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java b/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java index 400338ddd4..f369bb1783 100644 --- a/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java +++ b/modules/java/pure_test/src/org/opencv/test/OpenCVTestCase.java @@ -270,6 +270,10 @@ public class OpenCVTestCase extends TestCase { TestCase.fail(msg); } + public static void assertGE(double v1, double v2) { + assertTrue("Failed: " + v1 + " >= " + v2, v1 >= v2); + } + public static void assertListEquals(List list1, List list2) { if (list1.size() != list2.size()) { throw new UnsupportedOperationException(); @@ -484,10 +488,10 @@ public class OpenCVTestCase extends TestCase { if (isEqualityMeasured) assertTrue("Max difference between expected and actiual Mats is "+ maxDiff + ", that bigger than " + eps, - Core.checkRange(diff, true, 0.0, eps)); + maxDiff <= eps); else assertFalse("Max difference between expected and actiual Mats is "+ maxDiff + ", that less than " + eps, - Core.checkRange(diff, true, 0.0, eps)); + maxDiff <= eps); } protected static String readFile(String path) { From 0bdea2b7148e57b22db66afb7d7b854e737f3fda Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Wed, 23 Nov 2016 13:43:08 +0300 Subject: [PATCH 3/3] core: fix absdiff (non-optimized, fp) to prevent "-0" results --- modules/core/src/arithm_core.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/modules/core/src/arithm_core.hpp b/modules/core/src/arithm_core.hpp index b92d47a817..99b564cf74 100644 --- a/modules/core/src/arithm_core.hpp +++ b/modules/core/src/arithm_core.hpp @@ -97,6 +97,22 @@ template struct OpAbsDiff T operator()(T a, T b) const { return a > b ? a - b : b - a; } }; +// specializations to prevent "-0" results +template<> struct OpAbsDiff +{ + typedef float type1; + typedef float type2; + typedef float rtype; + float operator()(float a, float b) const { return std::abs(a - b); } +}; +template<> struct OpAbsDiff +{ + typedef double type1; + typedef double type2; + typedef double rtype; + double operator()(double a, double b) const { return std::abs(a - b); } +}; + template struct OpAnd { typedef T type1;