lib/std/lang/sexpr/tests.rad 3.5 KiB raw
1
//! Tests for S-expression output sinks and printing.
2
3
use std::mem;
4
use std::testing;
5
use std::lang::alloc;
6
use std::mem::heap;
7
8
/// Test that a buffer sink appends writes in order.
9
@test unsafe fn testBufferWritesInOrder() throws (testing::TestError) {
10
    static STORAGE: [u8; 16] = [0; 16];
11
    let mut out = super::Buffer { buf: &mut STORAGE[..], pos: 0 };
12
    {
13
        let sink: &mut opaque super::Output = &mut out;
14
        sink.write("ab");
15
        sink.write("");
16
        sink.write("cd");
17
    }
18
    try testing::expect(out.pos == 4);
19
    try testing::expect(mem::eq(&STORAGE[.. out.pos], "abcd"));
20
}
21
22
/// Test that a buffer sink drops the bytes that do not fit.
23
@test unsafe fn testBufferTruncatesWhenFull() throws (testing::TestError) {
24
    static STORAGE: [u8; 4] = [0; 4];
25
    let mut out = super::Buffer { buf: &mut STORAGE[..], pos: 0 };
26
    {
27
        let sink: &mut opaque super::Output = &mut out;
28
        sink.write("abc");
29
        sink.write("def");
30
        sink.write("g");
31
    }
32
    try testing::expect(out.pos == 4);
33
    try testing::expect(mem::eq(&STORAGE[.. out.pos], "abcd"));
34
}
35
36
/// Test decimal number output for signed and unsigned values.
37
@test unsafe fn testWriteNumbers() throws (testing::TestError) {
38
    static STORAGE: [u8; 64] = [0; 64];
39
    let mut out = super::Buffer { buf: &mut STORAGE[..], pos: 0 };
40
    {
41
        let sink: &mut opaque super::Output = &mut out;
42
        super::writeI64(sink, -12345);
43
        sink.write(" ");
44
        super::writeI64(sink, 0);
45
        sink.write(" ");
46
        super::writeI64(sink, -1234567890123);
47
    }
48
    try testing::expect(mem::eq(&STORAGE[.. out.pos], "-12345 0 -1234567890123"));
49
}
50
51
/// Test that strings and characters are printed with escapes.
52
@test unsafe fn testPrintEscapes() throws (testing::TestError) {
53
    static STORAGE: [u8; 32] = [0; 32];
54
    let mut out = super::Buffer { buf: &mut STORAGE[..], pos: 0 };
55
    {
56
        let sink: &mut opaque super::Output = &mut out;
57
        super::printStringTo(sink, "a\n\\b");
58
        sink.write(" ");
59
        super::printCharTo(sink, '\'');
60
    }
61
    try testing::expect(mem::eq(&STORAGE[.. out.pos], "\"a\\n\\\\b\" '\\''"));
62
}
63
64
/// Test that a symbol and a list print with the expected separators.
65
@test unsafe fn testPrintListToBuffer() throws (testing::TestError) {
66
    static STORAGE: [u8; 32] = [0; 32];
67
    static ARENA_STORAGE: [u8; 256] = [0; 256];
68
    let arena = heap::over(&mut ARENA_STORAGE[..]);
69
    let mut out = super::Buffer { buf: &mut STORAGE[..], pos: 0 };
70
    let expr = super::list(arena, "add", &[super::sym("x"), super::Expr::Char('y')]);
71
    super::printTo(expr, 0, &mut out);
72
73
    try testing::expect(mem::eq(&STORAGE[.. out.pos], "(add x 'y')"));
74
}
75
76
/// Test that an empty expression array allocates nothing and has no items.
77
@test unsafe fn testAllocExprsEmpty() throws (testing::TestError) {
78
    static ARENA_STORAGE: [u8; 64] = [0; 64];
79
    let arena = heap::over(&mut ARENA_STORAGE[..]);
80
    let items = try! super::allocExprs(arena, 0);
81
82
    try testing::expect(items.len == 0);
83
    try testing::expect(heap::used(arena) == 0);
84
}
85
86
/// Test that exhausting the allocator is reported as an error.
87
@test unsafe fn testAllocExprsOutOfMemory() throws (testing::TestError) {
88
    static ARENA_STORAGE: [u8; 64] = [0; 64];
89
    let arena = heap::over(&mut ARENA_STORAGE[..]);
90
91
    let mut caught = false;
92
    try super::allocExprs(arena, 2) catch error as alloc::AllocError {
93
        try testing::expect(error == alloc::AllocError::OutOfMemory);
94
        set caught = true;
95
    };
96
    try testing::expect(caught);
97
}