transm/test/assistant.h

58 lines
1.6 KiB
C
Raw Normal View History

#ifndef ASSISTANT_H
#define ASSISTANT_H
#include <fstream>
#include <random>
#include <string>
2025-04-13 00:08:50 +08:00
#include <functional>
#ifdef USE_BOOST
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
bool random_file(const std::string& file, size_t size);
/**
* @brief
*
* @tparam T
* @tparam Compare
* @param value
* @param expected
* @param comparator ( std::equal_to)
* @param timeout_ms ()0
* @param interval_ms ()
* @return true
* @return false
*/
template <typename T, typename Compare = std::equal_to<T>>
2025-04-13 00:08:50 +08:00
bool value_wait(const std::function<T()>& value_ref, const T& expected, Compare comparator = Compare(),
unsigned long timeout_ms = 0, unsigned long interval_ms = 100)
{
auto start = std::chrono::steady_clock::now();
while (true) {
2025-04-13 00:08:50 +08:00
T value = value_ref();
if (comparator(value, expected)) {
return true;
}
if (timeout_ms > 0) {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start)
.count();
if (elapsed >= timeout_ms) {
return false;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
}
}
#endif // ASSISTANT_H