コード例 #1
0
        private void TestItem_Click(object sender, RoutedEventArgs e)
        {
            WirelessUnitTest test = ((MenuItem)sender).DataContext as WirelessUnitTest;

            if (test != null)
            {
                WirelessUnitTestInstance instance = WirelessUnitTestInstance.RunUnitTest(Network, test);
                MessageBox.Show(instance.TestPassed ? "Passed!" : "Failed!");
            }
        }
コード例 #2
0
        private void ReportItem_Click(object sender, RoutedEventArgs e)
        {
            WirelessReport rpt = ((MenuItem)sender).DataContext as WirelessReport;

            if (rpt != null && Simulation != null)
            {
                WirelessUnitTestInstance instance = WirelessUnitTestInstance.InstanceFromSimulation(Simulation);
                string output = instance.RunReport(rpt);
                System.Diagnostics.Debug.WriteLine(output);
            }
        }
コード例 #3
0
        public static void VerifyBehavior(WirelessUnitTestInstance instance)
        {
            var node = instance.GetRandomNode();

            instance.Simulation.SetButtonState(node, true);
            instance.Simulation.SimulateTime(0.5);
            instance.Simulation.SetButtonState(node, false);
            instance.VerifyAllLedsChange(0.5, Colors.Green);
            instance.VerifyAllLedsChange(2 - 0.005, Colors.Black);

            //throw new Exception("Temporary failure"); // For testing while building the debug viewer UI.
        }
コード例 #4
0
        public static WirelessUnitTestInstance RunUnitTest(WirelessNetwork net, WirelessUnitTest test)
        {
            WirelessUnitTestInstance instance = new WirelessUnitTestInstance(net);

            instance.TestPassed = false;
            try
            {
                UnitTestDelegate d = (UnitTestDelegate)Delegate.CreateDelegate(typeof(UnitTestDelegate), test.UnitTestMethod);
                d(instance);
                instance.TestPassed = true;
            }
            catch (Exception ex)
            {
                instance.TestException = ex;
            }

            return(instance);
        }
コード例 #5
0
        public static string NodeResponseReport(WirelessUnitTestInstance instance)
        {
            List <string> strings   = new List <string>();
            double        startTime = 0;

            while (true)
            {
                var evt = instance.FindNextButtonPress(startTime);
                if (evt == null)
                {
                    break;
                }

                if (strings.Count != 0)
                {
                    strings.Add("");
                }
                startTime = evt.StartTime;
                strings.Add($"Button press on Node {evt.Origin.MyID} at {startTime}:");

                List <NodeTime> LedChange   = new List <NodeTime>();
                List <NodeTime> FirstPacket = new List <NodeTime>();

                foreach (var node in instance.Simulation.SimulationNodes)
                {
                    evt = instance.FindFirstLedColor(node.Node, startTime, startTime + 1.5, (c) => c == Colors.Green);
                    if (evt == null)
                    {
                        LedChange.Add(new NodeTime()
                        {
                            Success = false, Node = node.Node
                        });
                    }
                    else
                    {
                        LedChange.Add(new NodeTime()
                        {
                            Success = true, Time = evt.StartTime - startTime, Node = node.Node
                        });
                    }

                    evt = instance.FindFirstReceivedPacket(node.Node, startTime, startTime + 1.5);
                    if (evt == null)
                    {
                        FirstPacket.Add(new NodeTime()
                        {
                            Success = false, Node = node.Node
                        });
                    }
                    else
                    {
                        FirstPacket.Add(new NodeTime()
                        {
                            Success = true, Time = evt.StartTime - startTime, Node = node.Node
                        });
                    }
                }

                LedChange.Sort((n1, n2) => n1.Time.CompareTo(n2.Time));
                FirstPacket.Sort((n1, n2) => n1.Time.CompareTo(n2.Time));

                foreach (var nt in FirstPacket)
                {
                    strings.Add($"  Node {nt.Node.MyID}: Received at {nt.Time} ({nt.Success})");
                }
                foreach (var nt in LedChange)
                {
                    strings.Add($"  Node {nt.Node.MyID}: Led Change at {nt.Time} ({nt.Success})");
                }
            }
            return(string.Join("\n", strings));
        }
コード例 #6
0
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            int count     = IterationCount;
            int pass      = 0;
            int fail      = 0;
            int completed = 0;

            // Determine which test to use
            WirelessUnitTest test = ((ComboBoxItem)comboSelectTest.SelectedItem)?.DataContext as WirelessUnitTest;

            if (test == null)
            {
                return;
            }

            listBox.Items.Clear();
            listBox.Items.Add(new ListBoxItem()
            {
                Content = $"Starting {count} iterations of {test.UnitTestMethod.Name}", Background = Brushes.LightBlue
            });


            btnStart.IsEnabled = false;
            ThreadPool.QueueUserWorkItem((context) =>
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Parallel.For(0, count, (index) =>
                {
                    WirelessUnitTestInstance instance = WirelessUnitTestInstance.RunUnitTest(LinkedWindow.Network, test);

                    if (instance.TestPassed)
                    {
                        Interlocked.Increment(ref pass);
                    }
                    else
                    {
                        Interlocked.Increment(ref fail);

                        Dispatcher.Invoke(() =>
                        {
                            ListBoxItem lb = new ListBoxItem()
                            {
                                Content = $"({index}) Test Failed: {instance.TestException.ToString()}", DataContext = instance, Background = Brushes.LightPink
                            };
                            lb.MouseDoubleClick += OpenTestFailureItem;
                            listBox.Items.Add(lb);
                        });
                    }
                    if (Interlocked.Increment(ref completed) % 50 == 0)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            Title = $"Running tests... ({(pass + fail)}/{count})";
                        });
                    }
                });
                sw.Stop();

                double ms = Math.Floor(sw.Elapsed.TotalMilliseconds * 100) / 100;

                Dispatcher.Invoke(() => {
                    listBox.Items.Add(new ListBoxItem()
                    {
                        Content = $"Completed in {ms}ms. {pass} Passed, {fail} Failed.", Background = Brushes.LightBlue
                    });

                    Title = "Run Unit Tests";
                    btnStart.IsEnabled = true;
                });
            });
        }