use std::sync::Arc;
use cljrs_reader::Parser;
use cljrs_runtime::env::env::{Env, GlobalEnv};
use cljrs_value::Value;
fn make_env() -> (Arc<GlobalEnv>, Env) {
let globals = cljrs_runtime::Runtime::builder()
.execution_mode(cljrs_runtime::ExecutionMode::TreeWalk)
.eager_clojure_test(true)
.build()
.expect("runtime")
.into_globals();
let env = Env::new(globals.clone(), "user");
(globals, env)
}
fn eval_str(src: &str) -> Value {
let (_, mut env) = make_env();
eval_in(src, &mut env)
}
fn eval_in(src: &str, env: &mut Env) -> Value {
let mut parser = Parser::new(src.to_string(), "<test>".to_string());
let forms = parser.parse_all().expect("parse error");
let mut result = Value::Nil;
for form in forms {
result = cljrs_runtime::interp::eval::eval(&form, env).expect("eval error");
}
result
}
fn assert_partition(expr: &str, expected: &str) {
let normalised = eval_str(&format!("(pr-str (mapv vec {}))", expr));
match normalised {
Value::Str(s) => assert_eq!(
s.get().as_str(),
expected,
"partition result mismatch for {}",
expr
),
other => panic!("pr-str returned non-string: {:?}", other),
}
}
#[test]
fn partition_2arg_basic() {
assert_partition("(partition 2 [1 2 3 4])", "[[1 2] [3 4]]");
}
#[test]
fn partition_2arg_drops_incomplete_tail() {
assert_partition("(partition 2 [1 2 3])", "[[1 2]]");
}
#[test]
fn partition_2arg_empty() {
assert_partition("(partition 2 [])", "[]");
}
#[test]
fn partition_3arg_overlapping() {
assert_partition("(partition 2 1 [1 2 3 4])", "[[1 2] [2 3] [3 4]]");
}
#[test]
fn partition_3arg_step_equals_n() {
assert_partition("(partition 2 2 [1 2 3 4])", "[[1 2] [3 4]]");
}
#[test]
fn partition_3arg_skipping() {
assert_partition("(partition 2 3 [1 2 3 4 5 6])", "[[1 2] [4 5]]");
}
#[test]
fn partition_3arg_drops_incomplete_tail() {
assert_partition("(partition 3 1 [1 2 3 4])", "[[1 2 3] [2 3 4]]");
}
#[test]
fn partition_4arg_pads_last_chunk() {
assert_partition("(partition 2 2 [99] [1 2 3])", "[[1 2] [3 99]]");
}
#[test]
fn partition_4arg_repeat_nil_pad() {
assert_partition("(partition 2 2 (repeat nil) [1 2 3])", "[[1 2] [3 nil]]");
}
#[test]
fn partition_4arg_no_padding_needed() {
assert_partition("(partition 2 2 [99 99] [1 2 3 4])", "[[1 2] [3 4]]");
}
#[test]
fn partition_4arg_overlapping_with_pad() {
assert_partition(
"(partition 3 2 [0 0] [1 2 3 4 5])",
"[[1 2 3] [3 4 5] [5 0 0]]",
);
}