Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 41 additions & 38 deletions modules/core/test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ class CV_MiscIOTest : public cvtest::BaseTest
CV_Assert( ov1 == v1 );
CV_Assert( osc1 == sc1 );
CV_Assert( og1 == g1 );
fs.release();
remove(fname.c_str());
}
catch(...)
{
Expand Down Expand Up @@ -489,6 +491,7 @@ TEST(Core_InputOutput, FileStorage)
char arr[66];
snprintf(arr, sizeof(arr), "snprintf is hell %d", 666);
EXPECT_NO_THROW(f << arr);
remove(file.c_str());
}

TEST(Core_InputOutput, FileStorageKey)
Expand Down Expand Up @@ -534,6 +537,7 @@ TEST(Core_InputOutput, FileStorageSpaces)
ASSERT_STREQ(values[i].c_str(), valuesReadAppend[i].c_str());
}
g3.release();
EXPECT_EQ(0, remove(fileName.c_str()));
}

struct data_t
Expand Down Expand Up @@ -585,12 +589,15 @@ struct data_t

static void test_filestorage_basic(int write_flags, const char* suffix_name, bool testReadWrite, bool useMemory = false)
{
const bool generateTestData = false; // enable to regenerate reference in opencv_extra
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
CV_Assert(test_info);
std::string name = (std::string(test_info->test_case_name()) + "--" + test_info->name() + suffix_name);
std::string name_34 = string(cvtest::TS::ptr()->get_data_path()) + "io/3_4/" + name;
if (!testReadWrite)
if (!testReadWrite || generateTestData)
name = string(cvtest::TS::ptr()->get_data_path()) + "io/" + name;
else
name = cv::tempfile(name.c_str());

{
const size_t rawdata_N = 40;
Expand Down Expand Up @@ -636,10 +643,7 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
rawdata.push_back(tmp);
}
}
#ifdef GENERATE_TEST_DATA
#else
if (testReadWrite || useMemory)
#endif
if (testReadWrite || useMemory || generateTestData)
{
cv::FileStorage fs(name, write_flags + (useMemory ? cv::FileStorage::MEMORY : 0));
fs << "normal_2d_mat" << _2d_out;
Expand Down Expand Up @@ -684,6 +688,7 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
}
std::cout << "Storage size: " << sz << std::endl;
EXPECT_LE(sz, (size_t)6000);

}
{ /* read */
cv::FileStorage fs(name, cv::FileStorage::READ + (useMemory ? cv::FileStorage::MEMORY : 0));
Expand Down Expand Up @@ -761,6 +766,10 @@ static void test_filestorage_basic(int write_flags, const char* suffix_name, boo
ASSERT_EQ(_rd_in.dims , _rd_out.dims);
ASSERT_EQ(_rd_in.depth(), _rd_out.depth());
EXPECT_EQ(0, cv::norm(_rd_in, _rd_out, NORM_INF));
if (testReadWrite && !useMemory && !generateTestData)
{
EXPECT_EQ(0, remove(name.c_str()));
}
}
}

Expand Down Expand Up @@ -807,7 +816,7 @@ TEST(Core_InputOutput, filestorage_heap_overflow)
const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
CV_Assert(test_info);

std::string name = std::string(test_info->test_case_name()) + "--" + test_info->name();
std::string name = cv::tempfile();
const char data[] = {0x00, 0x2f, 0x4a, 0x4a, 0x50, 0x4a, 0x4a };

std::ofstream file;
Expand All @@ -819,6 +828,7 @@ TEST(Core_InputOutput, filestorage_heap_overflow)

// This just shouldn't segfault, otherwise it's fine
EXPECT_ANY_THROW(FileStorage(name, FileStorage::READ));
EXPECT_EQ(0, remove(name.c_str()));
}

TEST(Core_InputOutput, filestorage_base64_valid_call)
Expand All @@ -829,18 +839,6 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)
: (std::string(test_info->test_case_name()) + "--" + test_info->name());

