-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstruct_programs_test.go
More file actions
272 lines (240 loc) · 6.86 KB
/
Copy pathcstruct_programs_test.go
File metadata and controls
272 lines (240 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
// TestCStructPrograms tests C struct interop
func TestCStructPrograms(t *testing.T) {
tests := []struct {
name string
source string
expected string
}{
{
name: "simple_cstruct",
source: `cstruct Point {
x as float64,
y as float64
}
println(Point.size)
println(Point.x.offset)
println(Point.y.offset)
`,
expected: "16\n0\n8\n",
},
{
name: "packed_cstruct",
source: `cstruct Data packed {
a as uint8,
b as uint32,
c as uint8
}
println(Data.size)
`,
expected: "6\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testInlineTim(t, tt.name, tt.source, tt.expected)
})
}
}
// TestCStructFieldAccess tests reading fields from cstruct pointers via dot-access
func TestCStructFieldAccess(t *testing.T) {
tests := []struct {
name string
source string
expected string
}{
{
name: "read_uint32_field",
source: `import libc as c
cstruct Point {
x as uint32
y as uint32
}
p := c.malloc(8) as Point
p[0] <- 42 as uint32
p[1] <- 99 as uint32
println(p.x)
println(p.y)
c.free(p)
`,
expected: "42\n99\n",
},
{
name: "read_float64_field",
source: `import libc as c
cstruct Vec {
a as float64
b as float64
}
v := c.malloc(16) as Vec
v[0] <- 10.0 as float64
v[1] <- 20.0 as float64
println(v.a)
println(v.b)
c.free(v)
`,
expected: "10\n20\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testInlineTim(t, tt.name, tt.source, tt.expected)
})
}
}
// TestCStructByValue covers struct values: the constructor Vec(...), passing a
// struct to a function and re-tagging the param with `as`, returning a struct,
// and inferred return typing (no cast needed on the call result).
func TestCStructByValue(t *testing.T) {
source := `cstruct Vec { x as float64, y as float64 }
vadd = (a, b) -> {
aa = a as Vec
bb = b as Vec
Vec(aa.x + bb.x, aa.y + bb.y)
}
dot = (a, b) -> {
aa = a as Vec
bb = b as Vec
aa.x*bb.x + aa.y*bb.y
}
main = {
p = Vec(3.0, 4.0)
q = Vec(10.0, 20.0)
r = vadd(p, q)
println(r.x)
println(r.y)
println(dot(p, q))
}
`
testInlineTim(t, "cstruct_by_value", source, "13\n24\n110\n")
}
// TestReadWriteFloat32 covers the 32-bit-float FFI accessors. write_f32 existed
// only on x86; read_f32 did not exist at all. C graphics/audio APIs (vertex
// buffers, colours, SDL mouse coords, PCM samples) are float32-heavy, so both
// directions must work on both backends. Values chosen to be exactly
// representable in float32 so the round-trip is bit-clean.
func TestReadWriteFloat32(t *testing.T) {
source := `import c
buf = c.malloc(16.0)
write_f32(buf, 0, 3.5)
write_f32(buf, 4, 0.0-2.25)
write_f32(buf, 8, 1000.5)
println(read_f32(buf, 0))
println(read_f32(buf, 4))
println(read_f32(buf, 8))
c.free(buf)
`
testInlineTim(t, "read_write_f32", source, "3.5\n-2.25\n1000.5\n")
}
// TestChainedCStructMethodsCompileFast guards against the inliner blowing up on
// deeply chained cstruct method calls. Each such call inlines to a body whose
// arguments (the constructors) were being deep-copied at every use site, and the
// whole result was re-inlined at every level — so a chain like a matrix build +
// transform expanded the AST exponentially and OOM-killed the compiler. The fix
// binds a multiply-used constructor arg once and re-inlines only the body, not
// the already-inlined argument bindings. If that regresses, this test hangs
// instead of returning a value — a clear signal in CI.
func TestChainedCStructMethodsCompileFast(t *testing.T) {
source := `cstruct V3 { x as float64, y as float64, z as float64 }
fun V3.add(o: V3) = V3(self.x+o.x, self.y+o.y, self.z+o.z)
fun V3.sub(o: V3) = V3(self.x-o.x, self.y-o.y, self.z-o.z)
fun V3.scale(s) = V3(self.x*s, self.y*s, self.z*s)
fun V3.dot(o: V3) = self.x*o.x + self.y*o.y + self.z*o.z
fun V3.cross(o: V3) = V3(self.y*o.z-self.z*o.y, self.z*o.x-self.x*o.z, self.x*o.y-self.y*o.x)
fun V3.norm() {
l = sqrt(self.x*self.x + self.y*self.y + self.z*self.z)
if l > 0.0 { V3(self.x/l, self.y/l, self.z/l) } else { V3(0.0,0.0,0.0) }
}
fun build(t) {
a = V3(1.0, 2.0, 3.0)
b = V3(4.0, 5.0, 6.0)
// A long chain of struct-returning methods, the shape that used to explode.
r = a.add(b).sub(a).scale(2.0).add(b.cross(a)).norm().scale(3.0)
r.x + r.y + r.z
}
main = {
println(build(1.0))
}
`
// A non-hanging result is the real assertion; the value just pins correctness
// (sum of the normalized-and-scaled chain = 90/sqrt(362) ≈ 4.7302). Match a
// prefix so a last-digit rounding difference between backends can't flake it.
testInlineTim(t, "chained_cstruct_methods", source, "4.7302")
}
// TestCStructMethodOnLocalInIfArm covers a cstruct local defined inside an
// `if`-expression arm and then used as a method-call receiver in the SAME arm.
// The operator-overload/method desugar pass used to skip locals declared inside
// a value-position `if` block, so `a.add(horizon)` kept its unresolved `a.add`
// form and was reported as an undefined function (and, in heavier scenes,
// showed up as a segfault once worked around). It must resolve to V_add now.
func TestCStructMethodOnLocalInIfArm(t *testing.T) {
source := `cstruct V { x as float64, y as float64, z as float64 }
fun V.add(o: V) = V(self.x + o.x, self.y + o.y, self.z + o.z)
fun V.scale(s) = V(self.x*s, self.y*s, self.z*s)
fun f(dy) {
horizon = V(0.45, 0.10, 0.38)
base = if dy < 0.0 {
a = horizon.scale(2.0)
a.add(horizon)
} else {
horizon
}
base
}
main = {
w = f(-0.5)
println(w.x)
println(w.y)
println(w.z)
}
`
testInlineTim(t, "cstruct_method_on_local_in_if_arm", source, "1.35\n0.3\n1.14\n")
}
func TestExistingCStructPrograms(t *testing.T) {
tests := []string{
"cstruct_test",
"cstruct_syntax_test",
"cstruct_helpers_test",
"cstruct_modifiers_test",
"cstruct_arena_test",
}
for _, name := range tests {
t.Run(name, func(t *testing.T) {
srcPath := filepath.Join("testprograms", name+".tim")
resultPath := filepath.Join("testprograms", name+".result")
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
t.Skipf("Source file %s not found", srcPath)
return
}
var expected string
if data, err := os.ReadFile(resultPath); err == nil {
expected = string(data)
}
tmpDir := t.TempDir()
exePath := filepath.Join(tmpDir, name)
platform := GetDefaultPlatform()
if err := CompileTim(srcPath, exePath, platform); err != nil {
t.Fatalf("Compilation failed: %v", err)
}
output, err := runWithTimeout(exePath, 5)
if err != nil {
if _, ok := err.(*exec.ExitError); !ok {
t.Fatalf("Execution failed: %v", err)
}
}
if expected != "" {
actual := string(output)
if actual != expected {
t.Errorf("Output mismatch:\nExpected:\n%s\nActual:\n%s",
expected, actual)
}
}
})
}
}