Exemple #1
0
        public void Test_AddAndRemoveComponentsToCircuit()
        {
            Circuit   circuit       = new Circuit();
            TrueConst true_constant = new TrueConst();

            Assert.That(() => circuit.AddComponent(true_constant), Throws.Nothing);
            // Check we can't add the same component twice:
            Assert.That(() => circuit.AddComponent(true_constant), Throws.ArgumentException);
            // Try removing:
            Assert.That(() => circuit.RemoveComponent(true_constant), Throws.Nothing);
            Assert.That(() => circuit.RemoveComponent(true_constant), Throws.TypeOf <KeyNotFoundException>());
            // Try re-adding again:
            Assert.That(() => circuit.AddComponent(true_constant), Throws.Nothing);
        }
Exemple #2
0
        public void Test_RemoveComponent()
        {
            Circuit circuit    = new Circuit();
            var     and_gate   = new AndGate();
            var     true_const = new TrueConst();

            circuit.AddComponent(and_gate);
            circuit.AddComponent(true_const);
            circuit.Connect(true_const, 0, and_gate, 0);
            circuit.Connect(and_gate, 0, and_gate, 1);

            circuit.RemoveComponent(and_gate);
            circuit.RemoveComponent(true_const);
        }
        protected override void Awake()
        {
            base.Awake();
            OutConnectors.AddRange(Enumerable.Repeat <SPConnector>(null, 1));

            // Set up connectors
            OutConnector = Instantiate(SPOutConnectorPrefab, gameObject.transform, false);
            Assert.IsNotNull(OutConnector);
            OutConnector.gameObject.name         = "OutConnector";
            OutConnector.transform.localPosition = new Vector3(1, 0, -1);
            OutConnector.Register(this, SPConnectorType.SPOutConnector, 0);

            LogicComponent = new TrueConst();
            Canvas.Circuit.AddComponent(LogicComponent);
        }
Exemple #4
0
        public void Test_ConstantComponents()
        {
            LogicComponent true_constant = new TrueConst();

            Assert.AreEqual(true_constant.Outputs, new List <bool>()
            {
                true
            });
            Assert.AreEqual(true_constant.Simulate(new bool[] {}), new List <bool> {
                true
            });

            LogicComponent false_constant = new FalseConst();

            Assert.AreEqual(false_constant.Outputs, new List <bool>()
            {
                false
            });
            Assert.AreEqual(false_constant.Simulate(new bool[] {}), new List <bool> {
                false
            });
        }
Exemple #5
0
        public void Test_CircuitSimulate_SmallTree()
        {
            LogicComponent true_constant1 = new TrueConst();
            LogicComponent true_constant2 = new TrueConst();
            LogicComponent false_constant = new FalseConst();
            LogicComponent and_gate       = new AndGate();
            LogicComponent or_gate        = new OrGate();

            LogicComponent true_constant1_connection = new Connection();
            LogicComponent true_constant2_connection = new Connection();
            LogicComponent false_constant_connection = new Connection();
            LogicComponent and_gate_connection       = new Connection();

            Circuit circuit = new Circuit();

            /* Test will be 2 TRUEs connected to an AND gate.
             * This AND gate and a FALSE will then be connected to the OR gate.
             */
            circuit.AddComponent(true_constant1);
            circuit.AddComponent(true_constant2);
            circuit.AddComponent(false_constant);
            circuit.AddComponent(and_gate);
            circuit.AddComponent(or_gate);
            circuit.AddComponent(true_constant1_connection);
            circuit.AddComponent(true_constant2_connection);
            circuit.AddComponent(false_constant_connection);
            circuit.AddComponent(and_gate_connection);

            circuit.Connect(true_constant1, 0, true_constant1_connection, 0);
            circuit.Connect(true_constant2, 0, true_constant2_connection, 0);
            circuit.Connect(true_constant1_connection, 0, and_gate, 0);
            circuit.Connect(true_constant2_connection, 0, and_gate, 1);
            circuit.Connect(and_gate, 0, and_gate_connection, 0);
            circuit.Connect(false_constant, 0, false_constant_connection, 0);
            circuit.Connect(and_gate_connection, 0, or_gate, 0);
            circuit.Connect(false_constant_connection, 0, or_gate, 1);

            // Try first step: the output of the connections from the constants should change.
            circuit.Simulate();
            Assert.AreEqual(true_constant1_connection.Outputs, new List <bool>()
            {
                true
            });
            Assert.AreEqual(true_constant2_connection.Outputs, new List <bool>()
            {
                true
            });
            // Try second step: the output from the and gate should change.
            circuit.Simulate();
            Assert.AreEqual(and_gate.Outputs, new List <bool>()
            {
                true
            });
            // Try third step: the output of the connections from the and gate should change.
            circuit.Simulate();
            Assert.AreEqual(and_gate_connection.Outputs, new List <bool>()
            {
                true
            });
            // Try fourth step: the output of the or gate should change.
            circuit.Simulate();
            Assert.AreEqual(or_gate.Outputs, new List <bool>()
            {
                true
            });

            // Ensure the state has stabilized into the expected state:
            for (int i = 0; i < 100; i++)
            {
                circuit.Simulate();
                Assert.AreEqual(true_constant1_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(true_constant2_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(and_gate.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(and_gate_connection.Outputs, new List <bool>()
                {
                    true
                });
                Assert.AreEqual(or_gate.Outputs, new List <bool>()
                {
                    true
                });
            }
        }