/***************************************
Auteur : Pierre Aubert
Mail : pierre.aubert@lapp.in2p3.fr
Licence : CeCILL-C
****************************************/
//{We have to include the following as modules to make sure all the other import and tests will work and the \file{generic_vec_mock} will be able to compile using \file{abstract_mock_backend}. Basically, the main problem here is the import system of \lib{Rust} is totally broken. You need to define everything here in \lif{lib.rs} but you still have to \rust{use crate::abstract_mock_backend::AbstractMockBackend;} in \file{generic_vec_mock.rs} but without the \rust{mod generic_vec_mock} otherwise \prog{rustc} will tell you there is no crate named \lib{generic_vec_mock} :
mod abstract_mock_backend;
mod generic_vec_mock;
//{Now we can create a \lib{prelude} to expose what we want :
pub mod prelude{
pub use crate::abstract_mock_backend::AbstractMockBackend;
pub use crate::generic_vec_mock::GenericVecMock;
}
//{An other thing which is, off course, completelly non-intuitive is the position of the \rust{mod} by respect to the \rust{use} makes no difference at all. So, if there is a hidden \rust{mod} somewhere far in this file (and still in this module) all the \rust{use} relative to files will work. So do not fight with it.
//{Let's define the unit test :
#[cfg(test)]
mod tests {
//{Then, we can test is our \lib{prelude} is working :
use crate::prelude::{AbstractMockBackend, GenericVecMock};
///Create a mock file
/// # Parameters
/// - `filename` : filename to be created
fn create_mock_value(filename: &String){
let mut mock: GenericVecMock<u64> = Default::default();
mock.set_is_record(true);
mock.set_filename(&filename);
mock.append(&0);
mock.append(&1);
mock.append(&2);
mock.append(&3);
mock.close();
}
///Get the current value of the mock
/// # Parameters
/// - `mock` : mock to be used
/// # Returns
/// current value of the mock
fn get_value_from_mock(mock: &mut GenericVecMock<u64>) -> u64{
let mut value: u64 = 0;
mock.get_current_value(&mut value);
return value;
}
#[test]
fn test_generic_vec_mock() {
let filename = String::from("some_mock_file.pgenericmock");
create_mock_value(&filename);
let mut mock: GenericVecMock<u64> = Default::default();
mock.set_filename(&filename);
assert_eq!(get_value_from_mock(&mut mock), 0);
assert_eq!(get_value_from_mock(&mut mock), 1);
assert_eq!(get_value_from_mock(&mut mock), 2);
assert_eq!(get_value_from_mock(&mut mock), 3);
assert_eq!(get_value_from_mock(&mut mock), 0);
assert!(mock.check_current_value(&1));
assert!(mock.check_current_value(&2));
assert!(!mock.check_current_value(&18));
}
}