Skip to content

Issue with Allergierss #1636

Open
Open
@BadKarmaZen

Description

@BadKarmaZen

A student posted the following solution to the Allergies exercise (or something along this lines)

public class Allergies
{
    private readonly int _allergy;

    public Allergies(int mask)
    {
        _allergy = mask;
    }

    public bool IsAllergicTo(Allergen allergen)
    {
        return 1 == ((_allergy >> (int)allergen) & 1);
    }

    public Allergen[] List()
    {
        return Enum.GetValues(typeof(Allergen))
            .Cast<Allergen>()
            .Where(IsAllergicTo)
            .ToArray();
    }
}

I believe this solution to be incorrect, but it passes all test.
The problem i see is that the IsAllergicTo function works only in the case that the enum is left untouched.

public enum Allergen
{
    Eggs,
    Peanuts,
    Shellfish,
    Strawberries,
    Tomatoes,
    Chocolate,
    Pollen,
    Cats 
}

I believe that this is caused by not using the enum values in the test but hardcoded values. The following Test failed.

[Fact]
  public void Using_enum_values()
  {
    var sut = new Allergies((int)Allergen.Eggs +  (int)Allergen.Shellfish);
    var expected = new[] { Allergen.Eggs, Allergen.Shellfish };
    Assert.Equal(expected, sut.List());
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions