Skip to content

Add support for testing large return values (> 255) #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,45 @@ function expr() {
items "$expected" "exit($input);"
}

# try_large - test shecc with large return values (> 255)
# Usage:
# - try_large expected_value input_code
# compile "input_code" with shecc and verify the return value by printing it
# instead of using exit code (which is limited to 0-255).
function try_large() {
local expected="$1"
local input="$(cat)"

local tmp_in="$(mktemp --suffix .c)"
local tmp_exe="$(mktemp)"

# Wrap the input to print the return value
cat > "$tmp_in" << EOF
int printf(char *format, ...);
$input
int main() {
int result = test_function();
printf("%d", result);
return 0;
}
EOF

$SHECC -o "$tmp_exe" "$tmp_in"
chmod +x $tmp_exe

local output=$($TARGET_EXEC "$tmp_exe")

if [ "$output" != "$expected" ]; then
echo "$input => $expected expected, but got $output"
echo "input: $tmp_in"
echo "executable: $tmp_exe"
exit 1
else
echo "Large value test: $expected"
echo "output => $output"
fi
}

# just a number
expr 0 0
expr 42 42
Expand Down Expand Up @@ -317,6 +356,41 @@ int main() {
}
EOF

# Test large fibonacci values using the new try_large function
try_large 987 << EOF
int fib(int n, int a, int b)
{
if (n == 0)
return a;
if (n == 1)
return b;
return fib(n - 1, b, a + b);
}

int test_function() {
return fib(16, 0, 1); /* fib(16) = 987 */
}
EOF

# Test other large values
try_large 1000 << EOF
int test_function() {
return 1000;
}
EOF

try_large 65536 << EOF
int test_function() {
return 1 << 16; /* 2^16 = 65536 */
}
EOF

try_large 999999 << EOF
int test_function() {
return 999999;
}
EOF

try_compile_error << EOF
int main() {
int a = 03, b = 01118, c = 091;
Expand Down