use crate::base::{INVALID_KEY_ID, INVALID_LINK_ID};
#[derive(Debug, Clone, Copy)]
pub struct History {
node_id: u32,
louds_pos: u32,
key_pos: u32,
link_id: u32,
key_id: u32,
}
impl Default for History {
fn default() -> Self {
Self::new()
}
}
impl History {
pub fn new() -> Self {
History {
node_id: 0,
louds_pos: 0,
key_pos: 0,
link_id: INVALID_LINK_ID,
key_id: INVALID_KEY_ID,
}
}
#[inline]
pub fn set_node_id(&mut self, node_id: usize) {
assert!(node_id <= u32::MAX as usize, "Node ID exceeds u32::MAX");
self.node_id = node_id as u32;
}
#[inline]
pub fn set_louds_pos(&mut self, louds_pos: usize) {
assert!(
louds_pos <= u32::MAX as usize,
"LOUDS position exceeds u32::MAX"
);
self.louds_pos = louds_pos as u32;
}
#[inline]
pub fn set_key_pos(&mut self, key_pos: usize) {
assert!(
key_pos <= u32::MAX as usize,
"Key position exceeds u32::MAX"
);
self.key_pos = key_pos as u32;
}
#[inline]
pub fn set_link_id(&mut self, link_id: usize) {
assert!(link_id <= u32::MAX as usize, "Link ID exceeds u32::MAX");
self.link_id = link_id as u32;
}
#[inline]
pub fn set_key_id(&mut self, key_id: usize) {
assert!(key_id <= u32::MAX as usize, "Key ID exceeds u32::MAX");
self.key_id = key_id as u32;
}
#[inline]
pub fn node_id(&self) -> usize {
self.node_id as usize
}
#[inline]
pub fn louds_pos(&self) -> usize {
self.louds_pos as usize
}
#[inline]
pub fn key_pos(&self) -> usize {
self.key_pos as usize
}
#[inline]
pub fn link_id(&self) -> usize {
self.link_id as usize
}
#[inline]
pub fn key_id(&self) -> usize {
self.key_id as usize
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_history_new() {
let history = History::new();
assert_eq!(history.node_id(), 0);
assert_eq!(history.louds_pos(), 0);
assert_eq!(history.key_pos(), 0);
assert_eq!(history.link_id(), INVALID_LINK_ID as usize);
assert_eq!(history.key_id(), INVALID_KEY_ID as usize);
}
#[test]
fn test_history_default() {
let history = History::default();
assert_eq!(history.node_id(), 0);
assert_eq!(history.louds_pos(), 0);
assert_eq!(history.key_pos(), 0);
assert_eq!(history.link_id(), INVALID_LINK_ID as usize);
assert_eq!(history.key_id(), INVALID_KEY_ID as usize);
}
#[test]
fn test_history_set_node_id() {
let mut history = History::new();
history.set_node_id(100);
assert_eq!(history.node_id(), 100);
}
#[test]
fn test_history_set_louds_pos() {
let mut history = History::new();
history.set_louds_pos(200);
assert_eq!(history.louds_pos(), 200);
}
#[test]
fn test_history_set_key_pos() {
let mut history = History::new();
history.set_key_pos(50);
assert_eq!(history.key_pos(), 50);
}
#[test]
fn test_history_set_link_id() {
let mut history = History::new();
history.set_link_id(123);
assert_eq!(history.link_id(), 123);
}
#[test]
fn test_history_set_key_id() {
let mut history = History::new();
history.set_key_id(456);
assert_eq!(history.key_id(), 456);
}
#[test]
fn test_history_all_fields() {
let mut history = History::new();
history.set_node_id(10);
history.set_louds_pos(20);
history.set_key_pos(5);
history.set_link_id(30);
history.set_key_id(40);
assert_eq!(history.node_id(), 10);
assert_eq!(history.louds_pos(), 20);
assert_eq!(history.key_pos(), 5);
assert_eq!(history.link_id(), 30);
assert_eq!(history.key_id(), 40);
}
#[test]
fn test_history_max_values() {
let mut history = History::new();
history.set_node_id(u32::MAX as usize);
history.set_louds_pos(u32::MAX as usize);
history.set_key_pos(u32::MAX as usize);
history.set_link_id(u32::MAX as usize);
history.set_key_id(u32::MAX as usize);
assert_eq!(history.node_id(), u32::MAX as usize);
assert_eq!(history.louds_pos(), u32::MAX as usize);
assert_eq!(history.key_pos(), u32::MAX as usize);
assert_eq!(history.link_id(), u32::MAX as usize);
assert_eq!(history.key_id(), u32::MAX as usize);
}
#[test]
fn test_history_clone() {
let mut history1 = History::new();
history1.set_node_id(10);
history1.set_key_pos(5);
let history2 = history1;
assert_eq!(history2.node_id(), 10);
assert_eq!(history2.key_pos(), 5);
}
#[test]
fn test_history_copy() {
let mut history1 = History::new();
history1.set_node_id(10);
let history2 = history1;
assert_eq!(history1.node_id(), 10);
assert_eq!(history2.node_id(), 10);
}
}