module scheme import arena fn test_string_plus_accepts_infix_prefix_and_variadic_calls() { mut vm := new() for source, expected in { '"string 1" + "string 2"': 'string 1string 2' '"a" + "b" + "c"': 'abc' '+("a", "b", "c")': 'abc' '"" + ""': '' 'string-append("a", "b")': 'ab' } { assert (vm.eval_alt(source) or { panic(err) }).as_string() or { panic(err) } == expected } assert (vm.eval('(+ "a" "b" "c")') or { panic(err) }).as_string() or { panic(err) } == 'abc' assert (vm.eval('(apply + (list "a" "b" "c"))') or { panic(err) }).as_string() or { panic(err) } == 'abc' } fn test_string_plus_preserves_unicode_lengths_and_embedded_nuls() { mut vm := new() assert (vm.eval_alt('"位" + "馃榾" + "茅"') or { panic(err) }).as_string() or { panic(err) } == '位馃榾茅' assert (vm.eval_alt('string-length("位" + "馃榾" + "茅")') or { panic(err) }).as_integer() or { panic(err) } == 3 assert (vm.eval_alt(r'"a\x0;b" + "\x0;"') or { panic(err) }).as_string() or { panic(err) } == 'a\0b\0' assert (vm.eval_alt(r'string-length("a\x0;b" + "\x0;")') or { panic(err) }).as_integer() or { panic(err) } == 4 } fn test_string_plus_evaluates_operands_once_in_order() { mut vm := new() value := vm.eval_alt('seen := ""\nf(s) := { seen = seen + s, s }\nf("a") + f("b") + f("c")') or { panic(err) } assert value.as_string() or { panic(err) } == 'abc' assert (vm.lookup('seen') or { panic(err) }).as_string() or { panic(err) } == 'abc' } fn test_string_plus_returns_independent_mutable_strings_including_unary_calls() { mut vm := new() value := vm.eval(r'(define a (string-copy "ab")) (define b (+ a)) (define c (+ a "cd")) (string-set! b 0 #\z) (string-set! c 1 #\y) (list a b c)') or { panic(err) } assert write(value) or { panic(err) } == '("ab" "zb" "aycd")' } fn test_string_plus_rejects_mixed_types_without_changing_numeric_addition() { mut vm := new() for source in ['(+ "x" 1)', '(+ 1 "x")', '(+ "x" #t)', '(+ "x" (quote y))', r'(+ "x" #\a)'] { if _ := vm.eval(source) { assert false, source } else { assert err is EvalError } } for source, expected in { '(+)': '0' '(+ 1)': '1' '(+ 1 2)': '3' '(+ 1 2 3 4)': '10' '(+ 1/2 1/3)': '5/6' '(+ 9223372036854775807 1)': '9223372036854775808' } { assert write(vm.eval(source) or { panic(err) }) or { panic(err) } == expected } assert (vm.eval_alt('3 + 2 * 4') or { panic(err) }).as_integer() or { panic(err) } == 11 } fn test_string_plus_host_calls_share_the_same_operation() { mut vm := new() plus := vm.lookup('+') or { panic(err) } result := vm.call(plus, [string_value('hello '), string_value('位')]) or { panic(err) } assert result.as_string() or { panic(err) } == 'hello 位' } fn test_string_plus_large_inputs_retain_one_output_buffer() { left := string_value('a'.repeat(200000)) right := string_value('位'.repeat(100000)) args := [left, right] mut region := arena.new(chunk_size: 1048576) region.push() result := append_strings(args) or { panic(err) } assert result.text().len == 400000 && result.string_pointer().length == 300000 // Allow object headers/alignment while rejecting an intermediate full copy // or geometric buffer growth retained by a GC-free arena. assert region.used() <= 400128 assert result.text().starts_with('aaa') && result.text().ends_with('位位位') region.pop() region.free() }