ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# vlzware's solution

## to Allergies in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

• eggs (1)
• peanuts (2)
• shellfish (4)
• strawberries (8)
• tomatoes (16)
• chocolate (32)
• pollen (64)
• cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

• Whether Tom is allergic to any one of those allergens listed above.
• All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

Jumpstart Lab Warm-up http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_allergies.c

``````#include "vendor/unity.h"
#include "../src/allergies.h"
#include <stdlib.h>

void setUp(void)
{
}

void tearDown(void)
{
}

//helper functions
void list_count_is(int count, allergen_list_t * list)
{
TEST_ASSERT_EQUAL(count, list->count);
}

void list_contains(allergen_t allergen, allergen_list_t * list)
{
bool allergen_found = false;

for (int i = 0; i < list->count; i++) {
if (list->allergens[i] == allergen) {
allergen_found = true;
break;
}
}

TEST_ASSERT_TRUE(allergen_found);
}

//Tests start here
void test_no_allergies_means_not_allergic(void)
{
int score = 0;

TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_PEANUTS, score));
TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_CATS, score));
TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_STRAWBERRIES, score));
}

void test_is_allergic_to_eggs(void)
{
TEST_IGNORE();               // delete this line to run test
int score = 1;

TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_EGGS, score));
}

{
TEST_IGNORE();
int score = 5;

TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_EGGS, score));
TEST_ASSERT_TRUE(is_allergic_to(ALLERGEN_SHELLFISH, score));
TEST_ASSERT_FALSE(is_allergic_to(ALLERGEN_STRAWBERRIES, score));
}

void test_no_allergies_at_all(void)
{
TEST_IGNORE();
int score = 0;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(0, &list);

free(list.allergens);
}

void test_allergic_to_just_eggs(void)
{
TEST_IGNORE();
int score = 1;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(1, &list);
list_contains(ALLERGEN_EGGS, &list);

free(list.allergens);
}

void test_allergic_to_just_peanuts(void)
{
TEST_IGNORE();
int score = 2;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(1, &list);
list_contains(ALLERGEN_PEANUTS, &list);

free(list.allergens);
}

void test_allergic_to_just_strawberries(void)
{
TEST_IGNORE();
int score = 8;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(1, &list);
list_contains(ALLERGEN_STRAWBERRIES, &list);

free(list.allergens);
}

void test_allergic_to_eggs_and_peanuts(void)
{
TEST_IGNORE();
int score = 3;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(2, &list);
list_contains(ALLERGEN_EGGS, &list);
list_contains(ALLERGEN_PEANUTS, &list);

free(list.allergens);
}

void test_allergic_to_more_than_eggs_but_not_peanuts(void)
{
TEST_IGNORE();
int score = 5;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(2, &list);
list_contains(ALLERGEN_EGGS, &list);
list_contains(ALLERGEN_SHELLFISH, &list);

free(list.allergens);
}

void test_allergic_to_lots_of_stuff(void)
{
TEST_IGNORE();
int score = 248;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(5, &list);
list_contains(ALLERGEN_STRAWBERRIES, &list);
list_contains(ALLERGEN_TOMATOES, &list);
list_contains(ALLERGEN_CHOCOLATE, &list);
list_contains(ALLERGEN_POLLEN, &list);
list_contains(ALLERGEN_CATS, &list);

free(list.allergens);
}

void test_allergic_to_everything(void)
{
TEST_IGNORE();
int score = 255;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(8, &list);
list_contains(ALLERGEN_EGGS, &list);
list_contains(ALLERGEN_PEANUTS, &list);
list_contains(ALLERGEN_SHELLFISH, &list);
list_contains(ALLERGEN_STRAWBERRIES, &list);
list_contains(ALLERGEN_TOMATOES, &list);
list_contains(ALLERGEN_CHOCOLATE, &list);
list_contains(ALLERGEN_POLLEN, &list);
list_contains(ALLERGEN_CATS, &list);

free(list.allergens);
}

void test_ignore_non_allergen_score_parts(void)
{
TEST_IGNORE();
int score = 509;
allergen_list_t list;

get_allergens(score, &list);

list_count_is(7, &list);
list_contains(ALLERGEN_EGGS, &list);
list_contains(ALLERGEN_SHELLFISH, &list);
list_contains(ALLERGEN_STRAWBERRIES, &list);
list_contains(ALLERGEN_TOMATOES, &list);
list_contains(ALLERGEN_CHOCOLATE, &list);
list_contains(ALLERGEN_POLLEN, &list);
list_contains(ALLERGEN_CATS, &list);

free(list.allergens);
}

int main(void)
{
UnityBegin("test/test_allergies.c");

RUN_TEST(test_no_allergies_means_not_allergic);
RUN_TEST(test_is_allergic_to_eggs);
RUN_TEST(test_no_allergies_at_all);
RUN_TEST(test_allergic_to_just_eggs);
RUN_TEST(test_allergic_to_just_peanuts);
RUN_TEST(test_allergic_to_just_strawberries);
RUN_TEST(test_allergic_to_eggs_and_peanuts);
RUN_TEST(test_allergic_to_more_than_eggs_but_not_peanuts);
RUN_TEST(test_allergic_to_lots_of_stuff);
RUN_TEST(test_allergic_to_everything);
RUN_TEST(test_ignore_non_allergen_score_parts);

UnityEnd();
return 0;
}``````

### src/allergies.c

``````#include "allergies.h"
#include <stdlib.h>
#include <stdio.h>

bool is_allergic_to(allergen_t allergen, int score)
{
if (score < 0) {
fprintf(stderr, "Invalid score\n");
return false;
}
return (score & (1 << allergen));
}

void get_allergens(int score, allergen_list_t *list)
{
if (score < 0) {
fprintf(stderr, "Invalid score\n");
return;
}

list->allergens =
(allergen_t *) malloc(list->count * sizeof(allergen_t));
if (list->allergens == NULL) {
fprintf(stderr, "Memory error\n");
return;
}

list->count = 0;
int i;
for (i = 0; i <= ALLERGEN_COUNT - 1; i++) {
if ((score & (1 << i))) {
list->allergens[list->count++] = (allergen_t) i;
}
}
}``````

### src/allergies.h

``````#ifndef ALLERGIES_H
#define ALLERGIES_H

#include <stdbool.h>

typedef enum {
ALLERGEN_EGGS = 0,		/* 2^0 = 1 */
ALLERGEN_PEANUTS,		/* 2^1 = 2 */
ALLERGEN_SHELLFISH,		/* 2^2 = 4 */
ALLERGEN_STRAWBERRIES,	/* ... */
ALLERGEN_TOMATOES,
ALLERGEN_CHOCOLATE,
ALLERGEN_POLLEN,
ALLERGEN_CATS,		/* 2^7 = 128 */
ALLERGEN_COUNT
} allergen_t;

typedef struct {
int count;
allergen_t *allergens;
} allergen_list_t;

bool is_allergic_to(allergen_t allergen, int score);
void get_allergens(int score, allergen_list_t *list);

#endif``````