From fb3ae0b5de434b41bccb8638b4989c307e111a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Tue, 9 Apr 2024 14:21:29 -0500 Subject: [PATCH 01/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 85 +++++++++++++++++++++++++++++ src/controllers/mod.rs | 1 + 2 files changed, 86 insertions(+) create mode 100644 src/controllers/cmd_stress_async.rs diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs new file mode 100644 index 0000000..ad9eecb --- /dev/null +++ b/src/controllers/cmd_stress_async.rs @@ -0,0 +1,85 @@ +use std::{collections::VecDeque, path::PathBuf}; + +use exitfailure::ExitFailure; + +use crate::{ + cli::model::{stress_command::StressCommand, traits::AdapterCommand}, + constants::CACHE_FOLDER, + file_handler::file::{create_folder_or_error, load_testcases_from_prefix}, + language::{ + get_language::{get_executor_generator, get_executor_target}, + language_handler::LanguageHandler, + }, +}; + +pub struct StressController { + command: StressCommand, + target_file_lang: Option, + generator_file_lang: Option, + cases: VecDeque, + test_number: u32, + cases_len: usize, + current_case: Option, +} + +impl StressController { + pub fn new(command: StressCommand) -> StressController { + StressController { + command, + target_file_lang: None, + generator_file_lang: None, + cases: VecDeque::new(), + test_number: 0, + cases_len: 0, + current_case: None, + } + } + + pub async fn run(&mut self) -> Result<(), ExitFailure> { + self.create_initial_files()?; + self.initialize_variables()?; + self.load_testcases()?; + + while self.are_tests_pending() { + self.increment_test_count(); + self.update_next_case(); + } + + Ok(()) + } + + fn create_initial_files(&self) -> Result<(), ExitFailure> { + // Check if the CACHE_FOLDER folder is already created + create_folder_or_error(CACHE_FOLDER)?; + Ok(()) + } + + fn initialize_variables(&mut self) -> Result<(), ExitFailure> { + // Get the language depending on the extension of the target_file + let target_file_lang: LanguageHandler = *get_executor_target(&self.command)?; + self.target_file_lang = Some(target_file_lang); + + let generator_file_lang: LanguageHandler = *get_executor_generator(&self.command)?; + self.generator_file_lang = Some(generator_file_lang); + Ok(()) + } + + fn load_testcases(&mut self) -> Result<(), ExitFailure> { + let prefix = &self.command.get_prefix()[..]; + load_testcases_from_prefix(&mut self.cases, prefix)?; + self.cases_len = self.cases.len(); + Ok(()) + } + + fn are_tests_pending(&self) -> bool { + (self.test_number as usize) < self.cases_len + } + + fn increment_test_count(&mut self) { + self.test_number += 1; + } + + fn update_next_case(&mut self) { + self.current_case = self.cases.pop_front(); + } +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index b90c84f..96983d4 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -11,3 +11,4 @@ pub mod cmd_stress; pub mod cmd_output_async; pub mod cmd_setup_async; +pub mod cmd_stress_async; From 6263847ce07890a20cc2a00228fced122b200711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Tue, 9 Apr 2024 14:29:24 -0500 Subject: [PATCH 02/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 46 +++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index ad9eecb..c36c25f 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -4,12 +4,14 @@ use exitfailure::ExitFailure; use crate::{ cli::model::{stress_command::StressCommand, traits::AdapterCommand}, - constants::CACHE_FOLDER, - file_handler::file::{create_folder_or_error, load_testcases_from_prefix}, + constants::{CACHE_FOLDER, QTEST_INPUT_FILE}, + file_handler::file::{copy_file, create_folder_or_error, load_testcases_from_prefix}, + generator::generator::execute_generator, language::{ get_language::{get_executor_generator, get_executor_target}, language_handler::LanguageHandler, }, + runner::types::Language, }; pub struct StressController { @@ -43,6 +45,28 @@ impl StressController { while self.are_tests_pending() { self.increment_test_count(); self.update_next_case(); + self.load_case_file()?; + + let mut can_continue = false; + + // run generator or load testcases using prefix + execute_generator( + &self.get_generator_lang_handler(), + &self.command, + &mut self.cases, + self.test_number, + &mut can_continue, + )?; + + if !can_continue { + break; + } + + let _response_target = self.get_target_lang_handler().execute( + self.command.get_timeout(), + self.command.get_memory_limit(), + self.test_number, + ); } Ok(()) @@ -82,4 +106,22 @@ impl StressController { fn update_next_case(&mut self) { self.current_case = self.cases.pop_front(); } + + fn get_current_case(&self) -> PathBuf { + self.current_case.clone().unwrap() + } + + fn load_case_file(&self) -> Result<(), ExitFailure> { + // Load test case in stdin + copy_file(self.get_current_case().to_str().unwrap(), QTEST_INPUT_FILE)?; + Ok(()) + } + + fn get_target_lang_handler(&self) -> LanguageHandler { + self.target_file_lang.clone().unwrap() + } + + fn get_generator_lang_handler(&self) -> LanguageHandler { + self.generator_file_lang.clone().unwrap() + } } From d2399f0a9fe37f08bf022f5aa68101d4fee13cb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 09:39:27 -0500 Subject: [PATCH 03/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 196 +++++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 5 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index c36c25f..f626e09 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -1,19 +1,77 @@ -use std::{collections::VecDeque, path::PathBuf}; +use std::{collections::VecDeque, path::PathBuf, time::Duration}; use exitfailure::ExitFailure; use crate::{ cli::model::{stress_command::StressCommand, traits::AdapterCommand}, - constants::{CACHE_FOLDER, QTEST_INPUT_FILE}, - file_handler::file::{copy_file, create_folder_or_error, load_testcases_from_prefix}, + constants::{ + CACHE_FOLDER, GEN_BINARY_FILE, PREFIX_AC_FILES, PREFIX_MLE_FILES, PREFIX_RTE_FILES, + PREFIX_TLE_FILES, QTEST_ERROR_FILE, QTEST_INPUT_FILE, QTEST_OUTPUT_FILE, + TARGET_BINARY_FILE, TEST_CASES_FOLDER, + }, + error::handle_error::throw_compiler_error_msg, + file_handler::{ + async_file::remove_files_async, + file::{ + copy_file, create_folder_or_error, format_filename_test_case, + load_testcases_from_prefix, save_test_case, + }, + }, generator::generator::execute_generator, language::{ get_language::{get_executor_generator, get_executor_target}, language_handler::LanguageHandler, }, - runner::types::Language, + runner::types::{ + is_accepted, is_compiled_error, is_memory_limit_exceeded, is_runtime_error, + is_time_limit_exceeded, Language, StatusResponse, + }, + views::style::{ + show_accepted, show_memory_limit_exceeded_error, show_runtime_error, + show_time_limit_exceeded, + }, }; +pub struct StateCounter { + tle: u32, + rte: u32, + ac: u32, + mle: u32, +} + +impl StateCounter { + pub fn new() -> Self { + Self { + tle: 0, + rte: 0, + ac: 0, + mle: 0, + } + } + + pub fn increase_tle(&mut self) { + self.tle += 1; + } + + pub fn increase_rte(&mut self) { + self.rte += 1; + } + + pub fn increase_ac(&mut self) { + self.ac += 1; + } + + pub fn increase_mle(&mut self) { + self.mle += 1; + } +} + +impl Default for StateCounter { + fn default() -> Self { + Self::new() + } +} + pub struct StressController { command: StressCommand, target_file_lang: Option, @@ -22,6 +80,7 @@ pub struct StressController { test_number: u32, cases_len: usize, current_case: Option, + state_counter: StateCounter, } impl StressController { @@ -34,6 +93,7 @@ impl StressController { test_number: 0, cases_len: 0, current_case: None, + state_counter: StateCounter::default(), } } @@ -62,11 +122,23 @@ impl StressController { break; } - let _response_target = self.get_target_lang_handler().execute( + let response_target = self.get_target_lang_handler().execute( self.command.get_timeout(), self.command.get_memory_limit(), self.test_number, ); + + if is_runtime_error(&response_target.status) { + self.runtime_error_handler(&response_target).await?; + } else if is_compiled_error(&response_target.status) { + return throw_compiler_error_msg("target", ""); + } else if is_memory_limit_exceeded(&response_target.status) { + self.memory_limit_exceeded_handler(&response_target).await?; + } else if self.is_target_time_limit_exceeded(&response_target) { + self.time_limit_exceeded_handler().await?; + } else if is_accepted(&response_target.status) { + self.accepted_handler(&response_target)?; + } } Ok(()) @@ -124,4 +196,118 @@ impl StressController { fn get_generator_lang_handler(&self) -> LanguageHandler { self.generator_file_lang.clone().unwrap() } + + async fn runtime_error_handler( + &mut self, + response_target: &StatusResponse, + ) -> Result<(), ExitFailure> { + self.state_counter.increase_rte(); + let mills_target = response_target.time.as_millis(); + show_runtime_error(self.test_number, mills_target as u32); + + // Save the input of the test case that gave status tle + if self.command.get_save_bad() || self.command.get_save_all() { + // Example: test_cases/testcase_rte_01.txt + let file_name: &str = &format_filename_test_case( + TEST_CASES_FOLDER, + PREFIX_RTE_FILES, + self.state_counter.rte, + )[..]; + // save testcase + save_test_case(file_name, QTEST_INPUT_FILE); + } + + // check if the tle_breck flag is high + if self.command.get_break_bad() { + // remove input, output and error files + self.delete_temporary_files_cmd_stress().await.ok(); + return Err(failure::err_msg("").into()); // TODO: Errors Refactor + } + Ok(()) + } + + async fn memory_limit_exceeded_handler( + &mut self, + response_target: &StatusResponse, + ) -> Result<(), ExitFailure> { + self.state_counter.increase_mle(); + let mills_target = response_target.time.as_millis(); + show_memory_limit_exceeded_error(self.test_number, mills_target as u32); + + if self.command.get_save_bad() || self.command.get_save_all() { + // Example: test_cases/testcase_mle_01.txt + let file_name: &str = &format_filename_test_case( + TEST_CASES_FOLDER, + PREFIX_MLE_FILES, + self.state_counter.mle, + )[..]; + // save testcase + save_test_case(file_name, QTEST_INPUT_FILE); + } + + // check if the tle_breck flag is high + if self.command.get_break_bad() { + // remove input, output and error files + self.delete_temporary_files_cmd_stress().await.ok(); + return Err(failure::err_msg("").into()); // TODO: Errors Refactor + } + Ok(()) + } + + fn is_target_time_limit_exceeded(&self, response_target: &StatusResponse) -> bool { + response_target.time >= Duration::from_millis(self.command.get_timeout() as u64) + || is_time_limit_exceeded(&response_target.status) + } + + async fn time_limit_exceeded_handler(&mut self) -> Result<(), ExitFailure> { + self.state_counter.increase_tle(); + show_time_limit_exceeded(self.test_number, self.command.get_timeout()); + + // Save the input of the test case that gave status tle + if self.command.get_save_bad() || self.command.get_save_all() { + // Example: test_cases/testcase_tle_01.txt + let file_name: &str = &format_filename_test_case( + TEST_CASES_FOLDER, + PREFIX_TLE_FILES, + self.state_counter.tle, + )[..]; + // save testcase + save_test_case(file_name, QTEST_INPUT_FILE); + } + + // check if the tle_breck flag is high + if self.command.get_break_bad() { + // remove input, output and error files + self.delete_temporary_files_cmd_stress().await.ok(); + return Err(failure::err_msg("").into()); // TODO: Errors Refactor + } + Ok(()) + } + + fn accepted_handler(&mut self, response_target: &StatusResponse) -> Result<(), ExitFailure> { + self.state_counter.increase_ac(); + if self.command.get_save_all() { + // Example: test_cases/testcase_ac_01.txt + let file_name: &str = &format_filename_test_case( + TEST_CASES_FOLDER, + PREFIX_AC_FILES, + self.state_counter.ac, + )[..]; + save_test_case(file_name, QTEST_INPUT_FILE); + } + let mills_target = response_target.time.as_millis(); + show_accepted(self.test_number, mills_target as u32); + Ok(()) + } + + async fn delete_temporary_files_cmd_stress(&mut self) -> Result<(), tokio::io::Error> { + remove_files_async(vec![ + QTEST_INPUT_FILE, + QTEST_OUTPUT_FILE, + QTEST_ERROR_FILE, + TARGET_BINARY_FILE, + GEN_BINARY_FILE, + ]) + .await + } } From 61e15b46bca48fb390763bdcb372311223c2b039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 09:53:56 -0500 Subject: [PATCH 04/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/{ => legacy}/cmd_stress.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/controllers/{ => legacy}/cmd_stress.rs (100%) diff --git a/src/controllers/cmd_stress.rs b/src/controllers/legacy/cmd_stress.rs similarity index 100% rename from src/controllers/cmd_stress.rs rename to src/controllers/legacy/cmd_stress.rs From 88b3218a549769600850659b2505031927aba2c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 09:54:00 -0500 Subject: [PATCH 05/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 12 ++++++++- src/controllers/mod.rs | 1 - src/main.rs | 38 ++++++++++++++++------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index f626e09..aecb066 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -14,7 +14,7 @@ use crate::{ async_file::remove_files_async, file::{ copy_file, create_folder_or_error, format_filename_test_case, - load_testcases_from_prefix, save_test_case, + load_test_cases_from_status, load_testcases_from_prefix, remove_folder, save_test_case, }, }, generator::generator::execute_generator, @@ -98,6 +98,7 @@ impl StressController { } pub async fn run(&mut self) -> Result<(), ExitFailure> { + self.delete_test_case_folder(); self.create_initial_files()?; self.initialize_variables()?; self.load_testcases()?; @@ -160,7 +161,16 @@ impl StressController { Ok(()) } + fn delete_test_case_folder(&self) { + if self.command.get_save_bad() || self.command.get_save_all() { + // Remove all previous test cases + remove_folder(TEST_CASES_FOLDER); + } + } + fn load_testcases(&mut self) -> Result<(), ExitFailure> { + load_test_cases_from_status(&self.command, &mut self.cases)?; + let prefix = &self.command.get_prefix()[..]; load_testcases_from_prefix(&mut self.cases, prefix)?; self.cases_len = self.cases.len(); diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 96983d4..6afe2fc 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -7,7 +7,6 @@ pub mod cmd_check; pub mod cmd_cmp; pub mod cmd_example; -pub mod cmd_stress; pub mod cmd_output_async; pub mod cmd_setup_async; diff --git a/src/main.rs b/src/main.rs index 31c47f0..84b4b8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,23 +49,27 @@ async fn main() -> Result<(), ExitFailure> { run_tle, run_rte, run_mle, - } => controllers::cmd_stress::run(&StressCommand::new( - target_file, - gen_file, - test_cases, - timeout, - memory_limit, - prefix, - break_bad, - save_bad, - save_all, - run_all, - run_ac, - run_wa, - run_tle, - run_rte, - run_mle, - )), + } => { + controllers::cmd_stress_async::StressController::new(StressCommand::new( + target_file, + gen_file, + test_cases, + timeout, + memory_limit, + prefix, + break_bad, + save_bad, + save_all, + run_all, + run_ac, + run_wa, + run_tle, + run_rte, + run_mle, + )) + .run() + .await + } Opt::Cmp { target_file, correct_file, From 287083632d490c47165661edd314d67e73b831fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 10:16:58 -0500 Subject: [PATCH 06/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 8 ++++++-- src/error/handle_error.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index aecb066..c959396 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -9,7 +9,7 @@ use crate::{ PREFIX_TLE_FILES, QTEST_ERROR_FILE, QTEST_INPUT_FILE, QTEST_OUTPUT_FILE, TARGET_BINARY_FILE, TEST_CASES_FOLDER, }, - error::handle_error::throw_compiler_error_msg, + error::handle_error::{throw_break_found_msg, throw_compiler_error_msg}, file_handler::{ async_file::remove_files_async, file::{ @@ -259,7 +259,11 @@ impl StressController { if self.command.get_break_bad() { // remove input, output and error files self.delete_temporary_files_cmd_stress().await.ok(); - return Err(failure::err_msg("").into()); // TODO: Errors Refactor + return throw_break_found_msg( + "Memory Limit Exceeded", + "MLE", + self.command.get_test_cases(), + ); } Ok(()) } diff --git a/src/error/handle_error.rs b/src/error/handle_error.rs index f3ccd15..9c46933 100644 --- a/src/error/handle_error.rs +++ b/src/error/handle_error.rs @@ -104,7 +104,7 @@ pub fn throw_break_found_msg( "Wrong answer {} on test {}", status_name, test_number ))); - Ok(error.context(format!("{} status - break flag on", status))?) + Err(error.context(format!("{} status - break flag on", status))?) } #[allow(dead_code)] From 37a594a79b771c2b56a25ad49c8311810c421015 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 10:25:51 -0500 Subject: [PATCH 07/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index c959396..1e82ca0 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -1,4 +1,4 @@ -use std::{collections::VecDeque, path::PathBuf, time::Duration}; +use std::{collections::VecDeque, path::PathBuf, process, time::Duration}; use exitfailure::ExitFailure; @@ -27,7 +27,7 @@ use crate::{ is_time_limit_exceeded, Language, StatusResponse, }, views::style::{ - show_accepted, show_memory_limit_exceeded_error, show_runtime_error, + show_accepted, show_memory_limit_exceeded_error, show_runtime_error, show_stats, show_time_limit_exceeded, }, }; @@ -64,6 +64,10 @@ impl StateCounter { pub fn increase_mle(&mut self) { self.mle += 1; } + + pub fn has_stress_command_error(&self) -> bool { + (self.tle + self.rte + self.mle) > 0 + } } impl Default for StateCounter { @@ -142,6 +146,16 @@ impl StressController { } } + self.show_summary(); + + self.delete_temporary_files_cmd_stress().await?; + + // check if the target file has errors + if self.state_counter.has_stress_command_error() { + // exit status as error in software + process::exit(exitcode::SOFTWARE); + } + Ok(()) } @@ -314,6 +328,16 @@ impl StressController { Ok(()) } + fn show_summary(&self) { + show_stats( + self.state_counter.ac, + 0, + self.state_counter.tle, + self.state_counter.rte, + self.state_counter.mle, + ); + } + async fn delete_temporary_files_cmd_stress(&mut self) -> Result<(), tokio::io::Error> { remove_files_async(vec![ QTEST_INPUT_FILE, From 06a32b76ec1de6bf0d62cbc10e83947a848f6408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 10:39:01 -0500 Subject: [PATCH 08/14] [HOUSEKEEPING] add object-oriented structure to the stress command --- src/controllers/cmd_stress_async.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index 1e82ca0..8e99eff 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -192,7 +192,7 @@ impl StressController { } fn are_tests_pending(&self) -> bool { - (self.test_number as usize) < self.cases_len + self.command.has_test_cases(self.test_number) } fn increment_test_count(&mut self) { From 6df944d59319a3e7f1d4d12d72efea399e756d9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 11:23:53 -0500 Subject: [PATCH 09/14] refactor: code review suggestions --- src/controllers/cmd_stress_async.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index 8e99eff..bf1c761 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -192,7 +192,14 @@ impl StressController { } fn are_tests_pending(&self) -> bool { - self.command.has_test_cases(self.test_number) + // self.command.has_test_cases(self.test_number) + println!( + "{} {} {}", + self.test_number, + self.command.get_test_cases(), + self.command.can_run_cases() + ); + self.test_number < self.command.get_test_cases() || self.command.can_run_cases() } fn increment_test_count(&mut self) { @@ -203,13 +210,11 @@ impl StressController { self.current_case = self.cases.pop_front(); } - fn get_current_case(&self) -> PathBuf { - self.current_case.clone().unwrap() - } - fn load_case_file(&self) -> Result<(), ExitFailure> { // Load test case in stdin - copy_file(self.get_current_case().to_str().unwrap(), QTEST_INPUT_FILE)?; + if let Some(case) = self.current_case.clone() { + copy_file(case.to_str().unwrap(), QTEST_INPUT_FILE)?; + } Ok(()) } From ec7a8e5437c62783aec3262d000be9d02834ea6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 11:44:26 -0500 Subject: [PATCH 10/14] refactor: code review suggestions --- src/controllers/cmd_stress_async.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index bf1c761..4ea1bf5 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -147,14 +147,8 @@ impl StressController { } self.show_summary(); - self.delete_temporary_files_cmd_stress().await?; - - // check if the target file has errors - if self.state_counter.has_stress_command_error() { - // exit status as error in software - process::exit(exitcode::SOFTWARE); - } + self.check_errors_and_exit(); Ok(()) } @@ -333,6 +327,13 @@ impl StressController { Ok(()) } + fn check_errors_and_exit(&self) { + // check if the target file has errors + if self.state_counter.has_stress_command_error() { + process::exit(exitcode::SOFTWARE); + } + } + fn show_summary(&self) { show_stats( self.state_counter.ac, From 42672f95552e747d17fa4624947549c31849e85d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 13:34:02 -0500 Subject: [PATCH 11/14] refactor: code review suggestions --- src/controllers/cmd_stress_async.rs | 81 ++++++++--------------------- src/runner/mod.rs | 1 + src/runner/state_counter.rs | 49 +++++++++++++++++ 3 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 src/runner/state_counter.rs diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index 4ea1bf5..c56b3dc 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -22,9 +22,12 @@ use crate::{ get_language::{get_executor_generator, get_executor_target}, language_handler::LanguageHandler, }, - runner::types::{ - is_accepted, is_compiled_error, is_memory_limit_exceeded, is_runtime_error, - is_time_limit_exceeded, Language, StatusResponse, + runner::{ + state_counter::StateCounter, + types::{ + is_accepted, is_compiled_error, is_memory_limit_exceeded, is_runtime_error, + is_time_limit_exceeded, Language, StatusResponse, + }, }, views::style::{ show_accepted, show_memory_limit_exceeded_error, show_runtime_error, show_stats, @@ -32,50 +35,6 @@ use crate::{ }, }; -pub struct StateCounter { - tle: u32, - rte: u32, - ac: u32, - mle: u32, -} - -impl StateCounter { - pub fn new() -> Self { - Self { - tle: 0, - rte: 0, - ac: 0, - mle: 0, - } - } - - pub fn increase_tle(&mut self) { - self.tle += 1; - } - - pub fn increase_rte(&mut self) { - self.rte += 1; - } - - pub fn increase_ac(&mut self) { - self.ac += 1; - } - - pub fn increase_mle(&mut self) { - self.mle += 1; - } - - pub fn has_stress_command_error(&self) -> bool { - (self.tle + self.rte + self.mle) > 0 - } -} - -impl Default for StateCounter { - fn default() -> Self { - Self::new() - } -} - pub struct StressController { command: StressCommand, target_file_lang: Option, @@ -112,18 +71,9 @@ impl StressController { self.update_next_case(); self.load_case_file()?; - let mut can_continue = false; + let generator_execution_success = self.execute_generator_hander()?; - // run generator or load testcases using prefix - execute_generator( - &self.get_generator_lang_handler(), - &self.command, - &mut self.cases, - self.test_number, - &mut can_continue, - )?; - - if !can_continue { + if !generator_execution_success { break; } @@ -212,6 +162,21 @@ impl StressController { Ok(()) } + fn execute_generator_hander(&mut self) -> Result { + let mut can_continue = false; + + // run generator or load testcases using prefix + execute_generator( + &self.get_generator_lang_handler(), + &self.command, + &mut self.cases, + self.test_number, + &mut can_continue, + )?; + + Ok(can_continue) + } + fn get_target_lang_handler(&self) -> LanguageHandler { self.target_file_lang.clone().unwrap() } diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 015bc8e..d990561 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -5,4 +5,5 @@ */ pub mod cmd; +pub mod state_counter; pub mod types; diff --git a/src/runner/state_counter.rs b/src/runner/state_counter.rs new file mode 100644 index 0000000..6dc4916 --- /dev/null +++ b/src/runner/state_counter.rs @@ -0,0 +1,49 @@ +pub struct StateCounter { + pub tle: u32, + pub rte: u32, + pub ac: u32, + pub mle: u32, + pub wa: u32, +} + +impl StateCounter { + pub fn new() -> Self { + Self { + tle: 0, + rte: 0, + ac: 0, + mle: 0, + wa: 0, + } + } + + pub fn increase_tle(&mut self) { + self.tle += 1; + } + + pub fn increase_rte(&mut self) { + self.rte += 1; + } + + pub fn increase_ac(&mut self) { + self.ac += 1; + } + + pub fn increase_mle(&mut self) { + self.mle += 1; + } + + pub fn increase_wa(&mut self) { + self.wa += 1; + } + + pub fn has_stress_command_error(&self) -> bool { + (self.tle + self.rte + self.mle) > 0 + } +} + +impl Default for StateCounter { + fn default() -> Self { + Self::new() + } +} From 39c4603d8e003e28a00470cdab75a69803c54567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 13:49:03 -0500 Subject: [PATCH 12/14] refactor: code review suggestions --- src/controllers/cmd_stress_async.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index c56b3dc..f34081d 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -71,17 +71,13 @@ impl StressController { self.update_next_case(); self.load_case_file()?; - let generator_execution_success = self.execute_generator_hander()?; + let generator_execution_success: bool = self.execute_generator_handler()?; if !generator_execution_success { break; } - let response_target = self.get_target_lang_handler().execute( - self.command.get_timeout(), - self.command.get_memory_limit(), - self.test_number, - ); + let response_target: StatusResponse = self.execute_target_handler()?; if is_runtime_error(&response_target.status) { self.runtime_error_handler(&response_target).await?; @@ -162,7 +158,7 @@ impl StressController { Ok(()) } - fn execute_generator_hander(&mut self) -> Result { + fn execute_generator_handler(&mut self) -> Result { let mut can_continue = false; // run generator or load testcases using prefix @@ -177,6 +173,15 @@ impl StressController { Ok(can_continue) } + fn execute_target_handler(&self) -> Result { + let response_target = self.get_target_lang_handler().execute( + self.command.get_timeout(), + self.command.get_memory_limit(), + self.test_number, + ); + Ok(response_target) + } + fn get_target_lang_handler(&self) -> LanguageHandler { self.target_file_lang.clone().unwrap() } From 2449e9a9f5ba1e229fc63907242f67e4357a574d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 13:52:01 -0500 Subject: [PATCH 13/14] refactor: code review suggestions --- src/controllers/cmd_stress_async.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index f34081d..01c9230 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -307,7 +307,7 @@ impl StressController { fn show_summary(&self) { show_stats( self.state_counter.ac, - 0, + self.state_counter.wa, self.state_counter.tle, self.state_counter.rte, self.state_counter.mle, From 08e2da80c4ac3cfc4c71e51f9cda3b288d2fa2d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Miguel=20B=C3=A1ez?= Date: Thu, 11 Apr 2024 15:25:51 -0500 Subject: [PATCH 14/14] Update src/controllers/cmd_stress_async.rs --- src/controllers/cmd_stress_async.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/controllers/cmd_stress_async.rs b/src/controllers/cmd_stress_async.rs index 01c9230..ae12f1e 100644 --- a/src/controllers/cmd_stress_async.rs +++ b/src/controllers/cmd_stress_async.rs @@ -132,13 +132,6 @@ impl StressController { } fn are_tests_pending(&self) -> bool { - // self.command.has_test_cases(self.test_number) - println!( - "{} {} {}", - self.test_number, - self.command.get_test_cases(), - self.command.can_run_cases() - ); self.test_number < self.command.get_test_cases() || self.command.can_run_cases() }