Finish ISBN verifier
This commit is contained in:
parent
603fbcef5f
commit
c7950bda2d
2 changed files with 17 additions and 19 deletions
|
@ -1,4 +1,20 @@
|
||||||
/// Determines whether the supplied string is a valid ISBN number
|
/// Determines whether the supplied string is a valid ISBN number
|
||||||
pub fn is_valid_isbn(isbn: &str) -> bool {
|
pub fn is_valid_isbn(isbn: &str) -> bool {
|
||||||
unimplemented!("Is {:?} a valid ISBN number?", isbn);
|
if isbn.len() < 10 { return false; }
|
||||||
|
let mut multiplier:i8 = 10;
|
||||||
|
let mut output = 0;
|
||||||
|
for char in isbn.chars(){
|
||||||
|
match char.to_digit(10){
|
||||||
|
Some(value) =>{
|
||||||
|
output += value * multiplier as u32;
|
||||||
|
multiplier -= 1;
|
||||||
|
},
|
||||||
|
None =>{
|
||||||
|
if char.to_ascii_lowercase() == 'x' && multiplier == 1{
|
||||||
|
output += 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output % 11 == 0 && multiplier >= 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,112 +6,94 @@ fn test_valid() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_check_digit() {
|
fn test_invalid_check_digit() {
|
||||||
assert!(!is_valid_isbn("3-598-21508-9"));
|
assert!(!is_valid_isbn("3-598-21508-9"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_valid_check_digit_of_10() {
|
fn test_valid_check_digit_of_10() {
|
||||||
assert!(is_valid_isbn("3-598-21507-X"));
|
assert!(is_valid_isbn("3-598-21507-X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_character_as_check_digit() {
|
fn test_invalid_character_as_check_digit() {
|
||||||
assert!(!is_valid_isbn("3-598-21507-A"));
|
assert!(!is_valid_isbn("3-598-21507-A"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_character_in_isbn() {
|
fn test_invalid_character_in_isbn() {
|
||||||
assert!(!is_valid_isbn("3-598-P1581-X"));
|
assert!(!is_valid_isbn("3-598-P1581-X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
fn test_invalid_isbn_with_invalid_X() {
|
fn test_invalid_isbn_with_invalid_X() {
|
||||||
assert!(!is_valid_isbn("3-598-2X507-9"));
|
assert!(!is_valid_isbn("3-598-2X507-9"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_valid_isbn_without_dashes() {
|
fn test_valid_isbn_without_dashes() {
|
||||||
assert!(is_valid_isbn("3598215088"));
|
assert!(is_valid_isbn("3598215088"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
fn test_valid_isbn_without_dashes_and_X_as_check() {
|
fn test_valid_isbn_without_dashes_and_X_as_check() {
|
||||||
assert!(is_valid_isbn("359821507X"));
|
assert!(is_valid_isbn("359821507X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_isbn_without_dashes_and_no_check_digit() {
|
fn test_invalid_isbn_without_dashes_and_no_check_digit() {
|
||||||
assert!(!is_valid_isbn("359821507"));
|
assert!(!is_valid_isbn("359821507"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_isbn_without_dashes_and_too_long() {
|
fn test_invalid_isbn_without_dashes_and_too_long() {
|
||||||
assert!(!is_valid_isbn("3598215078X"));
|
assert!(!is_valid_isbn("3598215078X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn too_short_isbn() {
|
fn too_short_isbn() {
|
||||||
assert!(!is_valid_isbn("00"));
|
assert!(!is_valid_isbn("00"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_invalid_isbn_without_check_digit() {
|
fn test_invalid_isbn_without_check_digit() {
|
||||||
assert!(!is_valid_isbn("3-598-21507"));
|
assert!(!is_valid_isbn("3-598-21507"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_valid_digits_invalid_length() {
|
fn test_valid_digits_invalid_length() {
|
||||||
assert!(!is_valid_isbn("35982150881"));
|
assert!(!is_valid_isbn("35982150881"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn test_special_characters() {
|
fn test_special_characters() {
|
||||||
assert!(!is_valid_isbn("!@#%!@"));
|
assert!(!is_valid_isbn("!@#%!@"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
fn test_invalid_isbn_with_check_digit_X_instead_of_0() {
|
fn test_invalid_isbn_with_check_digit_X_instead_of_0() {
|
||||||
assert!(!is_valid_isbn("3-598-21515-X"));
|
assert!(!is_valid_isbn("3-598-21515-X"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn empty_isbn() {
|
fn empty_isbn() {
|
||||||
assert!(!is_valid_isbn(""));
|
assert!(!is_valid_isbn(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn input_is_9_characters() {
|
fn input_is_9_characters() {
|
||||||
assert!(!is_valid_isbn("134456729"));
|
assert!(!is_valid_isbn("134456729"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn invalid_characters_are_not_ignored() {
|
fn invalid_characters_are_not_ignored() {
|
||||||
assert!(!is_valid_isbn("3132P34035"));
|
assert!(!is_valid_isbn("3132P34035"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
|
||||||
fn too_long_but_contains_a_valid_isbn() {
|
fn too_long_but_contains_a_valid_isbn() {
|
||||||
assert!(!is_valid_isbn("98245726788"));
|
assert!(!is_valid_isbn("98245726788"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue