Skip to content

Commit 41f5f07

Browse files
authored
Merge pull request #232 from sysprog21/extend-tests
Add support for testing large return values (> 255)
2 parents 72fc6b5 + 40d1e2b commit 41f5f07

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

tests/driver.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,45 @@ function expr() {
114114
items "$expected" "exit($input);"
115115
}
116116

117+
# try_large - test shecc with large return values (> 255)
118+
# Usage:
119+
# - try_large expected_value input_code
120+
# compile "input_code" with shecc and verify the return value by printing it
121+
# instead of using exit code (which is limited to 0-255).
122+
function try_large() {
123+
local expected="$1"
124+
local input="$(cat)"
125+
126+
local tmp_in="$(mktemp --suffix .c)"
127+
local tmp_exe="$(mktemp)"
128+
129+
# Wrap the input to print the return value
130+
cat > "$tmp_in" << EOF
131+
int printf(char *format, ...);
132+
$input
133+
int main() {
134+
int result = test_function();
135+
printf("%d", result);
136+
return 0;
137+
}
138+
EOF
139+
140+
$SHECC -o "$tmp_exe" "$tmp_in"
141+
chmod +x $tmp_exe
142+
143+
local output=$($TARGET_EXEC "$tmp_exe")
144+
145+
if [ "$output" != "$expected" ]; then
146+
echo "$input => $expected expected, but got $output"
147+
echo "input: $tmp_in"
148+
echo "executable: $tmp_exe"
149+
exit 1
150+
else
151+
echo "Large value test: $expected"
152+
echo "output => $output"
153+
fi
154+
}
155+
117156
# just a number
118157
expr 0 0
119158
expr 42 42
@@ -317,6 +356,41 @@ int main() {
317356
}
318357
EOF
319358

359+
# Test large fibonacci values using the new try_large function
360+
try_large 987 << EOF
361+
int fib(int n, int a, int b)
362+
{
363+
if (n == 0)
364+
return a;
365+
if (n == 1)
366+
return b;
367+
return fib(n - 1, b, a + b);
368+
}
369+
370+
int test_function() {
371+
return fib(16, 0, 1); /* fib(16) = 987 */
372+
}
373+
EOF
374+
375+
# Test other large values
376+
try_large 1000 << EOF
377+
int test_function() {
378+
return 1000;
379+
}
380+
EOF
381+
382+
try_large 65536 << EOF
383+
int test_function() {
384+
return 1 << 16; /* 2^16 = 65536 */
385+
}
386+
EOF
387+
388+
try_large 999999 << EOF
389+
int test_function() {
390+
return 999999;
391+
}
392+
EOF
393+
320394
try_compile_error << EOF
321395
int main() {
322396
int a = 03, b = 01118, c = 091;

0 commit comments

Comments
 (0)