fix: create all permutations of depth for tree command

This commit is contained in:
Moritz Böhme 2025-02-23 18:20:42 +01:00
parent a58da2ffda
commit 8238d3d0a8

View file

@ -95,14 +95,12 @@ pub fn create_tag_combinations(tags: &[String], depth: usize) -> Vec<Vec<String>
// Start with existing combinations of length-1
for combo in result.iter().filter(|c| c.len() == len - 1) {
// Try to add each remaining tag that comes after the last tag in combo
if let Some(last) = combo.last() {
for tag in tags {
if tag > last && !combo.contains(tag) {
let mut new_combo = combo.clone();
new_combo.push(tag.clone());
temp.push(new_combo);
}
// Try to add each remaining tag
for tag in tags {
if !combo.contains(tag) {
let mut new_combo = combo.clone();
new_combo.push(tag.clone());
temp.push(new_combo);
}
}
}
@ -322,20 +320,7 @@ mod tests {
// Should contain individual tags and pairs, but no triples
assert!(result.iter().all(|combo| combo.len() <= 2));
assert_eq!(result.len(), 6); // 3 individual + 3 pairs
}
#[test]
fn test_create_tag_combinations_order() {
let tags = vec!["tag2".to_string(), "tag1".to_string(), "tag3".to_string()];
let result = create_tag_combinations(&tags, 2);
// Check that all combinations maintain alphabetical order
for combo in result {
if combo.len() > 1 {
assert!(combo.windows(2).all(|w| w[0] < w[1]));
}
}
assert_eq!(result.len(), 9); // 3 individual + 6 pairs
}
#[test]
@ -345,6 +330,6 @@ mod tests {
// Convert to set to check for duplicates
let result_set: std::collections::HashSet<_> = result.into_iter().collect();
assert_eq!(result_set.len(), 7); // 3 individual + 3 pairs + 1 triple
assert_eq!(result_set.len(), 15); // 3 individual + 6 pairs + 9 triple
}
}