Skip to content

Commit

Permalink
unittests addition
Browse files Browse the repository at this point in the history
  • Loading branch information
inesmaria08 committed Sep 24, 2024
1 parent fcb8809 commit 372e94b
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 9 deletions.
1 change: 1 addition & 0 deletions apis/interface/servo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cargo-features = ["edition2024"]
[package]
name = "libtock_servo"
version = "0.1.0"
Expand Down
19 changes: 10 additions & 9 deletions apis/interface/servo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#![no_std]

use libtock_platform::{CommandReturn, ErrorCode, Syscalls};
//use libtock_unittest::command_return::success;
use libtock_platform::{ErrorCode, Syscalls};

pub struct Servo<S: Syscalls>(S);

impl<S: Syscalls> Servo<S> {
pub fn servo_exists() -> Result<(), ErrorCode> {
let val = S::command(DRIVER_NUM, 0, 0, 0).is_success();
let val = S::command(DRIVER_NUM, SERVO_EXISTS, 0, 0).is_success();
if val == true {
Ok(())
} else {
Expand All @@ -16,15 +15,15 @@ impl<S: Syscalls> Servo<S> {
}
}
pub fn set_angle(index: u32, angle: u32) -> Result<(), ErrorCode> {
S::command(DRIVER_NUM, 1, index, angle).to_result()
S::command(DRIVER_NUM, SET_ANGLE, index, angle).to_result()
}
pub fn get_angle(index: u32) -> Result<(), ErrorCode> {
S::command(DRIVER_NUM, 2, index, 0).to_result()
pub fn get_angle(index: u32) -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_ANGLE, index, 0).to_result()
}
}

// #[cfg(test)]
// mod tests;
#[cfg(test)]
mod tests;

// -----------------------------------------------------------------------------
// Driver number and command IDs
Expand All @@ -33,4 +32,6 @@ impl<S: Syscalls> Servo<S> {
const DRIVER_NUM: u32 = 0x90009;

// Command IDs
//const SET_ANGLE: u16 = 1;
const SERVO_EXISTS: u32 = 0;
const SET_ANGLE: u32 = 1;
const GET_ANGLE: u32 = 2;
32 changes: 32 additions & 0 deletions apis/interface/servo/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use libtock_platform::ErrorCode;
use libtock_unittest::fake;

type Servo = super::Servo<fake::Syscalls>;

#[test]
fn no_driver() {
let _kernel = fake::Kernel::new();
assert_eq!(Servo::servo_exists(), Err(ErrorCode::Fail))
}
#[test]
fn servo_exists() {
let kernel = fake::Kernel::new();
let driver = fake::Servo::<2>::new();
kernel.add_driver(&driver);
assert_eq!(Servo::servo_exists(), Ok(()));
}
#[test]
fn set_angle() {
let kernel = fake::Kernel::new();
let driver = fake::Servo::<2>::new();
kernel.add_driver(&driver);
assert_eq!(Servo::set_angle(1, 90), Ok(()));
}
#[test]
fn get_angle() {
let kernel = fake::Kernel::new();
let driver = fake::Servo::<2>::new();
kernel.add_driver(&driver);
assert_eq!(Servo::set_angle(1, 45), Ok(()));
assert_eq!(Servo::get_angle(1), Ok(45));
}
2 changes: 2 additions & 0 deletions unittest/src/fake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod leds;
mod low_level_debug;
mod ninedof;
mod proximity;
mod servo;
mod sound_pressure;
mod syscall_driver;
mod syscalls;
Expand All @@ -42,6 +43,7 @@ pub use leds::Leds;
pub use low_level_debug::{LowLevelDebug, Message};
pub use ninedof::{NineDof, NineDofData};
pub use proximity::Proximity;
pub use servo::Servo;
pub use sound_pressure::SoundPressure;
pub use syscall_driver::SyscallDriver;
pub use syscalls::Syscalls;
Expand Down
64 changes: 64 additions & 0 deletions unittest/src/fake/servo/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::cell::Cell;

use crate::DriverInfo;
use libtock_platform::{CommandReturn, ErrorCode};

pub struct Servo<const NUM_SERVO: usize> {
servo: [Cell<u16>; NUM_SERVO],
}

impl<const NUM_SERVO: usize> Servo<NUM_SERVO> {
pub fn new() -> std::rc::Rc<Servo<NUM_SERVO>> {
#[allow(clippy::declare_interior_mutable_const)]
const ANGLE: Cell<u16> = Cell::new(0);
std::rc::Rc::new(Servo {
servo: [ANGLE; NUM_SERVO],
})
}
}

impl<const NUM_SERVO: usize> crate::fake::SyscallDriver for Servo<NUM_SERVO> {
fn info(&self) -> DriverInfo {
DriverInfo::new(DRIVER_NUM)
}

fn command(&self, command_num: u32, servo_index: u32, angle: u32) -> CommandReturn {
match command_num {
0 => crate::command_return::success(),
1 => {
if servo_index >= NUM_SERVO as u32 {
crate::command_return::failure(ErrorCode::NoDevice)
} else if angle <= 180 {
self.servo[servo_index as usize].set(angle as u16);
crate::command_return::success()
} else {
crate::command_return::failure(ErrorCode::Fail)
}
}
// Return the current angle.
2 => {
if servo_index >= NUM_SERVO as u32 {
crate::command_return::failure(ErrorCode::NoDevice)
} else {
let angle = self.servo[servo_index as usize].get();
crate::command_return::success_u32(angle as u32)
}
}
_ => crate::command_return::failure(ErrorCode::NoSupport),
}
}
}

#[cfg(test)]
mod tests;

// -----------------------------------------------------------------------------
// Implementation details below
// -----------------------------------------------------------------------------

const DRIVER_NUM: u32 = 0x90009;

// Command numbers
const SERVO_EXISTS: u32 = 0;
const SET_ANGLE: u32 = 1;
const GET_ANGLE: u32 = 2;
47 changes: 47 additions & 0 deletions unittest/src/fake/servo/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::fake;
use fake::servo::*;
use libtock_platform::CommandReturn;

// Tests the command implementation.
#[test]
fn command() {
use fake::SyscallDriver;
let servo = Servo::<1>::new();
let value = servo.command(SERVO_EXISTS, 0, 0);
assert_eq!(CommandReturn::is_success(&value), true);
assert_eq!(
CommandReturn::is_success(&servo.command(SET_ANGLE, 0, 90)),
true
);
assert_eq!(
CommandReturn::get_success_u32(&servo.command(GET_ANGLE, 0, 0)),
Some(90)
);
}

#[test]
fn kernel_integration() {
use libtock_platform::Syscalls;
let kernel = fake::Kernel::new();
let servo = Servo::<1>::new();
kernel.add_driver(&servo);
let value = fake::Syscalls::command(DRIVER_NUM, SERVO_EXISTS, 0, 0);
assert_eq!(CommandReturn::is_success(&value), true);
assert_eq!(
fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 2, 90).get_failure(),
Some(ErrorCode::NoDevice)
);
assert_eq!(
fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 181).get_failure(),
Some(ErrorCode::Fail)
);
assert!(fake::Syscalls::command(DRIVER_NUM, SET_ANGLE, 0, 90).is_success());
assert_eq!(
fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 0, 0).get_success_u32(),
Some(90)
);
assert_eq!(
fake::Syscalls::command(DRIVER_NUM, GET_ANGLE, 2, 0).get_failure(),
Some(ErrorCode::NoDevice)
);
}

0 comments on commit 372e94b

Please sign in to comment.