.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
gen/
il/
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
tests.rad
3.5 KiB
alloc.rad
1.2 KiB
ast.rad
36.0 KiB
gen.rad
513 B
il.rad
48.5 KiB
layout.rad
6.9 KiB
lower.rad
487.6 KiB
manifest.rad
22.5 KiB
module.rad
17.8 KiB
package.rad
1.5 KiB
parser.rad
114.1 KiB
pretty.rad
103.6 KiB
resolver.rad
508.1 KiB
scanner.rad
19.3 KiB
sexpr.rad
7.1 KiB
strings.rad
3.3 KiB
types.rad
620 B
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/lang/sexpr/tests.rad
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 | } |