.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
tests.rad
3.1 KiB
hash/
lang/
mem/
sys/
arch.rad
68 B
bits.rad
1.6 KiB
char.rad
1.5 KiB
collections.rad
39 B
fmt.rad
11.6 KiB
fs.rad
11.0 KiB
hash.rad
41 B
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
335 B
mem.rad
12.0 KiB
mem.ras
919 B
process.rad
6.2 KiB
std.manifest
4.9 KiB
std.rad
349 B
sys.rad
1.0 KiB
testing.rad
2.4 KiB
tests.rad
26.6 KiB
vec.rad
3.2 KiB
scripts/
seed/
sublime/
test/
vim/
.gitignore
333 B
.gitsigners
224 B
AGENTS.md
8.3 KiB
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
2.5 KiB
README
3.4 KiB
STYLE
3.2 KiB
std.lib
2.1 KiB
std.lib.test
1.1 KiB
lib/std/fs/tests.rad
raw
| 1 | //! Tests for file system paths, listings and reads. |
| 2 | |
| 3 | use std::fs; |
| 4 | use std::lang::alloc; |
| 5 | use std::mem; |
| 6 | use std::mem::heap; |
| 7 | use std::sys; |
| 8 | use std::sys::host; |
| 9 | use std::testing; |
| 10 | |
| 11 | /// Relative normal paths have nonempty components and no zero bytes. |
| 12 | @test fn normalRelativePaths() throws (testing::TestError) { |
| 13 | for path in &["a", "a/b", "a.b/c", ".hidden/name", "name..suffix", "two words/a-b"] { |
| 14 | assert fs::isNormalRelativePath(path); |
| 15 | } |
| 16 | for path in &[ |
| 17 | "", "/", "/a", ".", "..", "a/.", "a/..", "a/", "../a", "./a", "a//b", "a\0b" |
| 18 | ] { |
| 19 | assert not fs::isNormalRelativePath(path); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | /// Storage for the listings and reads of the tests. |
| 24 | static ARENA: heap::Heap = heap::new(16384); |
| 25 | |
| 26 | /// File whose contents the read tests check. |
| 27 | constant TEXT_PATH: *[u8] = "test/std/fs/text"; |
| 28 | /// Contents of the file at `TEXT_PATH`. |
| 29 | constant TEXT: *[u8] = "Radiance\n"; |
| 30 | /// Directory that holds the files `one` and `two`. |
| 31 | constant LISTING_PATH: *[u8] = "test/std/fs/listing"; |
| 32 | |
| 33 | /// An allocated listing holds each name once, followed by a zero byte. |
| 34 | @test fn listDirAlloc() throws (testing::TestError) { |
| 35 | let arena = &ARENA; |
| 36 | let listing = try fs::listDirAlloc(LISTING_PATH, 64, arena) catch { |
| 37 | throw testing::TestError::Failed; |
| 38 | }; |
| 39 | assert listing.len == 8; |
| 40 | let mut one = false; |
| 41 | let mut two = false; |
| 42 | let mut names = mem::split(listing, 0); |
| 43 | while let name = names.next() { |
| 44 | set one = one or mem::eq(name, "one"); |
| 45 | set two = two or mem::eq(name, "two"); |
| 46 | } |
| 47 | assert one and two; |
| 48 | } |
| 49 | |
| 50 | /// A listing larger than the capacity throws `TooLarge`. |
| 51 | @test fn listDirAllocTooLarge() throws (testing::TestError) { |
| 52 | let arena = &ARENA; |
| 53 | try fs::listDirAlloc(LISTING_PATH, 7, arena) catch failure as sys::Error { |
| 54 | assert failure == sys::Error::TooLarge; |
| 55 | return; |
| 56 | } catch failure as alloc::AllocError { |
| 57 | throw testing::TestError::Failed; |
| 58 | }; |
| 59 | throw testing::TestError::Failed; |
| 60 | } |
| 61 | |
| 62 | /// An allocated read holds the whole file. |
| 63 | @test fn readFileAlloc() throws (testing::TestError) { |
| 64 | let arena = &ARENA; |
| 65 | let text = try fs::readFileAlloc(TEXT_PATH, arena) catch { |
| 66 | throw testing::TestError::Failed; |
| 67 | }; |
| 68 | assert mem::eq(text, TEXT); |
| 69 | } |
| 70 | |
| 71 | /// Reads append to the existing bytes, at most the requested count at a time. |
| 72 | @test fn readInto() throws (testing::TestError) { |
| 73 | let arena = &ARENA; |
| 74 | let mut bytes: *mut [u8] = &mut []; |
| 75 | try bytes.append('>', arena) catch { |
| 76 | throw testing::TestError::Failed; |
| 77 | }; |
| 78 | let fd = try fs::openRead(TEXT_PATH) catch { |
| 79 | throw testing::TestError::Failed; |
| 80 | }; |
| 81 | let first = try host::readInto(fd, &mut bytes, 4, arena) catch { |
| 82 | throw testing::TestError::Failed; |
| 83 | }; |
| 84 | assert first == 4; |
| 85 | assert mem::eq(&bytes[..], ">Radi"); |
| 86 | let rest = try host::readInto(fd, &mut bytes, 64, arena) catch { |
| 87 | throw testing::TestError::Failed; |
| 88 | }; |
| 89 | assert rest == 5; |
| 90 | let end = try host::readInto(fd, &mut bytes, 64, arena) catch { |
| 91 | throw testing::TestError::Failed; |
| 92 | }; |
| 93 | assert end == 0; |
| 94 | assert mem::eq(&bytes[..], ">Radiance\n"); |
| 95 | try fs::close(fd) catch { |
| 96 | throw testing::TestError::Failed; |
| 97 | }; |
| 98 | } |