diff --git a/src/allergies.rs b/src/allergies.rs new file mode 100644 index 0000000..a2fd308 --- /dev/null +++ b/src/allergies.rs @@ -0,0 +1,27 @@ +pub struct Allergies; + +#[derive(Debug, PartialEq, Eq)] +pub enum Allergen { + Eggs, + Peanuts, + Shellfish, + Strawberries, + Tomatoes, + Chocolate, + Pollen, + Cats, +} + +impl Allergies { + pub fn new(score: u32) -> Self { + todo!("Given the '{score}' score, construct a new Allergies struct."); + } + + pub fn is_allergic_to(&self, allergen: &Allergen) -> bool { + todo!("Determine if the patient is allergic to the '{allergen:?}' allergen."); + } + + pub fn allergies(&self) -> Vec { + todo!("Return the list of allergens contained within the score with which the Allergies struct was made."); + } +} diff --git a/src/lib.rs b/src/lib.rs index 1eed0b8..7f457ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ pub mod sublist; pub mod anagram; pub mod acronym; pub mod luhn_from; +pub mod allergies; pub mod luhn_trait; pub mod custom_set; pub mod palindrome; diff --git a/tests/allergies/mod.rs b/tests/allergies/mod.rs new file mode 100644 index 0000000..9756b0f --- /dev/null +++ b/tests/allergies/mod.rs @@ -0,0 +1,119 @@ +use exercism::allergies::*; +fn compare_allergy_vectors(expected: &[Allergen], actual: &[Allergen]) { + for element in expected { + if !actual.contains(element) { + panic!("Allergen missing\n {element:?} should be in {actual:?}"); + } + } + if actual.len() != expected.len() { + panic!( + "Allergy vectors are of different lengths\n expected {expected:?}\n got {actual:?}" + ); + } +} +#[test] +fn is_not_allergic_to_anything() { + let allergies = Allergies::new(0); + assert!(!allergies.is_allergic_to(&Allergen::Peanuts)); + assert!(!allergies.is_allergic_to(&Allergen::Cats)); + assert!(!allergies.is_allergic_to(&Allergen::Strawberries)); +} +#[test] +#[ignore] +fn is_allergic_to_eggs() { + assert!(Allergies::new(1).is_allergic_to(&Allergen::Eggs)); +} +#[test] +#[ignore] +fn is_allergic_to_eggs_and_shellfish_but_not_strawberries() { + let allergies = Allergies::new(5); + assert!(allergies.is_allergic_to(&Allergen::Eggs)); + assert!(allergies.is_allergic_to(&Allergen::Shellfish)); + assert!(!allergies.is_allergic_to(&Allergen::Strawberries)); +} +#[test] +#[ignore] +fn no_allergies_at_all() { + let expected = &[]; + let allergies = Allergies::new(0).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_just_eggs() { + let expected = &[Allergen::Eggs]; + let allergies = Allergies::new(1).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_just_peanuts() { + let expected = &[Allergen::Peanuts]; + let allergies = Allergies::new(2).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_just_strawberries() { + let expected = &[Allergen::Strawberries]; + let allergies = Allergies::new(8).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_eggs_and_peanuts() { + let expected = &[Allergen::Eggs, Allergen::Peanuts]; + let allergies = Allergies::new(3).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_eggs_and_shellfish() { + let expected = &[Allergen::Eggs, Allergen::Shellfish]; + let allergies = Allergies::new(5).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_many_things() { + let expected = &[ + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(248).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn allergic_to_everything() { + let expected = &[ + Allergen::Eggs, + Allergen::Peanuts, + Allergen::Shellfish, + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(255).allergies(); + compare_allergy_vectors(expected, &allergies); +} +#[test] +#[ignore] +fn scores_over_255_do_not_trigger_false_positives() { + let expected = &[ + Allergen::Eggs, + Allergen::Shellfish, + Allergen::Strawberries, + Allergen::Tomatoes, + Allergen::Chocolate, + Allergen::Pollen, + Allergen::Cats, + ]; + let allergies = Allergies::new(509).allergies(); + compare_allergy_vectors(expected, &allergies); +} diff --git a/tests/libtest.rs b/tests/libtest.rs index 0853686..38ec166 100644 --- a/tests/libtest.rs +++ b/tests/libtest.rs @@ -36,3 +36,4 @@ mod parallel_letter_frequency; mod rna_transcription; mod binary_search; mod spiral_matrix; +mod allergies;