zvec/tests/test_util.h

72 lines
1.8 KiB
C++

// Copyright 2025-present the zvec project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <cstdlib>
#include <string>
#include <zvec/ailego/utility/file_helper.h>
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#include <chrono>
#include <thread>
#endif
namespace zvec {
namespace test_util {
inline void RemoveTestPath(const std::string &path) {
if (!ailego::FileHelper::RemovePath(path.c_str())) {
#ifdef _WIN32
system(("rmdir /s /q \"" + path + "\" 2>NUL").c_str());
#endif
}
}
inline void RemoveTestFiles(const std::string &pattern) {
if (pattern.find('*') != std::string::npos ||
pattern.find('?') != std::string::npos) {
#ifdef _WIN32
system(("del /f /q " + pattern + " 2>NUL").c_str());
#else
system(("rm -rf " + pattern).c_str());
#endif
} else {
ailego::FileHelper::RemovePath(pattern.c_str());
}
}
} // namespace test_util
} // namespace zvec
#ifdef _MSC_VER
inline void sleep(unsigned int seconds) {
std::this_thread::sleep_for(std::chrono::seconds(seconds));
}
inline void usleep(unsigned int microseconds) {
std::this_thread::sleep_for(std::chrono::microseconds(microseconds));
}
inline int truncate(const char *path, long length) {
int fd = _open(path, _O_RDWR);
if (fd < 0) return -1;
int ret = _chsize(fd, length);
_close(fd);
return ret;
}
#endif