Skip to content

Commit

Permalink
Small modifications on channel tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyma98 committed Aug 13, 2024
1 parent ea91d89 commit 468f187
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
5 changes: 5 additions & 0 deletions examples/tests/sync/channel/concurrency_and_stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! (This test is similar to the test "multiple_producers", but includes more tasks and elements produced on the channel)
//! It creates four tasks, each producing a sequence of numbers, and verifies that all numbers are correctly
//! produced and consumed in the expected order.
#![no_main]
#![no_std]

Expand Down Expand Up @@ -74,6 +75,10 @@ fn main(_: cortex_m::Peripherals) {
fn fill_channel(producer: &mut Producer<usize, 16>, task_num: usize) {
// Each task produces a sequence of 3 (NUM_ITEMS) numbers based on its task number
for j in 0..NUM_ITEMS {
// Conditionally yield to add more stress.
if j == task_num {
task::yield_current();
}
producer.produce(task_num * NUM_ITEMS + j);
}

Expand Down
5 changes: 3 additions & 2 deletions examples/tests/sync/channel/multiple_consumers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests the correct behavior of a multi-consumer, single-producer channel
//! It fills a channel with four elements, spawns two consumer tasks with different priorities to consume the elements,
//! and then checks that the channel is empty after all elements have been consumed.
#![no_main]
#![no_std]

Expand Down Expand Up @@ -48,6 +49,6 @@ fn main(_: cortex_m::Peripherals) {
}

fn consume_function(consumer: &mut Consumer<usize, 4>) {
consumer.consume();
consumer.consume();
hprintln!("{}", consumer.consume());
hprintln!("{}", consumer.consume());
}
4 changes: 4 additions & 0 deletions examples/tests/sync/channel/multiple_consumers.txt
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
1
2
3
4
Test Passed
6 changes: 3 additions & 3 deletions examples/tests/sync/channel/multiple_producers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Tests behavior of a multi-producer, single-consumer channel
//! It spawns two producer tasks with different priorities that add elements to a shared channel,
//! and then consumes the elements in a specific order to verify the correct sequence
#![no_main]
#![no_std]

extern crate alloc;
use alloc::vec;
use hopter::{boot::main, debug::semihosting, hprintln, sync, sync::Producer, task};

#[main]
Expand Down Expand Up @@ -33,15 +33,15 @@ fn main(_: cortex_m::Peripherals) {
task::change_current_priority(10).unwrap();

// Consume values from the channel and store them in a vector
let values = vec![
let values = [
consumer.consume(),
consumer.consume(),
consumer.consume(),
consumer.consume(),
];

// Verify the consumed values against the expected order
if values != vec![1, 2, 3, 4] {
if values != [1, 2, 3, 4] {
hprintln!("Test Failed");
semihosting::terminate(false);
}
Expand Down
7 changes: 4 additions & 3 deletions examples/tests/sync/channel/produce_consume_single_task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Test basic functionaliy of produce() and consume()
#![no_main]
#![no_std]

Expand All @@ -7,16 +8,16 @@ use hopter::{boot::main, debug::semihosting, hprintln, sync};

#[main]
fn main(_: cortex_m::Peripherals) {
// create a channel with a buffer capacity of 4
// Create a channel with a buffer capacity of 4
let (producer, consumer) = sync::create_channel::<usize, 4>();

// fill a channel with values 23-26
// Fill a channel with values 23-26
producer.produce(23);
producer.produce(24);
producer.produce(25);
producer.produce(26);

// consume each element from channel, verifying the expected value
// Consume each element from channel, verifying the expected value
for i in 0..4 {
let value = consumer.consume();
if value != 23 + i {
Expand Down
14 changes: 7 additions & 7 deletions examples/tests/sync/channel/produce_with_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ use hopter::{boot::main, debug::semihosting, hprintln, sync};

#[main]
fn main(_: cortex_m::Peripherals) {
// create a channel with a buffer capacity of 4
// Create a channel with a buffer capacity of 4
let (producer, consumer) = sync::create_channel::<usize, 4>();

// fill channel with values 0-3 inclusive
for i in 0..4 {
// Fill channel with values 0-3 inclusive
for i in 0..=3 {
producer.produce(i);
}

// attempt to push values 4-7
// produce_with_overflow_allow_isr should return the value we attempt to push each time
for i in 4..8 {
// Attempt to push values 4-7. `produce_with_overflow_allow_isr` should
// return the value we attempt to push each time.
for i in 4..=7 {
let result = producer.produce_with_overflow_allow_isr(i);
if result != Some(i) {
hprintln!("Test Failed");
semihosting::terminate(false);
}
}

// consume each value in channel, confirming the values are 0-3
// Consume each value in channel, confirming the values are 0-3
for i in 0..4 {
let value = consumer.consume();
if value != i {
Expand Down
16 changes: 7 additions & 9 deletions examples/tests/sync/channel/try_consume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ use hopter::{boot::main, debug::semihosting, hprintln, sync};

#[main]
fn main(_: cortex_m::Peripherals) {
// create a channel with a buffer capacity of 4 elements
// Create a channel with a buffer capacity of 4 elements
let (producer, consumer) = sync::create_channel::<usize, 4>();

// attempt to consume from an empty channel
// should return None
// Attempt to consume from an empty channel. Should return `None`.
let result = consumer.try_consume_allow_isr();

// check against expected behavior
// Check against expected behavior
if result != None {
hprintln!("consumed from an empty channel");
semihosting::terminate(false);
}

// fill channel to capacity with 4 elements
// 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, throw an error
// 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 {
Expand All @@ -35,8 +34,7 @@ fn main(_: cortex_m::Peripherals) {
}
}

// attempt to consume from the empty channel
//should return None
// Attempt to consume from the empty channel. Should return `None`.
let final_result = consumer.try_consume_allow_isr();
match final_result {
None => {
Expand Down

0 comments on commit 468f187

Please sign in to comment.