public void CircuitProjectSaveLoadTest()
        {
            string dir  = Path.Combine(this.TestContext.TestRunDirectory, this.TestContext.TestName + DateTime.UtcNow.Ticks, "Some Test Sub Directory");
            string file = Path.Combine(dir, "My Test File.CircuitProject");

            // save in inexistent folder
            CircuitProject project1 = CircuitProject.Create(null);

            project1.InTransaction(() => {
                LogicalCircuit main        = project1.ProjectSet.Project.LogicalCircuit;
                CircuitButton button       = project1.CircuitButtonSet.Create("b", false, PinSide.Right);
                CircuitSymbol buttonSymbol = project1.CircuitSymbolSet.Create(button, main, 1, 2);
                Gate led = project1.GateSet.Gate(GateType.Led, 1, false);
                project1.CircuitSymbolSet.Create(led, main, 6, 2);
                Wire wire = project1.WireSet.Create(main, new GridPoint(3, 3), new GridPoint(6, 3));
            });
            Assert.IsTrue(!Directory.Exists(dir));
            project1.Save(file);
            Assert.IsTrue(File.Exists(file));
            CircuitProject project2 = CircuitProject.Create(file);

            Assert.IsTrue(ProjectTester.Equal(project1, project2));

            // save in existing folder and existing file.
            CircuitProject project3 = ProjectTester.Load(this.TestContext, Properties.Resources.Digital_Clock, null);

            Assert.IsTrue(File.Exists(file));
            project3.Save(file);
            CircuitProject project4 = CircuitProject.Create(file);

            File.Delete(file);
            Assert.IsTrue(ProjectTester.Equal(project3, project4));
        }
        public void ExpressionParserVariableParseTest()
        {
            CircuitProject    project = ProjectTester.Load(this.TestContext, Properties.Resources.Digital_Clock, "4 bit adder");
            CircuitTestSocket socket  = new CircuitTestSocket(project.ProjectSet.Project.LogicalCircuit);
            ExpressionParser  parser  = new ExpressionParser(socket);
            TruthState        state   = new TruthState(socket.Inputs.Count(), socket.Outputs.Count());

            for (int i = 0; i < state.Result.Length; i++)
            {
                state.Result[i] = 0x5555555555555555L;
            }
            bool success = state.Unpack(socket.Outputs.Select(o => o.Function.ParameterCount).ToArray());

            Assert.IsTrue(success);

            state.Input[this.InputIndex(socket, "c")]    = 1;
            state.Input[this.InputIndex(socket, "x1")]   = 5;
            state.Input[this.InputIndex(socket, "x2")]   = 4;
            state.Output[this.OutputIndex(socket, "s")]  = 9;
            state.Output[this.OutputIndex(socket, "c'")] = 1;

            this.Valid(parser, state, 1, "c");
            this.Valid(parser, state, 5, "x1");
            this.Valid(parser, state, 4, "x2");
            this.Valid(parser, state, 9, "s");
            this.Valid(parser, state, 1, "\"c'\"");

            this.Invalid(parser, state, "d");
            this.Invalid(parser, state, "\"c'");
            this.Invalid(parser, state, "\"c'\\");
            this.Invalid(parser, state, "\"c'\\\"\"");
        }
        public void ExpressionParserVariableCaseParseTest()
        {
            CircuitProject project = ProjectTester.Load(this.TestContext, Properties.Resources.Digital_Clock, "4 bit adder");
            Pin            x1 = null, x2 = null, s = null, c = null;

            foreach (Pin pin in project.PinSet.SelectByCircuit(project.ProjectSet.Project.LogicalCircuit))
            {
                switch (pin.Name)
                {
                case "x1":
                    x1 = pin;
                    break;

                case "x2":
                    x2 = pin;
                    break;

                case "s":
                    s = pin;
                    break;

                case "c'":
                    c = pin;
                    break;
                }
            }
            Assert.IsNotNull(x1);
            Assert.IsNotNull(x2);
            Assert.IsNotNull(s);
            Assert.IsNotNull(c);
            project.InTransaction(() => {
                x1.Name = "variant";
                x2.Name = "vaRIAnt";
                s.Name  = "VAriaNT";
                c.Name  = "VARIANT";
            });

            CircuitTestSocket socket = new CircuitTestSocket(project.ProjectSet.Project.LogicalCircuit);
            ExpressionParser  parser = new ExpressionParser(socket);
            TruthState        state  = new TruthState(socket.Inputs.Count(), socket.Outputs.Count());

            for (int i = 0; i < state.Result.Length; i++)
            {
                state.Result[i] = 0x5555555555555555L;
            }
            state.Unpack(socket.Outputs.Select(o => o.Function.ParameterCount).ToArray());

            state.Input[this.InputIndex(socket, "c")]         = 1;
            state.Input[this.InputIndex(socket, "variant")]   = 5;
            state.Input[this.InputIndex(socket, "vaRIAnt")]   = 4;
            state.Output[this.OutputIndex(socket, "VAriaNT")] = 9;
            state.Output[this.OutputIndex(socket, "VARIANT")] = 1;

            this.Valid(parser, state, 1, "c");
            this.Valid(parser, state, 5, "variant");
            this.Valid(parser, state, 4, "vaRIAnt");
            this.Valid(parser, state, 9, "VAriaNT");
            this.Valid(parser, state, 1, "VARIANT");
        }
        public void CircuitMapCleanUpTest()
        {
            CircuitProject circuitProject = ProjectTester.Load(this.TestContext, Properties.Resources.CircuitMapCleanUpTest, null);

            this.CircuitMapCleanUpTest(circuitProject, "1. Empty 1", 0);
            Assert.AreEqual(0, circuitProject.ProjectSet.Project.LogicalCircuit.CircuitSymbols().Count());

            this.CircuitMapCleanUpTest(circuitProject, "2. Empty 2", 0);
            Assert.AreEqual(7, circuitProject.ProjectSet.Project.LogicalCircuit.CircuitSymbols().Count());

            this.CircuitMapCleanUpTest(circuitProject, "3. Single Out", 3);
            Assert.AreEqual(4, circuitProject.ProjectSet.Project.LogicalCircuit.CircuitSymbols().Count());

            this.CircuitMapCleanUpTest(circuitProject, "4. Chain Out", 8);
            Assert.AreEqual(11, circuitProject.ProjectSet.Project.LogicalCircuit.CircuitSymbols().Count());
        }
        private void CircuitMapPerfTest(string project, string initialCircuit, int maxCount, int maxSeconds)
        {
            CircuitProject circuitProject = ProjectTester.Load(this.TestContext, project, initialCircuit);
            Stopwatch      stopwatch      = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i < maxCount; i++)
            {
                CircuitMap   circuitMap   = new CircuitMap(circuitProject.ProjectSet.Project.LogicalCircuit);
                CircuitState circuitState = circuitMap.Apply(CircuitRunner.HistorySize);
                Assert.IsNotNull(circuitState);
                circuitMap.TurnOn();
            }
            stopwatch.Stop();
            this.TestContext.WriteLine("{0} CircuitMap(s) created and applied in {1} - {2:N2} sec per each map", maxCount, stopwatch.Elapsed, stopwatch.Elapsed.TotalSeconds / maxCount);
            Assert.IsTrue(stopwatch.Elapsed < new TimeSpan(0, 0, maxSeconds), "CircuitMap was created and applied successfully but too slow");
        }
        public void XmlLoadReadElementTextTest()
        {
            string      text = Properties.Resources.XmlLoadReadElementTextTest;
            XmlDocument xml  = new XmlDocument();

            xml.LoadXml(text);
            Assert.AreEqual(@"http://LogicCircuit.net/1.0.0.2/CircuitProject.xsd", xml.DocumentElement.NamespaceURI);

            CircuitProject circuitProject = ProjectTester.Load(this.TestContext, text, null);

            Assert.IsNotNull(circuitProject);

            Assert.AreEqual(1, circuitProject.CircuitButtonSet.Count());
            CircuitButton button = circuitProject.CircuitButtonSet.First();

            Assert.AreEqual(@"<a>", button.Notation);
            Assert.AreEqual(@"<a>b</a>", button.Note);
            Assert.AreEqual(1, circuitProject.CircuitSymbolSet.SelectByCircuit(button).Count());
            CircuitSymbol buttonSymbol = circuitProject.CircuitSymbolSet.SelectByCircuit(button).First();

            Assert.AreEqual(3, buttonSymbol.X);
            Assert.AreEqual(8, buttonSymbol.Y);

            Assert.AreEqual(2, circuitProject.CircuitSymbolSet.Count());
            CircuitSymbol ledSymbol = circuitProject.CircuitSymbolSet.First(s => s != buttonSymbol);

            Assert.IsNotNull(ledSymbol);
            Assert.AreEqual(9, ledSymbol.X);
            Assert.AreEqual(8, ledSymbol.Y);

            Assert.AreEqual(1, circuitProject.WireSet.Count());
            Wire wire = circuitProject.WireSet.First();

            Assert.IsNotNull(wire);
            Assert.AreEqual(5, wire.X1);
            Assert.AreEqual(9, wire.Y1);
            Assert.AreEqual(9, wire.X2);
            Assert.AreEqual(9, wire.Y1);
        }
        public ProjectTester(TestContext testContext, string projectText, string initialCircuit)
        {
            this.CircuitProject = ProjectTester.Load(testContext, projectText, initialCircuit);

            // Create map and state
            this.CircuitMap   = new CircuitMap(this.CircuitProject.ProjectSet.Project.LogicalCircuit);
            this.CircuitState = this.CircuitMap.Apply(CircuitRunner.HistorySize);

            // Init controlling points of the main circuit: constants and probes
            List <CircuitSymbol> inputSymbol = new List <CircuitSymbol>();

            foreach (CircuitSymbol symbol in this.Project.LogicalCircuit.CircuitSymbols())
            {
                if (symbol.Circuit is Constant)
                {
                    inputSymbol.Add(symbol);
                }
            }
            CircuitSymbolComparer circuitSymbolComparer = new CircuitSymbolComparer(true);

            inputSymbol.Sort(circuitSymbolComparer);
            this.Input = inputSymbol.Select(s => this.CircuitMap.FunctionConstant(s)).ToArray();

            List <CircuitSymbol> outputSymbol = new List <CircuitSymbol>();

            foreach (CircuitSymbol symbol in this.Project.LogicalCircuit.CircuitSymbols())
            {
                if (symbol.Circuit is CircuitProbe)
                {
                    outputSymbol.Add(symbol);
                }
            }
            outputSymbol.Sort(circuitSymbolComparer);
            this.Output = outputSymbol.Select(s => this.CircuitMap.FunctionProbe(s)).ToArray();

            this.CircuitMap.TurnOn();
        }