.githooks/
compiler/
kernel/
lib/
examples/
std/
arch/
bits/
char/
collections/
fs/
hash/
lang/
alloc/
ast/
printer.rad
28.9 KiB
tests.rad
4.1 KiB
gen/
il/
layout/
lower/
manifest/
module/
parser/
resolver/
scanner/
sexpr/
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/ast/tests.rad
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 | } |