1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #25351 from Kumataro:fix25073_format_g

core: persistence: output reals as human-friendly expression. #25351

Close #25073
Related https://github.com/opencv/opencv/pull/25087

This patch is need to merge same time with https://github.com/opencv/opencv_contrib/pull/3714

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Kumataro
2024-04-10 21:17:15 +09:00
committed by GitHub
parent cf3a130be6
commit b14ea19466
9 changed files with 81 additions and 52 deletions
+57 -39
View File
@@ -1189,11 +1189,7 @@ TEST(Core_InputOutput, FileStorage_DMatch)
EXPECT_NO_THROW(fs << "d" << d);
cv::String fs_result = fs.releaseAndGetString();
#if defined _MSC_VER && _MSC_VER <= 1800 /* MSVC 2013 and older */
EXPECT_STREQ(fs_result.c_str(), "%YAML:1.0\n---\nd: [ 1, 2, 3, -1.5000000000000000e+000 ]\n");
#else
EXPECT_STREQ(fs_result.c_str(), "%YAML:1.0\n---\nd: [ 1, 2, 3, -1.5000000000000000e+00 ]\n");
#endif
EXPECT_STREQ(fs_result.c_str(), "%YAML:1.0\n---\nd: [ 1, 2, 3, -1.5 ]\n");
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
@@ -1220,25 +1216,14 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector)
EXPECT_NO_THROW(fs << "dv" << dv);
cv::String fs_result = fs.releaseAndGetString();
#if defined _MSC_VER && _MSC_VER <= 1800 /* MSVC 2013 and older */
EXPECT_STREQ(fs_result.c_str(),
"%YAML:1.0\n"
"---\n"
"dv:\n"
" - [ 1, 2, 3, -1.5000000000000000e+000 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+000 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-001 ]\n"
" - [ 1, 2, 3, -1.5 ]\n"
" - [ 2, 3, 4, 1.5 ]\n"
" - [ 3, 2, 1, 0.5 ]\n"
);
#else
EXPECT_STREQ(fs_result.c_str(),
"%YAML:1.0\n"
"---\n"
"dv:\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+00 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
);
#endif
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
@@ -1278,33 +1263,18 @@ TEST(Core_InputOutput, FileStorage_DMatch_vector_vector)
EXPECT_NO_THROW(fs << "dvv" << dvv);
cv::String fs_result = fs.releaseAndGetString();
#ifndef OPENCV_TRAITS_ENABLE_DEPRECATED
#if defined _MSC_VER && _MSC_VER <= 1800 /* MSVC 2013 and older */
EXPECT_STREQ(fs_result.c_str(),
"%YAML:1.0\n"
"---\n"
"dvv:\n"
" -\n"
" - [ 1, 2, 3, -1.5000000000000000e+000 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+000 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-001 ]\n"
" - [ 1, 2, 3, -1.5 ]\n"
" - [ 2, 3, 4, 1.5 ]\n"
" - [ 3, 2, 1, 0.5 ]\n"
" -\n"
" - [ 3, 2, 1, 5.0000000000000000e-001 ]\n"
" - [ 1, 2, 3, -1.5000000000000000e+000 ]\n"
" - [ 3, 2, 1, 0.5 ]\n"
" - [ 1, 2, 3, -1.5 ]\n"
);
#else
EXPECT_STREQ(fs_result.c_str(),
"%YAML:1.0\n"
"---\n"
"dvv:\n"
" -\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
" - [ 2, 3, 4, 1.5000000000000000e+00 ]\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
" -\n"
" - [ 3, 2, 1, 5.0000000000000000e-01 ]\n"
" - [ 1, 2, 3, -1.5000000000000000e+00 ]\n"
);
#endif
#endif // OPENCV_TRAITS_ENABLE_DEPRECATED
cv::FileStorage fs_read(fs_result, cv::FileStorage::READ | cv::FileStorage::MEMORY);
@@ -1966,5 +1936,53 @@ TEST(Core_InputOutput, FileStorage_invalid_path_regression_21448_JSON)
fs.release();
}
// see https://github.com/opencv/opencv/issues/25073
typedef testing::TestWithParam< std::string > Core_InputOutput_regression_25073;
TEST_P(Core_InputOutput_regression_25073, my_double)
{
cv::String res = "";
double my_double = 0.5;
FileStorage fs( GetParam(), cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
EXPECT_NO_THROW( fs << "my_double" << my_double );
EXPECT_NO_THROW( fs << "my_int" << 5 );
EXPECT_NO_THROW( res = fs.releaseAndGetString() );
EXPECT_NE( res.find("0.5"), String::npos ) << res; // Found "0.5"
EXPECT_EQ( res.find("5.0"), String::npos ) << res; // Not Found "5.000000000000000000e-01"
fs.release();
}
TEST_P(Core_InputOutput_regression_25073, my_float)
{
cv::String res = "";
float my_float = 0.5;
FileStorage fs( GetParam(), cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
EXPECT_NO_THROW( fs << "my_float" << my_float );
EXPECT_NO_THROW( fs << "my_int" << 5 );
EXPECT_NO_THROW( res = fs.releaseAndGetString() );
EXPECT_NE( res.find("0.5"), String::npos ) << res; // Found "0.5"
EXPECT_EQ( res.find("5.0"), String::npos ) << res; // Not Found "5.00000000e-01",
fs.release();
}
TEST_P(Core_InputOutput_regression_25073, my_float16)
{
cv::String res = "";
cv::float16_t my_float16(0.5);
FileStorage fs( GetParam(), cv::FileStorage::WRITE | cv::FileStorage::MEMORY);
EXPECT_NO_THROW( fs << "my_float16" << my_float16 );
EXPECT_NO_THROW( fs << "my_int" << 5 );
EXPECT_NO_THROW( res = fs.releaseAndGetString() );
EXPECT_NE( res.find("0.5"), String::npos ) << res; // Found "0.5".
EXPECT_EQ( res.find("5.0"), String::npos ) << res; // Not Found "5.0000e-01".
fs.release();
}
INSTANTIATE_TEST_CASE_P( /*nothing*/,
Core_InputOutput_regression_25073,
Values("test.json", "test.xml", "test.yml") );
}} // namespace