Open
Description
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
Labels
No labels