opencv/modules/core/test/test_async.cpp
Sean McBride e64857c561
Merge pull request #23736 from seanm:c++11-simplifications
Removed all pre-C++11 code, workarounds, and branches #23736

This removes a bunch of pre-C++11 workrarounds that are no longer necessary as C++11 is now required.
It is a nice clean up and simplification.

* No longer unconditionally #include <array> in cvdef.h, include explicitly where needed
* Removed deprecated CV_NODISCARD, already unused in the codebase
* Removed some pre-C++11 workarounds, and simplified some backwards compat defines
* Removed CV_CXX_STD_ARRAY
* Removed CV_CXX_MOVE_SEMANTICS and CV_CXX_MOVE
* Removed all tests of CV_CXX11, now assume it's always true. This allowed removing a lot of dead code.
* Updated some documentation consequently.
* Removed all tests of CV_CXX11, now assume it's always true
* Fixed links.

---------

Co-authored-by: Maksim Shabunin <maksim.shabunin@gmail.com>
Co-authored-by: Alexander Smorkalov <alexander.smorkalov@xperience.ai>
2024-01-19 16:53:08 +03:00

156 lines
3.6 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"
#include <opencv2/core/async.hpp>
#include <opencv2/core/detail/async_promise.hpp>
#include <opencv2/core/bindings_utils.hpp>
#if !defined(OPENCV_DISABLE_THREAD_SUPPORT)
#include <thread>
#include <chrono>
#endif
namespace opencv_test { namespace {
TEST(Core_Async, BasicCheck)
{
Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
AsyncPromise p;
AsyncArray r = p.getArrayResult();
EXPECT_TRUE(r.valid());
// Follow the limitations of std::promise::get_future
// https://en.cppreference.com/w/cpp/thread/promise/get_future
EXPECT_THROW(AsyncArray r2 = p.getArrayResult(), cv::Exception);
p.setValue(m);
Mat m2;
r.get(m2);
EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));
// Follow the limitations of std::future::get
// https://en.cppreference.com/w/cpp/thread/future/get
EXPECT_FALSE(r.valid());
Mat m3;
EXPECT_THROW(r.get(m3), cv::Exception);
}
TEST(Core_Async, ExceptionCheck)
{
Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
AsyncPromise p;
AsyncArray r = p.getArrayResult();
EXPECT_TRUE(r.valid());
try
{
CV_Error(Error::StsOk, "Test: Generated async error");
}
catch (const cv::Exception& e)
{
p.setException(e);
}
try {
Mat m2;
r.get(m2);
FAIL() << "Exception is expected";
}
catch (const cv::Exception& e)
{
EXPECT_EQ(Error::StsOk, e.code) << e.what();
}
// Follow the limitations of std::future::get
// https://en.cppreference.com/w/cpp/thread/future/get
EXPECT_FALSE(r.valid());
}
TEST(Core_Async, LikePythonTest)
{
Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
AsyncArray r = cv::utils::testAsyncArray(m);
EXPECT_TRUE(r.valid());
Mat m2;
r.get(m2);
EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));
// Follow the limitations of std::future::get
// https://en.cppreference.com/w/cpp/thread/future/get
EXPECT_FALSE(r.valid());
}
#if !defined(OPENCV_DISABLE_THREAD_SUPPORT)
TEST(Core_Async, AsyncThread_Simple)
{
Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
AsyncPromise p;
AsyncArray r = p.getArrayResult();
std::thread t([&]{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
try {
p.setValue(m);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "Unknown C++ exception" << std::endl;
}
});
try
{
Mat m2;
r.get(m2);
EXPECT_EQ(0, cvtest::norm(m, m2, NORM_INF));
t.join();
}
catch (...)
{
t.join();
throw;
}
}
TEST(Core_Async, AsyncThread_DetachedResult)
{
Mat m(3, 3, CV_32FC1, Scalar::all(5.0f));
AsyncPromise p;
{
AsyncArray r = p.getArrayResult();
r.release();
}
bool exception_ok = false;
std::thread t([&]{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
try {
p.setValue(m);
} catch (const cv::Exception& e) {
if (e.code == Error::StsError)
exception_ok = true;
else
std::cout << e.what() << std::endl;
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "Unknown C++ exception" << std::endl;
}
});
t.join();
EXPECT_TRUE(exception_ok);
}
#endif
}} // namespace