public void DequeueTest(int[] arg) { GenericQueue <int> queue = new GenericQueue <int>(arg); queue.Dequeue(); queue.Dequeue(); Assert.AreEqual(new int[] { 3, 4 }, queue.ToArray()); }
public void PeekTest(int[] arg) { GenericQueue <int> queue = new GenericQueue <int>(arg); queue.Dequeue(); queue.Dequeue(); Assert.AreEqual(3, queue.Peek()); }
public void GenericQueueTest() { GenericQueue <int> queue = new GenericQueue <int>(); queue.Enqueue(10); queue.Enqueue(11); queue.Enqueue(12); Assert.AreEqual(10, queue.Dequeue()); Assert.AreEqual(11, queue.Dequeue()); Assert.AreEqual(12, queue.Dequeue()); }
public void Enqueue_And_Dequeue_Tests(int[] data1) { GenericQueue <int> test1 = new GenericQueue <int>(data1); test1.Dequeue(); test1.Dequeue(); GenericQueue <int> test2 = new GenericQueue <int>(10); for (int i = 3; i <= 10; i++) { test2.Enqueue(i); } CollectionAssert.AreEqual(test1, test2); }
public void CheckDequeue(int[] array) { var queue = new GenericQueue <int>(array); var element = queue.Dequeue(); Assert.That(element, Is.EqualTo(array[0])); Assert.That(queue.ToArray(), Is.EqualTo(array[1..]));
private static void Main() { var list = new List <Person> { new Person { Age = 20, Name = "Billy" }, new Person { Age = 19, Name = "Van" }, new Person { Age = 33, Name = "Kojima" }, new Person { Age = 4, Name = "Kazuma" }, new Person { Age = 12, Name = "Subaru" } }; var sortedListAsc = list.Sort((a, b) => a != null && a.CompareTo(b) > 0); foreach (var item in sortedListAsc) { Console.WriteLine(item.Age + " - " + item.Name); } Console.WriteLine(); Console.WriteLine("root of n degree: " + NewtonMethod.NewtonMethod.Pow(5, 10)); Console.WriteLine(Math.Pow(10, (double)1 / 5)); var q = new GenericQueue <string>(5); q.Enqueue("First item"); q.Enqueue("Second item"); q.Enqueue("Third item"); q.Enqueue("Fourth item"); Console.WriteLine("Deleted: " + q.Dequeue()); Console.WriteLine("Deleted: " + q.Dequeue()); }
public void GenericQueue_Dequeue_Success() { GenericQueue <int> queue = new GenericQueue <int>(); queue.Enqueue(10); queue.Enqueue(5); int dequeuedItem = queue.Dequeue(); int expected = 10; Assert.AreEqual(expected, dequeuedItem); }
public void GenericQueue_Dequeue_Success() { GenericQueue <string> queue = new GenericQueue <string>(); queue.Enqueue("a"); queue.Enqueue("b"); queue.Enqueue("c"); queue.Enqueue("d"); queue.Enqueue("e"); queue.Enqueue("f"); string dequeuedItem = queue.Dequeue(); string expected = "a"; string[] expected1 = { "b", "c", "d", "e", "f" }; NUnit.Framework.Assert.AreEqual(expected, dequeuedItem); NUnit.Framework.CollectionAssert.AreEqual(queue, expected1); }
static void Main(string[] args) { GenericQueue<int> example = new GenericQueue<int>(); Console.WriteLine(example.QueueEmpty()); example.Enqueue(15); example.Enqueue(25); example.Enqueue(35); example.Enqueue(45); Console.WriteLine(example.QueueEmpty()); Console.WriteLine(example.Dequeue()); Console.WriteLine(example.Dequeue()); example.Enqueue(1); example.Enqueue(1); Console.WriteLine(example.Dequeue()); Console.WriteLine(example.Dequeue()); Console.WriteLine(example.QueueEmpty()); Console.WriteLine(example.Dequeue()); Console.WriteLine(example.Dequeue()); Console.WriteLine(example.QueueEmpty()); Console.WriteLine(example.Dequeue()); }
//method to organize droids by type public bool Organize() { GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>(); GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>(); GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>(); GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>(); GenericQueue<IDroid> droidQueue = new GenericQueue<IDroid>(); //Add droids to appropriate stack types for (int i = 0; i < lengthOfCollection; i++) { try { astromechStack.Add((AstromechDroid)droidCollection[i]); } catch { try { janitorStack.Add((JanitorDroid)droidCollection[i]); } catch { try { utilityStack.Add((UtilityDroid)droidCollection[i]); } catch { try { protocolStack.Add((ProtocolDroid)droidCollection[i]); } catch { return false; } } } } } //Add droids in order to the queue while (astromechStack.Head != null) { droidQueue.Add((IDroid)astromechStack.Pop()); } while (janitorStack.Head != null) { droidQueue.Add((IDroid)janitorStack.Pop()); } while (utilityStack.Head != null) { droidQueue.Add((IDroid)utilityStack.Pop()); } while (protocolStack.Head != null) { droidQueue.Add((IDroid)protocolStack.Pop()); } //Dequeue droids back into the array for (int i = 0; droidQueue.Tail != null; i++) { droidCollection[i] = (IDroid)droidQueue.Dequeue(); } return true; }
/// <summary> /// public method to Sort the droids into categories using a modified bucket sort /// </summary> public void SortIntoCategories() { //Create a generic stack for each type of droid, and pass in the droid type as the generic that will //come through on the stack class as T. GenericStack<ProtocolDroid> protocolStack = new GenericStack<ProtocolDroid>(); GenericStack<UtilityDroid> utilityStack = new GenericStack<UtilityDroid>(); GenericStack<JanitorDroid> janitorStack = new GenericStack<JanitorDroid>(); GenericStack<AstromechDroid> astromechStack = new GenericStack<AstromechDroid>(); //Create a queue to hold the droids as we pop them off the stack. GenericQueue<IDroid> categorizedDroidQueue = new GenericQueue<IDroid>(); //For each IDroid in the droidCollection foreach (IDroid droid in this.droidCollection) { //if the droid is not null we want to process it. If it is null we will go to the else if (droid != null) { //The testing of the droids must occur in this order. It must be done in the order of //most specific droid to least specific. //If we were to test a droid that IS of type Astromech against Utility BEFORE we test against //Astromech, it would pass and be put into the Utility stack and not the Astromech. That is why it //is important to test from most specific to least. //If the droid is an Astromech, push it on the astromech stack if (droid is AstromechDroid) { astromechStack.Push((AstromechDroid)droid); } //Else if it is a JanitorDroid, push it on the janitor stack else if (droid is JanitorDroid) { janitorStack.Push((JanitorDroid)droid); } //Do for Utility else if (droid is UtilityDroid) { utilityStack.Push((UtilityDroid)droid); } //Do for Protocol else if (droid is ProtocolDroid) { protocolStack.Push((ProtocolDroid)droid); } } //The droid we are trying to consider is null, break out of the loop. else { break; } } //Now that the droids are all in thier respective stacks we can do the work //of poping them off of the stacks and adding them to the queue. //It is required that they be popped off from each stack in this order so that they have //the correct order going into the queue. //This is a primer pop. It gets the first droid off the stack, which could be null if the stack is empty AstromechDroid currentAstromechDroid = astromechStack.Pop(); //While the droid that is popped off is not null while (currentAstromechDroid != null) { //Add the popped droid to the queue. categorizedDroidQueue.Enqueue(currentAstromechDroid); //Pop off the next droid for the loop test currentAstromechDroid = astromechStack.Pop(); } //See above method for Astromech. It is the same except for Janitor JanitorDroid currentJanitorDroid = janitorStack.Pop(); while (currentJanitorDroid != null) { categorizedDroidQueue.Enqueue(currentJanitorDroid); currentJanitorDroid = janitorStack.Pop(); } //See above method for Astromech. It is the same except for Utility UtilityDroid currentUtilityDroid = utilityStack.Pop(); while (currentUtilityDroid != null) { categorizedDroidQueue.Enqueue(currentUtilityDroid); currentUtilityDroid = utilityStack.Pop(); } //See above method for Astromech. It is the same except for Protocol ProtocolDroid currentProtocolDroid = protocolStack.Pop(); while (currentProtocolDroid != null) { categorizedDroidQueue.Enqueue(currentProtocolDroid); currentProtocolDroid = protocolStack.Pop(); } //Now that the droids have all been removed from the stacks and put into the queue //we need to dequeue them all and put them back into the original array. //Set a int counter to 0. int counter = 0; //This is a primer dequeue that will get the first droid out of the queue. IDroid iDroid = categorizedDroidQueue.Dequeue(); //While the dequeued droid is not null. while (iDroid != null) { //Add the droid to the droid collection using the int counter as the index this.droidCollection[counter] = iDroid; //increment the counter counter++; //dequeue the next droid off the queue so it can be used in the while condition iDroid = categorizedDroidQueue.Dequeue(); } //set the lenght of the collection to the value of the counter. It should be the same, but in case it changed. this.lengthOfCollection = counter; }
public void Dequeue_NotAddingAnyIntegersToQueue_ThrowsException() { Assert.Throws <IndexOutOfRangeException>(() => _genericQueue.Dequeue()); }
public void SortByModel() { //Create generic stacks for each model type GenericStack<Droid> protocolStack = new GenericStack<Droid>(); GenericStack<Droid> utilityStack = new GenericStack<Droid>(); GenericStack<Droid> janitorStack = new GenericStack<Droid>(); GenericStack<Droid> astromechStack = new GenericStack<Droid>(); //Create new generic queue GenericQueue<Droid> droidQueue = new GenericQueue<Droid>(); foreach (Droid droid in droidArray) { if (droid != null) { switch (droid.GetModel()) { case "Protocol": protocolStack.Push(droid); break; case "Utility": utilityStack.Push(droid); break; case "Janitor": janitorStack.Push(droid); break; case "Astromech": astromechStack.Push(droid); break; default: break; } } } //Get droids from each stack and enqueue them while (astromechStack.IsNotEmpty()) { droidQueue.Enqueue(astromechStack.Pop()); } while (janitorStack.IsNotEmpty()) { droidQueue.Enqueue(janitorStack.Pop()); } while (utilityStack.IsNotEmpty()) { droidQueue.Enqueue(utilityStack.Pop()); } while (protocolStack.IsNotEmpty()) { droidQueue.Enqueue(protocolStack.Pop()); } int i = 0; while (droidQueue.IsNotEmpty()) { droidArray[i] = droidQueue.Dequeue(); i++; } }
public void SortbyModel() { GenericStack<ProtocolDroid> ProtocolStack = new GenericStack<ProtocolDroid>(); GenericStack<UtilityDroid> UtilityStack = new GenericStack<UtilityDroid>(); GenericStack<JanitorDroid> JanitorStack = new GenericStack<JanitorDroid>(); GenericStack<AstromechDroid> AstromechStack = new GenericStack<AstromechDroid>(); GenericQueue<Droid> DroidsQueue = new GenericQueue<Droid>(); foreach (IDroid droid in droidCollection) { if (droid is ProtocolDroid) { ProtocolStack.Push((ProtocolDroid)droid); } else if (droid is UtilityDroid) { UtilityStack.Push((UtilityDroid)droid); } else if (droid is JanitorDroid) { JanitorStack.Push((JanitorDroid)droid); } else if (droid is AstromechDroid) { AstromechStack.Push((AstromechDroid)droid); } } while (ProtocolStack != null) { DroidsQueue.Enqueue(ProtocolStack.Pop()); } while (UtilityStack != null) { DroidsQueue.Enqueue(UtilityStack.Pop()); } while (JanitorStack != null) { DroidsQueue.Enqueue(JanitorStack.Pop()); } while (AstromechStack != null) { DroidsQueue.Enqueue(AstromechStack.Pop()); } while (DroidsQueue != null) { int i =0; droidCollection[i] = DroidsQueue.Dequeue(); i++; } }
public void GenericQueue_Empty_InvalidOperationException() { GenericQueue <int> queue = new GenericQueue <int>(); Assert.Throws <InvalidOperationException>(() => queue.Dequeue()); }