Stuck on Face ID 2.0

I just can’t get tests 7 and 8 to pass (7 is returning True instead of the expected False, 8 is returning False instead of the expected True), can anyone assist? I tried to post in Discord, but the code goes over the character limit there, and I don’t want to leave any out, as I’m not sure what’s causing the issue.

public class FacialFeatures
{
    public string EyeColor { get; }
    public decimal PhiltrumWidth { get; }
    
    public FacialFeatures(string eyeColor, decimal philtrumWidth)
    {
        EyeColor = eyeColor;
        PhiltrumWidth = philtrumWidth;
    }
    
    // TODO: implement equality and GetHashCode() methods
    
    public override bool Equals(object obj) => this.Equals(obj as FacialFeatures);
    public bool Equals(FacialFeatures face)
    {
        return EyeColor == face.EyeColor && PhiltrumWidth == face.PhiltrumWidth;        
    }
    public int GetHashCode()
    {
        return (EyeColor, PhiltrumWidth).GetHashCode();
    }
}

public class Identity
{
    public string Email { get; }
    public FacialFeatures FacialFeatures { get; }

    public Identity(string email, FacialFeatures facialFeatures)
    {
        Email = email;
        FacialFeatures = facialFeatures;
    }
    
    // TODO: implement equality and GetHashCode() methods
    
    public override bool Equals(object obj) => this.Equals(obj as Identity); 
    public bool Equals(Identity ident)
    {
        return Email == ident.Email && FacialFeatures.Equals(ident.FacialFeatures);
    }
    public int GetHashCode()
    {
        return (Email, FacialFeatures).GetHashCode();
    }
}

public class Authenticator
{
    private readonly HashSet<Identity> uniqueFaces = new HashSet<Identity>();
    
    public static bool AreSameFace(FacialFeatures faceA, FacialFeatures faceB) => faceA.Equals(faceB);
    
    public bool IsAdmin(Identity identity) 
    {
        var adminIdent = new Identity("admin@exerc.ism", new FacialFeatures("green", 0.9m));
        return identity.Equals(adminIdent);        
    }

    public bool Register(Identity identity) => uniqueFaces.Contains(identity) ? false : uniqueFaces.Add(identity);
       
    public bool IsRegistered(Identity identity) => uniqueFaces.Contains(identity);
    
    public static bool AreSameObject(Identity identityA, Identity identityB)
    {
        return System.Object.ReferenceEquals(identityA, identityB);
    }
}

What do you get when you run the tests? (Please do not use an image to share results.)

Test 7 is as follows:

var authenticator = new Authenticator();
authenticator.Register(new Identity("tunde@thecompetition.com", new FacialFeatures("blue", 0.9m)));
Assert.False(authenticator.Register(new Identity("tunde@thecompetition.com", new FacialFeatures("blue", 0.9m))));

with result:

Assert.False() Failure
Expected: False
Actual:   True

Test 8 is as follows:

var authenticator = new Authenticator();
authenticator.Register(new Identity("alice@thecompetition.com", new FacialFeatures("blue", 0.9m)));
Assert.True(authenticator.IsRegistered(new Identity("alice@thecompetition.com", new FacialFeatures("blue", 0.9m))));

with result:

Assert.True() Failure
Expected: True
Actual:   False
  1. Looking at the CODE RUN section, do you understand what this test is doing?
  2. Looking at the CODE RUN section, do you understand what this test is expecting and why?
  3. Looking at the TEST FAILURE section, do you understand what your code returns and how it differs?
  4. Looking at your code, do you understand why your code is returning what it returns?

Test 7:

  1. The test is calling the Register() method, then calling it again with exactly the same parameters.
  2. The test is expecting False, due to the identity already existing.
  3. My code returns True, so is successfully registering the details again.
  4. I do not understand why my code doesn’t return False.

Test 8:

  1. The test is calling the Register() method, then calling the IsRegistered() method to check if the identity is registered.
  2. The test is expecting True, as the identity should be registered.
  3. My code returns False, so either the identity did not register, or my IsRegistered() code is not checking registration correctly.
  4. I don’t understand why my code doesn’t return True.

@rwebb13 Do you know how HashSet compares objects? This should tell you, why .Contains() doesn’t do what you expect it to do.

Thanks, your nudge plus one of my monthly ChatGPT-4 uses got me there.

2 Likes