Skip to content
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
46 changes: 46 additions & 0 deletions exercises/practice/forth/src/test/kotlin/ForthTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import org.junit.Rule
import org.junit.rules.ExpectedException
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals

Expand All @@ -17,15 +18,18 @@ abstract class ForthTest {

class Addition : ForthTest() {

@Ignore
@Test
fun `can add two numbers`() = assertEquals(listOf(3), Forth().evaluate("1 2 +"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("+")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -35,15 +39,18 @@ abstract class ForthTest {

class Subtraction : ForthTest() {

@Ignore
@Test
fun `can subtract two numbers`() = assertEquals(listOf(-1), Forth().evaluate("3 4 -"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("-")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -53,15 +60,18 @@ abstract class ForthTest {

class Multiplication : ForthTest() {

@Ignore
@Test
fun `can multiply two numbers`() = assertEquals(listOf(8), Forth().evaluate("2 4 *"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("*")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -71,24 +81,29 @@ abstract class ForthTest {

class Division : ForthTest() {

@Ignore
@Test
fun `can divide two numbers`() = assertEquals(listOf(4), Forth().evaluate("12 3 /"))

@Ignore
@Test
fun `performs integer division`() = assertEquals(listOf(2), Forth().evaluate("8 3 /"))

@Ignore
@Test
fun `errors if dividing by zero`() {
expectedException.expectMessage("divide by zero")
Forth().evaluate("4 0 /")
}

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("/")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -98,21 +113,26 @@ abstract class ForthTest {

class CombinedArithmetic : ForthTest() {

@Ignore
@Test
fun `addition and subtraction`() = assertEquals(listOf(-1), Forth().evaluate("1 2 + 4 -"))

@Ignore
@Test
fun `multiplication and division`() = assertEquals(listOf(2), Forth().evaluate("2 4 * 3 /"))
}

class Dup : ForthTest() {

@Ignore
@Test
fun `copies a value on the stack`() = assertEquals(listOf(1, 1), Forth().evaluate("1 dup"))

@Ignore
@Test
fun `copies the top value on the stack`() = assertEquals(listOf(1, 2, 2), Forth().evaluate("1 2 dup"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Expand All @@ -122,12 +142,15 @@ abstract class ForthTest {

class Drop : ForthTest() {

@Ignore
@Test
fun `removes the top value on the stack if it is the only one`() = assertEquals(listOf(), Forth().evaluate("1 drop"))

@Ignore
@Test
fun `removes the top value on the stack if it is not the only one`() = assertEquals(listOf(1), Forth().evaluate("1 2 drop"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Expand All @@ -137,18 +160,22 @@ abstract class ForthTest {

class Swap : ForthTest() {

@Ignore
@Test
fun `swaps the top two values on the stack if they are the only ones`() = assertEquals(listOf(2, 1), Forth().evaluate("1 2 swap"))

@Ignore
@Test
fun `swaps the top two values on the stack if they are not the only ones`() = assertEquals(listOf(1, 3, 2), Forth().evaluate("1 2 3 swap"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("swap")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -158,18 +185,22 @@ abstract class ForthTest {

class Over : ForthTest() {

@Ignore
@Test
fun `copies the second element if there are only two`() = assertEquals(listOf(1, 2, 1), Forth().evaluate("1 2 over"))

@Ignore
@Test
fun `copies the second element if there are more than two`() = assertEquals(listOf(1, 2, 3, 2), Forth().evaluate("1 2 3 over"))

@Ignore
@Test
fun `errors if there is nothing on the stack`() {
expectedException.expectMessage("empty stack")
Forth().evaluate("over")
}

@Ignore
@Test
fun `errors if there is only one value on the stack`() {
expectedException.expectMessage("only one value on the stack")
Expand All @@ -179,33 +210,42 @@ abstract class ForthTest {

class UserDefinedWords : ForthTest() {

@Ignore
@Test
fun `can consist of built-in words`() = assertEquals(listOf(1, 1, 1), Forth().evaluate(": dup-twice dup dup ;", "1 dup-twice"))

@Ignore
@Test
fun `execute in the right order`() = assertEquals(listOf(1, 2, 3), Forth().evaluate(": countup 1 2 3 ;", "countup"))

@Ignore
@Test
fun `can override other user-defined words`() = assertEquals(listOf(1, 1, 1), Forth().evaluate(": foo dup ;", ": foo dup dup ;", "1 foo"))

@Ignore
@Test
fun `can override built-in words`() = assertEquals(listOf(1, 1), Forth().evaluate(": swap dup ;", "1 swap"))

@Ignore
@Test
fun `can override built-in operators`() = assertEquals(listOf(12), Forth().evaluate(": + * ;", "3 4 +"))

@Ignore
@Test
fun `can use different words with the same name`() = assertEquals(listOf(5, 6), Forth().evaluate(": foo 5 ;", ": bar foo ;", ": foo 6 ;", "bar foo"))

@Ignore
@Test
fun `can define word that uses word with the same name`() = assertEquals(listOf(11), Forth().evaluate(": foo 10 ;", ": foo foo 1 + ;", "foo"))

@Ignore
@Test
fun `cannot redefine numbers`() {
expectedException.expectMessage("illegal operation")
Forth().evaluate(": 1 2 ;")
}

@Ignore
@Test
fun `errors if executing a non-existent word`() {
expectedException.expectMessage("undefined operation")
Expand All @@ -215,21 +255,27 @@ abstract class ForthTest {

class CaseInsensitivity : ForthTest() {

@Ignore
@Test
fun `DUP is case-insensitive`() = assertEquals(listOf(1, 1, 1, 1), Forth().evaluate("1 DUP Dup dup"))

@Ignore
@Test
fun `DROP is case-insensitive`() = assertEquals(listOf(1), Forth().evaluate("1 2 3 4 DROP Drop drop"))

@Ignore
@Test
fun `SWAP is case-insensitive`() = assertEquals(listOf(2, 3, 4, 1), Forth().evaluate("1 2 SWAP 3 Swap 4 swap"))

@Ignore
@Test
fun `OVER is case-insensitive`() = assertEquals(listOf(1, 2, 1, 2, 1), Forth().evaluate("1 2 OVER Over over"))

@Ignore
@Test
fun `user-defined words are case-insensitive`() = assertEquals(listOf(1, 1, 1, 1), Forth().evaluate(": foo dup ;", "1 FOO Foo foo"))

@Ignore
@Test
fun `definitions are case-insensitive`() = assertEquals(listOf(1, 1, 1, 1), Forth().evaluate(": SWAP DUP Dup dup ;", "1 swap"))
}
Expand Down