Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit d66360d

Browse files
committed
python: add 'int' conversion test
1 parent f387e3e commit d66360d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

modules/core/include/opencv2/core/bindings_utils.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ AsyncArray testAsyncException()
4343
return p.getArrayResult();
4444
}
4545

46+
CV_WRAP static inline
47+
String dumpInt(int argument) { return cv::format("Int: %d", argument); }
48+
4649
//! @}
4750
}} // namespace
4851

modules/python/test/test_misc.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ def test_InputArrayOfArrays(self):
8484
self.assertEqual(res4, "InputArrayOfArrays: empty()=false kind=0x00050000 flags=0x01050000 total(-1)=3 dims(-1)=1 size(-1)=3x1 type(0)=CV_32FC2 dims(0)=2 size(0)=3x1 type(0)=CV_32FC2")
8585

8686

87+
def test_conversion_int(self):
88+
self.assertEqual(cv.utils.dumpInt(42), "Int: 42")
89+
self.assertEqual(cv.utils.dumpInt(-1), "Int: -1")
90+
self.assertEqual(cv.utils.dumpInt(0), "Int: 0")
91+
with self.assertRaises(TypeError, msg="Should not convert floating point values into 'int' type"):
92+
print(cv.utils.dumpInt(5.5))
93+
self.assertEqual(cv.utils.dumpInt(int(5.1)), "Int: 5")
94+
self.assertEqual(cv.utils.dumpInt((42)), "Int: 42") # single element tuple is not a tuple
95+
with self.assertRaises(TypeError, msg="Should not convert tuple values into 'int' type"):
96+
print(cv.utils.dumpInt((42,)), "Int: 42")
97+
with self.assertRaises(TypeError, msg="Should not convert list values into 'int' type"):
98+
print(cv.utils.dumpInt([5]))
99+
with self.assertRaises(TypeError, msg="Should not convert symbol values into 'int' type"):
100+
print(cv.utils.dumpInt('a'))
101+
with self.assertRaises(TypeError, msg="Should not convert string values into 'int' type"):
102+
print(cv.utils.dumpInt('abc'))
103+
# numpy
104+
b = np.array([17], dtype=np.int32)
105+
self.assertEqual(cv.utils.dumpInt(int(b[0])), "Int: 17")
106+
self.assertEqual(cv.utils.dumpInt(b[0]), "Int: 17") # regression from new patch
107+
#if 1: # buggy implicit conversion in OpenCV 3.4.8
108+
with self.assertRaises(TypeError, msg="Should not convert numpy array into 'int' type"):
109+
print(cv.utils.dumpInt(b))
110+
111+
112+
87113
class SamplesFindFile(NewOpenCVTests):
88114

89115
def test_ExistedFile(self):

0 commit comments

Comments
 (0)