Exemplo n.º 1
0
        public void ShouldNotPermitTrafficLightsToParticipateInMultiplePolarClusters()
        {
            var lights = new TrafficLights();

            var l1 = new TrafficLight { Name = "TL-1" };
            var l2 = new TrafficLight { Name = "TL-2" };
            var l3 = new TrafficLight { Name = "TL-3" };
            var l4 = new TrafficLight { Name = "TL-4" };
            var l5 = new TrafficLight { Name = "TL-5" };
            var l6 = new TrafficLight { Name = "TL-6" };

            var t1 = new PolarTrafficLightCluster(l3) {l1, l2}; 
            t1.Name = "Polar Cluster 1";

            var t2 = new PolarTrafficLightCluster(l5) {l4};
            t2.Name = "Polar Cluster 2";

            var t3 = new PolarTrafficLightCluster(l5) {l6}; //l5 already assigned to Polar Cluster
            t3.Name = "Polar Cluster 3";

            Assert.IsTrue(lights.Add(t1));
            Assert.IsTrue(lights.Add(t2));
            Assert.IsFalse(lights.Add(t3));

            Assert.AreEqual(5, lights.Count()); //Traffic lights in invalid clusters not added
            Assert.AreEqual(2, lights.AllPolarClusters.Count());  //l3 & l5 Polar Clusters
        }
Exemplo n.º 2
0
        public void ShouldTreatDistinctClustersTypesAsNotEqual()
        {
            var cluster1 = new TetheredTrafficLightCluster();
            var cluster2 = new PolarTrafficLightCluster(new TrafficLight());

            Assert.AreNotEqual(cluster1, cluster2);
        }
Exemplo n.º 3
0
        public void ShouldStartTrafficLightCycle()
        {
            var configuration = new TrafficLightConfig();

            configuration.Add(TrafficLightState.Go, 4);
            configuration.Add(TrafficLightState.Stop, 4);
            SetTrafficLightConfiguration(configuration);

            var lights = new TrafficLights {
                _l1, _l2, _l3, _l4
            };
            var timer = new TrafficLightTimer();

            var tc1 = new TetheredTrafficLightCluster {
                _l1, _l2
            };

            tc1.Name = "TC-1";

            var pc1 = new PolarTrafficLightCluster(_l3)
            {
                { _l4 }
            };

            pc1.Name = "TC-2";

            Assert.IsTrue(lights.Add(tc1));
            Assert.IsTrue(lights.Add(pc1));

            var controller = new TrafficLightController(lights, timer);

            //Assert initial state is where expected
            Assert.AreEqual(TrafficLightState.StopThenGo, _l1.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l2.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l3.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l4.State);

            controller.Start();  //Will trigger traffic light state changes

            Thread.Sleep(2000);
            Assert.AreEqual(TrafficLightState.Go, _l1.State);
            Assert.AreEqual(TrafficLightState.Go, _l2.State);
            Assert.AreEqual(TrafficLightState.Go, _l3.State);
            Assert.AreEqual(TrafficLightState.Stop, _l4.State);

            Thread.Sleep(5000);
            Assert.AreEqual(TrafficLightState.Stop, _l1.State);
            Assert.AreEqual(TrafficLightState.Stop, _l2.State);
            Assert.AreEqual(TrafficLightState.Stop, _l3.State);
            Assert.AreEqual(TrafficLightState.Go, _l4.State);

            controller.Stop();
        }
Exemplo n.º 4
0
        public void ShouldNotAcceptAnInvalidPolarTrafficLight()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();

            var cluster = new PolarTrafficLightCluster(t1)
            {
                t2, t1
            };

            Assert.AreEqual(1, cluster.GetTrafficLightIds().Count());
            Assert.AreEqual(t2.Id, cluster.GetTrafficLightIds().First());
        }
Exemplo n.º 5
0
        public void ShouldNotPermitSubjectTrafficLightToParticipateInCluster()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();

            var cluster = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };

            Assert.IsFalse(cluster.Add(t1));
        }
Exemplo n.º 6
0
        public void ShouldNotPermitPolarSubjectsToBelongToTethers()
        {
            var lights = new TrafficLights();

            var l1 = new TrafficLight { Name = "TL-1" };
            var l2 = new TrafficLight { Name = "TL-2" };
            var l3 = new TrafficLight { Name = "TL-3" };
            var l4 = new TrafficLight { Name = "TL-4" };

            var t1 = new PolarTrafficLightCluster(l1) { l2, l3, }; 
            var t2 = new TetheredTrafficLightCluster { l1, l4 };  
            Assert.IsTrue(lights.Add(t1));
            Assert.IsFalse(lights.Add(t2));
        }
Exemplo n.º 7
0
        public void ShouldNotPermitPolarSubjectsToBeMembersOfOtherPolarClusters()
        {
            var lights = new TrafficLights();

            var l1 = new TrafficLight { Name = "TL-1" };
            var l2 = new TrafficLight { Name = "TL-2" };
            var l3 = new TrafficLight { Name = "TL-3" };
            var l4 = new TrafficLight { Name = "TL-4" };
            var l5 = new TrafficLight { Name = "TL-5" };

            var t1 = new PolarTrafficLightCluster(l1) { l2, l3, };
            var t2 = new PolarTrafficLightCluster(l4) { l1, l5, };
            Assert.IsTrue(lights.Add(t1));
            Assert.IsFalse(lights.Add(t2));
        }
Exemplo n.º 8
0
        public void ShouldConsiderTethersWithOverlappingMembersAsInCompatible()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();

            var polarCluster = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };
            var tetheredCluster = new TetheredTrafficLightCluster {
                t2, t3
            };

            Assert.IsFalse(polarCluster.IsCompatible(tetheredCluster));
        }
Exemplo n.º 9
0
        public void ShouldUpdateClusterStateWhenRequested()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();

            var cluster = new PolarTrafficLightCluster(t1)
            {
                t2
            };

            Assert.AreEqual(TrafficLightState.StopThenGo, t1.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, t2.State);

            cluster.SetMasterStateTo(TrafficLightState.Go);
            Assert.AreEqual(TrafficLightState.Go, t1.State);
            Assert.AreEqual(TrafficLightState.Stop, t2.State);
        }
Exemplo n.º 10
0
        public void ShouldTreatPolarClustersWithDifferentSubjectTrafficLightAsNotEqual()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();
            var t4 = new TrafficLight();

            var cluster1 = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };
            var cluster2 = new PolarTrafficLightCluster(t4)
            {
                t2, t3
            };

            Assert.AreNotEqual(cluster1, cluster2);
        }
Exemplo n.º 11
0
        public void ShouldPermitTrafficLightToBelongToTetherAndPolarClusters()
        {
            var lights = new TrafficLights();

            var l1 = new TrafficLight { Name = "TL-1" };
            var l2 = new TrafficLight { Name = "TL-2" };
            var l3 = new TrafficLight { Name = "TL-3" };
            var l4 = new TrafficLight { Name = "TL-4" };

            var t1 = new TetheredTrafficLightCluster {{l1, l2}};      //l1 belongs to 
            var t2 = new PolarTrafficLightCluster(l1) { l3, l4 };   //  both clusters.

            Assert.IsTrue(lights.Add(t1));
            Assert.IsTrue(lights.Add(t2));

            Assert.AreEqual(4, lights.Count()); //All unique traffic lights added
            Assert.AreEqual(1, lights.AllTetheredClusters.Count());
            Assert.AreEqual(1, lights.AllPolarClusters.Count());
        }
