Last active
January 12, 2023 04:52
-
-
Save mbeidler/9e9f566ba7356302c57e to your computer and use it in GitHub Desktop.
Priority Queue Contains Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PriorityQueueTest | |
{ | |
[Fact] | |
public void ContainsOnlyEnqueuedItems() | |
{ | |
// Arrange | |
var jdoe = new Patient { Id = 3, Triage = 1 }; | |
var jsmith = new Patient { Id = 5, Triage = 3 }; | |
var unqueued = new Patient { Id = 7, Triage = 3 }; | |
var sut = new PriorityQueue<Patient>(Patient.TriageComparer); | |
sut.Enqueue(jdoe); | |
sut.Enqueue(jsmith); | |
// Act && Assert | |
Assert.True(sut.Contains(jdoe)); | |
Assert.True(sut.Contains(jsmith)); | |
Assert.False(sut.Contains(unqueued)); | |
} | |
private class Patient | |
{ | |
public static readonly IComparer<Patient> TriageComparer = | |
new ByTriage(); | |
public int Id { get; set; } | |
public string MRN { get; set; } | |
public int Triage { get; set; } | |
public override bool Equals(object obj) | |
{ | |
if (ReferenceEquals(this, obj)) return true; | |
var other = obj as Patient; | |
if (other == null) return false; | |
return Id.Equals(other.Id); | |
} | |
public override int GetHashCode() | |
{ | |
return Id.GetHashCode(); | |
} | |
private class ByTriage : IComparer<Patient> | |
{ | |
public int Compare(Patient x, Patient y) | |
{ | |
if (x == null && y == null) return 0; | |
if (x == null) return -1; | |
if (y == null) return 1; | |
return x.Triage - y.Triage; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment