vile / lexer / collector_test.v
38 lines · 36 sloc · 1.24 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module lexer
2
3fn test_collector_capacity_hint_handles_short_streams_and_huge_scalar_tokens() {
4 for count in [0, 1, 30, 31, 32, 33, 100, 10000] {
5 source := 'word '.repeat(count) + ';' + 'comment '.repeat(10000)
6 tokens := tokenize(source) or { panic(err) }
7 assert tokens.len == count + 1
8 for i in 0 .. count {
9 assert tokens[i].kind == .identifier
10 assert tokens[i].lexeme == 'word'
11 assert tokens[i].start == i * 5
12 }
13 assert tokens.last().kind == .eof
14 assert tokens.last().start == source.len
15 }
16 for source in ['a'.repeat(100000), '"' + 'x'.repeat(100000) + '"'] {
17 tokens := tokenize(source) or { panic(err) }
18 assert tokens.len == 2
19 assert tokens.cap <= 4
20 assert tokens[0].lexeme == source
21 }
22}
23
24fn test_published_collector_array_slices_survive_later_growth() {
25 mut tokens := tokenize('word '.repeat(10000)) or { panic(err) }
26 assert !tokens.flags.has(.noslices)
27 // Request a view explicitly; V otherwise clones a slice of a mutable array.
28 saved := unsafe { tokens[30..40] }
29 length := tokens.len
30 for _ in 0 .. length * 2 { tokens << Token{ kind: .eof } }
31 gc_collect()
32 assert saved.len == 10
33 for index, token in saved {
34 assert token.kind == .identifier
35 assert token.lexeme == 'word'
36 assert token.start == (index + 30) * 5
37 }
38}
39