-
-
Notifications
You must be signed in to change notification settings - Fork 142
Update expr.c #91
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
base: main
Are you sure you want to change the base?
Update expr.c #91
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| #!/bin/bash | ||
|
|
||
| # check if file is present and executable | ||
| if [ ! -f expr ]; then | ||
| echo "File 'expr' found!" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -x expr ]; then | ||
| echo "File 'expr' not executable!" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "=== Testing expr calculator ===" | ||
|
|
||
| # Test 1: Simple addition | ||
| T=1 | ||
| result=$(./expr 2 3 +) | ||
| expected="result: 5.000" | ||
| if [[ "$result" == "$expected" ]]; then | ||
| echo "Test $T PASS: 2 + 3 = 5" | ||
| else | ||
| echo "Test $T FAIL: Expected '$expected', got '$result'" | ||
| fi | ||
|
|
||
| # Test 2: Commutativity of addition | ||
| T=2 | ||
| result=$(./expr 3 2 +) | ||
| expected="result: 5.000" | ||
| if [[ "$result" == "$expected" ]]; then | ||
| echo "Test $T PASS: 3 + 2 = 5" | ||
| else | ||
| echo "Test $T FAIL: Expected '$expected', got '$result'" | ||
| fi | ||
|
|
||
| # Test 3: Subtraction | ||
| T=3 | ||
| result=$(./expr 10 3 -) | ||
| expected="result: 7.000" | ||
| if [[ "$result" == "$expected" ]]; then | ||
| echo "Test $T PASS: 10 - 3 = 7" | ||
| else | ||
| echo "Test $T FAIL: Expected '$expected', got '$result'" | ||
| fi | ||
|
|
||
| # Test 4: Complex expression | ||
| T=4 | ||
| # ------------------------------------ | ||
| # Expected: | ||
| # 5 --> [5] | ||
| # 1 --> [5, 1] | ||
| # 2 --> [5, 1, 2] | ||
| # + --> (1 + 2 = 3) --> [5, 3] | ||
| # 4 --> [5, 3, 4] | ||
| # * --> (3 * 4 = 12) --> [5, 12] | ||
| # + --> (5 + 12 = 17) --> [17] | ||
| # 3 --> [17, 3] | ||
| # - --> (17 - 3 = 14) --> [14] | ||
| # ------------------------------------ | ||
| result=$(./expr 5 1 2 + 4 \* + 3 -) | ||
| expected="result: 14.000" | ||
| if [[ "$result" == "$expected" ]]; then | ||
| echo "Test $T PASS: Complex RPN expression" | ||
| else | ||
| echo "Test $T FAIL: Expected '$expected', got '$result'" | ||
| fi | ||
|
|
||
| # Test 5: Division by zero | ||
| T=5 | ||
| result=$(./expr 5 0 / 2>&1) | ||
| if [[ "$result" == *"division by zero"* ]]; then | ||
| echo "Test $T PASS: Division by zero detected" | ||
| else | ||
| echo "Test $T FAIL: Should detect division by zero" | ||
| fi | ||
|
|
||
| # Test 6: Missing operator | ||
| T=6 | ||
| result=$(./expr 5 1) | ||
| expected="Error: Operator is missing!" | ||
| if [[ "$result" == "$expected" ]]; then | ||
| echo "Test $T PASS: Missing operator" | ||
| else | ||
| echo "Test $T FAIL: Expected '$expected', got '$result'" | ||
| fi | ||
|
Comment on lines
+79
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test 6 expected output is inconsistent with error message formatting. The test expects
This is a minor cosmetic issue, but fixing it in both Related: The suggested fix in the review comment for 🧰 Tools🪛 Shellcheck (0.11.0)[warning] 79-79: 'expr' expects 3+ arguments, but sees 2. Make sure each operator/operand is a separate argument, and escape <>&|. (SC2307) 🤖 Prompt for AI Agents |
||
|
|
||
| echo "=== Tests Complete ===" | ||
Uh oh!
There was an error while loading. Please reload this page.