test/compiler/discovery.rad 2.6 KiB raw
1
//! Test discovery storage and declaration metadata.
2
use std::mem;
3
use std::lang::ast;
4
use std::mem::heap;
5
use std::lang::module;
6
use std::lang::parser;
7
use std::lang::scanner;
8
use std::lang::strings;
9
use testgen;
10
11
/// Number of elements reserved for frontend storage.
12
constant FRONTEND_CAPACITY: u32 = 65536;
13
/// Number of elements reserved for storage storage.
14
constant STORAGE_CAPACITY: u32 = 4096;
15
16
/// Parser storage that keeps both test module ASTs alive.
17
static FRONTEND: [u8; FRONTEND_CAPACITY] = [0; FRONTEND_CAPACITY];
18
/// Backing storage for declaration descriptors and their paths.
19
static STORAGE: [u8; STORAGE_CAPACITY] = [0; STORAGE_CAPACITY];
20
/// Slots of the parser's identifier pool.
21
unsafe static TABLE: [*[u8]; strings::TABLE_SIZE] = undefined;
22
/// Module graph storage.
23
unsafe static ENTRIES: [module::ModuleEntry; 2] = undefined;
24
25
/// Discovery returns allocation failures and copies the module's path.
26
@default unsafe fn main() -> u64 {
27
    let mut frontend = parser::newParser(ast::nodeArena(&mut FRONTEND[..]), strings::pool(
28
        &mut TABLE[..]
29
    ));
30
    let empty = try! parser::parseModule(
31
        &mut frontend, scanner::SourceLoc::String, ""
32
    );
33
    let tests = try! parser::parseModule(
34
        &mut frontend, scanner::SourceLoc::String, "fn helper() {} @test fn check() {}"
35
    );
36
    let mut graph = module::moduleGraph(&mut ENTRIES[..]);
37
    let id = try! module::registerRoot(
38
        &mut graph, &mut frontend.pool, 0, "fixture.rad"
39
    );
40
    let original = module::entry(&graph, id) else panic;
41
    let mut entry = *original;
42
    set entry.ast = empty;
43
    let mut pathBytes: u32 = 0;
44
    {
45
        let arena = heap::over(&mut STORAGE[..]);
46
        let mut found: *mut [testgen::TestDesc] = &mut [];
47
        try! testgen::collect(&entry, &mut found, arena);
48
        assert found.len == 0;
49
        set pathBytes = heap::used(arena);
50
    }
51
    set entry.ast = tests;
52
    let limits: [u32; 2] = [0, pathBytes];
53
    for limit in limits {
54
        let arena = heap::withCapacity(&mut STORAGE[..], limit);
55
        let mut found: *mut [testgen::TestDesc] = &mut [];
56
        let mut rejected = false;
57
        try testgen::collect(&entry, &mut found, arena) catch {
58
            set rejected = true;
59
        };
60
        assert rejected and found.len == 0;
61
    }
62
    let arena = heap::over(&mut STORAGE[..]);
63
    let mut found: *mut [testgen::TestDesc] = &mut [];
64
    try! testgen::collect(&entry, &mut found, arena);
65
    assert found.len == 1;
66
    assert mem::eq(found[0].fnName, "check");
67
    set entry.path[0] = "other";
68
    assert mem::eq(found[0].modPath[0], "fixture");
69
    return 0;
70
}