public void AnimalShelterTest() { AnimalShelter s = new AnimalShelter(); s.Enqueue(newDog("toto")); s.Enqueue(newDog("barney")); s.Enqueue(newCat("boris")); s.Enqueue(newCat("hans")); var a = s.DequeueAny(); Assert.AreEqual(a.Name, "toto"); a = s.DequeueCat(); Assert.AreEqual(a.Name, "boris"); a = s.DequeueAny(); Assert.AreEqual(a.Name, "barney"); Assert.ThrowsException <InvalidOperationException>(() => s.DequeueDog()); a = s.DequeueAny(); Assert.AreEqual(a.Name, "hans"); Assert.ThrowsException <InvalidOperationException>(() => s.DequeueAny()); }
public void AnimalShelterTest() { var shelter = new AnimalShelter(); shelter.Enqueue(new Dog(4)); shelter.Enqueue(new Cat(3)); shelter.Enqueue(new Cat(6)); shelter.Enqueue(new Cat(5)); Assert.AreEqual(6, shelter.DequeueAny().Age); Assert.AreEqual(5, shelter.DequeueAny().Age); Assert.AreEqual(4, shelter.DequeueAny().Age); Assert.AreEqual(3, shelter.DequeueAny().Age); }
private static void DequeueAndValidateAnimals <T>(AnimalShelter shelter, params int[] ids) where T : Animal { foreach (var id in ids) { var animal = shelter.DequeueAny(); Assert.AreEqual(typeof(T), animal.GetType()); Assert.AreEqual(id, animal.Id); } }
public static void AnimalShelterMy() { AnimalShelter ash = new AnimalShelter(); ash.Enque(new Animal(Type.Dog, 1)); ash.Enque(new Animal(Type.Cat, 2)); ash.Enque(new Animal(Type.Cat, 3)); ash.Enque(new Animal(Type.Dog, 4)); ash.Enque(new Animal(Type.Cat, 5)); Console.WriteLine(ash.DequeueAny()); Console.WriteLine(ash.DequeCat()); Console.WriteLine(ash.DequeDog()); Console.WriteLine(ash.DequeCat()); Console.WriteLine(ash.DequeDog()); }
public void InvalidInputTest() { var shelter = new AnimalShelter(); // Dequeuing doesn't work when there are no animals TestHelpers.AssertExceptionThrown(() => shelter.DequeueAny(), typeof(InvalidOperationException)); TestHelpers.AssertExceptionThrown(() => shelter.DequeueCat(), typeof(InvalidOperationException)); TestHelpers.AssertExceptionThrown(() => shelter.DequeueDog(), typeof(InvalidOperationException)); // Dequeuing doesn't work when there are only animals of the other type EnqueueDogs(shelter, 1); TestHelpers.AssertExceptionThrown(() => shelter.DequeueCat(), typeof(InvalidOperationException)); DequeueAndValidateDogs(shelter, 1); EnqueueCats(shelter, 2); TestHelpers.AssertExceptionThrown(() => shelter.DequeueDog(), typeof(InvalidOperationException)); DequeueAndValidateCats(shelter, 2); // Enqueuing doesn't work for unknown animals var elephant = new Elephant(3); TestHelpers.AssertExceptionThrown(() => shelter.Enqueue(elephant), typeof(InvalidOperationException)); }
public void TestAnimalShelterMethods() { // Arrange AnimalShelter emptyShelter = new AnimalShelter(); AnimalShelter noCatsShelter = new AnimalShelter(); AnimalShelter noDogsShelter = new AnimalShelter(); AnimalShelter shelter = new AnimalShelter(); Dog d1 = new Dog(); Cat c1 = new Cat(); Cat c2 = new Cat(); Dog d2 = new Dog(); Dog d3 = new Dog(); Cat c3 = new Cat(); // Act Animal emptyShelterDequeueAnyResult = emptyShelter.DequeueAny(); noCatsShelter.Enqueue(new Dog()); Cat noCatsShelterDequeueCatResult = noCatsShelter.DequeueCat(); noDogsShelter.Enqueue(new Cat()); Dog noDogsShelterDequeueDogResult = noDogsShelter.DequeueDog(); shelter.Enqueue(d1); shelter.Enqueue(c1); shelter.Enqueue(c2); shelter.Enqueue(d2); shelter.Enqueue(d3); shelter.Enqueue(c3); shelter.DequeueCat(); shelter.DequeueAny(); shelter.DequeueDog(); // Assert Assert.IsNull(emptyShelterDequeueAnyResult); Assert.IsNull(noCatsShelterDequeueCatResult); Assert.IsNull(noDogsShelterDequeueDogResult); Animal currAnimal = shelter.NextAnimal; Assert.AreEqual(c2, currAnimal); currAnimal = currAnimal.NextAnimal; Assert.AreEqual(d3, currAnimal); currAnimal = currAnimal.NextAnimal; Assert.AreEqual(c3, currAnimal); currAnimal = currAnimal.NextAnimal; Assert.IsNull(currAnimal); Dog currDog = shelter.NextDog; Assert.AreEqual(d3, currDog); currDog = currDog.NextDog; Assert.IsNull(currDog); Cat currCat = shelter.NextCat; Assert.AreEqual(c2, currCat); currCat = currCat.NextCat; Assert.AreEqual(c3, currCat); currCat = currCat.NextCat; Assert.IsNull(currCat); }