From e326bfb71a3ebf53212a5e7db398c789bb73a467 Mon Sep 17 00:00:00 2001 From: cl8192 Date: Sun, 15 May 2022 09:48:24 +0800 Subject: [PATCH] update usertests --- user/src/bin/usertests.rs | 88 +++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 23 deletions(-) diff --git a/user/src/bin/usertests.rs b/user/src/bin/usertests.rs index 7d104d27..095bed17 100644 --- a/user/src/bin/usertests.rs +++ b/user/src/bin/usertests.rs @@ -3,6 +3,10 @@ #[macro_use] extern crate user_lib; +extern crate alloc; + +use alloc::{vec::Vec, string::ToString}; +use alloc::string::String; static SUCC_TESTS: &[&str] = &[ "matrix\0", @@ -22,7 +26,6 @@ static SUCC_TESTS: &[&str] = &[ "race_adder_atomic\0", "race_adder_mutex_blocking\0", "race_adder_mutex_spin\0", - "race_adder_arg\0", "sleep_simple\0", "sleep\0", "sleep_simple\0", @@ -33,7 +36,7 @@ static SUCC_TESTS: &[&str] = &[ "yield\0", "run_pipe_test\0", ]; - + static FAIL_TESTS: &[&str] = &[ "stack_overflow\0", "race_adder_loop\0", @@ -43,35 +46,74 @@ static FAIL_TESTS: &[&str] = &[ "until_timeout\0", "stack_overflow\0", "race_adder\0", - "huge_write_mt\0", + "race_adder_arg\0", ]; use user_lib::{exec, fork, waitpid}; -fn run_tests(tests: &[&str], judge: F) { - for test in tests { - println!("Usertests: Running {}", test); - let pid = fork(); - if pid == 0 { - exec(*test, &[core::ptr::null::()]); - panic!("unreachable!"); - } else { - let mut exit_code: i32 = Default::default(); - let wait_pid = waitpid(pid as usize, &mut exit_code); - assert_eq!(pid, wait_pid); - judge(exit_code); - println!( - "\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", - test, pid, exit_code - ); - } +fn run_one_test(test: &str) -> i32 { + println!("Usertests: Running {}", test); + let pid = fork(); + if pid == 0 { + exec(test, &[core::ptr::null::()]); + return -1 + } else { + let mut exit_code: i32 = Default::default(); + let wait_pid = waitpid(pid as usize, &mut exit_code); + assert_eq!(pid, wait_pid); + println!( + "\x1b[32mUsertests: Test {} in Process {} exited with code {}\x1b[0m", + test, pid, exit_code + ); + + return exit_code } } + + #[no_mangle] pub fn main() -> i32 { - run_tests(SUCC_TESTS, |code| assert!(code == 0)); - run_tests(FAIL_TESTS, |code| assert!(code != 0)); - println!("Usertests passed!"); + // tests should succ + let mut succ_tests: Vec = Vec::new(); + // tests should fail + let mut fail_tests: Vec = Vec::new(); + + // other + let mut fail_list: Vec = Vec::new(); + + for test in SUCC_TESTS { + let result = run_one_test(test); + if result == 0{ + succ_tests.push(test.to_string()); + }else{ + fail_list.push(test.to_string()) + } + } + for test in FAIL_TESTS { + let result = run_one_test(test); + if result != 0{ + fail_tests.push(test.to_string()); + }else{ + fail_list.push(test.to_string()) + } + } + + if succ_tests.len() == SUCC_TESTS.len() && fail_tests.len() == FAIL_TESTS.len() { + println!("\x1b[32mALL Usertests passed!\x1b[0m"); + }else{ + println!("\x1b[31mUsertests failed!\x1b[0m"); + println!("\x1b[32mPassed List\x1b[0m"); + for i in succ_tests.iter(){ + println!("{}",i); + } + for i in fail_tests.iter(){ + println!("{}",i); + } + println!("\x1b[31mFail List\x1b[0m"); + for i in fail_list.iter(){ + println!("{}",i); + } + } 0 }