use crate::alu::*;
use crate::message::*;

use std::sync::mpsc;
use std::time;
use rust_decimal::Decimal;

#[test]
fn the_clear_entry_function_should_reset_the_value_stored_in_the_m_register() {

    //  Arrange

    let memory = MemoryBuilder::new()
        .with_m(Decimal::new(123, 0))
        .with_precision(2)
        .build();

    let (sender, _system) = mpsc::channel();
    let mut alu = DefaultAlu::new_with_memory(sender, memory);

    // Act

    let result = alu.clear_entry();
    
    // Assert

    assert_eq!(Ok(()), result);
    assert_eq!(Decimal::new(0, 0), alu.memory.m.read());
}

#[test]
fn the_clear_entry_message_should_clear_the_input_buffer() {

    //  Arrange

    let timeout = time::Duration::from_millis(1);
    let mut messages = Vec::new();

    let memory = MemoryBuilder::new()
        .with_m(Decimal::new(123, 0))
        .with_precision(2)
        .build();

    let (sender, system) = mpsc::channel();
    let mut alu = DefaultAlu::new_with_memory(sender, memory);

    // Act

    let result = alu.clear_entry();
    messages.push(system.recv_timeout(timeout));
    messages.push(system.recv_timeout(timeout));
    
    // Assert

    assert_eq!(Ok(()), result);
    assert!(messages.contains(&Ok(Message::Debug(DebugEvent::RegisterUpdated(p101_is::Register::M, 0.into())))));
}

