use fusevm::awk_host::{
awk_chr, awk_compl, awk_fold_and, awk_fold_or, awk_fold_xor, awk_index, awk_lshift, awk_mktime,
awk_ord, awk_rshift, awk_substr,
};
use fusevm::value::Value;
#[test]
fn awk_substr_negative_start_with_overlap_returns_clamped_prefix() {
let v = awk_substr(&Value::str("abcdef"), -5, Some(10)).to_str();
assert_eq!(
v, "abcd",
"negative m with m+n>1 must return clamped prefix"
);
}
#[test]
fn awk_substr_counts_chars_not_bytes_on_utf8() {
let v = awk_substr(&Value::str("héllo"), 2, Some(3)).to_str();
assert_eq!(v, "éll", "substr must count chars not bytes");
}
#[test]
fn awk_index_returns_char_position_not_byte_offset() {
let pos = awk_index(&Value::str("caféx"), &Value::str("x"));
assert_eq!(pos, 5, "index must return 1-based char position, not bytes");
}
#[test]
fn awk_lshift_masks_shift_amount_to_six_bits() {
assert_eq!(awk_lshift(&Value::Int(1), &Value::Int(64)), 1);
assert_eq!(awk_lshift(&Value::Int(1), &Value::Int(65)), 2);
let r = awk_lshift(&Value::Int(1), &Value::Int(63));
assert_eq!(r as u64, 1u64 << 63, "shift by 63 sets the top bit");
}
#[test]
fn awk_rshift_masks_shift_amount_to_six_bits() {
assert_eq!(awk_rshift(&Value::Int(-1), &Value::Int(63)), 1);
assert_eq!(awk_rshift(&Value::Int(-1), &Value::Int(64)), -1);
}
#[test]
fn awk_fold_ops_return_zero_on_empty_input() {
assert_eq!(awk_fold_and(&[]), 0);
assert_eq!(awk_fold_or(&[]), 0);
assert_eq!(awk_fold_xor(&[]), 0);
}
#[test]
fn awk_compl_of_zero_is_negative_one() {
assert_eq!(awk_compl(&Value::Int(0)), -1);
assert_eq!(awk_compl(&Value::Int(-1)), 0);
}
#[test]
fn awk_chr_returns_empty_for_surrogate_codepoint() {
let v = awk_chr(&Value::Int(0xD800)).to_str();
assert_eq!(v, "", "surrogate codepoint must yield empty string");
let v = awk_chr(&Value::Int(0x110000)).to_str();
assert_eq!(v, "", "codepoint above U+10FFFF must yield empty string");
}
#[test]
fn awk_ord_returns_scalar_value_of_first_char_not_first_byte() {
let v = awk_ord(&Value::str("ñoño")).to_float();
assert_eq!(
v, 241.0,
"ord must read first char's scalar, not first byte"
);
}
#[test]
fn awk_mktime_returns_minus_one_on_too_few_parts() {
let v = awk_mktime(&[Value::str("2020 1 1 0 0")]).to_float();
assert_eq!(v, -1.0, "mktime with <6 parts must return -1");
}
#[test]
fn awk_mktime_returns_minus_one_on_non_numeric_part() {
let v = awk_mktime(&[Value::str("2020 1 1 abc 0 0")]).to_float();
assert_eq!(v, -1.0, "mktime with non-numeric part must return -1");
}
#[test]
fn awk_mktime_utc_epoch_zero() {
let v = awk_mktime(&[Value::str("1970 1 1 0 0 0"), Value::Int(1)]).to_float();
assert_eq!(v, 0.0, "1970-01-01 00:00:00 UTC must map to epoch 0");
}