From e8edfdfbef08f3468211171c4767b34cf3a2cb77 Mon Sep 17 00:00:00 2001 From: fiona Date: Mon, 31 Mar 2025 21:58:08 -0400 Subject: [PATCH 1/3] remove incorrect lines in handout & ref --- week9/splitlab/src/split_pattern.rs | 2 +- week9/splitlab/src/split_str.rs | 5 +---- week9/splitlab_ref/src/split_pattern.rs | 2 +- week9/splitlab_ref/src/split_str.rs | 5 +---- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/week9/splitlab/src/split_pattern.rs b/week9/splitlab/src/split_pattern.rs index a32634f..f9d9a3e 100644 --- a/week9/splitlab/src/split_pattern.rs +++ b/week9/splitlab/src/split_pattern.rs @@ -31,7 +31,7 @@ where /// Returns the next substring of the original `haystack` string, split by some delimiter /// pattern. /// - /// Panics if the delimiter is empty (length 0). + /// Should panic if the delimiter is empty (length 0). fn next(&mut self) -> Option { todo!("Implement me!") } diff --git a/week9/splitlab/src/split_str.rs b/week9/splitlab/src/split_str.rs index bc67580..9522444 100644 --- a/week9/splitlab/src/split_str.rs +++ b/week9/splitlab/src/split_str.rs @@ -20,7 +20,7 @@ pub struct Split { impl Split { /// Creates a new `Split` instance with the given haystack and delimiter. /// - /// Panics if the delimiter is empty (length 0). + /// Should panic if the delimiter is empty (length 0). /// /// TODO(student): Replace the `'static` lifetimes with other lifetimes! pub fn new(haystack: &'static str, delimiter: &'static str) -> Self { @@ -46,9 +46,6 @@ impl Iterator for Split { type Item = &'static str; /// Returns the next substring of the original `haystack` string, split by some delimiter. - /// - /// If the delimiter is the empty string, returns the next character (as a string) to avoid - /// infinitely looping. fn next(&mut self) -> Option { todo!("Implement me (make sure to fix the lifetimes!)") } diff --git a/week9/splitlab_ref/src/split_pattern.rs b/week9/splitlab_ref/src/split_pattern.rs index fff6616..cce31cb 100644 --- a/week9/splitlab_ref/src/split_pattern.rs +++ b/week9/splitlab_ref/src/split_pattern.rs @@ -31,7 +31,7 @@ where /// Returns the next substring of the original `haystack` string, split by some delimiter /// pattern. /// - /// Panics if the delimiter is empty (length 0). + /// Should panic if the delimiter is empty (length 0). fn next(&mut self) -> Option { // If `remainder` is `None`, then there is nothing left to yield, and we should return // `None` immediately (with `?`). diff --git a/week9/splitlab_ref/src/split_str.rs b/week9/splitlab_ref/src/split_str.rs index 05d9062..9b5ef9a 100644 --- a/week9/splitlab_ref/src/split_str.rs +++ b/week9/splitlab_ref/src/split_str.rs @@ -14,7 +14,7 @@ pub struct Split<'haystack, 'delimiter> { impl<'haystack, 'delimiter> Split<'haystack, 'delimiter> { /// Creates a new `Split` instance with the given haystack and delimiter. /// - /// Panics if the delimiter is empty (length 0). + /// Should panic if the delimiter is empty (length 0). pub fn new(haystack: &'haystack str, delimiter: &'delimiter str) -> Self { assert!( !delimiter.is_empty(), @@ -37,8 +37,6 @@ impl<'haystack> Iterator for Split<'haystack, '_> { /// Returns the next substring of the original `haystack` string, split by some delimiter. /// - /// If the delimiter is the empty string, returns the next character (as a string) to avoid - /// infinitely looping. fn next(&mut self) -> Option { // If `remainder` is `None`, then there is nothing left to yield, and we should return // `None` immediately (with `?`). @@ -51,7 +49,6 @@ impl<'haystack> Iterator for Split<'haystack, '_> { }; let (start, end) = (index, index + self.delimiter.len()); - assert_ne!(start, end, "Delimiter cannot be empty"); // Compute the next string to yield as well as the new remainder. let next_str = &remainder[..start]; From 654842e00a059d92b367b15e13707ba5e64b27d8 Mon Sep 17 00:00:00 2001 From: fiona Date: Mon, 31 Mar 2025 22:22:58 -0400 Subject: [PATCH 2/3] fixed ref sol & handout, added tests for the empty delim case --- week9/splitlab/src/split_str.rs | 4 ---- week9/splitlab/src/tests/pattern.rs | 6 ++++++ week9/splitlab/src/tests/str.rs | 6 ++++++ week9/splitlab_ref/src/split_pattern.rs | 4 +++- week9/splitlab_ref/src/split_str.rs | 7 +++---- week9/splitlab_ref/src/tests/pattern.rs | 6 ++++++ week9/splitlab_ref/src/tests/str.rs | 6 ++++++ 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/week9/splitlab/src/split_str.rs b/week9/splitlab/src/split_str.rs index 9522444..9222fc5 100644 --- a/week9/splitlab/src/split_str.rs +++ b/week9/splitlab/src/split_str.rs @@ -24,10 +24,6 @@ impl Split { /// /// TODO(student): Replace the `'static` lifetimes with other lifetimes! pub fn new(haystack: &'static str, delimiter: &'static str) -> Self { - assert!( - !delimiter.is_empty(), - "Delimiter must not be an empty string" - ); Self { remainder: Some(haystack), diff --git a/week9/splitlab/src/tests/pattern.rs b/week9/splitlab/src/tests/pattern.rs index 3f0523f..66c72d9 100644 --- a/week9/splitlab/src/tests/pattern.rs +++ b/week9/splitlab/src/tests/pattern.rs @@ -1,5 +1,11 @@ use super::*; +#[test] +#[should_panic] +fn test_pattern_empty_delimiter() { + let _ = SplitPattern::new(&String::from("a b c"), "").collect::>(); +} + test_pattern!( test_crust_of_rust_until_char_test, "hello world", diff --git a/week9/splitlab/src/tests/str.rs b/week9/splitlab/src/tests/str.rs index 0ece839..529dde6 100644 --- a/week9/splitlab/src/tests/str.rs +++ b/week9/splitlab/src/tests/str.rs @@ -15,6 +15,12 @@ fn test_str_compiles() { assert_eq!(results, vec!["a", "b", "c", "d", "e"]); } +#[test] +#[should_panic] +fn test_str_empty_delimiter() { + let _ = Split::new(&String::from("a b c"), &String::from("")); +} + test_str!( test_crust_of_rust_it_works, "a b c d e", diff --git a/week9/splitlab_ref/src/split_pattern.rs b/week9/splitlab_ref/src/split_pattern.rs index cce31cb..c10e906 100644 --- a/week9/splitlab_ref/src/split_pattern.rs +++ b/week9/splitlab_ref/src/split_pattern.rs @@ -42,7 +42,9 @@ where // If there is no delimiter in the remainder, return the entire remainder. return self.remainder.take(); }; - assert_ne!(start, end, "Delimiter cannot be empty"); + if start == end { + panic!("Delimiter cannot be empty"); + } // Compute the next string to yield as well as the new remainder. let next_str = &remainder[..start]; diff --git a/week9/splitlab_ref/src/split_str.rs b/week9/splitlab_ref/src/split_str.rs index 9b5ef9a..bb23bb4 100644 --- a/week9/splitlab_ref/src/split_str.rs +++ b/week9/splitlab_ref/src/split_str.rs @@ -16,10 +16,9 @@ impl<'haystack, 'delimiter> Split<'haystack, 'delimiter> { /// /// Should panic if the delimiter is empty (length 0). pub fn new(haystack: &'haystack str, delimiter: &'delimiter str) -> Self { - assert!( - !delimiter.is_empty(), - "Delimiter must not be an empty string" - ); + if delimiter.is_empty() { + panic!("Delimiter cannot be empty") + } Self { remainder: Some(haystack), diff --git a/week9/splitlab_ref/src/tests/pattern.rs b/week9/splitlab_ref/src/tests/pattern.rs index 3f0523f..66c72d9 100644 --- a/week9/splitlab_ref/src/tests/pattern.rs +++ b/week9/splitlab_ref/src/tests/pattern.rs @@ -1,5 +1,11 @@ use super::*; +#[test] +#[should_panic] +fn test_pattern_empty_delimiter() { + let _ = SplitPattern::new(&String::from("a b c"), "").collect::>(); +} + test_pattern!( test_crust_of_rust_until_char_test, "hello world", diff --git a/week9/splitlab_ref/src/tests/str.rs b/week9/splitlab_ref/src/tests/str.rs index 0ece839..529dde6 100644 --- a/week9/splitlab_ref/src/tests/str.rs +++ b/week9/splitlab_ref/src/tests/str.rs @@ -15,6 +15,12 @@ fn test_str_compiles() { assert_eq!(results, vec!["a", "b", "c", "d", "e"]); } +#[test] +#[should_panic] +fn test_str_empty_delimiter() { + let _ = Split::new(&String::from("a b c"), &String::from("")); +} + test_str!( test_crust_of_rust_it_works, "a b c d e", From 6ff81abacae6b3afc436e11dc407f2729dc2ff9c Mon Sep 17 00:00:00 2001 From: fiona Date: Wed, 29 Oct 2025 13:45:02 -0400 Subject: [PATCH 3/3] add tests to autograder --- week9/autograder/source/splitlab/src/tests/pattern.rs | 6 ++++++ week9/autograder/source/splitlab/src/tests/str.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/week9/autograder/source/splitlab/src/tests/pattern.rs b/week9/autograder/source/splitlab/src/tests/pattern.rs index 3f0523f..66c72d9 100644 --- a/week9/autograder/source/splitlab/src/tests/pattern.rs +++ b/week9/autograder/source/splitlab/src/tests/pattern.rs @@ -1,5 +1,11 @@ use super::*; +#[test] +#[should_panic] +fn test_pattern_empty_delimiter() { + let _ = SplitPattern::new(&String::from("a b c"), "").collect::>(); +} + test_pattern!( test_crust_of_rust_until_char_test, "hello world", diff --git a/week9/autograder/source/splitlab/src/tests/str.rs b/week9/autograder/source/splitlab/src/tests/str.rs index 0ece839..529dde6 100644 --- a/week9/autograder/source/splitlab/src/tests/str.rs +++ b/week9/autograder/source/splitlab/src/tests/str.rs @@ -15,6 +15,12 @@ fn test_str_compiles() { assert_eq!(results, vec!["a", "b", "c", "d", "e"]); } +#[test] +#[should_panic] +fn test_str_empty_delimiter() { + let _ = Split::new(&String::from("a b c"), &String::from("")); +} + test_str!( test_crust_of_rust_it_works, "a b c d e",