Skip to content

Commit 09b35ad

Browse files
committed
feat: add test cases for challenge 2
1 parent 86f0ae0 commit 09b35ad

20 files changed

Lines changed: 57 additions & 2 deletions

src/challenge/challenge_02.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use anyhow::Result;
33
use colored::Colorize;
44

5-
use crate::utils::split_whitespace_and_parse;
5+
use crate::utils::{VecPreview, split_whitespace_and_parse};
66

77
pub fn run(input: &str, output: Option<&str>) -> Result<String> {
88
let mut lines = input.lines();
@@ -21,7 +21,10 @@ pub fn run(input: &str, output: Option<&str>) -> Result<String> {
2121
output
2222
);
2323

24-
return Ok(format!("Result matches the specified output: {output:#?}"));
24+
return Ok(format!(
25+
"Result matches the specified output: {}",
26+
result.preview(15)
27+
));
2528
}
2629

2730
Ok(format!(

src/utils.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::str::FromStr;
23

34
pub fn split_whitespace_and_parse<T: FromStr + Sized>(source: &str) -> Result<Vec<T>, T::Err> {
@@ -6,3 +7,27 @@ pub fn split_whitespace_and_parse<T: FromStr + Sized>(source: &str) -> Result<Ve
67
.map(|x| x.parse::<T>())
78
.collect::<Result<Vec<T>, T::Err>>()
89
}
10+
11+
pub trait VecPreview<T> {
12+
fn preview(&self, count: usize) -> String;
13+
}
14+
15+
impl<T: std::fmt::Display + std::fmt::Debug> VecPreview<T> for [T] {
16+
fn preview(&self, count: usize) -> String {
17+
if self.len() <= count {
18+
return format!("{:?}", self);
19+
}
20+
21+
let mut s = String::from("[");
22+
for (i, item) in self.iter().take(count).enumerate() {
23+
if i > 0 {
24+
s.push_str(", ");
25+
}
26+
27+
write!(&mut s, "{}", item).unwrap();
28+
}
29+
30+
s.push_str(&format!(", ... ({} total)]", self.len()));
31+
s
32+
}
33+
}

testcases/02/02_input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3
2+
22 21 20

testcases/02/02_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 0 0

testcases/02/03_input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
1 2 3 4 5

testcases/02/03_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 1 1 1 0

testcases/02/04_input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
4
2+
10 10 10 10

testcases/02/04_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 0 0 0

testcases/02/05_input.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
6
2+
5 10 5 10 5 10

testcases/02/05_output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 0 1 0 1 0

0 commit comments

Comments
 (0)