lib/std/lang/ast/tests.rad 4.1 KiB raw
1
//! Synthetic AST construction and allocation failures.
2
3
use std::fmt;
4
use std::lang::alloc::AllocError;
5
use std::lang::ast;
6
use std::mem;
7
use std::mem::heap;
8
use std::testing;
9
10
/// Arena storage for synthetic nodes and literal text.
11
static STORAGE: [u8; 8192] = [0; 8192];
12
13
/// Decimal literals keep their complete unsigned magnitude and text.
14
@test unsafe fn numberValues() throws (testing::TestError) {
15
    let arena = heap::over(&mut STORAGE[..]);
16
    let values: [u64; 3] = [0, 42, fmt::U64_MAX];
17
    for value in values {
18
        let case ast::NodeValue::Number(literal) = try! ast::numberValue(value, arena) else panic;
19
        assert literal.magnitude == value;
20
        assert literal.radix == fmt::Radix::Decimal;
21
        assert (try! fmt::parseInt(literal.text)).magnitude == value;
22
    }
23
}
24
25
/// Record construction copies temporary field lists and preserves their order.
26
@test unsafe fn recordFields() throws (testing::TestError) {
27
    let mut arena = ast::nodeArena(&mut STORAGE[..]);
28
    let mut fields = [
29
        ast::field("path", ast::stringValue("main.rad")),
30
        ast::field("enabled", ast::NodeValue::Bool(true)),
31
    ];
32
    let node = try! ast::synthRecord(&mut arena, "Module", &fields[..]);
33
    set fields[0] = ast::field("changed", ast::NodeValue::Nil);
34
    let case ast::NodeValue::RecordLit(literal) = node.value else panic;
35
    let name = literal.typeName else panic;
36
    let case ast::NodeValue::Ident(typeName) = name.value else panic;
37
    assert mem::eq(typeName, "Module");
38
    assert literal.fields.len == 2 and not literal.ignoreRest;
39
    let case ast::NodeValue::RecordLitField(first) = literal.fields[0].value else panic;
40
    let label = first.label else panic;
41
    let case ast::NodeValue::Ident(key) = label.value else panic;
42
    let case ast::NodeValue::String(text) = first.value.value else panic;
43
    assert mem::eq(key, "path") and mem::eq(text, "main.rad");
44
    let case ast::NodeValue::RecordLitField(second) = literal.fields[1].value else panic;
45
    let other = second.label else panic;
46
    let case ast::NodeValue::Ident(otherKey) = other.value else panic;
47
    let case ast::NodeValue::Bool(enabled) = second.value.value else panic;
48
    assert mem::eq(otherKey, "enabled") and enabled;
49
    assert node.span.offset == 0 and node.span.length == 0;
50
    let empty = try! ast::synthRecord(&mut arena, nil, &[]);
51
    let case ast::NodeValue::RecordLit(anonymous) = empty.value else panic;
52
    assert anonymous.typeName == nil and anonymous.fields.len == 0;
53
}
54
55
/// Build a record with literals that share the node arena.
56
fn example(arena: &mut ast::NodeArena) -> *ast::Node throws (AllocError) {
57
    let number = try ast::numberValue(42, arena.heap);
58
    let hex = try ast::hexStringValue("hi", arena.heap);
59
    return try ast::synthRecord(
60
        arena,
61
        "Config",
62
        &[
63
            ast::field("answer", number),
64
            ast::field("label", ast::stringValue("hi")),
65
            ast::field("hex", hex),
66
            ast::field("items", ast::arrayValue(&[])),
67
        ]
68
    );
69
}
70
71
/// Hexadecimal string literals own their encoded bytes and preserve leading zeroes.
72
@test unsafe fn hexStrings() throws (testing::TestError) {
73
    let arena = heap::over(&mut STORAGE[..]);
74
    let mut bytes: [u8; 4] = [0, 1, 0x7f, 0xff];
75
    let value = try! ast::hexStringValue(&bytes[..], arena);
76
    set bytes[0] = 0x55;
77
    let case ast::NodeValue::String(text) = value else panic;
78
    assert mem::eq(text, "00017fff");
79
    let case ast::NodeValue::String(empty) = try! ast::hexStringValue("", arena) else panic;
80
    assert empty.len == 0;
81
}
82
83
/// Every construction stage returns allocation failure when storage is too small.
84
@test unsafe fn allocationErrors() throws (testing::TestError) {
85
    let mut capacity: u32 = 0;
86
    loop {
87
        let mut arena = ast::NodeArena {
88
            heap: heap::withCapacity(&mut STORAGE[..], capacity), nextId: 0
89
        };
90
        try example(&mut arena) catch error {
91
            assert error == AllocError::OutOfMemory;
92
            set capacity += 16;
93
            assert capacity < STORAGE.len - 48;
94
            continue;
95
        };
96
        assert capacity > 0;
97
        break;
98
    }
99
}