From 5eabd393b990c071d2a3ef65a83cd6ababbe72b7 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sun, 2 Oct 2022 21:57:01 -0400 Subject: [PATCH 1/3] Remove unnecessary main function and unused "use" --- src/general/nqueens.rs | 50 ------------------------------------------ 1 file changed, 50 deletions(-) diff --git a/src/general/nqueens.rs b/src/general/nqueens.rs index 6eb91756fcc..c776a35b6f6 100644 --- a/src/general/nqueens.rs +++ b/src/general/nqueens.rs @@ -1,38 +1,3 @@ -#[allow(unused_imports)] -use std::env::args; - -#[allow(dead_code)] -fn main() { - let mut board_width = 0; - - for arg in args() { - board_width = match arg.parse() { - Ok(x) => x, - _ => 0, - }; - - if board_width != 0 { - break; - } - } - - if board_width < 4 { - println!( - "Running algorithm with 8 as a default. Specify an alternative Chess board size for \ - N-Queens as a command line argument.\n" - ); - board_width = 8; - } - - let board = match nqueens(board_width) { - Ok(success) => success, - Err(err) => panic!("{}", err), - }; - - println!("N-Queens {} by {} board result:", board_width, board_width); - print_board(&board); -} - /* The n-Queens search is a backtracking algorithm. Each row of the Chess board where a Queen is placed is dependent on all earlier rows. As only one Queen can fit per row, a one-dimensional @@ -94,21 +59,6 @@ pub fn nqueens(board_width: i64) -> Result, &'static str> { Ok(board_rows) } -fn print_board(board: &[i64]) { - for row in 0..board.len() { - print!("{}\t", board[row as usize]); - - for column in 0..board.len() as i64 { - if board[row as usize] == column { - print!("Q"); - } else { - print!("."); - } - } - println!(); - } -} - #[cfg(test)] mod test { use super::*; From 2d40675d97c68eb8cc0b4c111c56644b7bacbef3 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sun, 2 Oct 2022 21:58:55 -0400 Subject: [PATCH 2/3] Move unnecesary constant variable to test module --- src/ciphers/salsa.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ciphers/salsa.rs b/src/ciphers/salsa.rs index 77ccd7514e0..7c18bc2c3ee 100644 --- a/src/ciphers/salsa.rs +++ b/src/ciphers/salsa.rs @@ -14,9 +14,6 @@ macro_rules! quarter_round { }; } -#[allow(dead_code)] -pub const C: [u32; 4] = [0x65787061, 0x6e642033, 0x322d6279, 0x7465206b]; - /** * `salsa20` function takes as input an array of 16 32-bit integers (512 bits) * of which 128 bits is the constant 'expand 32-byte k', 256 bits is the key, @@ -86,6 +83,8 @@ mod tests { use super::*; use std::fmt::Write; + const C: [u32; 4] = [0x65787061, 0x6e642033, 0x322d6279, 0x7465206b]; + fn output_hex(inp: &[u32; 16]) -> String { let mut res = String::new(); res.reserve(512 / 4); From 65a64c1cfe2a7db441196799b528cdffee8295e2 Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sun, 2 Oct 2022 22:00:18 -0400 Subject: [PATCH 3/3] Move the function to the test module and make it public only for testing --- src/ciphers/hashing_traits.rs | 2 +- src/ciphers/sha256.rs | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/ciphers/hashing_traits.rs b/src/ciphers/hashing_traits.rs index 3ef24f924ad..f636b23eb86 100644 --- a/src/ciphers/hashing_traits.rs +++ b/src/ciphers/hashing_traits.rs @@ -69,7 +69,7 @@ impl> #[cfg(test)] mod tests { - use super::super::sha256::get_hash_string; + use super::super::sha256::tests::get_hash_string; use super::super::SHA256; use super::HMAC; diff --git a/src/ciphers/sha256.rs b/src/ciphers/sha256.rs index 52591895e24..c21b96bbbbe 100644 --- a/src/ciphers/sha256.rs +++ b/src/ciphers/sha256.rs @@ -5,8 +5,6 @@ * integer multiple of 8 */ -use std::fmt::Write; - // The constants are tested to make sure they are correct pub const H0: [u32; 8] = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, @@ -100,17 +98,6 @@ fn process_block(h: &mut [u32; 8], w: &mut [u32; 64], round: &mut [u32; 8], buf: } } -#[allow(dead_code)] -// Let's keep this utility function -pub fn get_hash_string(hash: &[u8; 32]) -> String { - let mut result = String::new(); - result.reserve(64); - for &ch in hash { - write!(&mut result, "{ch:02x}").unwrap(); - } - result -} - impl SHA256 { pub fn new_default() -> Self { SHA256 { @@ -219,9 +206,20 @@ impl super::Hasher<32> for SHA256 { } #[cfg(test)] -mod tests { +pub mod tests { use super::*; use crate::math::LinearSieve; + use std::fmt::Write; + + // Let's keep this utility function + pub fn get_hash_string(hash: &[u8; 32]) -> String { + let mut result = String::new(); + result.reserve(64); + for &ch in hash { + write!(&mut result, "{ch:02x}").unwrap(); + } + result + } #[test] fn test_constants() {