use ciphers::{Cipher, CipherInputError, PolybiusSquare};
#[test]
fn encipher_abcde_small() {
let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ABCDE");
let ctext = ps.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
assert_eq!(
ctext.unwrap(),
"CEBCCDBCCBCEEBABBCBCBDEAEBEDBDCACACCCDEBABBCDDBDEAEBCABC"
);
}
#[test]
fn decipher_abcde_small() {
let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ABCDE");
let ptext = ps.decipher("CEBCCDBCCBCEEBABBCBCBDEAEBEDBDCACACCCDEBABBCDDBDEAEBCABC");
assert_eq!(ptext.unwrap(), "DEFENDTHEEASTWALLOFTHECASTLE");
}
#[test]
fn encipher_zebra_small() {
let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ZEBRA");
let ctext = ps.encipher("DEFENDTHEEASTWALLOFTHECASTLE");
assert_eq!(
ctext.unwrap(),
"BAEBBREBBEBAAEZEEBEBERAZAEARERBZBZBBBRAEZEEBRRERAZAEBZEB"
);
}
#[test]
fn decipher_zebra_small() {
let ps = PolybiusSquare::new("PHQGIUMEAYLNOFDXKRCVSTZWB", "ZEBRA");
let ptext = ps.decipher("BAEBBREBBEBAAEZEEBEBERAZAEARERBZBZBBBRAEZEEBRRERAZAEBZEB");
assert_eq!(ptext.unwrap(), "DEFENDTHEEASTWALLOFTHECASTLE");
}
#[test]
fn encipher_abcde_large() {
let ps = PolybiusSquare::new("ZYXWVUTSRQPONMLKIHGFEDCBA", "ABCDE");
let ctext = ps.encipher("ABCDEFGHIIKLMNOPQRSTUVWXYZABCDEFGHIIKLMNOPQRSTUVWXYZ");
assert_eq!(
ctext.unwrap(),
"EEEDECEBEADEDDDCDBDBDACECDCCCBCABEBDBCBBBAAEADACABAAEEEDECEBEADEDDDCDBDBDACECDCCCBCABEBDBC\
BBBAAEADACABAA"
);
}
#[test]
fn decipher_abcde_large() {
let ps = PolybiusSquare::new("ZYXWVUTSRQPONMLKIHGFEDCBA", "ABCDE");
let ptext = ps.decipher(
"EEEDECEBEADEDDDCDBDBDACECDCCCBCABEBDBCBBBAAEADACABAAEEEDECEBEADEDDDCDBDBDACECDCCCBCABEBDBC\
BBBAAEADACABAA"
);
assert_eq!(
ptext.unwrap(),
"ABCDEFGHIIKLMNOPQRSTUVWXYZABCDEFGHIIKLMNOPQRSTUVWXYZ"
);
}
#[test]
fn encipher_zebra_large() {
let ps = PolybiusSquare::new("ZYXWVUTSRQPONMLKIHGFEDCBA", "ZEBRA");
let ctext = ps.encipher("ABCDEFGHIIKLMNOPQRSTUVWXYZABCDEFGHIIKLMNOPQRSTUVWXYZ");
assert_eq!(
ctext.unwrap(),
"AAARABAEAZRARRRBRERERZBABRBBBEBZEAEREBEEEZZAZRZBZEZZAAARABAEAZRARRRBRERERZBABRBBBEBZEAEREB\
EEEZZAZRZBZEZZ"
);
}
#[test]
fn decipher_zebra_large() {
let ps = PolybiusSquare::new("ZYXWVUTSRQPONMLKIHGFEDCBA", "ZEBRA");
let ptext = ps.decipher(
"AAARABAEAZRARRRBRERERZBABRBBBEBZEAEREBEEEZZAZRZBZEZZAAARABAEAZRARRRBRERERZBABRBBBEBZEAEREB\
EEEZZAZRZBZEZZ"
);
assert_eq!(
ptext.unwrap(),
"ABCDEFGHIIKLMNOPQRSTUVWXYZABCDEFGHIIKLMNOPQRSTUVWXYZ"
);
}
#[test]
fn encipher_lowercase() {
let ps = PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcde");
let ctext = ps.encipher("defendtheeastwallofthecastle");
assert_eq!(
ctext.unwrap(),
"cebccdbccbceebabbcbcbdeaebedbdcacacccdebabbcddbdeaebcabc"
);
}
#[test]
fn decipher_lowercase() {
let ps = PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcde");
let ptext = ps.decipher("cebccdbccbceebabbcbcbdeaebedbdcacacccdebabbcddbdeaebcabc");
assert_eq!(ptext.unwrap(), "defendtheeastwallofthecastle");
}
#[test]
#[should_panic]
fn key_non_ascii() {
PolybiusSquare::new("phqgiumèaylnofdxkrcvstzwb", "abcde");
}
#[test]
#[should_panic]
fn chars_non_ascii() {
PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcdè");
}
#[test]
#[should_panic]
fn key_repeated_chars() {
PolybiusSquare::new("phqgiomeaylnofdxkrcvstzwb", "abcde");
}
#[test]
#[should_panic]
fn chars_repeated_chars() {
PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "accde");
}
#[test]
#[should_panic]
fn key_wrong_length() {
PolybiusSquare::new("phqgiumeaylnofdxkrcvstzw", "abcde");
}
#[test]
#[should_panic]
fn chars_wrong_length() {
PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcd");
}
#[test]
fn ptext_not_in_key() {
let ps = PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcde");
let ctext = ps.encipher("djfendtheeastwallofthecasjle");
assert_eq!(ctext, Err(CipherInputError::NotInAlphabet));
}
#[test]
fn ctext_not_in_chars() {
let ps = PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcde");
let ptext = ps.decipher("cebccdbccbceebabbcbcbdeazbedbdcacacccdebabbcddbdeaebcabc");
assert_eq!(ptext, Err(CipherInputError::NotInAlphabet));
}
#[test]
fn ctext_uneven_chars() {
let ps = PolybiusSquare::new("phqgiumeaylnofdxkrcvstzwb", "abcde");
let ptext = ps.decipher("cebccdbccbceebabbcbcbdeaebedbdcacacccdebabbcddbdeaebcac");
assert_eq!(
ptext,
Err(CipherInputError::BadInput(String::from(
"`ctext` must contain an even number of chars"
)))
);
}