char const * filenames[] = {
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
"core_io_base64_other_test.yml?base64",
"core_io_base64_other_test.xml?base64",
"core_io_base64_other_test.json?base64",
0
};
char const * real_name[] = {
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
"core_io_base64_other_test.yml",
"core_io_base64_other_test.xml",
"core_io_base64_other_test.json",
Comment on lines -832 to 844
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this test we iterate over 3 formats (yml, xml,json) and 2 modes (default, base64). These two arrays contained 6 items each for actual file name and accessed file name (with base64 suffix). I've replaced these arrays with calculated values: name = filename[n/2], suffix = n%2 ? '' : 'base64'.

Expand All @@ -852,14 +850,16 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)

for (int n = 0; n < 6; n++)
{
char const* suffix_name = filenames[n];
SCOPED_TRACE(suffix_name);
std::string name = basename + '_' + suffix_name;
std::string file_name = basename + '_' + real_name[n];
const int idx = n / 2;
const std::string mode_suffix = (n % 2 == 0) ? "" : "?base64";
std::string suffix_name = basename + "_" + filenames[idx];
std::string file_name = cv::tempfile(suffix_name.c_str());
std::string mode_file_name = file_name + mode_suffix;
SCOPED_TRACE(mode_file_name);

EXPECT_NO_THROW(
{
cv::FileStorage fs(name, cv::FileStorage::WRITE_BASE64);
cv::FileStorage fs(mode_file_name, cv::FileStorage::WRITE_BASE64);

fs << "manydata" << "[";
fs << "[:";
Expand Down Expand Up @@ -887,7 +887,7 @@ TEST(Core_InputOutput, filestorage_base64_valid_call)

EXPECT_NO_THROW(
{
cv::FileStorage fs(name, cv::FileStorage::WRITE);
cv::FileStorage fs(mode_file_name, cv::FileStorage::WRITE);

fs << "manydata" << "[";
fs << str_out;
Expand Down Expand Up @@ -931,10 +931,10 @@ TEST(Core_InputOutput, filestorage_base64_invalid_call)
0
};

for (char const ** ptr = filenames; *ptr; ptr++)
for (int idx = 0; idx < 3; ++idx)
{
char const * suffix_name = *ptr;
std::string name = basename + '_' + suffix_name;
const string base_suffix = basename + '_' + filenames[idx];
std::string name = cv::tempfile(base_suffix.c_str());

EXPECT_NO_THROW({
cv::FileStorage fs(name, cv::FileStorage::WRITE);
Expand All @@ -955,7 +955,7 @@ TEST(Core_InputOutput, filestorage_base64_invalid_call)

TEST(Core_InputOutput, filestorage_yml_vec2i)
{
const std::string file_name = "vec2i.yml";
const std::string file_name = cv::tempfile("vec2i.yml");
cv::Vec2i vec(2, 1), ovec;

/* write */
Expand Down Expand Up @@ -1037,7 +1037,7 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)
}
}

String fileName = "vec_vec_io_test.";
String basename = "vec_vec_io_test.";

std::vector<String> formats;
formats.push_back("xml");
Expand All @@ -1046,11 +1046,13 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)

for(size_t i = 0; i < formats.size(); i++)
{
FileStorage writer(fileName + formats[i], FileStorage::WRITE);
const String basename_plus(basename + formats[i]);
const String fileName = tempfile(basename_plus.c_str());
FileStorage writer(fileName, FileStorage::WRITE);
writer << "vecVecMat" << outputMats;
writer.release();

FileStorage reader(fileName + formats[i], FileStorage::READ);
FileStorage reader(fileName, FileStorage::READ);
std::vector<std::vector<Mat> > testMats;
reader["vecVecMat"] >> testMats;

Expand All @@ -1067,7 +1069,7 @@ TEST(Core_InputOutput, filestorage_vec_vec_io)
}

reader.release();
remove((fileName + formats[i]).c_str());
remove(fileName.c_str());
}
}

Expand Down Expand Up @@ -1658,7 +1660,7 @@ TEST(Core_InputOutput, FileStorage_json_bool)

TEST(Core_InputOutput, FileStorage_free_file_after_exception)
{
const std::string fileName = "FileStorage_free_file_after_exception_test.yml";
const std::string fileName = cv::tempfile("FileStorage_free_file_after_exception_test.yml");
const std::string content = "%YAML:1.0\n cameraMatrix;:: !<tag:yaml.org,2002:opencv-matrix>\n";

std::fstream testFile;
Expand All @@ -1681,32 +1683,33 @@ TEST(Core_InputOutput, FileStorage_free_file_after_exception)
TEST(Core_InputOutput, FileStorage_write_to_sequence)
{
const std::vector<std::string> formatExts = { ".yml", ".json", ".xml" };
const std::string fileName = "FileStorage_write_to_sequence";

for (const auto& ext : formatExts)
{
FileStorage fs(fileName + ext, FileStorage::WRITE);
const std::string name = tempfile(ext.c_str());

FileStorage fs(name, FileStorage::WRITE);
std::vector<int> in = { 23, 42 };
fs.startWriteStruct("some_sequence", cv::FileNode::SEQ);
for (int i : in)
fs.write("", i);
fs.endWriteStruct();
fs.release();

FileStorage fsIn(fileName + ext, FileStorage::READ);
FileStorage fsIn(name, FileStorage::READ);
FileNode seq = fsIn["some_sequence"];
FileNodeIterator it = seq.begin(), it_end = seq.end();
std::vector<int> out;
for (; it != it_end; ++it)
out.push_back((int)*it);

EXPECT_EQ(in, out);
EXPECT_EQ(0, remove(name.c_str()));
}
}

TEST(Core_InputOutput, FileStorage_YAML_parse_multiple_documents)
{
const std::string filename = "FileStorage_YAML_parse_multiple_documents.yml";
const std::string filename = cv::tempfile("FileStorage_YAML_parse_multiple_documents.yml");
FileStorage fs;

fs.open(filename, FileStorage::WRITE);
Expand Down
6 changes: 4 additions & 2 deletions modules/core/test/test_mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,21 @@ TEST(Core_PCA, accuracy)
ASSERT_LE(err, diffBackPrjEps) << "bad accuracy of cvBackProjectPCA() (CV_PCA_DATA_AS_COL)";
#endif
// Test read and write
FileStorage fs( "PCA_store.yml", FileStorage::WRITE );
const std::string filename = cv::tempfile("PCA_store.yml");
FileStorage fs( filename, FileStorage::WRITE );
rPCA.write( fs );
fs.release();

PCA lPCA;
fs.open( "PCA_store.yml", FileStorage::READ );
fs.open( filename, FileStorage::READ );
lPCA.read( fs.root() );
err = cvtest::norm(rPCA.eigenvectors, lPCA.eigenvectors, NORM_L2 | NORM_RELATIVE);
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
err = cvtest::norm(rPCA.eigenvalues, lPCA.eigenvalues, NORM_L2 | NORM_RELATIVE);
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
err = cvtest::norm(rPCA.mean, lPCA.mean, NORM_L2 | NORM_RELATIVE);
EXPECT_LE(err, 0) << "bad accuracy of write/load functions (YML)";
EXPECT_EQ(0, remove(filename.c_str()));
}

class Core_ArrayOpTest : public cvtest::BaseTest
Expand Down
1 change: 1 addition & 0 deletions modules/imgcodecs/test/test_tiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ TEST(Imgcodecs_Tiff_Modes, write_multipage)
{
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), read_pages[i], pages[i]);
}
EXPECT_EQ(0, remove(tmp_filename.c_str()));
}

//==================================================================================================
Expand Down