From 0a5d6e10b92f044f6370f833aa8cf7bd84cb5970 Mon Sep 17 00:00:00 2001 From: Koji Miyazato Date: Fri, 21 Mar 2014 23:52:12 +0900 Subject: [PATCH] Added test code for I/O of user-defined types. --- modules/core/test/test_io.cpp | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/modules/core/test/test_io.cpp b/modules/core/test/test_io.cpp index 23c0aad620..e6c412f869 100644 --- a/modules/core/test/test_io.cpp +++ b/modules/core/test/test_io.cpp @@ -380,6 +380,40 @@ TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run extern void testFormatter(); + +struct UserDefinedType +{ + int a; + float b; +}; + +static inline bool operator==(const UserDefinedType &x, + const UserDefinedType &y) { + return (x.a == y.a) && (x.b == y.b); +} + +static inline void write(FileStorage &fs, + const String&, + const UserDefinedType &value) +{ + fs << "{:" << "a" << value.a << "b" << value.b << "}"; +} + +static inline void read(const FileNode& node, + UserDefinedType& value, + const UserDefinedType& default_value + = UserDefinedType()) { + if(node.empty()) + { + value = default_value; + } + else + { + node["a"] >> value.a; + node["b"] >> value.b; + } +} + class CV_MiscIOTest : public cvtest::BaseTest { public: @@ -393,11 +427,14 @@ protected: string fname = cv::tempfile(".xml"); vector mi, mi2, mi3, mi4; vector mv, mv2, mv3, mv4; + vector vudt, vudt2, vudt3, vudt4; Mat m(10, 9, CV_32F); Mat empty; + UserDefinedType udt = { 8, 3.3f }; randu(m, 0, 1); mi3.push_back(5); mv3.push_back(m); + vudt3.push_back(udt); Point_ p1(1.1f, 2.2f), op1; Point3i p2(3, 4, 5), op2; Size s1(6, 7), os1; @@ -412,6 +449,8 @@ protected: fs << "mv" << mv; fs << "mi3" << mi3; fs << "mv3" << mv3; + fs << "vudt" << vudt; + fs << "vudt3" << vudt3; fs << "empty" << empty; fs << "p1" << p1; fs << "p2" << p2; @@ -428,6 +467,8 @@ protected: fs["mv"] >> mv2; fs["mi3"] >> mi4; fs["mv3"] >> mv4; + fs["vudt"] >> vudt2; + fs["vudt3"] >> vudt4; fs["empty"] >> empty; fs["p1"] >> op1; fs["p2"] >> op2; @@ -442,6 +483,8 @@ protected: CV_Assert( norm(mi3, mi4, CV_C) == 0 ); CV_Assert( mv4.size() == 1 ); double n = norm(mv3[0], mv4[0], CV_C); + CV_Assert( vudt2.empty() ); + CV_Assert( vudt3 == vudt4 ); CV_Assert( n == 0 ); CV_Assert( op1 == p1 ); CV_Assert( op2 == p2 );