test_io.cpp 25.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "test_precomp.hpp"

using namespace cv;
using namespace std;

static SparseMat cvTsGetRandomSparseMat(int dims, const int* sz, int type,
                                        int nzcount, double a, double b, RNG& rng)
{
    SparseMat m(dims, sz, type);
    int i, j;
    CV_Assert(CV_MAT_CN(type) == 1);
    for( i = 0; i < nzcount; i++ )
    {
        int idx[CV_MAX_DIM];
        for( j = 0; j < dims; j++ )
            idx[j] = cvtest::randInt(rng) % sz[j];
        double val = cvtest::randReal(rng)*(b - a) + a;
        uchar* ptr = m.ptr(idx, true, 0);
        if( type == CV_8U )
            *(uchar*)ptr = saturate_cast<uchar>(val);
        else if( type == CV_8S )
            *(schar*)ptr = saturate_cast<schar>(val);
        else if( type == CV_16U )
            *(ushort*)ptr = saturate_cast<ushort>(val);
        else if( type == CV_16S )
            *(short*)ptr = saturate_cast<short>(val);
        else if( type == CV_32S )
            *(int*)ptr = saturate_cast<int>(val);
        else if( type == CV_32F )
            *(float*)ptr = saturate_cast<float>(val);
        else
            *(double*)ptr = saturate_cast<double>(val);
    }
34

35 36 37 38 39 40 41 42
    return m;
}

static bool cvTsCheckSparse(const CvSparseMat* m1, const CvSparseMat* m2, double eps)
{
    CvSparseMatIterator it1;
    CvSparseNode* node1;
    int depth = CV_MAT_DEPTH(m1->type);
43

44 45 46
    if( m1->heap->active_count != m2->heap->active_count ||
       m1->dims != m2->dims || CV_MAT_TYPE(m1->type) != CV_MAT_TYPE(m2->type) )
        return false;
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    for( node1 = cvInitSparseMatIterator( m1, &it1 );
        node1 != 0; node1 = cvGetNextSparseNode( &it1 ))
    {
        uchar* v1 = (uchar*)CV_NODE_VAL(m1,node1);
        uchar* v2 = cvPtrND( m2, CV_NODE_IDX(m1,node1), 0, 0, &node1->hashval );
        if( !v2 )
            return false;
        if( depth == CV_8U || depth == CV_8S )
        {
            if( *v1 != *v2 )
                return false;
        }
        else if( depth == CV_16U || depth == CV_16S )
        {
            if( *(ushort*)v1 != *(ushort*)v2 )
                return false;
        }
        else if( depth == CV_32S )
        {
            if( *(int*)v1 != *(int*)v2 )
                return false;
        }
        else if( depth == CV_32F )
        {
            if( fabs(*(float*)v1 - *(float*)v2) > eps*(fabs(*(float*)v2) + 1) )
                return false;
        }
        else if( fabs(*(double*)v1 - *(double*)v2) > eps*(fabs(*(double*)v2) + 1) )
            return false;
    }
78

79 80 81 82 83 84 85
    return true;
}


