use crate::data::atomic::Word;
use crate::data::identification::{Address, Area};
use crate::data::composite::{Array, ProduceConsume, WordQueue, WordStack};
use crate::memory::Memory;
mod test_atomic;
mod test_identification;
#[test]
fn test_array() {
let mut array = Array::new(1);
assert_eq!(array.len(), 1);
array[Address::new(0)] = Word::new(0);
array.push(Word::new(1));
assert_eq!(array.len(), 2);
assert_eq!(array[Address::new(0)], Word::new(0));
assert_eq!(array[Address::new(1)], Word::new(1));
assert_eq!(&[Word::new(0), Word::new(1)], array.as_slice())
}
#[test]
fn test_queue() {
let mut queue = WordQueue::new();
assert!(queue.is_empty());
queue.produce(Word::new(1));
queue.produce(Word::new(2));
queue.produce(Word::new(3));
assert_eq!(queue.len(), 3);
assert_eq!(queue.consume(), Some(Word::new(1)));
assert_eq!(queue.len(), 2);
}
#[test]
fn test_stack() {
let mut stack = WordStack::new();
assert!(stack.is_empty());
stack.produce(Word::new(1));
stack.produce(Word::new(2));
stack.produce(Word::new(3));
assert_eq!(stack.len(), 3);
assert_eq!(stack.consume(), Some(Word::new(3)));
assert_eq!(stack.len(), 2);
}
#[test]
fn test_alloc() {
let mut memory = Memory::new();
assert!(memory.is_empty());
memory.alloc(1024);
assert_eq!(memory.len(), 1024);
memory.free(512).unwrap();
assert_eq!(memory.len(), 512);
}
#[test]
fn test_memory() {
let mut memory = Memory::with_size(4096);
let addr0 = Address::new(0);
let addr1 = Address::new(3);
let addr2 = Address::new(1024);
let addr3 = Address::new(1027);
memory.store(addr0, Word::new(1)).unwrap();
memory.store(addr2, Word::new(2)).unwrap();
assert_eq!(memory.load(addr0).unwrap(), Word::new(1));
assert_eq!(memory.load(addr1).unwrap(), Word::default());
assert_eq!(memory.load(addr2).unwrap(), Word::new(2));
assert_eq!(memory.load(addr3).unwrap(), Word::default());
assert_eq!(
memory.array(Area::from(addr0, addr1)).unwrap().as_slice(),
&[Word::new(1), Word::default(), Word::default(), Word::default()]
);
assert_eq!(
memory.array(Area::from(addr2, addr3)).unwrap().as_slice(),
&[Word::new(2), Word::default(), Word::default(), Word::default()]
);
memory.copy(addr0, Array::from(&[Word::new(1), Word::new(2), Word::new(3), Word::new(4)])).unwrap();
assert_eq!(
memory.array(Area::from(addr0, addr1)).unwrap().as_slice(),
&[Word::new(1), Word::new(2), Word::new(3), Word::new(4)]
);
}