From aa6e8fabbbc0404a5c2b0a2e5173bdb86e99a1a5 Mon Sep 17 00:00:00 2001 From: Brian Misiak Date: Mon, 26 Feb 2024 14:36:41 -0800 Subject: [PATCH] tests --- src/amx_arguments.rs | 5 +++++ src/scheduling.rs | 42 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/amx_arguments.rs b/src/amx_arguments.rs index e8633db..ff22be8 100644 --- a/src/amx_arguments.rs +++ b/src/amx_arguments.rs @@ -30,6 +30,11 @@ pub(crate) struct VariadicAmxArguments { } impl VariadicAmxArguments { + #[cfg(test)] + pub fn empty() -> Self { + Self { inner: vec![] } + } + fn get_type_letters( args: &mut samp::args::Args, skipped_args: usize, diff --git a/src/scheduling.rs b/src/scheduling.rs index 8017cd5..99b7305 100644 --- a/src/scheduling.rs +++ b/src/scheduling.rs @@ -115,10 +115,7 @@ fn deschedule_next_due(next_due: &NextDue) -> Result<(), TriggeringError> { Ok(()) } -fn change_next_trigger( - key: usize, - next_trigger: Instant, -) -> Result<(), TriggeringError> { +fn change_next_trigger(key: usize, next_trigger: Instant) -> Result<(), TriggeringError> { QUEUE.with(|q| { q.try_borrow_mut() .context(ReschedulingBorrowSnafu)? @@ -165,7 +162,7 @@ impl NextDue { change_next_trigger(self.key, next_trigger)?; TIMERS.with_borrow_mut(|t| { - let timer = t.get_mut(self.key).context(ExpectedInSlabSnafu)?; + let timer: &mut Timer = t.get_mut(self.key).context(ExpectedInSlabSnafu)?; prep(timer).context(StackPushSnafu) }) } else { @@ -188,3 +185,38 @@ pub(crate) fn next_timer_due_for_triggering(now: Instant) -> Option { _ => None, }) } + +#[cfg(test)] +mod test { + use std::ptr::null_mut; + + use samp::raw::types::AMX; + + use crate::Timer; + use crate::{amx_arguments::VariadicAmxArguments, scheduling::next_timer_due_for_triggering}; + + use super::{insert_and_schedule_timer, TimerScheduling}; + + fn mock_no_arg_timer() -> Timer { + let amx_pointer: *mut AMX = null_mut(); + Timer { + passed_arguments: VariadicAmxArguments::empty(), + amx_callback_index: samp::consts::AmxExecIdx::Continue, + amx_identifier: amx_pointer.into(), + } + } + + #[test] + fn hello() { + insert_and_schedule_timer( + mock_no_arg_timer(), + TimerScheduling { + next_trigger: std::time::Instant::now(), + repeat: super::Repeat::DontRepeat, + }, + ); + let next_due = next_timer_due_for_triggering(std::time::Instant::now()); + + assert!(next_due.is_some()); + } +}