-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_programs_test.go
More file actions
182 lines (169 loc) · 3.51 KB
/
Copy pathbasic_programs_test.go
File metadata and controls
182 lines (169 loc) · 3.51 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
package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestBasicPrograms tests simple Tim programs (print, arithmetic, etc.)
func TestBasicPrograms(t *testing.T) {
tests := []struct {
name string
source string
expected string
}{
{
name: "hello_world",
source: `println("Hello, World!")
`,
expected: "Hello, World!\n",
},
{
name: "simple_add",
source: `x := 5
y := 10
result := x + y
println(result)
`,
expected: "15\n",
},
{
name: "simple_subtract",
source: `x := 20
y := 8
result := x - y
println(result)
`,
expected: "12\n",
},
{
name: "simple_multiply",
source: `x := 6
y := 7
result := x * y
println(result)
`,
expected: "42\n",
},
{
name: "simple_divide",
source: `x := 100
y := 4
result := x / y
println(result)
`,
expected: "25\n",
},
{
name: "printf_basic",
source: `x := 42
printf("The answer is %v\n", x)
`,
expected: "The answer is 42",
},
{
name: "fstring_basic",
source: `name := "Tim"
msg := f"Hello, {name}!"
println(msg)
`,
expected: "SKIP: f-strings not yet fully implemented for ARM64",
},
{
name: "compound_assignment",
source: `x := 10
x += 5
println(x)
x -= 3
println(x)
x *= 2
println(x)
`,
expected: "15\n12\n24\n",
},
{
name: "comparison_operators",
source: `a := 5
b := 10
println(a < b)
println(a > b)
println(a == b)
println(a != b)
`,
expected: "1\n0\n0\n1\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testInlineTim(t, tt.name, tt.source, tt.expected)
})
}
}
// testInlineTim compiles and runs inline Tim source code
func testInlineTim(t *testing.T, name, source, expected string) {
// Check if this is a known issue that should be skipped
if strings.HasPrefix(expected, "SKIP:") {
t.Skip(strings.TrimPrefix(expected, "SKIP: "))
return
}
result := compileAndRun(t, source)
if !strings.Contains(result, expected) {
t.Errorf("Output mismatch:\nExpected to contain:\n%s\nActual:\n%s", expected, result)
}
}
// TestExistingBasicPrograms runs existing testprograms for basic functionality
func TestExistingBasicPrograms(t *testing.T) {
tests := []string{
"first",
"hello",
"add",
"subtract",
"multiply",
"divide",
"printf_demo",
"simple_print",
"simple_printf",
}
for _, name := range tests {
t.Run(name, func(t *testing.T) {
srcPath := filepath.Join("testprograms", name+".tim")
resultPath := filepath.Join("testprograms", name+".result")
// Skip if source doesn't exist
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
t.Skipf("Source file %s not found", srcPath)
return
}
// Read expected output if result file exists
var expected string
if data, err := os.ReadFile(resultPath); err == nil {
expected = string(data)
}
tmpDir := t.TempDir()
exePath := filepath.Join(tmpDir, name)
// Compile
platform := GetDefaultPlatform()
if err := CompileTim(srcPath, exePath, platform); err != nil {
t.Fatalf("Compilation failed: %v", err)
}
// Run with timeout
output, err := runWithTimeout(exePath, 5)
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
_ = exitErr // Non-zero exit OK
} else {
t.Fatalf("Execution failed: %v", err)
}
}
// If we have expected output, verify it
if expected != "" {
actual := string(output)
if !strings.Contains(actual, strings.TrimSpace(expected)) &&
actual != expected {
t.Errorf("Output mismatch:\nExpected:\n%s\nActual:\n%s",
expected, actual)
}
}
})
}
}