Exemplo n.º 1
0
        public void CanContains()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);
            q.Enqueue(4);

            Assert.False(q.Contains(1));
            Assert.True(q.Contains(2));
            Assert.True(q.Contains(3));
            Assert.True(q.Contains(4));
            Assert.False(q.Contains(5));

            var qq = new CappedQueue <string>(3);

            qq.Enqueue("one");
            qq.Enqueue("two");
            qq.Enqueue(null);

            Assert.True(qq.Contains("one"));
            Assert.True(qq.Contains("two"));
            Assert.True(qq.Contains(null));
            Assert.False(qq.Contains("three"));
        }
	    public void Foo2()
		{
			var queue = new CappedQueue<int>(5);
			
			queue.Enqueue(10);
			queue.Enqueue(20);
			queue.Enqueue(30);
			
			var average = queue.Average(x=>x);
		}
Exemplo n.º 3
0
	    public void Foo2()
		{
			var queue = new CappedQueue<int>(5);
			
			queue.Enqueue(10);
			queue.Enqueue(20);
			queue.Enqueue(30);
			
			var average = queue.Sum() / queue.Count();
		}
	    public void Foo()
		{
			var queue = new CappedQueue<DateTime>(5);
			
			queue.Enqueue(DateTime.Now); // each time the user press the key
			queue.Enqueue(DateTime.Now); // each time the user press the key
			queue.Enqueue(DateTime.Now); // each time the user press the key
			
			
			TimeSpan diff = (queue.Last() / queue.Fisrt())/queue.Count();
		}
Exemplo n.º 5
0
        public void CanCopyTo()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            var array = new int[5];

            q.CopyTo(array, 2);
            const int zero = default(int);

            Assert.Equal(Seq(zero, zero, 1, 2, 3), array);
        }
Exemplo n.º 6
0
        public void CanClear()
        {
            var q = new CappedQueue <int>(3);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);
            q.Enqueue(4);

            q.Clear();
            q.Clear();             // idempotent

            Assert.Equal(0, q.Count);
            Assert.Equal(3, q.Capacity);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Invoked when IP data is being delivered through the specified interface.
 /// </summary>
 /// <param name="ifc">The interface through which the data was received.</param>
 /// <param name="data">A sequence of bytes received.</param>
 /// <exception cref="ArgumentNullException">Thrown if either argument
 /// is null.</exception>
 void OnIpInput(Interface ifc, byte[] data)
 {
     ifc.ThrowIfNull("ifc");
     data.ThrowIfNull("data");
     WriteLine(ifc.FullName + " has received new IP packet.");
     try {
         var packet = IpPacket.Deserialize(data);
         // This method is called in "interrupt context" and can execute
         // on behalf of different NIC's simultaneously. The IP stack
         // is usually single threaded however, so incoming packets are queued
         // globally and processed sequentially.
         if (inputQueue.Count == 0)
         {
             Simulation.Callback(nodalProcessingDelay, ProcessPackets);
         }
         try {
             // Enqueue the packet and the interface through which it was
             // received.
             inputQueue.Enqueue(new Tuple <IpPacket, Interface>(packet, ifc));
         } catch (InvalidOperationException) {
             // If the host's input queue is full, we must drop the packet.
             WriteLine("IP input queue overflow, dropping packet.");
             // Send a "Source Quench" ICMP to the packet's originator.
             SendIcmp(ifc, packet.Source, IcmpPacket.SourceQuench(packet));
         }
     } catch (SerializationException) {
         WriteLine(ifc.FullName + " has detected a bad checksum, " +
                   "discarding IP packet.");
     }
 }
Exemplo n.º 8
0
        public void CanEnumerate()
        {
            var q = new CappedQueue <int>(3);

            Assert.Empty(q);

            q.Enqueue(1);

            Assert.Single(q, 1);

            q.Enqueue(2);

            Assert.Equal(Seq(1, 2), q);

            q.Enqueue(3);

            Assert.Equal(Seq(1, 2, 3), q);

            q.Enqueue(4);

            Assert.Equal(Seq(2, 3, 4), q);
        }
