lib/std/fs/tests.rad 3.1 KiB 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
}