Browse Source

tests: add test_utils.h to git (was ignored by tests/test_* rule)

topo_upd
evgeny 2 months ago
parent
commit
9066ae6a1f
  1. 1
      .gitignore
  2. 73
      tests/test_utils.h

1
.gitignore vendored

@ -77,6 +77,7 @@ logs/
# Test binaries (executables without extension), but track source/config files
tests/test_*
!tests/test_*.c
!tests/test_*.h
!tests/test_*.conf
!tests/test_*.json
tests/bench_*

73
tests/test_utils.h

@ -0,0 +1,73 @@
/**
* @file test_utils.h
* @brief Utility functions for cross-platform tests
*/
#ifndef TEST_UTILS_H
#define TEST_UTILS_H
#include "../lib/platform_compat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
// Windows temp directory buffer
static char test_temp_dir[MAX_PATH];
// Cross-platform mkdtemp for Windows
static inline int test_mkdtemp(char *template_str) {
char tmp_path[MAX_PATH];
GetTempPathA(MAX_PATH, tmp_path);
// Generate unique directory name
srand((unsigned int)GetTickCount());
int attempts = 0;
while (attempts < 100) {
snprintf(test_temp_dir, sizeof(test_temp_dir), "%s\\utun_test_%08x",
tmp_path, (unsigned int)rand());
if (_mkdir(test_temp_dir) == 0) {
// Copy path back to caller's buffer if provided
if (template_str) {
size_t len = strlen(test_temp_dir);
memcpy(template_str, test_temp_dir, len + 1); // Include null terminator
}
return 0; // Success
}
attempts++;
}
return -1; // Failed after 100 attempts
}
// Cross-platform unlink for Windows
static inline int test_unlink(const char *path) {
return _unlink(path);
}
// Cross-platform rmdir for Windows
static inline int test_rmdir(const char *path) {
return _rmdir(path);
}
#else
// Linux/macOS - use standard functions
#include <unistd.h>
static inline int test_mkdtemp(char *template_str) {
return (mkdtemp(template_str) == NULL) ? -1 : 0;
}
static inline int test_unlink(const char *path) {
return unlink(path);
}
static inline int test_rmdir(const char *path) {
return rmdir(path);
}
#endif
#endif // TEST_UTILS_H
Loading…
Cancel
Save