Skip to content

Commit

Permalink
Base allergies
Browse files Browse the repository at this point in the history
  • Loading branch information
vhgcuong committed Oct 11, 2023
1 parent d21fecd commit c12d442
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/allergies.rs
Original file line number Diff line number Diff line change
@@ -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<Allergen> {
todo!("Return the list of allergens contained within the score with which the Allergies struct was made.");
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
119 changes: 119 additions & 0 deletions tests/allergies/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions tests/libtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ mod parallel_letter_frequency;
mod rna_transcription;
mod binary_search;
mod spiral_matrix;
mod allergies;

0 comments on commit c12d442

Please sign in to comment.