Skip to content

Commit c438de3

Browse files
authored
Merge pull request #53 from codecrafters-io/alpha-fixes-3
Fix `return` statement and update fixtures
2 parents ec2998c + c7a6d89 commit c438de3

File tree

3 files changed

+48
-50
lines changed

3 files changed

+48
-50
lines changed

internal/test_helpers/fixtures/pass_functions

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Debug = true
55
[stage-9] [test-1] Writing contents to ./test.lox:
66
[stage-9] [test-1] [test.lox] print clock() + 23;
77
[stage-9] [test-1] $ ./your_program.sh run test.lox
8-
[your_program] 1.731520035676E9
9-
[stage-9] [test-1] ✓ 1731520035.676000
8+
[your_program] 1.731566537833E9
9+
[stage-9] [test-1] ✓ 1731566537.833000
1010
[stage-9] [test-1] ✓ 1 line(s) match on stdout
1111
[stage-9] [test-1] ✓ Received exit code 0.
1212
[stage-9] [test-2] Running test case: 2
1313
[stage-9] [test-2] Writing contents to ./test.lox:
1414
[stage-9] [test-2] [test.lox] print clock() / 1000;
1515
[stage-9] [test-2] $ ./your_program.sh run test.lox
16-
[your_program] 1731520.012833
17-
[stage-9] [test-2] ✓ 1731520.012833
16+
[your_program] 1731566.514949
17+
[stage-9] [test-2] ✓ 1731566.514949
1818
[stage-9] [test-2] ✓ 1 line(s) match on stdout
1919
[stage-9] [test-2] ✓ Received exit code 0.
2020
[stage-9] [test-3] Running test case: 3
@@ -83,24 +83,23 @@ Debug = true
8383
[stage-8] [test-3] ✓ Received exit code 0.
8484
[stage-8] [test-4] Running test case: 4
8585
[stage-8] [test-4] Writing contents to ./test.lox:
86-
[stage-8] [test-4] [test.lox] // This program demonstrates the use of recursive functions to determine if a number is even or odd.
87-
[stage-8] [test-4] [test.lox] // This is a classic example of mutual recursion.
88-
[stage-8] [test-4] [test.lox] fun isEven(n) {
89-
[stage-8] [test-4] [test.lox] if (n == 0) return true;
90-
[stage-8] [test-4] [test.lox] return isOdd(n - 1);
86+
[stage-8] [test-4] [test.lox] // This program calculates the cumulative sum of numbers from 1 to n.
87+
[stage-8] [test-4] [test.lox] fun cumulative_sum() {
88+
[stage-8] [test-4] [test.lox] var n = 10; // Fixed value
89+
[stage-8] [test-4] [test.lox] var total = 0;
90+
[stage-8] [test-4] [test.lox] var i = 1;
91+
[stage-8] [test-4] [test.lox] while (i <= n) {
92+
[stage-8] [test-4] [test.lox] total = total + i;
93+
[stage-8] [test-4] [test.lox] i = i + 1;
94+
[stage-8] [test-4] [test.lox] }
95+
[stage-8] [test-4] [test.lox] print "The cumulative sum from 1 to 10 is: ";
96+
[stage-8] [test-4] [test.lox] print total;
9197
[stage-8] [test-4] [test.lox] }
9298
[stage-8] [test-4] [test.lox]
93-
[stage-8] [test-4] [test.lox] fun isOdd(n) {
94-
[stage-8] [test-4] [test.lox] if (n == 0) return false;
95-
[stage-8] [test-4] [test.lox] return isEven(n - 1);
96-
[stage-8] [test-4] [test.lox] }
97-
[stage-8] [test-4] [test.lox]
98-
[stage-8] [test-4] [test.lox] print isEven(19);
99-
[stage-8] [test-4] [test.lox] print isOdd(43);
100-
[stage-8] [test-4] [test.lox]
99+
[stage-8] [test-4] [test.lox] cumulative_sum();
101100
[stage-8] [test-4] $ ./your_program.sh run test.lox
102-
[your_program] false
103-
[your_program] true
101+
[your_program] The cumulative sum from 1 to 10 is:
102+
[your_program] 55
104103
[stage-8] [test-4] ✓ 2 line(s) match on stdout
105104
[stage-8] [test-4] ✓ Received exit code 0.
106105
[stage-8] Test passed.

internal/test_helpers/fixtures/pass_functions_final

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Debug = true
55
[stage-9] [test-1] Writing contents to ./test.lox:
66
[stage-9] [test-1] [test.lox] print clock() + 23;
77
[stage-9] [test-1] $ ./your_program.sh run test.lox
8-
[your_program] 1.731520075985E9
9-
[stage-9] [test-1] ✓ 1731520075.985000
8+
[your_program] 1.731566502731E9
9+
[stage-9] [test-1] ✓ 1731566502.731000
1010
[stage-9] [test-1] ✓ 1 line(s) match on stdout
1111
[stage-9] [test-1] ✓ Received exit code 0.
1212
[stage-9] [test-2] Running test case: 2
1313
[stage-9] [test-2] Writing contents to ./test.lox:
1414
[stage-9] [test-2] [test.lox] print clock() / 1000;
1515
[stage-9] [test-2] $ ./your_program.sh run test.lox
16-
[your_program] 1731520.053144
17-
[stage-9] [test-2] ✓ 1731520.053144
16+
[your_program] 1731566.4798410002
17+
[stage-9] [test-2] ✓ 1731566.479841
1818
[stage-9] [test-2] ✓ 1 line(s) match on stdout
1919
[stage-9] [test-2] ✓ Received exit code 0.
2020
[stage-9] [test-3] Running test case: 3
@@ -83,24 +83,23 @@ Debug = true
8383
[stage-8] [test-3] ✓ Received exit code 0.
8484
[stage-8] [test-4] Running test case: 4
8585
[stage-8] [test-4] Writing contents to ./test.lox:
86-
[stage-8] [test-4] [test.lox] // This program demonstrates the use of recursive functions to determine if a number is even or odd.
87-
[stage-8] [test-4] [test.lox] // This is a classic example of mutual recursion.
88-
[stage-8] [test-4] [test.lox] fun isEven(n) {
89-
[stage-8] [test-4] [test.lox] if (n == 0) return true;
90-
[stage-8] [test-4] [test.lox] return isOdd(n - 1);
86+
[stage-8] [test-4] [test.lox] // This program calculates the cumulative sum of numbers from 1 to n.
87+
[stage-8] [test-4] [test.lox] fun cumulative_sum() {
88+
[stage-8] [test-4] [test.lox] var n = 10; // Fixed value
89+
[stage-8] [test-4] [test.lox] var total = 0;
90+
[stage-8] [test-4] [test.lox] var i = 1;
91+
[stage-8] [test-4] [test.lox] while (i <= n) {
92+
[stage-8] [test-4] [test.lox] total = total + i;
93+
[stage-8] [test-4] [test.lox] i = i + 1;
94+
[stage-8] [test-4] [test.lox] }
95+
[stage-8] [test-4] [test.lox] print "The cumulative sum from 1 to 10 is: ";
96+
[stage-8] [test-4] [test.lox] print total;
9197
[stage-8] [test-4] [test.lox] }
9298
[stage-8] [test-4] [test.lox]
93-
[stage-8] [test-4] [test.lox] fun isOdd(n) {
94-
[stage-8] [test-4] [test.lox] if (n == 0) return false;
95-
[stage-8] [test-4] [test.lox] return isEven(n - 1);
96-
[stage-8] [test-4] [test.lox] }
97-
[stage-8] [test-4] [test.lox]
98-
[stage-8] [test-4] [test.lox] print isEven(19);
99-
[stage-8] [test-4] [test.lox] print isOdd(43);
100-
[stage-8] [test-4] [test.lox]
99+
[stage-8] [test-4] [test.lox] cumulative_sum();
101100
[stage-8] [test-4] $ ./your_program.sh run test.lox
102-
[your_program] false
103-
[your_program] true
101+
[your_program] The cumulative sum from 1 to 10 is:
102+
[your_program] 55
104103
[stage-8] [test-4] ✓ 2 line(s) match on stdout
105104
[stage-8] [test-4] ✓ Received exit code 0.
106105
[stage-8] Test passed.

test_programs/f2/4.lox

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---
22
expected_error_type: none
33
---
4-
// This program demonstrates the use of recursive functions to determine if a number is even or odd.
5-
// This is a classic example of mutual recursion.
6-
fun isEven(n) {
7-
if (n == 0) return true;
8-
return isOdd(n - 1);
4+
// This program calculates the cumulative sum of numbers from 1 to n.
5+
fun cumulative_sum() {
6+
var n = 10; // Fixed value
7+
var total = 0;
8+
var i = 1;
9+
while (i <= n) {
10+
total = total + i;
11+
i = i + 1;
12+
}
13+
print "The cumulative sum from 1 to 10 is: ";
14+
print total;
915
}
1016

11-
fun isOdd(n) {
12-
if (n == 0) return false;
13-
return isEven(n - 1);
14-
}
15-
16-
print isEven(<<RANDOM_INTEGER_1>>);
17-
print isOdd(<<RANDOM_INTEGER_2>>);
17+
cumulative_sum();

0 commit comments

Comments
 (0)