Skip to content

Commit

Permalink
Subject: better test separation
Browse files Browse the repository at this point in the history
  • Loading branch information
k5924 committed Apr 13, 2024
1 parent 58c5e82 commit 542e625
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 433 deletions.
76 changes: 1 addition & 75 deletions src/chunk_processor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use crate::line_processor::process_line;
use crate::utils::is_newline_char;
use crate::Station;
use crate::station::Station;
use hashbrown::HashMap;

pub fn process_chunk(slice: &[u8], map: &mut HashMap<String, Station>) -> io::Result<()> {
Expand Down Expand Up @@ -34,77 +34,3 @@ pub fn process_chunk(slice: &[u8], map: &mut HashMap<String, Station>) -> io::Re
Ok(())
}

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

#[test]
fn test_process_chunk_single_line() -> io::Result<()> {
let mut map = HashMap::new();
let chunk = b"key1;10.0\n";
process_chunk(chunk, &mut map)?;

assert_eq!(
map.get("key1"),
Some(&Station {
min: 10.0,
max: 10.0,
sum: 10.0,
count: 1,
})
);

Ok(())
}

#[test]
fn test_process_chunk_multiple_lines() -> io::Result<()> {
let mut map = HashMap::new();
let chunk = b"key1;10.0\nkey2;20.0\nkey1;30.0\n";
process_chunk(chunk, &mut map)?;

assert_eq!(
map.get("key1"),
Some(&Station {
min: 10.0,
max: 30.0,
sum: 40.0,
count: 2,
})
);
assert_eq!(
map.get("key2"),
Some(&Station {
min: 20.0,
max: 20.0,
sum: 20.0,
count: 1,
})
);

Ok(())
}

#[test]
fn test_process_chunk_empty_input() -> io::Result<()> {
let mut map = HashMap::new();
let chunk = b"";
process_chunk(chunk, &mut map)?;

assert!(map.is_empty());

Ok(())
}

#[test]
fn test_process_chunk_newline_only() -> io::Result<()> {
let mut map = HashMap::new();
let chunk = b"\n\n";
process_chunk(chunk, &mut map)?;

assert!(map.is_empty());

Ok(())
}
}
53 changes: 0 additions & 53 deletions src/config_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,3 @@ pub fn get_mode(config: &Config) -> io::Result<RunningMode> {
Ok(mode_enum)
}

#[cfg(test)]
mod tests {
use super::*;
use config::{Config, File, FileFormat};

#[test]
fn test_get_mode_valid_modes() {
// Create a test configuration
let config = Config::builder()
.add_source(File::from_str("myapp.mode = 'single_thread'", FileFormat::Toml))
.build()
.expect("Failed to build config");

// Test single_thread mode
assert_eq!(get_mode(&config).unwrap(), RunningMode::SingleThreaded);

// Test multi_thread mode
let config = Config::builder()
.add_source(File::from_str("myapp.mode = 'multi_thread'", FileFormat::Toml))
.build()
.expect("Failed to build config");
assert_eq!(get_mode(&config).unwrap(), RunningMode::MultiThreaded);

// Test rayon mode
let config = Config::builder()
.add_source(File::from_str("myapp.mode = 'rayon'", FileFormat::Toml))
.build()
.expect("Failed to build config");
assert_eq!(get_mode(&config).unwrap(), RunningMode::Rayon);
}

#[test]
fn test_get_mode_invalid_mode() {
// Create a test configuration with invalid mode
let config = Config::builder()
.add_source(File::from_str("myapp.mode = 'invalid_mode'", FileFormat::Toml))
.build()
.expect("Failed to build config");

// Test invalid mode
assert!(get_mode(&config).is_err());
}

#[test]
fn test_get_mode_missing_mode() {
// Create an empty configuration
let config = Config::builder().build().expect("Failed to build config");

// Test missing mode
assert!(get_mode(&config).is_err());
}
}

39 changes: 0 additions & 39 deletions src/float_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,3 @@ pub unsafe fn parse_float(s: &[u8]) -> f64 {
value
}

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

#[test]
fn test_parse_float_positive() {
let input = b"123.45";
let result = unsafe { parse_float(input) };
assert_eq!(result, 123.4);
}

#[test]
fn test_parse_float_negative() {
let input = b"-123.45";
let result = unsafe { parse_float(input) };
assert_eq!(result, -123.4);
}

#[test]
fn test_parse_float_multiple_decimal_points() {
let input = b"12.3.45";
let result = unsafe { parse_float(input) };
assert_eq!(result, 0.0);
}

