[ISBN Verifier] Need one more test

When i finishied exercise i found it lack one test.

I thnik need one test like example below.

#[test]
fn invalid_isbn_with_special_character_in_the_middle() {
    assert!(!is_valid_isbn("3-598-2150?8-8"));
}

I will glad if someone can add this to exercise.

[edit]

The exercise checks how well and optimally you can parse a string to check if an ISBN is correct.

Important things are:

  • The string can have multiple dashes or zero dashes. Your parser needs to omit all the dashes.
  • If the digits are separated by something other than dashes, the ISBN is invalid.

The code below shows an example of how you can omit all characters that are not digits and pass all the tests.

pub fn is_valid_isbn(isbn: &str) -> bool {
    let mut acc = 0;
    let mut factor = 10;
    for character in isbn.chars() {
        if factor == 0 { return false; }
        if let Some(n) = character.to_digit(10) {
            acc += n * factor;
            factor -= 1;
        } else if character == 'X' && factor == 1 {
            acc += 10;
            factor -= 1;
        }
    }
    factor == 0 && acc % 11 == 0
}

If you add test i give at the beginning the code will not pass. And I believe the important thing in the exercise is how nicely you can parse the string. And this code in my opinion is wrong.

  1. Can you explain why you think the existing tests are insufficient and what the additional test would add?
  2. Have you read Suggesting Exercise Improvements | Exercism's Docs ?

I edit my post. I have hope i did good job now with it. The post is enough or i must do somethnig else to submit change?

Don’t we already have tests for those? The canonical data shows tests with zero and multiple dashes for valid ISBNs. The canonical data shows tests with something other than dashes, where the ISBN is invalid. What does your test add that isn’t already in the tests?

But there doesn’t exist a test to check if the correct numbers are separated by a special character, then the ISBN is not correct.

Why does the exercise exist when you can check only the digit and don’t care if there is any difference between dashes and special characters?

I give code to show how you can omit difference between dashes and special characters and dont use fillter() or if to separate it.

The canonical data has tests for an invalid character, "P". Is "?" more “special” than "P" in any way? Is there any reason they should be treated differently in any way?

The test data includes inputs "3-598-P1581-X", "3132P34035", "3598P215088". I’m failing to understand what the proposed test adds which isn’t already covered.

If someone omits all the characters that are not numbers, then this tests passes because the number of digits is not ten, but less or more.

However, if you provide a correct ISBN with 10 numbers and separate it by ‘P’ or ‘?’, then the code doesn’t pass. This is what my test checks.

What I mean is, my test checks for a correct ISBN separated by a non-digit character. None of the tests check for this.

I think you are suggesting that there should be a test which uses a valid 10-digit ISBN plus an 11th character added which is a non-digit. Is that correct?

The third test, 3598P215088, contains 10 digits with a non-digit separator, P. If you remove the P, the input is a valid ISBN value.

Wow u are correct i didint check canonical data
But that mean someone dont add this test to site in RUST exercises

It’s entirely possible the Rust track isn’t in sync with the problem specs. You can post a thread and ask if the Rust maintainer would appreciate PRs to sync exercises!