Skip to content

Commit 3ba15d5

Browse files
committed
Add a small test for DeferredComputation
1 parent 2301ea2 commit 3ba15d5

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

include/openPMD/auxiliary/Future.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class DeferredComputation
124124

125125
/** Check if the computation is valid
126126
*
127-
* @return true if the computation has not been forgotten
127+
* @return true if the computation has not been invalidated
128128
*/
129129
[[nodiscard]] auto valid() const noexcept -> bool;
130130
};

src/auxiliary/Future.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "openPMD/auxiliary/Future.hpp"
2+
#include "openPMD/Error.hpp"
23
#include "openPMD/RecordComponent.hpp"
34

45
#include <iostream>
@@ -39,13 +40,13 @@ auto OneTimeTask<T>::operator()() -> T
3940
{
4041
if (!members.m_task_valid)
4142
{
42-
throw std::runtime_error(
43+
throw error::WrongAPIUsage(
4344
"[DeferredComputation] No valid state. Probably already "
4445
"computed.");
4546
}
4647
if (!members.m_task)
4748
{
48-
throw std::runtime_error(
49+
throw error::WrongAPIUsage(
4950
"[DeferredComputation] No valid task was specified.");
5051
}
5152
members.m_task_valid = false;
@@ -168,6 +169,7 @@ auto DeferredComputation<T>::valid() const noexcept -> bool
168169

169170
template class DeferredComputation<void>;
170171
template class DeferredComputation<RecordComponent::shared_ptr_dataset_types>;
172+
template class DeferredComputation<std::string>; // used in tests
171173

172174
// need this for clang-tidy
173175
#define OPENPMD_ARRAY(type) type[]

test/AuxiliaryTest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121
// expose private and protected members for invasive testing
22+
#include "openPMD/Error.hpp"
23+
#include "openPMD/auxiliary/Future.hpp"
2224
#if openPMD_USE_INVASIVE_TESTS
2325
#define OPENPMD_private public:
2426
#define OPENPMD_protected public:
@@ -538,3 +540,31 @@ TEST_CASE("filesystem_test", "[auxiliary]")
538540
REQUIRE(!remove_file("./nonexistent_file_in_cmake_bin_directory"));
539541
#endif
540542
}
543+
544+
TEST_CASE("future_test", "[auxiliary]")
545+
{
546+
using task_type = auxiliary::DeferredComputation<std::string>;
547+
size_t counter = 0;
548+
549+
auto make_task = [&counter]() {
550+
counter = 0;
551+
return task_type{[&counter]() {
552+
++counter;
553+
return "success";
554+
}};
555+
};
556+
557+
auto move_construct = make_task();
558+
task_type move_constructed(std::move(move_construct));
559+
REQUIRE(counter == 0);
560+
REQUIRE(move_constructed() == "success");
561+
REQUIRE(counter == 1);
562+
REQUIRE_THROWS_AS(move_constructed(), error::WrongAPIUsage);
563+
564+
auto move_assign = make_task();
565+
task_type move_assigned = std::move(move_assign);
566+
REQUIRE(counter == 0);
567+
REQUIRE(move_assigned() == "success");
568+
REQUIRE(counter == 1);
569+
REQUIRE_THROWS_AS(move_assigned(), error::WrongAPIUsage);
570+
}

0 commit comments

Comments
 (0)