- Remove dependency on test tmp library for stdlib compiler tests. #2800

This commit is contained in:
Christoffer Lerno
2026-01-22 22:15:34 +01:00
parent 1845a515ca
commit 17f3db835c
5 changed files with 30 additions and 27 deletions

View File

@@ -1,21 +1,20 @@
module std::io::file @if(env::LIBC &&& env::POSIX);
import libc;
import std::io;
tlocal char[1024] buf;
fn void write_file(String path, char[] data)
fn String random_name()
{
File f = file::open(path, "wb")!!;
defer f.close()!!;
f.write(data)!!;
return libc::tmpnam(&buf).str_view();
}
fn void map_read_only() @test
{
const String FNAME = "tmp/map_test_ro";
String fname = random_name();
char[] msg = "Hello, world";
write_file(FNAME, msg);
file::save(fname, msg)!!;
File f = file::open(FNAME, "rb")!!;
File f = file::open(fname, "rb")!!;
defer f.close()!!;
FileMmap mapped_file = file::mmap_file(f, 0, 0, vm::VirtualMemoryAccess.READ)!!;
@@ -31,12 +30,12 @@ fn void map_read_only() @test
fn void map_read_write() @test
{
const String FNAME = "tmp/map_test_rw";
String fname = random_name();
char[4] data = "ABCD";
write_file(FNAME, data[..]);
file::save(fname, &data)!!;
bool shared = true;
File f = file::open(FNAME, "r+")!!;
File f = file::open(fname, "r+")!!;
mmap::FileMmap region = file::mmap_file(f, 0, 0, vm::VirtualMemoryAccess.READWRITE, shared)!!;
region.bytes()[1] = 'Z';
@@ -44,7 +43,7 @@ fn void map_read_write() @test
region.destroy()!!;
f.close()!!;
File fr = file::open(FNAME, "r")!!;
File fr = file::open(fname, "r")!!;
defer fr.close()!!;
char[4] check;
fr.read(check[..])!!;
@@ -71,37 +70,38 @@ fn void map_with_offset(String fname, usz offset, usz size, char[] reference_msg
fn void map_read_only_with_offset_and_size_zero() @test
{
const String FNAME = "tmp/map_test_ro_with_offset";
char[] msg = "Hello, world";
write_file(FNAME, msg);
String fname = random_name();
char[] msg = "Hello, world";
file::save(fname, msg)!!;
usz map_size = 0;
for (usz i = 0; i < msg.len; i += 1)
{
map_with_offset(FNAME, i, map_size, msg);
map_with_offset(fname, i, map_size, msg);
}
}
fn void map_read_only_with_offset() @test
{
const String FNAME = "tmp/map_test_ro_with_offset";
String fname = random_name();
char[] msg = "Hello, world";
write_file(FNAME, msg);
file::save(fname, msg)!!;
usz map_size = 3;
for (usz i = 0; i < msg.len - map_size; i++)
{
map_with_offset(FNAME, i, map_size, msg);
map_with_offset(fname, i, map_size, msg);
}
}
fn void map_from_filename() @test
{
const String FNAME = "tmp/map_test_ro";
String fname = random_name();
char[] msg = "Hello, world";
write_file(FNAME, msg);
file::save(fname, msg)!!;
mmap::FileMmap mapped_file = file::mmap_open(FNAME, "rb", 0, 0, vm::VirtualMemoryAccess.READ)!!;
mmap::FileMmap mapped_file = file::mmap_open(fname, "rb", 0, 0, vm::VirtualMemoryAccess.READ)!!;
defer mapped_file.destroy()!!;
char[] view = mapped_file.bytes();

View File

@@ -1,6 +1,5 @@
module std::io::file @test;
import std::io;
import std::io, libc;
struct Data
{
@@ -38,6 +37,7 @@ struct Data
}
}
fn void read_write_any()
{
Data data;
@@ -57,13 +57,15 @@ fn void read_write_any()
data.total = 0xA55A;
data.hello = 0xCC;
File file = file::open("tmp/__file_read_write_any_test_file", "wb")!!;
char[1024] buf;
String name = libc::tmpnam(&buf).str_view();
File file = file::open(name, "wb")!!;
io::write_any(&file, &data)!!;
file.flush()!!;
file.close()!!;
file = file::open("tmp/__file_read_write_any_test_file", "rb")!!;
file = file::open(name, "rb")!!;
Data rdata;
io::read_any(&file, &rdata)!!;