Feat: prelude and new words
This commit is contained in:
@@ -102,3 +102,43 @@ fn geom_zero_count() {
|
||||
fn geom_underflow() {
|
||||
expect_error("1 2 geom..", "stack underflow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_ascending() {
|
||||
expect_stack("0 1 0.25 .,", &[
|
||||
int(0),
|
||||
Value::Float(0.25, None),
|
||||
Value::Float(0.5, None),
|
||||
Value::Float(0.75, None),
|
||||
int(1),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_descending() {
|
||||
expect_stack("1 0 -0.5 .,", &[
|
||||
int(1),
|
||||
Value::Float(0.5, None),
|
||||
int(0),
|
||||
]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_integer_step() {
|
||||
expect_stack("0 6 2 .,", &[int(0), int(2), int(4), int(6)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_single() {
|
||||
expect_stack("5 5 1 .,", &[int(5)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_zero_step_error() {
|
||||
expect_error("0 10 0 .,", "step cannot be zero");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn step_range_underflow() {
|
||||
expect_error("0 1 .,", "stack underflow");
|
||||
}
|
||||
|
||||
@@ -152,3 +152,104 @@ fn clear_stack() {
|
||||
f.clear_stack();
|
||||
assert!(f.stack().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rev() {
|
||||
expect_stack("1 2 3 3 rev", &[int(3), int(2), int(1)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rev_partial() {
|
||||
expect_stack("1 2 3 4 2 rev", &[int(1), int(2), int(4), int(3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rev_one() {
|
||||
expect_stack("1 2 3 1 rev", &[int(1), int(2), int(3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rev_zero() {
|
||||
expect_stack("1 2 3 0 rev", &[int(1), int(2), int(3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rev_underflow() {
|
||||
expect_error("1 2 5 rev", "stack underflow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shuffle_preserves_elements() {
|
||||
let f = forth();
|
||||
f.evaluate("1 2 3 4 4 shuffle", &default_ctx()).unwrap();
|
||||
let mut stack: Vec<i64> = f
|
||||
.stack()
|
||||
.iter()
|
||||
.map(|v| match v {
|
||||
Value::Int(n, _) => *n,
|
||||
_ => panic!("expected int"),
|
||||
})
|
||||
.collect();
|
||||
stack.sort();
|
||||
assert_eq!(stack, vec![1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shuffle_underflow() {
|
||||
expect_error("1 2 5 shuffle", "stack underflow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort() {
|
||||
expect_stack("3 1 4 1 5 5 sort", &[int(1), int(1), int(3), int(4), int(5)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_partial() {
|
||||
expect_stack("9 3 1 2 3 sort", &[int(9), int(1), int(2), int(3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_underflow() {
|
||||
expect_error("1 2 5 sort", "stack underflow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rsort() {
|
||||
expect_stack("1 3 2 3 rsort", &[int(3), int(2), int(1)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sum() {
|
||||
expect_stack("1 2 3 4 4 sum", &[int(10)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sum_single() {
|
||||
expect_stack("42 1 sum", &[int(42)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sum_zero() {
|
||||
expect_stack("1 2 3 0 sum", &[int(1), int(2), int(3), int(0)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sum_underflow() {
|
||||
expect_error("1 2 5 sum", "stack underflow");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prod() {
|
||||
expect_stack("2 3 4 3 prod", &[int(24)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prod_single() {
|
||||
expect_stack("7 1 prod", &[int(7)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prod_zero_count() {
|
||||
expect_stack("1 2 3 0 prod", &[int(1), int(2), int(3), int(1)]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user