Exemplo n.º 9
0
        public void CanEnqueue()
        {
            var q1 = new CappedQueue <int>(1);

            q1.Enqueue(1);
            Assert.Equal(1, q1.Count);
            Assert.Equal(1, q1.GetElement(0));

            q1.Enqueue(2);
            Assert.Equal(1, q1.Count);
            Assert.Equal(2, q1.GetElement(0));

            var q2 = new CappedQueue <int>(2);

            q2.Enqueue(1);
            q2.Enqueue(2);
            q2.Enqueue(3);

            Assert.Equal(2, q2.Count);
            Assert.Equal(2, q2.GetElement(0));
            Assert.Equal(3, q2.GetElement(1));
        }
Exemplo n.º 10
0
        public void CanSetCapacity()
        {
            var q = new CappedQueue <int>(1);

            q.Capacity = 2;             // enlarge empty queue

            Assert.Equal(0, q.Count);
            Assert.Equal(2, q.Capacity);

            q.Enqueue(1);
            q.Enqueue(2);
            q.Enqueue(3);

            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));

            q.Capacity = 3;             // enlarge

            Assert.Equal(3, q.Capacity);
            Assert.Equal(2, q.Count);

            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));

            q.Enqueue(4);

            Assert.Equal(3, q.Count);
            Assert.Equal(2, q.GetElement(0));
            Assert.Equal(3, q.GetElement(1));
            Assert.Equal(4, q.GetElement(2));

            q.Capacity = 2;             // shrink

            Assert.Equal(2, q.Capacity);
            Assert.Equal(2, q.Count);

            Assert.Equal(3, q.GetElement(0));
            Assert.Equal(4, q.GetElement(1));
        }
Exemplo n.º 11
0
 /// <summary>
 /// Wraps the specified data into an Ethernet frame and queues it
 /// for transmission.
 /// </summary>
 /// <param name="destination">The MAC address of the destination
 /// host.</param>
 /// <param name="data">The data to transmit as part of the frame's
 /// payload.</param>
 /// <param name="type">The type of the data.</param>
 public override void Output(MacAddress destination, byte[] data,
                             EtherType type = EtherType.IPv4)
 {
     destination.ThrowIfNull("destination");
     data.ThrowIfNull("data");
     // Start emptying the FIFO, if we're not already doing it.
     if (emptyingFifo == false)
     {
         Simulation.Callback(0, EmptySendFifo);
     }
     // Enqueue the data.
     sendFifo.Enqueue(new Frame(destination, MacAddress, data, type));
 }
Exemplo n.º 12
0
    /// <summary>
    /// Attempts to retrieve a random child among all children nodes and processes it.
    /// This class has been somewhat tweaked for the context of this assignment.
    /// It has a capped queue data structure in it which is defined as memory. Once a child node
    /// is visited, the memory enqueues the child node's professor and if the memory is ever at its capacity
    /// then the first thing inserted into it is popped of the queue, so that it can never hold memory
    /// beyond its capacity.
    /// </summary>
    /// <returns></returns>
    public override BehaviorResult Process()
    {
        switch (_currentRandom.Process())
        {
        case BehaviorResult.FAIL:
            _memory.Enqueue(_currentRandom.Professor);
            Agent.Memory = _memory.Q.ToList();
            Result       = BehaviorResult.FAIL;
            return(Result);

        case BehaviorResult.SUCCESS:
            _memory.Enqueue(_currentRandom.Professor);
            Agent.Memory = _memory.Q.ToList();
            Result       = BehaviorResult.SUCCESS;
            return(Result);

        case BehaviorResult.RUNNING:
            Result = BehaviorResult.RUNNING;
            return(Result);
        }
        Result = BehaviorResult.RUNNING;
        return(Result);
    }