Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 31, 2023
1 parent 6b66356 commit a20138d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
1 change: 0 additions & 1 deletion src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ impl Iterator for Names {

//map the count so the first value is the last one (all "-"), the second one is the first one (all "_")...
let used_count = *count as isize - 1 + self.max_count as isize;

for (sep_index, char_index) in self.separator_indexes[..self.separator_count].iter().enumerate() {
let char = if used_count & (1 << sep_index) == 0 { b'_' } else { b'-' };
// SAFETY: We validated that `char_index` is a valid UTF-8 codepoint
Expand Down
40 changes: 19 additions & 21 deletions tests/names/mod.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
use crates_index::Names;

fn data_count(names: Names) -> usize {
names.collect::<Vec<String>>().len()
}

#[test]
fn empty_string() {
assert_eq!(data_count(Names::new("").unwrap()), 1);
fn empty_string_is_nothing_special() {
assert_eq!(assert_count(Names::new("").unwrap()), 1);
}

#[test]
fn name_without_separators_yields_name() {
assert_eq!(data_count(Names::new("serde").unwrap()), 1);
}

#[test]
fn permutation_count() {
assert_eq!(Names::new("a-b").unwrap().count(), 2);
assert_eq!(Names::new("a-b_c").unwrap().count(), 4);
assert_eq!(Names::new("a_b_c").unwrap().count(), 4);
assert_eq!(Names::new("a_b_c-d").unwrap().count(), 8);
assert_eq!(assert_count(Names::new("serde").unwrap()), 1);
}

#[test]
fn permutation_data_count() {
assert_eq!(data_count(Names::new("a-b").unwrap()), 2);
assert_eq!(data_count(Names::new("a-b_c").unwrap()), 4);
assert_eq!(data_count(Names::new("a_b_c").unwrap()), 4);
assert_eq!(data_count(Names::new("a_b_c-d").unwrap()), 8);
fn permutation_counts() {
assert_eq!(assert_count(Names::new("a-b").unwrap()), 2);
assert_eq!(assert_count(Names::new("a-b_c").unwrap()), 4);
assert_eq!(assert_count(Names::new("a_b_c").unwrap()), 4);
assert_eq!(assert_count(Names::new("a_b_c-d").unwrap()), 8);
}

#[test]
fn max_permutation_count_causes_error() {
assert_eq!(
data_count(Names::new("a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p").expect("15 separators are fine")),
assert_count(Names::new("a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p").expect("15 separators are fine")),
32768
);
assert!(
Expand Down Expand Up @@ -72,3 +60,13 @@ fn permutations() {
assert_eq!(&names, expected);
}
}

fn assert_count(names: Names) -> usize {
let expected = names.clone().collect::<Vec<_>>().len();
assert_eq!(
names.count(),
expected,
"the computed count should match the actual one"
);
expected
}

0 comments on commit a20138d

Please sign in to comment.