use crate::tree::range::cursor_key::CursorKey;
use crate::tree::range::helper::ReverseScanHelper;
use crate::tree::range::iterator::iter_flags::ReverseFlags;
fn handle_down_back(cursor_key: &mut CursorKey, flags: &mut ReverseFlags) {
cursor_key.shift_clear_reverse();
flags.set_upper_bound();
}
#[test]
fn test_handle_down_back_sets_cursor_to_max() {
let mut cursor_key = CursorKey::from_slice(b"test1234suffix");
let mut flags = ReverseFlags::new();
assert!(!flags.upper_bound());
let original_offset = cursor_key.offset();
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.current_ikey(), u64::MAX);
assert_eq!(cursor_key.current_len(), 9); assert_eq!(cursor_key.offset(), original_offset + 8); assert!(flags.upper_bound());
}
#[test]
fn test_handle_down_back_from_root() {
let mut cursor_key = CursorKey::from_slice(b"hello");
let mut flags = ReverseFlags::new();
assert_eq!(cursor_key.offset(), 0);
assert!(!flags.upper_bound());
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.offset(), 8);
assert_eq!(cursor_key.current_ikey(), u64::MAX);
assert!(flags.upper_bound());
}
#[test]
fn test_handle_down_back_multiple_descents() {
let mut cursor_key = CursorKey::from_slice(b"a]");
let mut flags = ReverseFlags::new();
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.offset(), 8);
assert!(flags.upper_bound());
flags.clear_upper_bound();
assert!(!flags.upper_bound());
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.offset(), 16);
assert!(flags.upper_bound());
}
#[test]
fn test_is_at_empty_root_false_normally() {
let mut cursor_key = CursorKey::from_slice(b"hello world!!!!!");
cursor_key.shift();
cursor_key.unshift();
assert!(!cursor_key.is_at_empty_root());
}
#[test]
fn test_is_at_empty_root_at_root() {
let cursor_key = CursorKey::empty();
assert_eq!(cursor_key.offset(), 0);
assert_eq!(cursor_key.current_len(), 0);
assert!(cursor_key.is_at_empty_root());
}
#[test]
fn test_flags_upper_bound_default_false() {
let flags = ReverseFlags::new();
assert!(!flags.upper_bound());
}
#[test]
fn test_flags_clear_upper_bound() {
let mut flags = ReverseFlags::new();
flags.set_upper_bound();
flags.clear_upper_bound();
assert!(!flags.upper_bound());
}
#[test]
fn test_helper_prev() {
assert_eq!(ReverseScanHelper::prev(5), 4);
assert_eq!(ReverseScanHelper::prev(1), 0);
assert_eq!(ReverseScanHelper::prev(0), -1);
assert_eq!(ReverseScanHelper::prev(-1), -2);
}
#[test]
fn test_shift_clear_reverse_sets_max_ikey() {
let mut cursor_key = CursorKey::from_slice(b"test");
cursor_key.shift_clear_reverse();
assert_eq!(cursor_key.current_ikey(), u64::MAX);
assert_eq!(cursor_key.current_len(), 9); assert_eq!(cursor_key.offset(), 8);
}
#[test]
fn test_unshift_sentinel_len() {
let mut cursor_key = CursorKey::from_slice(b"hello world!!!!!");
cursor_key.shift();
assert_eq!(cursor_key.offset(), 8);
cursor_key.unshift();
assert_eq!(cursor_key.offset(), 0);
assert_eq!(cursor_key.current_len(), 9); }
#[test]
fn test_reverse_scan_cursor_flow() {
let mut cursor_key = CursorKey::from_slice(b"prefix__suffix99");
let mut flags = ReverseFlags::new();
assert_eq!(cursor_key.offset(), 0);
let original_ikey = cursor_key.current_ikey();
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.offset(), 8);
assert_eq!(cursor_key.current_ikey(), u64::MAX);
assert!(flags.upper_bound());
flags.clear_upper_bound();
assert!(!flags.upper_bound());
cursor_key.unshift();
assert_eq!(cursor_key.offset(), 0);
assert_eq!(cursor_key.current_ikey(), original_ikey);
assert_eq!(cursor_key.current_len(), 9); }
#[test]
fn test_deep_descent_ascent_cycle() {
let mut cursor_key = CursorKey::from_slice(b"aaaaaaaa"); let mut flags = ReverseFlags::new();
for expected_offset in [8, 16, 24] {
handle_down_back(&mut cursor_key, &mut flags);
assert_eq!(cursor_key.offset(), expected_offset);
assert!(flags.upper_bound());
flags.clear_upper_bound();
}
for expected_offset in [16, 8, 0] {
cursor_key.unshift();
assert_eq!(cursor_key.offset(), expected_offset);
assert_eq!(cursor_key.current_len(), 9); }
}