Exemplo n.º 12
0
        public void ShouldSynchronizedAllPolarLights()
        {
            var lights = new TrafficLights {
                _l1, _l2, _l3, _l4
            };
            var timer = new TrafficLightTimer();

            var tc1 = new PolarTrafficLightCluster(_l3)
            {
                { _l1, _l2 }
            };

            tc1.Name = "TC-1";

            var tc2 = new PolarTrafficLightCluster(_l5)
            {
                { _l4 }
            };

            tc2.Name = "TC-2";

            Assert.IsTrue(lights.Add(tc1, tc2));

            var controller = new TrafficLightController(lights, timer);

            //Assert initial state is where expected
            Assert.AreEqual(TrafficLightState.StopThenGo, _l1.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l2.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l3.State);
            Assert.AreEqual(TrafficLightState.StopThenGo, _l4.State);

            controller.Start();  //Will trigger traffic light state changes
            Thread.Sleep(2500);

            Assert.AreEqual(TrafficLightState.Go, _l3.State);
            Assert.AreEqual(TrafficLightState.Go, _l5.State);
            Assert.AreEqual(TrafficLightState.Stop, _l1.State);
            Assert.AreEqual(TrafficLightState.Stop, _l2.State);
            Assert.AreEqual(TrafficLightState.Stop, _l4.State);

            controller.Stop();
        }
Exemplo n.º 13
0
        public void ShouldProduceTetheredClusterOfAllPolarLights()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();

            var cluster = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };

            var tetheredCluster = cluster.TetheredTrafficLightCluster;

            Assert.IsNotNull(tetheredCluster);

            Assert.AreEqual(2, tetheredCluster.GetTrafficLightIds().Count());
            Assert.IsFalse(tetheredCluster.Except(new List <ITrafficLight> {
                t2, t3
            }).Any());
        }
Exemplo n.º 14
0
        public void ShouldTreatPolarClustersWithIdenticalAttributesAsEqual()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();

            var cluster1 = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };
            var cluster2 = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };

            Assert.AreEqual(cluster1, cluster2);

            cluster2.Add(new TrafficLight());
            Assert.AreNotEqual(cluster1, cluster2);
        }
Exemplo n.º 15
0
        public void ShouldConsiderTethersCompatibleWhenThereAreNoCommonMembers()
        {
            var t1 = new TrafficLight();
            var t2 = new TrafficLight();
            var t3 = new TrafficLight();
            var t4 = new TrafficLight();

            var polarCluster = new PolarTrafficLightCluster(t1)
            {
                t2, t3
            };
            var tetheredCluster = new TetheredTrafficLightCluster {
                t4
            };

            Assert.IsTrue(polarCluster.IsCompatible(tetheredCluster));

            tetheredCluster.Add(t2);
            Assert.IsFalse(polarCluster.IsCompatible(tetheredCluster));
        }
Exemplo n.º 16
0
        public void ShouldNotPermitConflictingTetheredAndPolarClusters()
        {
            var lights = new TrafficLights();

            var l1 = new TrafficLight();
            var l2 = new TrafficLight();
            var l3 = new TrafficLight();
            var l4 = new TrafficLight();
            var l5 = new TrafficLight();
            var l6 = new TrafficLight();

            var t1 = new TetheredTrafficLightCluster { l2, l3 };  // I2 is Tethered to I3, but 
            var t2 = new PolarTrafficLightCluster(l3) { l1, l2, };    // also defined as a l3 Polar -- illogical
            Assert.IsTrue(lights.Add(t1));
            Assert.IsFalse(lights.Add(t2));

            var t3 = new TetheredTrafficLightCluster { l3, l4, l5 };    // Overlapping Traffic Lights,
            var t4 = new PolarTrafficLightCluster(l6) { l4, l5, };          //  this is not allowed.

            Assert.IsTrue(lights.Add(t4));  //Polar cluster first
            Assert.IsFalse(lights.Add(t3)); //  to ensure validation works regardless of order
        }
Exemplo n.º 17
0
        public void ShouldRaiseEventWhenClusterRequiresUpdating()
        {
            var configuration = new TrafficLightConfig();

            configuration.Add(TrafficLightState.Go, 3);

            var light = new TrafficLight
            {
                Name          = "State Expired Test Light",
                Configuration = configuration
            };

            var cluster = new PolarTrafficLightCluster(light);

            var receivedEvents = new List <PolarClusterRequiresUpdateEventArg>();

            cluster.ClusterRequiresUpdateEvent += (sender, e)
                                                  => receivedEvents.Add(e);

            light.State = TrafficLightState.Go;
            Thread.Sleep(4000);

            Assert.AreEqual(1, receivedEvents.Count);
        }