Skip to content

Commit

Permalink
additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
flexatone committed Mar 16, 2024
1 parent a0a3711 commit caf3096
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Complex Sieves combine Residuals with logical operators such as complementation,

![Sieve diagram](https://raw.githubusercontent.com/flexatone/xensieve-sandbox/default/images/sieve-a.svg)

While all Sieves are, by definition, periodic, combinations of Residuals can result in sequences and with great local complexity and inner patterning.
While all Sieves are, by definition, periodic, combinations of Residuals can result in sequences with great local complexity and inner patterning.


# The `xensieve.Sieve` Inteface
Expand Down
41 changes: 39 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) fn residual_to_ints(value: &str) -> Result<(u64, u64), &'static str>
if parts.len() != 2 {
return Err("Input must contain one '@' character separating two numbers.");
}
let m = parts[0].parse::<u64>().expect("Parse failure");
let s = parts[1].parse::<u64>().expect("Parse failure");
let m = parts[0].parse::<u64>().map_err(|_e| "Residual error parsing modulus")?;
let s = parts[1].parse::<u64>().map_err(|_e| "Residual error parsing shift")?;
Ok((m, s))
}

Expand Down Expand Up @@ -102,6 +102,30 @@ mod tests {
assert_eq!(residual_to_ints("0@5").unwrap(), (0, 5))
}

#[test]
fn test_residual_to_ints_d() {
assert!(residual_to_ints("0").is_err());
}

#[test]
fn test_residual_to_ints_e() {
assert!(residual_to_ints("3@wer").is_err());
}

#[test]
fn test_residual_to_ints_f() {
assert!(residual_to_ints("foo@3").is_err());
}


#[test]
fn test_char_to_precedence_a() {
assert_eq!(char_to_precedence('!'), 4);
assert_eq!(char_to_precedence('-'), 0);
assert_eq!(char_to_precedence('&'), 3);
}


#[test]
fn test_infix_to_postfix_a() {
let e1 = "!3@1 & 6@2 | !(10@0 | 2@0 | 3@0 )";
Expand Down Expand Up @@ -148,4 +172,17 @@ mod tests {
let px1 = infix_to_postfix(e1).unwrap();
assert_eq!(px1.iter().collect::<Vec<_>>(), vec!["10@0", "10@9", "^"]);
}

#[test]
fn test_infix_to_postfix_f() {
let e1 = "10@0 ^ -10@9";
assert!(infix_to_postfix(e1).is_err());
}

#[test]
fn test_infix_to_postfix_g() {
let e1 = "10@0 + 10@9";
assert!(infix_to_postfix(e1).is_err());
}

}
20 changes: 17 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

/// Find the greatest common divisor.
fn gcd<T>(mut n: T, mut m: T, zero: T) -> Result<T, &'static str>
where
T: std::ops::Rem<Output = T> + std::cmp::Ord + Copy,
Expand Down Expand Up @@ -56,9 +58,11 @@ pub(crate) fn intersection(
if d != 1 && (span % d != 0) {
return Ok((0, 0)); // no intersection
}
if d != 1 && (span % d == 0) && (s1 != s2) && (md1 == md2) {
return Ok((d, s1));
}
// NOTE: though this case was specified, it seems impossible to replicate
// if d != 1 && (span % d == 0) && (s1 != s2) && (md1 == md2) {
// return Ok((d, s1));
// }

// d might be 1
let m = md1 * md2 * d;
Ok((m, (s1 + (meziriac(md1, md2).unwrap() * span * md1)) % m))
Expand Down Expand Up @@ -95,6 +99,16 @@ mod tests {
assert_eq!(gcd(0, 3, 0).is_err(), true);
}

#[test]
fn test_intersection_a() {
assert_eq!(intersection(0, 0, 2, 3).unwrap(), (0, 0));
}

#[test]
fn test_intersection_b() {
assert_eq!(intersection(45, 40, 11, 1).unwrap(), (360, 101));
}

#[test]
fn test_meziriac_a() {
assert_eq!(meziriac(1, 1).unwrap(), 1);
Expand Down

0 comments on commit caf3096

Please sign in to comment.