v / vlib / v2 / gen / arm64 / tests / string_concat.v
113 lines · 89 sloc · 2.53 KB · 550f869387ec26427d3b2b82f066ab85194739f6
Raw
1// Tests for ARM64 backend large struct returns (> 16 bytes)
2// This tests the fix from commit 9919ddeb5 which handles:
3// - Saving/restoring x8 (indirect return pointer) for large struct returns
4// - Proper stack frame layout with callee-saved registers
5// - Large struct parameter copying
6// - String operations (string is 24 bytes: ptr + len + is_lit)
7
8fn print_str(s string) {
9 C.write(1, s.str, s.len)
10 C.write(1, c'\n', 1)
11}
12
13// ===================== FUNCTIONS RETURNING STRINGS =====================
14
15// Test 1: Return a string literal (24 bytes indirect return)
16fn get_hello() string {
17 return 'hello'
18}
19
20// Test 2: Return concatenated string
21fn get_greeting(name string) string {
22 return 'Hello, ' + name
23}
24
25// Test 3: Nested string returns
26fn get_formal_greeting(name string) string {
27 greeting := get_greeting(name)
28 return greeting + '!'
29}
30
31// Test 4: Multiple string concatenations
32fn concat_three(a string, b string, c string) string {
33 return a + b + c
34}
35
36// Test 5: String function calling another string function
37fn get_full_message() string {
38 greeting := get_hello()
39 suffix := ' world'
40 return greeting + suffix
41}
42
43// Test 6: Chain of string returns
44fn chain_a() string {
45 return 'A'
46}
47
48fn chain_b() string {
49 return chain_a() + 'B'
50}
51
52fn chain_c() string {
53 return chain_b() + 'C'
54}
55
56// Test 7: Conditional string return
57fn conditional_string(flag bool) string {
58 if flag {
59 return 'true_case'
60 }
61 return 'false_case'
62}
63
64// ===================== MAIN =====================
65
66fn main() {
67 print_str('--- String Return Tests (Large Struct > 16 bytes) ---')
68
69 // Test 1: Basic string literal return
70 s1 := get_hello()
71 print_str(s1)
72
73 // Test 2: String concatenation return
74 s2 := get_greeting('Alice')
75 print_str(s2)
76
77 // Test 3: Nested string function calls
78 s3 := get_formal_greeting('Bob')
79 print_str(s3)
80
81 // Test 4: Multiple parameters concatenation
82 s4 := concat_three('One', '-Two-', 'Three')
83 print_str(s4)
84
85 // Test 5: Function calling function returning strings
86 s5 := get_full_message()
87 print_str(s5)
88
89 // Test 6: Chain of string returns
90 s6 := chain_c()
91 print_str(s6)
92
93 // Test 7: Conditional string returns
94 s7a := conditional_string(true)
95 s7b := conditional_string(false)
96 print_str(s7a)
97 print_str(s7b)
98
99 // Test 8: Direct string operations
100 direct := 'Direct' + ' ' + 'Test'
101 print_str(direct)
102
103 // Test 9: Interpolation with string return
104 name := 'Claude'
105 interp := 'Name: ${name}'
106 print_str(interp)
107
108 // Test 10: Chained string concat expression
109 chained := 'A' + 'B' + 'C' + 'D' + 'E'
110 print_str(chained)
111
112 print_str('--- All tests completed ---')
113}
114