#[test]
fn test_parse_float_minus_sign_at_end() {
let input = b"123.45-";
let result = unsafe { parse_float(input) };
assert_eq!(result, 0.0);
}

#[test]
fn test_parse_float_invalid_character() {
let input = b"123.4x5";
let result = unsafe { parse_float(input) };
assert_eq!(result, 0.0);
}
}
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod station;
pub mod utils;
pub mod running_mode;
pub mod map_processor;
pub mod line_processor;
pub mod float_parser;
pub mod config_loader;
pub mod chunk_processor;
83 changes: 1 addition & 82 deletions src/line_processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::float_parser::parse_float;
use crate::Station;
use crate::station::Station;
use crate::map_processor::update_or_create;
use hashbrown::HashMap;

Expand All @@ -21,84 +21,3 @@ pub fn process_line(line: &[u8], map: &mut HashMap<String, Station>) {
update_or_create(map, key, part2_float);
}

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

#[test]
fn test_process_line_existing_key() {
let mut map = HashMap::new();
map.insert(String::from("key1"), Station::new(10.0));

let line = b"key1;20.0";
process_line(line, &mut map);

assert_eq!(
map.get("key1"),
Some(&Station {
min: 10.0,
max: 20.0,
sum: 30.0,
count: 2,
})
);
}

#[test]
fn test_process_line_new_key() {
let mut map = HashMap::new();

let line = b"key2;30.0";
process_line(line, &mut map);

assert_eq!(
map.get("key2"),
Some(&Station {
min: 30.0,
max: 30.0,
sum: 30.0,
count: 1,
})
);
}

#[test]
fn test_process_line_no_semicolon() {
let mut map = HashMap::new();

let line = b"no_semicolon";
process_line(line, &mut map);

assert_eq!(map.len(), 0);
}

#[test]
fn test_process_line_empty_value() {
let mut map = HashMap::new();

let line = b"empty_value;";
process_line(line, &mut map);

assert_eq!(map.len(), 1);
}

#[test]
fn test_process_line_empty_key() {
let mut map = HashMap::new();

let line = b";30.0";
process_line(line, &mut map);

assert_eq!(map.len(), 1);
}

#[test]
fn test_process_line_invalid_value() {
let mut map = HashMap::new();

let line = b"key3;invalid_value";
process_line(line, &mut map);

assert_eq!(map.len(), 1);
}
}
102 changes: 1 addition & 101 deletions src/map_processor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hashbrown::HashMap;
use crate::Station;
use crate::station::Station;

pub fn update_or_create(map: &mut HashMap<String, Station>, key: &str, value: f64) {
match map.raw_entry_mut().from_key(key) {
Expand All @@ -23,103 +23,3 @@ pub fn merge_or_create(map: &mut HashMap<String, Station>, key: &str, value: Sta
}
}

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

#[test]
fn test_update_or_create_existing_key() {
let mut map = HashMap::new();
map.insert(String::from("key1"), Station::new(10.0));

update_or_create(&mut map, "key1", 20.0);

assert_eq!(
map.get("key1"),
Some(&Station {
min: 10.0,
max: 20.0,
sum: 30.0,
count: 2,
})
);
}

#[test]
fn test_update_or_create_new_key() {
let mut map = HashMap::new();

update_or_create(&mut map, "key2", 30.0);

assert_eq!(
map.get("key2"),
Some(&Station {
min: 30.0,
max: 30.0,
sum: 30.0,
count: 1,
})
);
}

#[test]
fn test_merge_or_create_existing_key() {
let mut map = HashMap::new();
map.insert(
String::from("key1"),
Station {
min: 10.0,
max: 20.0,
sum: 30.0,
count: 2,
},
);

merge_or_create(
&mut map,
"key1",
Station {
min: 5.0,
max: 25.0,
sum: 30.0,
count: 3,
},
);

assert_eq!(
map.get("key1"),
Some(&Station {
min: 5.0,
max: 25.0,
sum: 60.0,
count: 5,
})
);
}

#[test]
fn test_merge_or_create_new_key() {
let mut map = HashMap::new();

merge_or_create(
&mut map,
"key2",
Station {
min: 5.0,
max: 25.0,
sum: 30.0,
count: 3,
},
);

assert_eq!(
map.get("key2"),
Some(&Station {
min: 5.0,
max: 25.0,
sum: 30.0,
count: 3,
})
);
}
}
Loading

0 comments on commit 542e625

Please sign in to comment.