Skip to content

Commit 979b0ae

Browse files
committed
Merge pull request opencv#7711 from alalek:fix_java_tests_2.4
2 parents 408e4a7 + da75d12 commit 979b0ae

File tree

7 files changed

+115
-20
lines changed

7 files changed

+115
-20
lines changed

modules/java/android_test/src/org/opencv/test/OpenCVTestCase.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import android.util.Log;
2727

2828
public class OpenCVTestCase extends TestCase {
29+
30+
public static class TestSkipException extends RuntimeException {
31+
public TestSkipException() {}
32+
}
33+
2934
//change to 'true' to unblock fail on fail("Not yet implemented")
3035
public static final boolean passNYI = true;
3136

@@ -182,12 +187,40 @@ protected void tearDown() throws Exception {
182187
protected void runTest() throws Throwable {
183188
// Do nothing if the precondition does not hold.
184189
if (isTestCaseEnabled) {
185-
super.runTest();
190+
try {
191+
super.runTest();
192+
} catch (TestSkipException ex) {
193+
Log.w(TAG, "Test case \"" + this.getClass().getName() + "\" skipped!");
194+
assertTrue(true);
195+
}
186196
} else {
187197
Log.e(TAG, "Test case \"" + this.getClass().getName() + "\" disabled!");
188198
}
189199
}
190200

201+
public void runBare() throws Throwable {
202+
Throwable exception = null;
203+
try {
204+
setUp();
205+
} catch (TestSkipException ex) {
206+
Log.w(TAG, "Test case \"" + this.getClass().getName() + "\" skipped!");
207+
assertTrue(true);
208+
return;
209+
}
210+
try {
211+
runTest();
212+
} catch (Throwable running) {
213+
exception = running;
214+
} finally {
215+
try {
216+
tearDown();
217+
} catch (Throwable tearingDown) {
218+
if (exception == null) exception = tearingDown;
219+
}
220+
}
221+
if (exception != null) throw exception;
222+
}
223+
191224
protected Mat getMat(int type, double... vals)
192225
{
193226
return new Mat(matSize, matSize, type, new Scalar(vals));
@@ -205,6 +238,10 @@ public static void fail(String msg) {
205238
TestCase.fail(msg);
206239
}
207240

241+
public static void assertGE(double v1, double v2) {
242+
assertTrue("Failed: " + v1 + " >= " + v2, v1 >= v2);
243+
}
244+
208245
public static <E extends Number> void assertListEquals(List<E> list1, List<E> list2) {
209246
if (list1.size() != list2.size()) {
210247
throw new UnsupportedOperationException();
@@ -419,10 +456,10 @@ static private void compareMats(Mat expected, Mat actual, double eps, boolean is
419456

420457
if (isEqualityMeasured)
421458
assertTrue("Max difference between expected and actiual Mats is "+ maxDiff + ", that bigger than " + eps,
422-
Core.checkRange(diff, true, 0.0, eps));
459+
maxDiff <= eps);
423460
else
424461
assertFalse("Max difference between expected and actiual Mats is "+ maxDiff + ", that less than " + eps,
425-
Core.checkRange(diff, true, 0.0, eps));
462+
maxDiff <= eps);
426463
}
427464

428465
protected static String readFile(String path) {

modules/java/android_test/src/org/opencv/test/calib3d/Calib3dTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,22 @@ public void testFindFundamentalMatListOfPointListOfPoint() {
249249

250250
Mat fm = Calib3d.findFundamentalMat(pts, pts);
251251

252-
truth = new Mat(3, 3, CvType.CV_64F);
253-
truth.put(0, 0, 0, -0.577, 0.288, 0.577, 0, 0.288, -0.288, -0.288, 0);
254-
assertMatEqual(truth, fm, EPS);
252+
// Check definition of fundamental matrix:
253+
// [p2; 1]T * F * [p1; 1] = 0
254+
// (p2 == p1 in this testcase)
255+
for (int i = 0; i < pts.rows(); i++)
256+
{
257+
Mat pt = new Mat(3, 1, fm.type());
258+
pt.put(0, 0, pts.get(i, 0)[0], pts.get(i, 0)[1], 1);
259+
260+
Mat pt_t = pt.t();
261+
262+
Mat tmp = new Mat();
263+
Mat res = new Mat();
264+
Core.gemm(pt_t, fm, 1.0, new Mat(), 0.0, tmp);
265+
Core.gemm(tmp, pt, 1.0, new Mat(), 0.0, res);
266+
assertTrue(Math.abs(res.get(0, 0)[0]) <= 1e-6);
267+
}
255268
}
256269

257270
public void testFindFundamentalMatListOfPointListOfPointInt() {

modules/java/android_test/src/org/opencv/test/core/CoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2052,7 +2052,7 @@ public void testSolvePolyMatMat() {
20522052
};
20532053
Mat roots = new Mat();
20542054

2055-
assertEquals(0.0, Core.solvePoly(coeffs, roots));
2055+
assertGE(1e-6, Math.abs(Core.solvePoly(coeffs, roots)));
20562056

20572057
truth = new Mat(3, 1, CvType.CV_32FC2) {
20582058
{

modules/java/android_test/src/org/opencv/test/core/MatTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,13 @@ public void testInvInt() {
488488
public void testIsContinuous() {
489489
assertTrue(gray0.isContinuous());
490490

491-
Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2);
491+
Mat subMat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
492492
assertFalse(subMat.isContinuous());
493493
}
494494

495495
public void testIsSubmatrix() {
496496
assertFalse(gray0.isSubmatrix());
497-
Mat subMat = gray0.submat(0, 0, gray0.rows() / 2, gray0.cols() / 2);
497+
Mat subMat = gray0.submat(0, gray0.rows() / 2, 0, gray0.cols() / 2);
498498
assertTrue(subMat.isSubmatrix());
499499
}
500500

modules/java/android_test/src/org/opencv/test/imgproc/ImgprocTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void testArcLength() {
165165

166166
double arcLength = Imgproc.arcLength(curve, false);
167167

168-
assertEquals(5.656854152679443, arcLength);
168+
assertEquals(5.656854152679443, arcLength, EPS);
169169
}
170170

171171
public void testBilateralFilterMatMatIntDoubleDouble() {
@@ -367,7 +367,7 @@ public void testCompareHist() {
367367

368368
double distance = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_CORREL);
369369

370-
assertEquals(1., distance);
370+
assertEquals(1., distance, EPS);
371371
}
372372

373373
public void testContourAreaMat() {
@@ -376,7 +376,7 @@ public void testContourAreaMat() {
376376

377377
double area = Imgproc.contourArea(contour);
378378

379-
assertEquals(45., area);
379+
assertEquals(45., area, EPS);
380380
}
381381

382382
public void testContourAreaMatBoolean() {
@@ -385,7 +385,7 @@ public void testContourAreaMatBoolean() {
385385

386386
double area = Imgproc.contourArea(contour, true);
387387

388-
assertEquals(45., area);
388+
assertEquals(45., area, EPS);
389389
// TODO_: write better test
390390
}
391391

modules/java/test/build.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<project>
22
<property file="ant-${opencv.build.type}.properties"/>
3+
<property name="test.dir" value="testResults"/>
4+
<property name="build.dir" value="build"/>
35

46
<path id="master-classpath">
57
<fileset dir="lib">
@@ -12,7 +14,7 @@
1214

1315
<target name="clean">
1416
<delete dir="build"/>
15-
<delete dir="testResults"/>
17+
<delete dir="${test.dir}"/>
1618
</target>
1719

1820
<target name="compile">
@@ -34,8 +36,8 @@
3436
</target>
3537

3638
<target name="test">
37-
<mkdir dir="testResults"/>
38-
<junit printsummary="true" haltonfailure="false" haltonerror="false" showoutput="false" logfailedtests="true" maxmemory="256m">
39+
<mkdir dir="${test.dir}"/>
40+
<junit printsummary="true" haltonfailure="false" haltonerror="false" showoutput="true" logfailedtests="true" maxmemory="256m">
3941
<sysproperty key="java.library.path" path="${opencv.lib.path}"/>
4042
<env key="PATH" path="${opencv.lib.path}"/>
4143
<classpath refid="master-classpath"/>
@@ -45,12 +47,18 @@
4547

4648
<formatter type="xml"/>
4749

48-
<batchtest fork="yes" todir="testResults">
50+
<batchtest fork="yes" todir="${test.dir}">
4951
<zipfileset src="build/jar/opencv-test.jar" includes="**/*.class" excludes="**/OpenCVTest*">
5052
<exclude name="**/*$*.class"/>
5153
</zipfileset>
5254
</batchtest>
5355
</junit>
56+
<junitreport todir="${test.dir}">
57+
<fileset dir="${test.dir}">
58+
<include name="TEST-*.xml"/>
59+
</fileset>
60+
<report format="noframes" todir="${test.dir}"/>
61+
</junitreport>
5462
</target>
5563

5664
<target name="build">

modules/java/test/src/org/opencv/test/OpenCVTestCase.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
import org.opencv.highgui.Highgui;
2929

3030
public class OpenCVTestCase extends TestCase {
31+
32+
public static class TestSkipException extends RuntimeException {
33+
public TestSkipException() {}
34+
}
35+
3136
//change to 'true' to unblock fail on fail("Not yet implemented")
3237
public static final boolean passNYI = true;
3338

@@ -212,12 +217,40 @@ protected void tearDown() throws Exception {
212217
protected void runTest() throws Throwable {
213218
// Do nothing if the precondition does not hold.
214219
if (isTestCaseEnabled) {
215-
super.runTest();
220+
try {
221+
super.runTest();
222+
} catch (TestSkipException ex) {
223+
OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" skipped!");
224+
assertTrue(true);
225+
}
216226
} else {
217227
OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" disabled!");
218228
}
219229
}
220230

231+
public void runBare() throws Throwable {
232+
Throwable exception = null;
233+
try {
234+
setUp();
235+
} catch (TestSkipException ex) {
236+
OpenCVTestRunner.Log(TAG + " :: " + "Test case \"" + this.getClass().getName() + "\" skipped!");
237+
assertTrue(true);
238+
return;
239+
}
240+
try {
241+
runTest();
242+
} catch (Throwable running) {
243+
exception = running;
244+
} finally {
245+
try {
246+
tearDown();
247+
} catch (Throwable tearingDown) {
248+
if (exception == null) exception = tearingDown;
249+
}
250+
}
251+
if (exception != null) throw exception;
252+
}
253+
221254
protected Mat getMat(int type, double... vals)
222255
{
223256
return new Mat(matSize, matSize, type, new Scalar(vals));
@@ -235,6 +268,10 @@ public static void fail(String msg) {
235268
TestCase.fail(msg);
236269
}
237270

271+
public static void assertGE(double v1, double v2) {
272+
assertTrue("Failed: " + v1 + " >= " + v2, v1 >= v2);
273+
}
274+
238275
public static <E extends Number> void assertListEquals(List<E> list1, List<E> list2) {
239276
if (list1.size() != list2.size()) {
240277
throw new UnsupportedOperationException();
@@ -449,10 +486,10 @@ static private void compareMats(Mat expected, Mat actual, double eps, boolean is
449486

450487
if (isEqualityMeasured)
451488
assertTrue("Max difference between expected and actiual Mats is "+ maxDiff + ", that bigger than " + eps,
452-
Core.checkRange(diff, true, 0.0, eps));
489+
maxDiff <= eps);
453490
else
454491
assertFalse("Max difference between expected and actiual Mats is "+ maxDiff + ", that less than " + eps,
455-
Core.checkRange(diff, true, 0.0, eps));
492+
maxDiff <= eps);
456493
}
457494

458495
protected static String readFile(String path) {

0 commit comments

Comments
 (0)