o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
Untitled conversation
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
mod tests {
use super::*;
use tempfile::tempdir;
use test_case::test_case;
#[test_case("xyzXYZ", "xyz",
SearchOptions::default(),
vec![0..3]; "case_sensitive_exact_match")]
#[test_case("xyzXYZ", "xyz",
SearchOptions { case_insensitive: true, ..Default::default() },
vec![0..3, 3..6]; "case_insensitive_match")]
#[test_case("xyzXYZ", "xyz",
SearchOptions { whole_word: true, ..Default::default() },
vec![]; "whole_word_no_match")]
#[test_case("abc abc1 abc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..3, 9..12]; "whole_word_multiple_matches")]
#[test_case("Apple apple XYZ", "aPpLe",
SearchOptions { case_insensitive: true, whole_word: true, ..Default::default() },
vec![0..5, 6..11]; "case_insensitive_whole_word")]
#[test_case("abc,abcabc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..3]; "whole_word_with_punctuation")]
#[test_case("ababab", "aba",
SearchOptions { whole_word: true, ..Default::default() },
vec![]; "no_overlapping_whole_word")]
#[test_case("test this is a test", "test",
SearchOptions { whole_word: true, ..Default::default() },
vec![0..4, 15..19]; "whole_word_at_end")]
#[test_case("abc_abc $abc", "abc",
SearchOptions { whole_word: true, ..Default::default() },
vec![9..12]; "whole_word_with_special_characters")]
fn test_find_cols(
text: &str,
pattern: &str,
options: SearchOptions,
result: Vec<Range<usize>>,
) {
assert_eq!(find_match_range(text, pattern, &options), result);
}
const LINE1: &str = "The quick brown fox jumps over the lazy dog.";
const LINE2: &str = "The quick brown fox is very quick.";
const LINE3: &str = "A quick brown fox is a quick animal.";
fn new_hash_map(ls: Vec<(usize, &str)>) -> HashMap<usize, String> {
let mut map = HashMap::new();
map.extend(
ls.iter()
.map(|(i, s)| (*i, s.to_string()))
.collect::<Vec<_>>(),
);
map
}
#[test_case(&format!("{LINE1}\n{LINE2}\n{LINE3}"), "is very",
(new_hash_map(vec![(2, LINE2)]),
vec![MatchPosition {row: 2, char_range: 20..27}]),
SearchOptions::default();
"single")]
#[test_case(&format!("{LINE1}\n{LINE2}\n{LINE3}"), "quick",
(new_hash_map(vec![(1, LINE1), (2, LINE2), (3, LINE3)]),
vec![MatchPosition {row: 1, char_range: 4..9}, MatchPosition {row: 2, char_range: 4..9}, MatchPosition {row: 2, char_range: 28..33}, MatchPosition {row: 3, char_range: 2..7}, MatchPosition {row: 3, char_range: 23..28}]),
SearchOptions::default();
"multiple same line")]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 4", (new_hash_map(vec![(3, "line 3"), (4, "line 4"), (5, "line 5")]),
vec![MatchPosition {row: 4, char_range: 0..6}]),
SearchOptions {line_buffer: 1, ..Default::default()};
"line buffer"
)]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 2", (new_hash_map(vec![(1, "line 1"), (2, "line 2"), (3, "line 3"), (4, "line 4")]),
vec![MatchPosition {row: 2, char_range: 0..6}]),
SearchOptions {line_buffer: 2, ..Default::default()};
"line buffer clipped at start"
)]
#[test_case("line 1
line 2
line 3
line 4
line 5
line 6", "line 6", (new_hash_map(vec![(4, "line 4"), (5, "line 5"), (6, "line 6")]),
vec![MatchPosition {row: 6, char_range: 0..6}]),
SearchOptions {line_buffer: 2, ..Default::default()};
"line buffer clipped at end"
)]
#[test_case("line 1
line 2
line a
line 4
line a
line 6", "line a", (new_hash_map(vec![(2, "line 2"), (3, "line a"), (4, "line 4"), (5, "line a"), (6, "line 6")]),
vec![MatchPosition {row: 3, char_range: 0..6}, MatchPosition {row: 5, char_range: 0..6}]),
SearchOptions {line_buffer: 1, ..Default::default()};
"line buffer overlap"
)]
fn test_search(file: &str, pattern: &str, result: FileMatchData, options: SearchOptions) {
let temp = tempdir().unwrap();
std::fs::write(temp.path().join("file"), file).unwrap();
assert_eq!(
search(temp.path(), pattern, &options).first().unwrap().1,
result
)
}
}
refactor and format
Here’s a more idiomatic, DRY-ed-up and rustfmt-ed version of your tests. I’ve
r!) to make ranges less noisytest_find_match_ranges and test_searchwrite_temp_file helper for test_searchrustfmt defaults#[cfg(test)]
mod tests {
use super::*;
use std::ops::Range;
use tempfile::tempdir;
use test_case::test_case;
/// A little macro to shorten `0..3` into `r!(0,3)`
macro_rules! r {
($start:expr, $end:expr) => {
$start..$end
};
}
/// Convenience alias
type R = Range<usize>;
// -----------------------------------
// Tests for `find_match_range`
// -----------------------------------
#[test_case("xyzXYZ", "xyz", SearchOptions::default(), vec![r!(0, 3)]; "case_sensitive_exact")]
#[test_case("xyzXYZ", "xyz", SearchOptions { case_insensitive: true, ..Default::default() }, vec![r!(0, 3), r!(3, 6)]; "case_insensitive")]
#[test_case("xyzXYZ", "xyz", SearchOptions { whole_word: true, ..Default::default() }, vec![]; "whole_word_no")]
#[test_case("abc abc1 abc", "abc", SearchOptions { whole_word: true, ..Default::default() },
vec![r!(0, 3), r!(9, 12)]; "whole_word_multi")]
#[test_case("Apple apple XYZ", "aPpLe",
SearchOptions { case_insensitive: true, whole_word: true, ..Default::default() },
vec![r!(0, 5), r!(6, 11)]; "ci_whole")]
#[test_case("abc,abcabc", "abc", SearchOptions { whole_word: true, ..Default::default() }, vec![r!(0, 3)]; "whole_word_punct")]
#[test_case("ababab", "aba", SearchOptions { whole_word: true, ..Default::default() }, vec![]; "no_overlap_whole")]
#[test_case("test this is a test", "test", SearchOptions { whole_word: true, ..Default::default() },
vec![r!(0, 4), r!(15, 19)]; "whole_at_end")]
#[test_case("abc_abc $abc", "abc", SearchOptions { whole_word: true, ..Default::default() },
vec![r!(9, 12)]; "whole_special")]
fn test_find_match_ranges(
text: &str,
pattern: &str,
options: SearchOptions,
expected: Vec<R>,
) {
let found = find_match_range(text, pattern, &options);
assert_eq!(found, expected);
}
// -----------------------------------
// Tests for `search`
// -----------------------------------
const LINE1: &str = "The quick brown fox jumps over the lazy dog.";
const LINE2: &str = "The quick brown fox is very quick.";
const LINE3: &str = "A quick brown fox is a quick animal.";
/// Build a HashMap<row, line> from a vec of tuples
fn new_hash_map(ls: Vec<(usize, &str)>) -> std::collections::HashMap<usize, String> {
ls.into_iter().map(|(i, s)| (i, s.to_string())).collect()
}
/// Write `content` to a temp file called "file" and return the tempdir path
fn write_temp_file(content: &str) -> tempfile::TempDir {
let td = tempdir().unwrap();
std::fs::write(td.path().join("file"), content).unwrap();
td
}
#[test_case(
&format!("{}\n{}\n{}", LINE1, LINE2, LINE3),
"is very",
(new_hash_map(vec![(2, LINE2)]),
vec![MatchPosition { row: 2, char_range: 20..27 }]),
SearchOptions::default();
"single"
)]
#[test_case(
&format!("{}\n{}\n{}", LINE1, LINE2, LINE3),
"quick",
(new_hash_map(vec![(1, LINE1), (2, LINE2), (3, LINE3)]),
vec![
MatchPosition { row: 1, char_range: 4..9 },
MatchPosition { row: 2, char_range: 4..9 },
MatchPosition { row: 2, char_range: 28..33 },
MatchPosition { row: 3, char_range: 2..7 },
MatchPosition { row: 3, char_range: 23..28 },
]),
SearchOptions::default();
"multi_same_line"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 4",
(new_hash_map(vec![(3, "line 3"), (4, "line 4"), (5, "line 5")]),
vec![MatchPosition { row: 4, char_range: 0..6 }]),
SearchOptions { line_buffer: 1, ..Default::default() };
"buffer_1"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 2",
(new_hash_map(vec![(1, "line 1"), (2, "line 2"), (3, "line 3"), (4, "line 4")]),
vec![MatchPosition { row: 2, char_range: 0..6 }]),
SearchOptions { line_buffer: 2, ..Default::default() };
"buffer_clipped_start"
)]
#[test_case(
"line 1\nline 2\nline 3\nline 4\nline 5\nline 6",
"line 6",
(new_hash_map(vec![(4, "line 4"), (5, "line 5"), (6, "line 6")]),
vec![MatchPosition { row: 6, char_range: 0..6 }]),
SearchOptions { line_buffer: 2, ..Default::default() };
"buffer_clipped_end"
)]
#[test_case(
"line 1\nline 2\nline a\nline 4\nline a\nline 6",
"line a",
(new_hash_map(vec![(2, "line 2"), (3, "line a"), (4, "line 4"), (5, "line a"), (6, "line 6")]),
vec![
MatchPosition { row: 3, char_range: 0..6 },
MatchPosition { row: 5, char_range: 0..6 },
]),
SearchOptions { line_buffer: 1, ..Default::default() };
"buffer_overlap"
)]
fn test_search(
file_content: &str,
pattern: &str,
expected: FileMatchData,
options: SearchOptions,
) {
let td = write_temp_file(file_content);
let results = search(td.path(), pattern, &options);
let (_path, data) = &results[0];
assert_eq!(data, &expected);
}
}
A few notes:
r! macro to make writing 0..12 ranges less error-prone.SearchOptions { …, ..Default::default() } are still explicit; you could further wrap those into builder methods if you prefer.write_temp_file helper centralizes the tempdir() + fs::write() dance.This should pass cargo test and be easier to extend or maintain. 🚀