Files
dat-thang/tests/test_paths.c
T

42 lines
1.1 KiB
C
Raw Normal View History

2026-10-19 00:12:36 -04:00
#include "dat_paths.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <process.h>
#define test_pid _getpid
#else
#include <unistd.h>
#define test_pid getpid
#endif
static void write_text(const char *path, const char *text)
{
FILE *file = fopen(path, "wb");
assert(file);
assert(fwrite(text, 1, strlen(text), file) == strlen(text));
assert(fclose(file) == 0);
}
int main(void)
{
char destination[256], temporary[256], contents[32];
FILE *file;
int n;
n = snprintf(destination, sizeof(destination), "build/test-paths-%ld", (long)test_pid());
assert(n > 0 && (size_t)n < sizeof(destination));
n = snprintf(temporary, sizeof(temporary), "%s.tmp", destination);
assert(n > 0 && (size_t)n < sizeof(temporary));
write_text(destination, "before");
write_text(temporary, "after");
assert(dat_replace_file(temporary, destination) == 0);
file = fopen(destination, "rb");
assert(file);
assert(fgets(contents, sizeof(contents), file));
assert(fclose(file) == 0);
assert(strcmp(contents, "after") == 0);
assert(remove(destination) == 0);
puts("user-state atomic replacement: PASS");
return 0;
}