Skip to content

Commit

Permalink
Merge pull request #1537 from hermit-os/tests-clippy
Browse files Browse the repository at this point in the history
fix(tests): clippy
  • Loading branch information
mkroening authored Jan 2, 2025
2 parents 0864777 + 9b90a9d commit 8bd7052
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 306 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ memory_addresses = { version = "0.2.2", default-features = false, features = [
[dev-dependencies]
float-cmp = "0.10"
num-traits = { version = "0.2", default-features = false }
x86 = { version = "0.52", default-features = false }

[build-dependencies]
anyhow = "1"
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ use core::hint::spin_loop;
use core::sync::atomic::{AtomicU32, Ordering};

use arch::core_local::*;
// Used for integration test status.
#[doc(hidden)]
pub use env::is_uhyve as _is_uhyve;

pub(crate) use crate::arch::*;
pub use crate::config::DEFAULT_STACK_SIZE;
Expand Down
14 changes: 14 additions & 0 deletions src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,17 @@ pub extern "C" fn sys_eventfd(initval: u64, flags: i16) -> i32 {
pub extern "C" fn sys_image_start_addr() -> usize {
crate::mm::kernel_start_address().as_usize()
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(target_os = "none")]
#[test_case]
fn test_get_application_parameters() {
crate::env::init();
let (argc, argv, _envp) = get_application_parameters();
assert_ne!(argc, 0);
assert_ne!(argv, ptr::null());
}
}
28 changes: 7 additions & 21 deletions tests/basic_math.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
#![feature(test)]
#![feature(bench_black_box)]
#![no_std]
#![no_main]
#![test_runner(common::test_case_runner)]
#![feature(custom_test_frameworks)]
#![reexport_test_harness_main = "test_main"]

/// Regarding `#[test]` and `#[test_case]` this comment explains the current implementation
/// https://github.com/rust-lang/rust/issues/50297#issuecomment-524180479
/// This is of course subject to change, since the whole feature is not stable
///
//extern crate hermit;
//extern crate x86_64;
#[macro_use]
extern crate float_cmp;

//use common::*;
use core::hint::black_box;

use common::exit;

// Either use black_box from core::hint or the value_fence definition
// core hint is a nop, but possibly only prevents dead code elimination
// value_fence has higher overhead but should be a bit safer regarding preventing optimizations
// pub fn black_box<T>(x: T) -> T {
// common::value_fence::<T>(x)
// }
mod common;

#[test_case]
Expand Down Expand Up @@ -123,19 +109,19 @@ fn test_f64_arithmetic() {
let z = x * y;
assert!(approx_eq!(f64, z, 5810.8196f64, ulps = 1));
let z = z * y;
assert!(approx_eq!(f64, z, 517877.6752108f64, ulps = 1));
assert!(approx_eq!(f64, z, 517_877.675_210_8f64, ulps = 1));
let z = z * y;
assert!(approx_eq!(f64, z, 46_154_812.047_812_13_f64, ulps = 2));
assert!(approx_eq!(f64, z, 46_154_812.047_812_13f64, ulps = 2));
let z = z * y;
assert!(approx_eq!(f64, z, 4_113_455_314.137_160_3_f64, ulps = 3));
assert!(approx_eq!(f64, z, 4_113_455_314.137_160_3f64, ulps = 3));

let z = black_box(z) / y;
assert!(approx_eq!(f64, z, 46_154_812.047_812_13_f64, ulps = 2));
assert!(!approx_eq!(f64, z, 46_154_812.047_812_13_f64, ulps = 1)); // If we haven't lost any precision, the something is fishy
assert!(approx_eq!(f64, z, 46_154_812.047_812_13f64, ulps = 2));
assert!(!approx_eq!(f64, z, 46_154_812.047_812_13f64, ulps = 1)); // If we haven't lost any precision, the something is fishy

let z = black_box(z) / y;
assert!(approx_eq!(f64, z, 517877.6752108f64, ulps = 2));
assert!(!approx_eq!(f64, z, 517877.6752108f64, ulps = 1));
assert!(approx_eq!(f64, z, 517_877.675_210_8f64, ulps = 2));
assert!(!approx_eq!(f64, z, 517_877.675_210_8f64, ulps = 1));

// Division
let x = black_box::<f64>(4.0);
Expand Down
39 changes: 20 additions & 19 deletions tests/basic_mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

extern crate alloc;

mod common;

use alloc::vec::Vec;
use core::mem::size_of;

//no-std otherwise std::mem::size_of
mod common;

const PATTERN: u8 = 0xab;

/// Mainly test if memcpy works as expected. Also somewhat tests memcmp
Expand Down Expand Up @@ -48,7 +47,7 @@ where
if T::from(vec_size).is_none() {
tmax.to_u64().unwrap() // If vec_size can't be represented in T, then tmax must fit in u64
} else {
(vec_size - 1) as u64
u64::from(vec_size - 1)
}
};
// ToDo - This loop should be rewritten in a nicer way
Expand All @@ -66,22 +65,22 @@ where

let mut b: Vec<T> =
Vec::with_capacity((vec_size + pre_dest_vec_size + post_dest_vec_size) as usize);
// Manually set length, since we will be manually filling the vector
unsafe {
b.set_len((vec_size + pre_dest_vec_size + post_dest_vec_size) as usize);
}
// Fill pre and post section with `pattern`
for i in 0..pre_dest_vec_size {
b[i as usize] = pattern;
b.spare_capacity_mut()[i as usize].write(pattern);
}
for i in 0..post_dest_vec_size {
b[(pre_dest_vec_size + vec_size + i) as usize] = pattern;
b.spare_capacity_mut()[(pre_dest_vec_size + vec_size + i) as usize].write(pattern);
}
// Manually set length, since we manually filled the vector
unsafe {
b.set_len((vec_size + pre_dest_vec_size + post_dest_vec_size) as usize);
}
// Copy the actual vector
unsafe {
memcpy(
b.as_mut_ptr().offset(pre_dest_vec_size as isize) as *mut u8,
a.as_ptr() as *const u8,
b.as_mut_ptr().add(pre_dest_vec_size as usize).cast::<u8>(),
a.as_ptr().cast::<u8>(),
((size_of::<T>() as u32) * vec_size) as usize,
);
}
Expand Down Expand Up @@ -111,19 +110,21 @@ where
unsafe {
assert_eq!(
memcmp(
b.as_ptr().offset(pre_dest_vec_size as isize) as *const u8,
a.as_ptr() as *const u8,
(size_of::<T>() as usize) * vec_size as usize,
b.as_ptr().add(pre_dest_vec_size as usize).cast::<u8>(),
a.as_ptr().cast::<u8>(),
size_of::<T>() * vec_size as usize,
),
0
);
// pattern is larger, a[0] is 0
assert!(memcmp(b.as_ptr() as *const u8, a.as_ptr() as *const u8, 1) > 0);
assert!(memcmp(a.as_ptr() as *const u8, b.as_ptr() as *const u8, 1) < 0);
assert!(memcmp(b.as_ptr().cast::<u8>(), a.as_ptr().cast::<u8>(), 1) > 0);
assert!(memcmp(a.as_ptr().cast::<u8>(), b.as_ptr().cast::<u8>(), 1) < 0);
assert!(
memcmp(
b.as_ptr().offset((vec_size + pre_dest_vec_size) as isize) as *const u8,
a.as_ptr() as *const u8,
b.as_ptr()
.add((vec_size + pre_dest_vec_size) as usize)
.cast::<u8>(),
a.as_ptr().cast::<u8>(),
1,
) > 0
);
Expand Down
14 changes: 7 additions & 7 deletions tests/basic_print.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![feature(test)]
#![no_std]
#![no_main]
//#![test_runner(hermit::test_runner)]
//#![feature(custom_test_frameworks)]
//#![reexport_test_harness_main = "test_main"]

//use core::panic::PanicInfo;
extern crate alloc;

#[macro_use]
extern crate hermit;

use common::*;
mod common;

use alloc::string::String;
use alloc::vec::Vec;

/// Print all Strings the application got passed as arguments
#[no_mangle]
pub fn main(args: Vec<String>) -> Result<(), ()> {
pub fn main(args: Vec<String>) -> Result<(), String> {
for s in args {
println!("{}", &s);
}
Expand Down
Loading

0 comments on commit 8bd7052

Please sign in to comment.