157 lines
4.5 KiB
C++
157 lines
4.5 KiB
C++
// ================================================================================================
|
|
// Audio Backend - 测试工具实现
|
|
// ================================================================================================
|
|
|
|
#include "test_utils.h"
|
|
#include <iostream>
|
|
#include <algorithm>
|
|
|
|
namespace audio_backend {
|
|
namespace test {
|
|
|
|
// 随机数生成器
|
|
static std::random_device rd;
|
|
static std::mt19937 gen(rd());
|
|
|
|
float TestUtils::generate_random_float(float min, float max) {
|
|
std::uniform_real_distribution<float> dist(min, max);
|
|
return dist(gen);
|
|
}
|
|
|
|
int TestUtils::generate_random_int(int min, int max) {
|
|
std::uniform_int_distribution<int> dist(min, max);
|
|
return dist(gen);
|
|
}
|
|
|
|
void TestUtils::generate_random_audio_data(float* buffer, size_t size, float min, float max) {
|
|
for (size_t i = 0; i < size; ++i) {
|
|
buffer[i] = generate_random_float(min, max);
|
|
}
|
|
}
|
|
|
|
std::string TestUtils::generate_random_string(size_t length) {
|
|
static const char charset[] =
|
|
"0123456789"
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"abcdefghijklmnopqrstuvwxyz";
|
|
|
|
std::string result;
|
|
result.resize(length);
|
|
|
|
std::uniform_int_distribution<size_t> dist(0, sizeof(charset) - 2);
|
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
result[i] = charset[dist(gen)];
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
std::string TestUtils::create_temp_file(const std::string& content, const std::string& extension) {
|
|
namespace fs = std::filesystem;
|
|
|
|
// 创建临时文件名
|
|
std::string temp_dir = fs::temp_directory_path().string();
|
|
std::string temp_filename = "audio_backend_test_" + generate_random_string(8) + extension;
|
|
std::string temp_path = (fs::path(temp_dir) / temp_filename).string();
|
|
|
|
// 写入文件内容
|
|
std::ofstream file(temp_path, std::ios::binary);
|
|
if (file.is_open()) {
|
|
file.write(content.c_str(), content.size());
|
|
file.close();
|
|
return temp_path;
|
|
} else {
|
|
std::cerr << "Error creating temporary file: " << temp_path << std::endl;
|
|
return "";
|
|
}
|
|
}
|
|
|
|
bool TestUtils::delete_temp_file(const std::string& filepath) {
|
|
try {
|
|
return std::filesystem::remove(filepath);
|
|
} catch (const std::filesystem::filesystem_error& e) {
|
|
std::cerr << "Error deleting temporary file: " << e.what() << std::endl;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
bool TestUtils::compare_audio_buffers(const float* buffer1, const float* buffer2, size_t size, float epsilon) {
|
|
for (size_t i = 0; i < size; ++i) {
|
|
if (std::abs(buffer1[i] - buffer2[i]) > epsilon) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int64_t TestUtils::measure_execution_time(const std::function<void()>& func) {
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
func();
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
|
|
}
|
|
|
|
std::string TestUtils::get_test_data_dir() {
|
|
namespace fs = std::filesystem;
|
|
|
|
// 使用相对于构建目录的路径
|
|
auto test_data_dir = fs::current_path() / "test_data";
|
|
|
|
// 确保目录存在
|
|
if (!fs::exists(test_data_dir)) {
|
|
fs::create_directories(test_data_dir);
|
|
}
|
|
|
|
return test_data_dir.string();
|
|
}
|
|
|
|
bool TestUtils::ensure_directory_exists(const std::string& dir) {
|
|
try {
|
|
namespace fs = std::filesystem;
|
|
if (!fs::exists(dir)) {
|
|
return fs::create_directories(dir);
|
|
}
|
|
return true;
|
|
} catch (const std::filesystem::filesystem_error& e) {
|
|
std::cerr << "Error creating directory: " << e.what() << std::endl;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// PerformanceTimer 实现
|
|
PerformanceTimer::PerformanceTimer(const std::string& label)
|
|
: label_(label),
|
|
start_time_(std::chrono::high_resolution_clock::now()),
|
|
running_(true) {
|
|
}
|
|
|
|
PerformanceTimer::~PerformanceTimer() {
|
|
if (running_) {
|
|
stop();
|
|
}
|
|
}
|
|
|
|
int64_t PerformanceTimer::stop() {
|
|
if (!running_) {
|
|
return 0;
|
|
}
|
|
|
|
auto end_time = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time_).count();
|
|
|
|
std::cout << "[PERFORMANCE] " << label_ << ": " << duration << " us ("
|
|
<< (duration / 1000.0) << " ms)" << std::endl;
|
|
|
|
running_ = false;
|
|
return duration;
|
|
}
|
|
|
|
void PerformanceTimer::restart() {
|
|
start_time_ = std::chrono::high_resolution_clock::now();
|
|
running_ = true;
|
|
}
|
|
|
|
} // namespace test
|
|
} // namespace audio_backend
|