Skip to content

Commit de4bc17

Browse files
Sync tests for practice exercise forth (#2536)
1 parent bf0a20a commit de4bc17

File tree

4 files changed

+99
-55
lines changed

4 files changed

+99
-55
lines changed

exercises/practice/forth/.meta/src/reference/java/Token.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static List<Token> fromString(final String string) {
7777
return Collections.singletonList(OpDefToken.opDefTokenFromString(string));
7878
} else if (string.matches("[A-z+/*\\-]+(?:-[A-z+/*\\-]+)*")) {
7979
return Collections.singletonList(new OpToken(string.toLowerCase()));
80-
} else if (string.matches("\\d+")) {
80+
} else if (string.matches("-?\\d+")) {
8181
return Collections.singletonList(new IntToken(Integer.parseInt(string)));
8282
} else {
8383
return Arrays.stream(string.split(" "))
Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,157 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[9962203f-f00a-4a85-b404-8a8ecbcec09d]
6-
description = "numbers just get pushed onto the stack"
13+
description = "parsing and numbers -> numbers just get pushed onto the stack"
14+
15+
[fd7a8da2-6818-4203-a866-fed0714e7aa0]
16+
description = "parsing and numbers -> pushes negative numbers onto the stack"
717

818
[9e69588e-a3d8-41a3-a371-ea02206c1e6e]
9-
description = "can add two numbers"
19+
description = "addition -> can add two numbers"
1020

1121
[52336dd3-30da-4e5c-8523-bdf9a3427657]
12-
description = "errors if there is nothing on the stack"
22+
description = "addition -> errors if there is nothing on the stack"
1323

1424
[06efb9a4-817a-435e-b509-06166993c1b8]
15-
description = "errors if there is only one value on the stack"
25+
description = "addition -> errors if there is only one value on the stack"
1626

1727
[09687c99-7bbc-44af-8526-e402f997ccbf]
18-
description = "can subtract two numbers"
28+
description = "subtraction -> can subtract two numbers"
1929

2030
[5d63eee2-1f7d-4538-b475-e27682ab8032]
21-
description = "errors if there is nothing on the stack"
31+
description = "subtraction -> errors if there is nothing on the stack"
2232

2333
[b3cee1b2-9159-418a-b00d-a1bb3765c23b]
24-
description = "errors if there is only one value on the stack"
34+
description = "subtraction -> errors if there is only one value on the stack"
2535

2636
[5df0ceb5-922e-401f-974d-8287427dbf21]
27-
description = "can multiply two numbers"
37+
description = "multiplication -> can multiply two numbers"
2838

2939
[9e004339-15ac-4063-8ec1-5720f4e75046]
30-
description = "errors if there is nothing on the stack"
40+
description = "multiplication -> errors if there is nothing on the stack"
3141

3242
[8ba4b432-9f94-41e0-8fae-3b3712bd51b3]
33-
description = "errors if there is only one value on the stack"
43+
description = "multiplication -> errors if there is only one value on the stack"
3444

3545
[e74c2204-b057-4cff-9aa9-31c7c97a93f5]
36-
description = "can divide two numbers"
46+
description = "division -> can divide two numbers"
3747

3848
[54f6711c-4b14-4bb0-98ad-d974a22c4620]
39-
description = "performs integer division"
49+
description = "division -> performs integer division"
4050

4151
[a5df3219-29b4-4d2f-b427-81f82f42a3f1]
42-
description = "errors if dividing by zero"
52+
description = "division -> errors if dividing by zero"
4353

4454
[1d5bb6b3-6749-4e02-8a79-b5d4d334cb8a]
45-
description = "errors if there is nothing on the stack"
55+
description = "division -> errors if there is nothing on the stack"
4656

4757
[d5547f43-c2ff-4d5c-9cb0-2a4f6684c20d]
48-
description = "errors if there is only one value on the stack"
58+
description = "division -> errors if there is only one value on the stack"
4959

5060
[ee28d729-6692-4a30-b9be-0d830c52a68c]
51-
description = "addition and subtraction"
61+
description = "combined arithmetic -> addition and subtraction"
5262

5363
[40b197da-fa4b-4aca-a50b-f000d19422c1]
54-
description = "multiplication and division"
64+
description = "combined arithmetic -> multiplication and division"
5565

5666
[c5758235-6eef-4bf6-ab62-c878e50b9957]
57-
description = "copies a value on the stack"
67+
description = "dup -> copies a value on the stack"
5868

5969
[f6889006-5a40-41e7-beb3-43b09e5a22f4]
60-
description = "copies the top value on the stack"
70+
description = "dup -> copies the top value on the stack"
6171

6272
[40b7569c-8401-4bd4-a30d-9adf70d11bc4]
63-
description = "errors if there is nothing on the stack"
73+
description = "dup -> errors if there is nothing on the stack"
6474

6575
[1971da68-1df2-4569-927a-72bf5bb7263c]
66-
description = "removes the top value on the stack if it is the only one"
76+
description = "drop -> removes the top value on the stack if it is the only one"
6777

6878
[8929d9f2-4a78-4e0f-90ad-be1a0f313fd9]
69-
description = "removes the top value on the stack if it is not the only one"
79+
description = "drop -> removes the top value on the stack if it is not the only one"
7080

7181
[6dd31873-6dd7-4cb8-9e90-7daa33ba045c]
72-
description = "errors if there is nothing on the stack"
82+
description = "drop -> errors if there is nothing on the stack"
7383

7484
[3ee68e62-f98a-4cce-9e6c-8aae6c65a4e3]
75-
description = "swaps the top two values on the stack if they are the only ones"
85+
description = "swap -> swaps the top two values on the stack if they are the only ones"
7686

7787
[8ce869d5-a503-44e4-ab55-1da36816ff1c]
78-
description = "swaps the top two values on the stack if they are not the only ones"
88+
description = "swap -> swaps the top two values on the stack if they are not the only ones"
7989

8090
[74ba5b2a-b028-4759-9176-c5c0e7b2b154]
81-
description = "errors if there is nothing on the stack"
91+
description = "swap -> errors if there is nothing on the stack"
8292

8393
[dd52e154-5d0d-4a5c-9e5d-73eb36052bc8]
84-
description = "errors if there is only one value on the stack"
94+
description = "swap -> errors if there is only one value on the stack"
8595

8696
[a2654074-ba68-4f93-b014-6b12693a8b50]
87-
description = "copies the second element if there are only two"
97+
description = "over -> copies the second element if there are only two"
8898

8999
[c5b51097-741a-4da7-8736-5c93fa856339]
90-
description = "copies the second element if there are more than two"
100+
description = "over -> copies the second element if there are more than two"
91101

92102
[6e1703a6-5963-4a03-abba-02e77e3181fd]
93-
description = "errors if there is nothing on the stack"
103+
description = "over -> errors if there is nothing on the stack"
94104

95105
[ee574dc4-ef71-46f6-8c6a-b4af3a10c45f]
96-
description = "errors if there is only one value on the stack"
106+
description = "over -> errors if there is only one value on the stack"
97107

98108
[ed45cbbf-4dbf-4901-825b-54b20dbee53b]
99-
description = "can consist of built-in words"
109+
description = "user-defined words -> can consist of built-in words"
100110

101111
[2726ea44-73e4-436b-bc2b-5ff0c6aa014b]
102-
description = "execute in the right order"
112+
description = "user-defined words -> execute in the right order"
103113

104114
[9e53c2d0-b8ef-4ad8-b2c9-a559b421eb33]
105-
description = "can override other user-defined words"
115+
description = "user-defined words -> can override other user-defined words"
106116

107117
[669db3f3-5bd6-4be0-83d1-618cd6e4984b]
108-
description = "can override built-in words"
118+
description = "user-defined words -> can override built-in words"
109119

110120
[588de2f0-c56e-4c68-be0b-0bb1e603c500]
111-
description = "can override built-in operators"
121+
description = "user-defined words -> can override built-in operators"
112122

113123
[ac12aaaf-26c6-4a10-8b3c-1c958fa2914c]
114-
description = "can use different words with the same name"
124+
description = "user-defined words -> can use different words with the same name"
115125

116126
[53f82ef0-2750-4ccb-ac04-5d8c1aefabb1]
117-
description = "can define word that uses word with the same name"
127+
description = "user-defined words -> can define word that uses word with the same name"
118128

119129
[35958cee-a976-4a0f-9378-f678518fa322]
120-
description = "cannot redefine numbers"
130+
description = "user-defined words -> cannot redefine non-negative numbers"
131+
132+
[df5b2815-3843-4f55-b16c-c3ed507292a7]
133+
description = "user-defined words -> cannot redefine negative numbers"
121134

122135
[5180f261-89dd-491e-b230-62737e09806f]
123-
description = "errors if executing a non-existent word"
136+
description = "user-defined words -> errors if executing a non-existent word"
137+
138+
[3c8bfef3-edbb-49c1-9993-21d4030043cb]
139+
description = "user-defined words -> only defines locally"
124140

125141
[7b83bb2e-b0e8-461f-ad3b-96ee2e111ed6]
126-
description = "DUP is case-insensitive"
142+
description = "case-insensitivity -> DUP is case-insensitive"
127143

128144
[339ed30b-f5b4-47ff-ab1c-67591a9cd336]
129-
description = "DROP is case-insensitive"
145+
description = "case-insensitivity -> DROP is case-insensitive"
130146

131147
[ee1af31e-1355-4b1b-bb95-f9d0b2961b87]
132-
description = "SWAP is case-insensitive"
148+
description = "case-insensitivity -> SWAP is case-insensitive"
133149

134150
[acdc3a49-14c8-4cc2-945d-11edee6408fa]
135-
description = "OVER is case-insensitive"
151+
description = "case-insensitivity -> OVER is case-insensitive"
136152

137153
[5934454f-a24f-4efc-9fdd-5794e5f0c23c]
138-
description = "user-defined words are case-insensitive"
154+
description = "case-insensitivity -> user-defined words are case-insensitive"
139155

140156
[037d4299-195f-4be7-a46d-f07ca6280a06]
141-
description = "definitions are case-insensitive"
157+
description = "case-insensitivity -> definitions are case-insensitive"

exercises/practice/forth/.meta/version

Lines changed: 0 additions & 1 deletion
This file was deleted.

exercises/practice/forth/src/test/java/ForthEvaluatorTest.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import static org.assertj.core.api.Assertions.assertThat;
2-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3-
41
import org.junit.Ignore;
52
import org.junit.Test;
63

74
import java.util.Arrays;
85
import java.util.Collections;
6+
import java.util.List;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
910

1011
public class ForthEvaluatorTest {
1112

@@ -17,6 +18,13 @@ public void testNumbersAreJustPushedOntoTheStack() {
1718
.containsExactly(1, 2, 3, 4, 5);
1819
}
1920

21+
@Ignore("Remove to run test")
22+
@Test
23+
public void testNegativeNumbersArePushedOntoTheStack() {
24+
assertThat(forthEvaluator.evaluateProgram(Collections.singletonList("-1 -2 -3 -4 -5")))
25+
.containsExactly(-1, -2, -3, -4, -5);
26+
}
27+
2028
@Ignore("Remove to run test")
2129
@Test
2230
public void testTwoNumbersCanBeAdded() {
@@ -284,12 +292,20 @@ public void testCanDefineWordThatUsesWordWithTheSameName() {
284292

285293
@Ignore("Remove to run test")
286294
@Test
287-
public void testCannotRedefineNumbers() {
295+
public void testCannotRedefineNonNegativeNumbers() {
288296
assertThatExceptionOfType(IllegalArgumentException.class)
289297
.isThrownBy(() -> forthEvaluator.evaluateProgram(Collections.singletonList(": 1 2 ;")))
290298
.withMessage("Cannot redefine numbers");
291299
}
292300

301+
@Ignore("Remove to run test")
302+
@Test
303+
public void testCannotRedefineNegativeNumbers() {
304+
assertThatExceptionOfType(IllegalArgumentException.class)
305+
.isThrownBy(() -> forthEvaluator.evaluateProgram(Collections.singletonList(": -1 2 ;")))
306+
.withMessage("Cannot redefine numbers");
307+
}
308+
293309
@Ignore("Remove to run test")
294310
@Test
295311
public void testErrorIfEvaluatingAnUndefinedOperator() {
@@ -340,4 +356,17 @@ public void testDefinitionsAreCaseInsensitive() {
340356
.containsExactly(1, 1, 1, 1);
341357
}
342358

359+
@Ignore
360+
@Test
361+
public void testDefinitionsAreOnlyDefinedLocally() {
362+
ForthEvaluator firstInstance = new ForthEvaluator();
363+
ForthEvaluator secondInstance = new ForthEvaluator();
364+
365+
List<Integer> firstOutput = firstInstance.evaluateProgram(Arrays.asList(": + - ;", "1 1 +"));
366+
List<Integer> secondOutput = secondInstance.evaluateProgram(Collections.singletonList("1 1 +"));
367+
368+
assertThat(firstOutput).containsExactly(0);
369+
assertThat(secondOutput).containsExactly(2);
370+
}
371+
343372
}

0 commit comments

Comments
 (0)