Added test code for I/O of user-defined types.

pull/2476/head
Koji Miyazato 11 years ago
parent a43ef9a6cd
commit 0a5d6e10b9
  1. 43
      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<int> mi, mi2, mi3, mi4;
vector<Mat> mv, mv2, mv3, mv4;
vector<UserDefinedType> 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_<float> 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 );

Loading…
Cancel
Save