forth: Test each operation individually
Previously, + and - were together, then * and / were together. Let's
have more focused tests so it's easier to tell which operation is
responsible for an individual test failure.
We keep the old tests, but move them after the individual tests.
@@ -25,25 +25,25 @@ fn non_word_characters_are_separators() {
#[test]
#[ignore]
-fn basic_arithmetic_1() {
+fn can_add_two_numbers() {
let mut f = Forth::new();
- assert!(f.eval("1 2 + 4 -").is_ok());
- assert_eq!(vec![-1], f.stack());
+ assert!(f.eval("1 2 +").is_ok());
+ assert_eq!(vec![3], f.stack());
}
#[test]
#[ignore]
-fn basic_arithmetic_2() {
+fn addition_error() {
let mut f = Forth::new();
- assert!(f.eval("2 4 * 3 /").is_ok());
- assert_eq!(vec![2], f.stack());
+ assert_eq!(Err(Error::StackUnderflow), f.eval("+"));
}
#[test]
#[ignore]
-fn addition_error() {
+fn can_subtract_two_numbers() {
let mut f = Forth::new();
- assert_eq!(Err(Error::StackUnderflow), f.eval("+"));
+ assert!(f.eval("3 4 -").is_ok());
+ assert_eq!(vec![-1], f.stack());
}
#[test]
@@ -53,6 +53,14 @@ fn subtraction_error() {
assert_eq!(Err(Error::StackUnderflow), f.eval("-"));
}
+#[test]
+#[ignore]
+fn can_multiply_two_numbers() {
+ let mut f = Forth::new();
+ assert!(f.eval("2 4 *").is_ok());
+ assert_eq!(vec![8], f.stack());
+}
+
#[test]
#[ignore]
fn multiplication_error() {
@@ -60,6 +68,22 @@ fn multiplication_error() {
assert_eq!(Err(Error::StackUnderflow), f.eval("*"));
}
+#[test]
+#[ignore]
+fn can_divide_two_numbers() {
+ let mut f = Forth::new();
+ assert!(f.eval("12 3 /").is_ok());
+ assert_eq!(vec![4], f.stack());
+}
+
+#[test]
+#[ignore]
+fn performs_integer_division() {
+ let mut f = Forth::new();
+ assert!(f.eval("8 3 /").is_ok());
+ assert_eq!(vec![2], f.stack());
+}
+
#[test]
#[ignore]
fn division_error() {
@@ -74,6 +98,22 @@ fn division_by_zero() {
assert_eq!(Err(Error::DivisionByZero), f.eval("4 2 2 - /"));
}
+#[test]
+#[ignore]
+fn addition_and_subtraction() {
+ let mut f = Forth::new();
+ assert!(f.eval("1 2 + 4 -").is_ok());
+ assert_eq!(vec![-1], f.stack());
+}
+
+#[test]
+#[ignore]
+fn multiplication_and_division() {
+ let mut f = Forth::new();
+ assert!(f.eval("2 4 * 3 /").is_ok());
+ assert_eq!(vec![2], f.stack());
+}
+
#[test]
#[ignore]
fn dup() {