class Core_IOTest : public cvtest::BaseTest
{
public:
86
    Core_IOTest() { }
87 88 89 90 91 92 93
protected:
    void run(int)
    {
        double ranges[][2] = {{0, 256}, {-128, 128}, {0, 65536}, {-32768, 32768},
            {-1000000, 1000000}, {-10, 10}, {-10, 10}};
        RNG& rng = ts->get_rng();
        RNG rng0;
94
        test_case_count = 4;
95 96
        int progress = 0;
        MemStorage storage(cvCreateMemStorage(0));
97

98 99 100 101
        for( int idx = 0; idx < test_case_count; idx++ )
        {
            ts->update_context( this, idx, false );
            progress = update_progress( progress, idx, test_case_count, 0 );
102

103
            cvClearMemStorage(storage);
104

105
            bool mem = (idx % 4) >= 2;
106
            string filename = tempfile(idx % 2 ? ".yml" : ".xml");
107

108
            FileStorage fs(filename, FileStorage::WRITE + (mem ? FileStorage::MEMORY : 0));
109

110 111 112
            int test_int = (int)cvtest::randInt(rng);
            double test_real = (cvtest::randInt(rng)%2?1:-1)*exp(cvtest::randReal(rng)*18-9);
            string test_string = "vw wv23424rt\"&amp;&lt;&gt;&amp;&apos;@#$@$%$%&%IJUKYILFD@#$@%$&*&() ";
113

114 115 116
            int depth = cvtest::randInt(rng) % (CV_64F+1);
            int cn = cvtest::randInt(rng) % 4 + 1;
            Mat test_mat(cvtest::randInt(rng)%30+1, cvtest::randInt(rng)%30+1, CV_MAKETYPE(depth, cn));
117

118 119 120 121 122 123 124 125
            rng0.fill(test_mat, CV_RAND_UNI, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
            if( depth >= CV_32F )
            {
                exp(test_mat, test_mat);
                Mat test_mat_scale(test_mat.size(), test_mat.type());
                rng0.fill(test_mat_scale, CV_RAND_UNI, Scalar::all(-1), Scalar::all(1));
                multiply(test_mat, test_mat_scale, test_mat);
            }
126

127 128
            CvSeq* seq = cvCreateSeq(test_mat.type(), (int)sizeof(CvSeq),
                                     (int)test_mat.elemSize(), storage);
129
            cvSeqPushMulti(seq, test_mat.ptr(), test_mat.cols*test_mat.rows);
130

131 132 133 134 135 136 137 138 139 140 141 142 143
            CvGraph* graph = cvCreateGraph( CV_ORIENTED_GRAPH,
                                           sizeof(CvGraph), sizeof(CvGraphVtx),
                                           sizeof(CvGraphEdge), storage );
            int edges[][2] = {{0,1},{1,2},{2,0},{0,3},{3,4},{4,1}};
            int i, vcount = 5, ecount = 6;
            for( i = 0; i < vcount; i++ )
                cvGraphAddVtx(graph);
            for( i = 0; i < ecount; i++ )
            {
                CvGraphEdge* edge;
                cvGraphAddEdge(graph, edges[i][0], edges[i][1], 0, &edge);
                edge->weight = (float)(i+1);
            }
144

145 146
            depth = cvtest::randInt(rng) % (CV_64F+1);
            cn = cvtest::randInt(rng) % 4 + 1;
147 148 149 150 151
            int sz[] = {
                static_cast<int>(cvtest::randInt(rng)%10+1),
                static_cast<int>(cvtest::randInt(rng)%10+1),
                static_cast<int>(cvtest::randInt(rng)%10+1),
            };
152
            MatND test_mat_nd(3, sz, CV_MAKETYPE(depth, cn));
153

154 155 156 157 158 159 160 161
            rng0.fill(test_mat_nd, CV_RAND_UNI, Scalar::all(ranges[depth][0]), Scalar::all(ranges[depth][1]));
            if( depth >= CV_32F )
            {
                exp(test_mat_nd, test_mat_nd);
                MatND test_mat_scale(test_mat_nd.dims, test_mat_nd.size, test_mat_nd.type());
                rng0.fill(test_mat_scale, CV_RAND_UNI, Scalar::all(-1), Scalar::all(1));
                multiply(test_mat_nd, test_mat_scale, test_mat_nd);
            }
162

163 164 165 166 167 168
            int ssz[] = {
                static_cast<int>(cvtest::randInt(rng)%10+1),
                static_cast<int>(cvtest::randInt(rng)%10+1),
                static_cast<int>(cvtest::randInt(rng)%10+1),
                static_cast<int>(cvtest::randInt(rng)%10+1),
            };
169 170
            SparseMat test_sparse_mat = cvTsGetRandomSparseMat(4, ssz, cvtest::randInt(rng)%(CV_64F+1),
                                                               cvtest::randInt(rng) % 10000, 0, 100, rng);
171

172 173 174 175
            fs << "test_int" << test_int << "test_real" << test_real << "test_string" << test_string;
            fs << "test_mat" << test_mat;
            fs << "test_mat_nd" << test_mat_nd;
            fs << "test_sparse_mat" << test_sparse_mat;
176

177 178 179
            fs << "test_list" << "[" << 0.0000000000001 << 2 << CV_PI << -3435345 << "2-502 2-029 3egegeg" <<
            "{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
            fs << "test_map" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";
180

181 182
            const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
            fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));
183

184 185
            fs << "]" << "}";
            cvWriteComment(*fs, "test comment", 0);
186

187 188 189
            fs.writeObj("test_seq", seq);
            fs.writeObj("test_graph",graph);
            CvGraph* graph2 = (CvGraph*)cvClone(graph);
190

191
            string content = fs.releaseAndGetString();
192

193
            if(!fs.open(mem ? content : filename, FileStorage::READ + (mem ? FileStorage::MEMORY : 0)))
194
            {
195
                ts->printf( cvtest::TS::LOG, "filename %s can not be read\n", !mem ? filename.c_str() : content.c_str());
196 197 198
                ts->set_failed_test_info( cvtest::TS::FAIL_MISSING_TEST_DATA );
                return;
            }
199

200 201
            int real_int = (int)fs["test_int"];
            double real_real = (double)fs["test_real"];
202
            String real_string = (String)fs["test_string"];
203

204 205 206 207 208 209 210 211
            if( real_int != test_int ||
               fabs(real_real - test_real) > DBL_EPSILON*(fabs(test_real)+1) ||
               real_string != test_string )
            {
                ts->printf( cvtest::TS::LOG, "the read scalars are not correct\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
212

213 214 215 216 217 218 219
            CvMat* m = (CvMat*)fs["test_mat"].readObj();
            CvMat _test_mat = test_mat;
            double max_diff = 0;
            CvMat stub1, _test_stub1;
            cvReshape(m, &stub1, 1, 0);
            cvReshape(&_test_mat, &_test_stub1, 1, 0);
            vector<int> pt;
220

221
            if( !m || !CV_IS_MAT(m) || m->rows != test_mat.rows || m->cols != test_mat.cols ||
A
Andrey Kamaev 已提交
222
               cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 )
223 224 225 226 227 228 229 230 231
            {
                ts->printf( cvtest::TS::LOG, "the read matrix is not correct: (%.20g vs %.20g) at (%d,%d)\n",
                            cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[0], pt[1]),
                            pt[0], pt[1] );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
            if( m && CV_IS_MAT(m))
                cvReleaseMat(&m);
232

233 234
            CvMatND* m_nd = (CvMatND*)fs["test_mat_nd"].readObj();
            CvMatND _test_mat_nd = test_mat_nd;
235

236 237 238 239 240 241
            if( !m_nd || !CV_IS_MATND(m_nd) )
            {
                ts->printf( cvtest::TS::LOG, "the read nd-matrix is not correct\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
242

243 244 245 246 247
            CvMat stub, _test_stub;
            cvGetMat(m_nd, &stub, 0, 1);
            cvGetMat(&_test_mat_nd, &_test_stub, 0, 1);
            cvReshape(&stub, &stub1, 1, 0);
            cvReshape(&_test_stub, &_test_stub1, 1, 0);
248

249 250
            if( !CV_ARE_TYPES_EQ(&stub, &_test_stub) ||
               !CV_ARE_SIZES_EQ(&stub, &_test_stub) ||
251
               //cvNorm(&stub, &_test_stub, CV_L2) != 0 )
A
Andrey Kamaev 已提交
252
               cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 )
253 254 255 256 257 258 259
            {
                ts->printf( cvtest::TS::LOG, "readObj method: the read nd matrix is not correct: (%.20g vs %.20g) vs at (%d,%d)\n",
                           cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[0], pt[1]),
                           pt[0], pt[1] );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
260

261 262 263 264 265
            MatND mat_nd2;
            fs["test_mat_nd"] >> mat_nd2;
            CvMatND m_nd2 = mat_nd2;
            cvGetMat(&m_nd2, &stub, 0, 1);
            cvReshape(&stub, &stub1, 1, 0);
266

267 268
            if( !CV_ARE_TYPES_EQ(&stub, &_test_stub) ||
               !CV_ARE_SIZES_EQ(&stub, &_test_stub) ||
269
               //cvNorm(&stub, &_test_stub, CV_L2) != 0 )
A
Andrey Kamaev 已提交
270
               cvtest::cmpEps( cv::cvarrToMat(&stub1), cv::cvarrToMat(&_test_stub1), &max_diff, 0, &pt, true) < 0 )
271 272 273 274 275 276 277
            {
                ts->printf( cvtest::TS::LOG, "C++ method: the read nd matrix is not correct: (%.20g vs %.20g) vs at (%d,%d)\n",
                           cvGetReal2D(&stub1, pt[0], pt[1]), cvGetReal2D(&_test_stub1, pt[1], pt[0]),
                           pt[0], pt[1] );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
278

279
            cvRelease((void**)&m_nd);
280

R
Roman Donchenko 已提交
281 282 283
            Ptr<CvSparseMat> m_s((CvSparseMat*)fs["test_sparse_mat"].readObj());
            Ptr<CvSparseMat> _test_sparse_(cvCreateSparseMat(test_sparse_mat));
            Ptr<CvSparseMat> _test_sparse((CvSparseMat*)cvClone(_test_sparse_));
284 285
            SparseMat m_s2;
            fs["test_sparse_mat"] >> m_s2;
R
Roman Donchenko 已提交
286
            Ptr<CvSparseMat> _m_s2(cvCreateSparseMat(m_s2));
287

288
            if( !m_s || !CV_IS_SPARSE_MAT(m_s) ||
R
Roman Donchenko 已提交
289 290
               !cvTsCheckSparse(m_s, _test_sparse, 0) ||
               !cvTsCheckSparse(_m_s2, _test_sparse, 0))
291 292 293 294 295
            {
                ts->printf( cvtest::TS::LOG, "the read sparse matrix is not correct\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
296

297 298 299 300 301 302
            FileNode tl = fs["test_list"];
            if( tl.type() != FileNode::SEQ || tl.size() != 6 ||
               fabs((double)tl[0] - 0.0000000000001) >= DBL_EPSILON ||
               (int)tl[1] != 2 ||
               fabs((double)tl[2] - CV_PI) >= DBL_EPSILON ||
               (int)tl[3] != -3435345 ||
303
               (String)tl[4] != "2-502 2-029 3egegeg" ||
304 305 306 307 308 309 310 311 312
               tl[5].type() != FileNode::MAP || tl[5].size() != 3 ||
               (int)tl[5]["month"] != 12 ||
               (int)tl[5]["day"] != 31 ||
               (int)tl[5]["year"] != 1969 )
            {
                ts->printf( cvtest::TS::LOG, "the test list is incorrect\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
313

314 315
            FileNode tm = fs["test_map"];
            FileNode tm_lbp = tm["lbp"];
316

317 318 319 320
            int real_x = (int)tm["x"];
            int real_y = (int)tm["y"];
            int real_width = (int)tm["width"];
            int real_height = (int)tm["height"];
321

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
            int real_lbp_val = 0;
            FileNodeIterator it;
            it = tm_lbp.begin();
            real_lbp_val |= (int)*it << 0;
            ++it;
            real_lbp_val |= (int)*it << 1;
            it++;
            real_lbp_val |= (int)*it << 2;
            it += 1;
            real_lbp_val |= (int)*it << 3;
            FileNodeIterator it2(it);
            it2 += 4;
            real_lbp_val |= (int)*it2 << 7;
            --it2;
            real_lbp_val |= (int)*it2 << 6;
            it2--;
            real_lbp_val |= (int)*it2 << 5;
            it2 -= 1;
            real_lbp_val |= (int)*it2 << 4;
            it2 += -1;
            CV_Assert( it == it2 );
343

344 345 346 347 348 349 350 351 352 353 354 355 356
            if( tm.type() != FileNode::MAP || tm.size() != 5 ||
               real_x != 1 ||
               real_y != 2 ||
               real_width != 100 ||
               real_height != 200 ||
               tm_lbp.type() != FileNode::SEQ ||
               tm_lbp.size() != 8 ||
               real_lbp_val != 0xb6 )
            {
                ts->printf( cvtest::TS::LOG, "the test map is incorrect\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
357

358 359 360 361 362 363 364 365
            CvGraph* graph3 = (CvGraph*)fs["test_graph"].readObj();
            if(graph2->active_count != vcount || graph3->active_count != vcount ||
               graph2->edges->active_count != ecount || graph3->edges->active_count != ecount)
            {
                ts->printf( cvtest::TS::LOG, "the cloned or read graph have wrong number of vertices or edges\n" );
                ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                return;
            }
366

367 368 369 370 371 372 373 374 375 376 377 378
            for( i = 0; i < ecount; i++ )
            {
                CvGraphEdge* edge2 = cvFindGraphEdge(graph2, edges[i][0], edges[i][1]);
                CvGraphEdge* edge3 = cvFindGraphEdge(graph3, edges[i][0], edges[i][1]);
                if( !edge2 || edge2->weight != (float)(i+1) ||
                   !edge3 || edge3->weight != (float)(i+1) )
                {
                    ts->printf( cvtest::TS::LOG, "the cloned or read graph do not have the edge (%d, %d)\n", edges[i][0], edges[i][1] );
                    ts->set_failed_test_info( cvtest::TS::FAIL_INVALID_OUTPUT );
                    return;
                }
            }
379

380
            fs.release();
381 382
            if( !mem )
                remove(filename.c_str());
383 384 385 386 387
        }
    }
};

TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); }
388

389
extern void testFormatter();
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

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;
    }
}

425 426 427 428
class CV_MiscIOTest : public cvtest::BaseTest
{
public:
    CV_MiscIOTest() {}
429
    ~CV_MiscIOTest() {}
430 431 432 433 434
protected:
    void run(int)
    {
        try
        {
435
            string fname = cv::tempfile(".xml");
436 437
            vector<int> mi, mi2, mi3, mi4;
            vector<Mat> mv, mv2, mv3, mv4;
438
            vector<UserDefinedType> vudt, vudt2, vudt3, vudt4;
439 440
            Mat m(10, 9, CV_32F);
            Mat empty;
441
            UserDefinedType udt = { 8, 3.3f };
442 443 444
            randu(m, 0, 1);
            mi3.push_back(5);
            mv3.push_back(m);
445
            vudt3.push_back(udt);
446 447 448 449 450 451 452 453
            Point_<float> p1(1.1f, 2.2f), op1;
            Point3i p2(3, 4, 5), op2;
            Size s1(6, 7), os1;
            Complex<int> c1(9, 10), oc1;
            Rect r1(11, 12, 13, 14), or1;
            Vec<int, 5> v1(15, 16, 17, 18, 19), ov1;
            Scalar sc1(20.0, 21.1, 22.2, 23.3), osc1;
            Range g1(7, 8), og1;
454

455
            FileStorage fs(fname, FileStorage::WRITE);
456 457 458 459
            fs << "mi" << mi;
            fs << "mv" << mv;
            fs << "mi3" << mi3;
            fs << "mv3" << mv3;
460 461
            fs << "vudt" << vudt;
            fs << "vudt3" << vudt3;
462
            fs << "empty" << empty;
463 464 465 466 467 468 469 470
            fs << "p1" << p1;
            fs << "p2" << p2;
            fs << "s1" << s1;
            fs << "c1" << c1;
            fs << "r1" << r1;
            fs << "v1" << v1;
            fs << "sc1" << sc1;
            fs << "g1" << g1;
471
            fs.release();
472

473
            fs.open(fname, FileStorage::READ);
474 475 476 477
            fs["mi"] >> mi2;
            fs["mv"] >> mv2;
            fs["mi3"] >> mi4;
            fs["mv3"] >> mv4;
478 479
            fs["vudt"] >> vudt2;
            fs["vudt3"] >> vudt4;
480
            fs["empty"] >> empty;
481 482 483 484 485 486 487 488
            fs["p1"] >> op1;
            fs["p2"] >> op2;
            fs["s1"] >> os1;
            fs["c1"] >> oc1;
            fs["r1"] >> or1;
            fs["v1"] >> ov1;
            fs["sc1"] >> osc1;
            fs["g1"] >> og1;
489 490
            CV_Assert( mi2.empty() );
            CV_Assert( mv2.empty() );
491
            CV_Assert( cvtest::norm(Mat(mi3), Mat(mi4), CV_C) == 0 );
492
            CV_Assert( mv4.size() == 1 );
493
            double n = cvtest::norm(mv3[0], mv4[0], CV_C);
494 495
            CV_Assert( vudt2.empty() );
            CV_Assert( vudt3 == vudt4 );
496
            CV_Assert( n == 0 );
497 498 499 500 501 502 503 504
            CV_Assert( op1 == p1 );
            CV_Assert( op2 == p2 );
            CV_Assert( os1 == s1 );
            CV_Assert( oc1 == c1 );
            CV_Assert( or1 == r1 );
            CV_Assert( ov1 == v1 );
            CV_Assert( osc1 == sc1 );
            CV_Assert( og1 == g1 );
505 506 507 508 509 510 511 512 513 514
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
        }
    }
};

TEST(Core_InputOutput, misc) { CV_MiscIOTest test; test.safe_run(); }

515 516 517 518
/*class CV_BigMatrixIOTest : public cvtest::BaseTest
{
public:
    CV_BigMatrixIOTest() {}
519
    ~CV_BigMatrixIOTest() {}
520 521 522 523 524 525 526 527 528
protected:
    void run(int)
    {
        try
        {
            RNG& rng = theRNG();
            int N = 1000, M = 1200000;
            Mat mat(M, N, CV_32F);
            rng.fill(mat, RNG::UNIFORM, 0, 1);
529
            FileStorage fs(cv::tempfile(".xml"), FileStorage::WRITE);
530 531 532 533 534 535 536 537 538 539 540 541 542
            fs << "mat" << mat;
            fs.release();
        }
        catch(...)
        {
            ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
        }
    }
};

TEST(Core_InputOutput, huge) { CV_BigMatrixIOTest test; test.safe_run(); }
*/

543
TEST(Core_globbing, accuracy)
M
marina.kolpakova 已提交
544 545 546 547
{
    std::string patternLena    = cvtest::TS::ptr()->get_data_path() + "lena*.*";
    std::string patternLenaPng = cvtest::TS::ptr()->get_data_path() + "lena.png";

548
    std::vector<String> lenas, pngLenas;
M
marina.kolpakova 已提交
549 550 551 552 553 554 555 556 557
    cv::glob(patternLena, lenas, true);
    cv::glob(patternLenaPng, pngLenas, true);

    ASSERT_GT(lenas.size(), pngLenas.size());

    for (size_t i = 0; i < pngLenas.size(); ++i)
    {
        ASSERT_NE(std::find(lenas.begin(), lenas.end(), pngLenas[i]), lenas.end());
    }
A
Andrey Kamaev 已提交
558 559
}

560 561 562 563 564 565 566 567 568
TEST(Core_InputOutput, FileStorage)
{
    std::string file = cv::tempfile(".xml");
    cv::FileStorage f(file, cv::FileStorage::WRITE);

    char arr[66];
    sprintf(arr, "sprintf is hell %d", 666);
    EXPECT_NO_THROW(f << arr);
}
569 570 571 572 573 574 575 576

TEST(Core_InputOutput, FileStorageKey)
{
    cv::FileStorage f("dummy.yml", cv::FileStorage::WRITE | cv::FileStorage::MEMORY);

    EXPECT_NO_THROW(f << "key1" << "value1");
    EXPECT_NO_THROW(f << "_key2" << "value2");
    EXPECT_NO_THROW(f << "key_3" << "value3");
M
MYLS 已提交
577
    const std::string expected = "%YAML 1.0\n---\nkey1: value1\n_key2: value2\nkey_3: value3\n";
578 579
    ASSERT_STREQ(f.releaseAndGetString().c_str(), expected.c_str());
}
M
MYLS 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

TEST(Core_InputOutput, filestorage_yml_compatibility)
{
    //EXPECT_ANY_THROW();
}

TEST(Core_InputOutput, filestorage_yml_base64)
{
    cv::Mat _em_out, _em_in;
    cv::Mat _2d_out, _2d_in;
    cv::Mat _nd_out, _nd_in;

    {   /* init */

        /* normal mat */
595
        _2d_out = cv::Mat(100, 100, CV_8UC3, cvScalar(1U, 2U, 127U));
M
MYLS 已提交
596 597 598 599 600 601
        for (int i = 0; i < _2d_out.rows; ++i)
            for (int j = 0; j < _2d_out.cols; ++j)
                _2d_out.at<cv::Vec3b>(i, j)[1] = i % 256;

        /* 4d mat */
        const int Size[] = {4, 4, 4, 4};
602
        cv::Mat _4d(4, Size, CV_64FC4, cvScalar(0.888, 0.111, 0.666, 0.444));
M
MYLS 已提交
603 604 605 606 607
        const cv::Range ranges[] = {
            cv::Range(0, 2),
            cv::Range(0, 2),
            cv::Range(1, 2),
            cv::Range(0, 2) };
M
MYLS 已提交
608 609 610 611 612
        _nd_out = _4d(ranges);
    }

    {   /* write */
        cv::FileStorage fs("test.yml", cv::FileStorage::WRITE);
613 614 615 616 617 618
        CvMat holder = _2d_out;
        cv::cvWriteMat_Base64(*fs, "normal_2d_mat", &holder);
        CvMatND holder_nd = _nd_out;
        cv::cvWriteMatND_Base64(*fs, "normal_nd_mat", &holder_nd);
        holder = _em_out;
        cv::cvWriteMat_Base64(*fs, "empty_2d_mat", &holder);
M
MYLS 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        fs.release();
    }

    {   /* read */
        cv::FileStorage fs("test.yml", cv::FileStorage::READ);
        fs["empty_2d_mat"]  >> _em_in;
        fs["normal_2d_mat"] >> _2d_in;
        fs["normal_nd_mat"] >> _nd_in;
        fs.release();
    }

    EXPECT_EQ(_em_in.rows   , _em_out.rows);
    EXPECT_EQ(_em_in.cols   , _em_out.cols);
    EXPECT_EQ(_em_in.dims   , _em_out.dims);
    EXPECT_EQ(_em_in.depth(), _em_out.depth());
    EXPECT_TRUE(_em_in.empty());

    EXPECT_EQ(_2d_in.rows   , _2d_in.rows);
    EXPECT_EQ(_2d_in.cols   , _2d_in.cols);
    EXPECT_EQ(_2d_in.dims   , _2d_in.dims);
    EXPECT_EQ(_2d_in.depth(), _2d_in.depth());
    for(int i = 0; i < _2d_in.rows; ++i)
        for (int j = 0; j < _2d_in.cols; ++j)
            EXPECT_EQ(_2d_in.at<cv::Vec3b>(i, j), _2d_out.at<cv::Vec3b>(i, j));

    EXPECT_EQ(_nd_in.rows   , _nd_in.rows);
    EXPECT_EQ(_nd_in.cols   , _nd_in.cols);
    EXPECT_EQ(_nd_in.dims   , _nd_in.dims);
    EXPECT_EQ(_nd_in.depth(), _nd_in.depth());
    EXPECT_EQ(cv::countNonZero(cv::mean(_nd_in != _nd_out)), 0);
}

TEST(Core_InputOutput, filestorage_xml_base64)
{
    cv::Mat _em_out, _em_in;
    cv::Mat _2d_out, _2d_in;
    cv::Mat _nd_out, _nd_in;

    {   /* init */

        /* normal mat */
660
        _2d_out = cv::Mat(100, 100, CV_8UC3, cvScalar(1U, 2U, 127U));
M
MYLS 已提交
661 662 663 664 665 666
        for (int i = 0; i < _2d_out.rows; ++i)
            for (int j = 0; j < _2d_out.cols; ++j)
                _2d_out.at<cv::Vec3b>(i, j)[1] = i % 256;

        /* 4d mat */
        const int Size[] = {4, 4, 4, 4};
667
        cv::Mat _4d(4, Size, CV_64FC4, cvScalar(0.888, 0.111, 0.666, 0.444));
M
MYLS 已提交
668 669 670 671 672
        const cv::Range ranges[] = {
            cv::Range(0, 2),
            cv::Range(0, 2),
            cv::Range(1, 2),
            cv::Range(0, 2) };
M
MYLS 已提交
673 674 675 676 677
        _nd_out = _4d(ranges);
    }

    {   /* write */
        cv::FileStorage fs("test.xml", cv::FileStorage::WRITE);
678 679 680 681 682 683
        CvMat holder = _2d_out;
        cv::cvWriteMat_Base64(*fs, "normal_2d_mat", &holder);
        CvMatND holder_nd = _nd_out;
        cv::cvWriteMatND_Base64(*fs, "normal_nd_mat", &holder_nd);
        holder = _em_out;
        cv::cvWriteMat_Base64(*fs, "empty_2d_mat", &holder);
M
MYLS 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
        fs.release();
    }

    {   /* read */
        cv::FileStorage fs("test.xml", cv::FileStorage::READ);
        fs["empty_2d_mat"]  >> _em_in;
        fs["normal_2d_mat"] >> _2d_in;
        fs["normal_nd_mat"] >> _nd_in;
        fs.release();
    }

    EXPECT_EQ(_em_in.rows   , _em_out.rows);
    EXPECT_EQ(_em_in.cols   , _em_out.cols);
    EXPECT_EQ(_em_in.dims   , _em_out.dims);
    EXPECT_EQ(_em_in.depth(), _em_out.depth());
    EXPECT_TRUE(_em_in.empty());

    EXPECT_EQ(_2d_in.rows   , _2d_in.rows);
    EXPECT_EQ(_2d_in.cols   , _2d_in.cols);
    EXPECT_EQ(_2d_in.dims   , _2d_in.dims);
    EXPECT_EQ(_2d_in.depth(), _2d_in.depth());
    for(int i = 0; i < _2d_in.rows; ++i)
        for (int j = 0; j < _2d_in.cols; ++j)
            EXPECT_EQ(_2d_in.at<cv::Vec3b>(i, j), _2d_out.at<cv::Vec3b>(i, j));

    EXPECT_EQ(_nd_in.rows   , _nd_in.rows);
    EXPECT_EQ(_nd_in.cols   , _nd_in.cols);
    EXPECT_EQ(_nd_in.dims   , _nd_in.dims);
    EXPECT_EQ(_nd_in.depth(), _nd_in.depth());
713
    EXPECT_EQ(cv::countNonZero(cv::sum(_nd_in != _nd_out)), 0);
M
MYLS 已提交
714
}