//! Test consume() and try_consume_allow_isr() functionality for empty and full channels #![no_main] #![no_std] extern crate alloc; use hopter::{ debug::semihosting::{self, dbg_println}, sync, task::main, }; #[main] fn main(_: cortex_m::Peripherals) { // Create a channel with a buffer capacity of 4 elements let (producer, consumer) = sync::create_channel::(); // Attempt to consume from an empty channel. Should return `None`. let result = consumer.try_consume_allow_isr(); // Check against expected behavior if result != None { dbg_println!("consumed from an empty channel"); #[cfg(feature = "qemu")] semihosting::terminate(false); #[cfg(not(feature = "qemu"))] { dbg_println!("test complete!"); loop {} } } // Fill channel to capacity with 4 elements for i in 0..4 { producer.produce(i); } // Empty channel by consuming iteratively, checking the element was consumed sucessfully each time // otherwise, report error. for _i in 0..4 { let result = consumer.try_consume_allow_isr(); if result == None { dbg_println!("failed to consume from a non-empty channel"); #[cfg(feature = "qemu")] semihosting::terminate(false); #[cfg(not(feature = "qemu"))] { dbg_println!("test complete!"); loop {} } } } // Attempt to consume from the empty channel. Should return `None`. let final_result = consumer.try_consume_allow_isr(); match final_result { None => { dbg_println!("Test Passed"); #[cfg(feature = "qemu")] semihosting::terminate(true); #[cfg(not(feature = "qemu"))] { dbg_println!("test complete!"); loop {} } } Some(_t) => { dbg_println!("consumed from an empty channel"); #[cfg(feature = "qemu")] semihosting::terminate(false); #[cfg(not(feature = "qemu"))] { dbg_println!("test complete!"); loop {} } } } }