Пример #1
0
        public void TestReadmeExample1()
        {
            int binCount = 4;
            Tally <float, float> tally
                = new Tally <float, float>(binCount, x => (int)(x * binCount),
                                           binCount, y => (int)(y * binCount));

            // Some where, this is called repeatedly.
            // tally.Add(sensor.value, effector.value);
            // But let's supply some fake values for demonstration purposes.
            tally.Add(0.6f, 0.1f);
            tally.Add(0.5f, 0.5f);
            tally.Add(0.7f, 0.9f);
            tally.Add(0.7f, 0.3f);

            // Finally we analyze it.
            float[] px = tally.probabilityX;
            Assert.Equal(new [] { 0f, 0f, 1f, 0f }, px);
            float[] py = tally.probabilityY;
            Assert.Equal(new [] { 1 / 4f, 1 / 4f, 1 / 4f, 1 / 4f }, py);
            float[,] pxy = tally.probabilityXY;
            Assert.Equal(new [, ] {
                { 0f, 0f, 0f, 0f },
                { 0f, 0f, 0f, 0f },
                { 1 / 4f, 1 / 4f, 1 / 4f, 1 / 4f },
                { 0f, 0f, 0f, 0f },
            }, pxy);
            float Hsensor   = ProbabilityDistribution.Entropy(px, 2);
            float Heffector = ProbabilityDistribution.Entropy(py, 2);
            // H(effector | sensor)
            float Heffector_sensor = ProbabilityDistribution.ConditionalEntropyYX(pxy, px, 2);

            Assert.Equal(0f, Hsensor, 1);
            // So the sensor carries no information. It's going to the second bin always
            // based on what's been seen.
            Assert.Equal(2f, Heffector, 1);
            // The effector carries 2 bits of information. It could show up in any of
            // the bins with equal probability.  It would take two bits to describe which bin.
            Assert.Equal(2f, Heffector_sensor, 1);
            // Given that we know the sensor, there's no reduction in randomness for the
            // effector. In fact since H(effector) = H(effector|sensor) we now know that
            // the sensor and effector are entirely independent of one another.
        }
Пример #2
0
        /// <summary>
        /// Check a test suite validity and run its tests if possible.
        /// Update the status to TestSuiteStatus.SKIP if invalid, then TestSuiteStatus.ERROR if any test fails, then TestSuiteStatus.WARNING if any test is skipped, then TestSuiteStatus.UNKNOWN if any test is not run, then TestSuiteStatus.PASS if succesfull.
        /// Registers any detected error to the test suite.
        /// </summary>
        /// <param name="testSuite"></param>
        /// <seealso cref="TestSuiteStatus"/>
        public static void RunTestSuite(Type testSuite)
        {
            try
            {
                IsValidTestSuite(testSuite);
            }
            catch (Exception e)
            {
                SetTestSuiteStatus(testSuite, TestSuiteStatus.SKIP);
                SetTestSuiteError(testSuite, e);
                return;
            }
            Tally <TestStatus> tally = new Tally <TestStatus>();

            foreach (MethodInfo test in GetTests(testSuite))
            {
                RunTest(test);
                tally[GetTestStatus(test)]++;
            }


            if (tally[TestStatus.ERROR] != 0)
            {
                SetTestSuiteStatus(testSuite, TestSuiteStatus.ERROR);
                SetTestSuiteError(testSuite, new Exception($"{tally[TestStatus.ERROR]} test failed"));
            }
            else if (tally[TestStatus.SKIP] != 0)
            {
                SetTestSuiteStatus(testSuite, TestSuiteStatus.WARNING);
                SetTestSuiteError(testSuite, new Exception($"{tally[TestStatus.SKIP]} test skipped"));
            }
            else if (tally[TestStatus.UNKNOWN] != 0)
            {
                SetTestSuiteStatus(testSuite, TestSuiteStatus.UNKNOWN);
                SetTestSuiteError(testSuite, new Exception($"{tally[TestStatus.UNKNOWN]} test not run yet"));
            }
            else
            {
                SetTestSuiteStatus(testSuite, TestSuiteStatus.PASS);
                SetTestSuiteError(testSuite, null);
            }
        }
Пример #3
0
        static void LogSummary(ICollection <Assembly> asms)
        {
            foreach (Assembly asm in asms)
            {
                Tally <TestSuiteStatus> tsTally = new Tally <TestSuiteStatus>();
                Tally <TestStatus>      tTally  = new Tally <TestStatus>();

                // test results tallying
                foreach (Type testSuite in GetTestSuites(asm))
                {
                    tsTally[GetTestSuiteStatus(testSuite)]++;

                    foreach (MethodInfo test in GetTests(testSuite))
                    {
                        tTally[GetTestStatus(test)]++;
                    }
                }

                string asmResult = BuildAsmSummary(asm, tsTally, tTally);

                switch (GetAssemblyStatus(asm))
                {
                case AssemblyStatus.UNKNOWN:
                case AssemblyStatus.PASS:
                    Info(asmResult);
                    continue;

                case AssemblyStatus.WARNING:
                    Warn(asmResult);
                    break;

                case AssemblyStatus.ERROR:
                    Err(asmResult);
                    break;

                default:
                    break;
                }
            }
        }
Пример #4
0
        public double pushvalue;         // new position to push to
        public Surf(SPARTA sparta)
        {
            bblo = new double[3];
            bbhi = new double[3];

            exist = false;
            surf_collision_check = true;

            gnames      = new string[MAXGROUP];
            bitmask     = new int[MAXGROUP];
            inversemask = new int[MAXGROUP];
            for (int i = 0; i < MAXGROUP; i++)
            {
                bitmask[i]     = 1 << i;
                inversemask[i] = bitmask[i] ^ ~0;
            }

            ngroup    = 1;
            gnames[0] = "all";

            npoint = nline = ntri = 0;
            //pts = NULL;
            //lines = NULL;
            //tris = NULL;
            //pushflag = 1;

            //nlocal = 0;
            //mysurfs = NULL;

            //nsc = maxsc = 0;
            //sc = NULL;

            //nsr = maxsr = 0;
            //sr = NULL;

            tally_comm  = Tally.TALLYAUTO;
            this.sparta = sparta;
        }
Пример #5
0
        private Snapshot GenerateSnapshot(Keccak hash, long number, Address candidate)
        {
            SortedList <Address, long> signers = new SortedList <Address, long>(AddressComparer.Instance);

            signers.Add(_signer1, number - 2);
            signers.Add(_signer2, number - 1);
            signers.Add(_signer3, number - 3);
            List <Vote> votes = new List <Vote>();

            votes.Add(new Vote(_signer1, number - 2, candidate, true));
            votes.Add(new Vote(_signer3, number - 3, candidate, true));
            votes.Add(new Vote(_signer3, number - 6, _signer2, false));
            Dictionary <Address, Tally> tally = new Dictionary <Address, Tally>();

            tally[candidate]       = new Tally(true);
            tally[candidate].Votes = 2;
            tally[_signer2]        = new Tally(false);
            tally[_signer2].Votes  = 1;
            Snapshot snapshot = new Snapshot(number, hash, signers, tally);

            snapshot.Votes = votes;
            return(snapshot);
        }
Пример #6
0
        public void TestClearWinnerElection()
        {
            Ballot sampleBallot = new Ballot();

            sampleBallot.Add("A", false);
            sampleBallot.Add("B", false);
            sampleBallot.Add("C", false);
            sampleBallot.Add("D", false);

            Tally tally = new Tally(sampleBallot);

            tally.AddBallot(sampleBallot.Clone().Approve("A").Approve("B"));
            tally.AddBallot(sampleBallot.Clone().Approve("A").Approve("B"));
            tally.AddBallot(sampleBallot.Clone().Approve("B"));
            tally.AddBallot(sampleBallot.Clone().Approve("C").Approve("D"));
            tally.AddBallot(sampleBallot.Clone().Approve("B").Approve("D"));

            Assert.AreEqual("B", tally.Winner());
            Assert.AreEqual(2, tally["A"]);
            Assert.AreEqual(4, tally["B"]);
            Assert.AreEqual(1, tally["C"]);
            Assert.AreEqual(2, tally["D"]);
        }
Пример #7
0
        private TraceItem StartTrace(TraceKind kind, PatternElement source)
        {
            TraceItem traceElement = new TraceItem
            {
                Kind           = kind,
                PatternElement = source,
                State          = TraceState.InProgress
            };

            Items.Add(traceElement);

            if (!Tally.TryGetValue(source, out TallyEntry entry))
            {
                entry = new TallyEntry
                {
                    LastPosition = 0,
                    Count        = 0
                };

                Tally.Add(source, entry);
            }

            if (entry.LastPosition != State.Enumerator.Index)
            {
                entry.Count        = 0;
                entry.LastPosition = State.Enumerator.Index;
            }

            entry.Count++;

            if (entry.Count > CycleThreshold)
            {
                throw new Exception("Possible cycle detected!");
            }

            return(traceElement);
        }
Пример #8
0
        public void TestTiedElection()
        {
            Ballot sampleBallot = new Ballot();

            sampleBallot.Add("A", false);
            sampleBallot.Add("B", false);
            sampleBallot.Add("C", false);
            sampleBallot.Add("D", false);

            Tally tally = new Tally(sampleBallot);

            tally.AddBallot(sampleBallot.Clone().Approve("A").Approve("C"));
            tally.AddBallot(sampleBallot.Clone().Approve("A").Approve("B"));
            tally.AddBallot(sampleBallot.Clone().Approve("B"));
            tally.AddBallot(sampleBallot.Clone().Approve("C").Approve("D"));
            tally.AddBallot(sampleBallot.Clone().Approve("D"));
            tally.AddBallot(sampleBallot.Clone().Approve("B").Approve("C"));

            Assert.AreEqual(2, tally["A"]);
            Assert.AreEqual(3, tally["B"]);
            Assert.AreEqual(3, tally["C"]);
            Assert.AreEqual(2, tally["D"]);

            string winner = tally.Winner();

            Assert.IsTrue(new List <string>()
            {
                "B", "C"
            }.Contains(winner));

            // winner stays the same each subsequent call
            for (int i = 0; i < 100; i++)
            {
                string winner2 = tally.Winner();
                Assert.AreEqual(winner, winner2);
            }
        }
Пример #9
0
        private Snapshot GenerateSnapshot(Keccak hash, UInt256 number, Address candidate)
        {
            CliqueConfig config = GetRinkebyConfig();
            SortedList <Address, UInt256> signers = new SortedList <Address, UInt256>(CliqueAddressComparer.Instance);

            signers.Add(_signer1, number - 2);
            signers.Add(_signer2, number - 1);
            signers.Add(_signer3, number - 3);
            List <Vote> votes = new List <Vote>();

            votes.Add(new Vote(_signer1, number - 2, candidate, true));
            votes.Add(new Vote(_signer3, number - 3, candidate, true));
            votes.Add(new Vote(_signer3, number - 6, _signer2, false));
            Dictionary <Address, Tally> tally = new Dictionary <Address, Tally>();

            tally[candidate]       = new Tally(true);
            tally[candidate].Votes = 2;
            tally[_signer2]        = new Tally(false);
            tally[_signer2].Votes  = 1;
            Snapshot snapshot = new Snapshot(config, null, number, hash, signers, tally);

            snapshot.Votes = votes;
            return(snapshot);
        }
Пример #10
0
        /// <summary>
        /// Run a assembly test suites.
        /// Update the status to AssemblyStatus.ERROR if any test suite fails, then AssemblyStatus.WARNING if any test suite is skipped or have warnings, then AssemblyStatus.UNKNOWN if any test suite is not run, then AssemblyStatus.PASS if succesfull.
        /// Registers any detected error to the test suite.
        /// </summary>
        /// <param name="asm"></param>
        /// <seealso cref="AssemblyStatus"/>
        public static void RunAssembly(Assembly asm)
        {
            Tally <TestSuiteStatus> tally = new Tally <TestSuiteStatus>();

            foreach (Type testSuite in GetTestSuites(asm))
            {
                RunTestSuite(testSuite);
                tally[GetTestSuiteStatus(testSuite)]++;
            }

            if (tally[TestSuiteStatus.ERROR] != 0)
            {
                SetAssemblyStatus(asm, AssemblyStatus.ERROR);
                SetAssemblyError(asm, new Exception($"{tally[TestSuiteStatus.ERROR]} test suite failed"));
            }
            else if (tally[TestSuiteStatus.SKIP] != 0)
            {
                SetAssemblyStatus(asm, AssemblyStatus.WARNING);
                SetAssemblyError(asm, new Exception($"{tally[TestSuiteStatus.SKIP]} test suite skipped"));
            }
            else if (tally[TestSuiteStatus.WARNING] != 0)
            {
                SetAssemblyStatus(asm, AssemblyStatus.WARNING);
                SetAssemblyError(asm, new Exception($"{tally[TestSuiteStatus.WARNING]} test suite have warnings"));
            }
            else if (tally[TestSuiteStatus.UNKNOWN] != 0)
            {
                SetAssemblyStatus(asm, AssemblyStatus.UNKNOWN);
                SetAssemblyError(asm, new Exception($"{tally[TestSuiteStatus.UNKNOWN]} test suite not run yet"));
            }
            else
            {
                SetAssemblyStatus(asm, AssemblyStatus.PASS);
                SetAssemblyError(asm, null);
            }
        }
Пример #11
0
        /// <summary>
        /// Create an NewLogger. 
        /// </summary>
        /// <param name="dir">base folder</param>
        /// <param name="fileName">name of the log</param>
        /// <param name="suffix">something to append to the fileName to make it unique</param>
        /// <param name="newFileCriterion">defines when to close current file and create a new file</param>
        /// <param name="maxLevel">maximum error level that gets logged, higher values get ignored</param>
        /// <param name="eventLevel">messages with an log level less than or equal to this number will also be sent to the OS Event Log</param>
        /// <param name="tallyLevel">for levels between 1 and tallyLevel, only show text first time, after that show a checksum and a count</param>
        private ErrorLogger(string dir, string fileName, string suffix, NewFile newFileCriterion, int maxLevel, int eventLevel, int tallyLevel)
        {
            try
            {
                _EventLevel = eventLevel;
                _TallyLevel = tallyLevel;
                _MaxLevel = maxLevel;
                _NewFile = newFileCriterion;
                _Tally = new Tally();
                _Name = fileName + suffix;
                _FileBaseName = Path.Combine(dir, _Name);

                if (_LogToDatabase == 1)
                {
                    byte[] bytes = new byte[0];
                  //  bool response = SaveLogToDatabase(bytes, 0);

                  //  if (!response) CreateFiles(dir, fileName, suffix);
                }
                else
                {
                    CreateFiles(dir, fileName, suffix);
                }
            }
            catch
            {
            }
        }
Пример #12
0
        /// <summary>
        /// Create an NewLogger. 
        /// </summary>
        /// <param name="udpLogServer">Server to send log info via udp to</param>
        /// <param name="port">Port to send log info via udp to</param>
        /// <param name="dir">base folder</param>
        /// <param name="fileName">name of the log</param>
        /// <param name="suffix">something to append to the fileName to make it unique</param>
        /// <param name="newFileCriterion">defines when to close current file and create a new file</param>
        /// <param name="maxLevel">maximum error level that gets logged, higher values get ignored</param>
        /// <param name="eventLevel">messages with an log level less than or equal to this number will also be sent to the OS Event Log</param>
        /// <param name="tallyLevel">for levels between 1 and tallyLevel, only show text first time, after that show a checksum and a count</param>
        private ErrorLogger(string udpLogServer, int port, string dir, string fileName, string suffix, NewFile newFileCriterion, int maxLevel, int eventLevel, int tallyLevel)
        {
            try
            {
                _UdpLogServer = (string.IsNullOrEmpty(udpLogServer)) ? "127.0.0.1" : udpLogServer;
                _UdpPort = port;
                _EventLevel = eventLevel;
                _TallyLevel = tallyLevel;
                _MaxLevel = maxLevel;
                _NewFile = newFileCriterion;
                _Tally = new Tally();
                _Name = fileName + suffix;
                _FileBaseName = Path.Combine(dir, _Name);

                if (_LogToDatabase == 1)
                {
                    //byte[] bytes = new byte[0];
                    //bool response = SaveLogToDatabase(bytes, 0);

                    //if (!response) CreateFiles(dir, fileName, suffix);
                }
                else
                {
                    CreateFiles(dir, fileName, suffix);
                }
            }
            catch
            {
            }
        }
Пример #13
0
        private static object Networkdays(List<Expression> p)
        {
            var date1 = (DateTime)p[0];
            var date2 = (DateTime)p[1];
            var bankHolidays = new List<DateTime>();
            if (p.Count == 3)
            {
                var t = new Tally {p[2]};

                bankHolidays.AddRange(t.Select(XLHelper.GetDate));
            }

            return BusinessDaysUntil(date1, date2, bankHolidays);
        }
Пример #14
0
        private static object SumIf(List<Expression> p)
        {
            // get parameters
            var range = p[0] as IEnumerable;
            var sumRange = p.Count < 3 ? range : p[2] as IEnumerable;
            var criteria = p[1].Evaluate();

            // build list of values in range and sumRange
            var rangeValues = new List<object>();
            foreach (var value in range)
            {
                rangeValues.Add(value);
            }
            var sumRangeValues = new List<object>();
            foreach (var value in sumRange)
            {
                sumRangeValues.Add(value);
            }

            // compute total
            var ce = new CalcEngine();
            var tally = new Tally();
            for (var i = 0; i < Math.Min(rangeValues.Count, sumRangeValues.Count); i++)
            {
                if (ValueSatisfiesCriteria(rangeValues[i], criteria, ce))
                {
                    tally.AddValue(sumRangeValues[i]);
                }
            }

            // done
            return tally.Sum();
        }
Пример #15
0
 private static object Sum(List<Expression> p)
 {
     var tally = new Tally();
     foreach (var e in p)
     {
         tally.Add(e);
     }
     return tally.Sum();
 }
Пример #16
0
 /// <summary>
 /// Bind event watcher to the class that handles running the tallies.
 /// </summary>
 private void BuildTally()
 {
     tally = new Tally(PageProvider, VoteCounter);
     tally.PropertyChanged += Tally_PropertyChanged;
 }
Пример #17
0
        public TallyTest()
        {
            BrandPlayer a = new BrandPlayer();

            a.add(new TubeBrand(1));
            a.add(new TubeBrand(1));
            a.add(new TubeBrand(1));
            a.add(new TubeBrand(1));

            a.add(new RopeBrand(1));
            a.add(new RopeBrand(1));
            a.add(new RopeBrand(1));
            a.add(new RopeBrand(1));

            a.add(new TenThousandBrand(1));
            a.add(new TenThousandBrand(1));
            a.add(new TenThousandBrand(1));
            a.add(new TenThousandBrand(1));

            a.add(new TubeBrand(9));
            a.add(new TubeBrand(9));
            a.add(new TubeBrand(9));
            a.add(new TubeBrand(9));

            a.add(new RopeBrand(9));
            a.add(new RopeBrand(9));
            a.add(new RopeBrand(9));
            a.add(new RopeBrand(9));


            //a.add(new WordBrand(4));
            a.add(new WordBrand(4));
            a.add(new WordBrand(4));

            ////a.add(new WordBrand(1));
            //a.add(new WordBrand(1));
            //a.add(new WordBrand(1));
            //a.add(new WordBrand(1));

            ////a.add(new WordBrand(2));
            //a.add(new WordBrand(2));
            //a.add(new WordBrand(2));
            //a.add(new WordBrand(2));

            ////a.add(new WordBrand(3));
            //a.add(new WordBrand(3));
            //a.add(new WordBrand(3));
            //a.add(new WordBrand(3));

            f = new Tally();

            Location   l   = new Location();
            AllPlayers all = new AllPlayers(4, 16);
            WordBrand  w   = new WordBrand(9);
            WordBrand  v   = new WordBrand(9);

            w.WhoPush = location.East;
            //v.WhoPush = location.Table;
            all.Show_Table.add(w);
            //all.NowPlayer.add(v);

            f.setPlayer(all);
            f.setPlayer(a);
            //l.next_Winer();
            f.setLocation(l, 0);

            f.ShowDialog();
        }
Пример #18
0
 private static object SumSq(List<Expression> p)
 {
     var t = new Tally(p);
     return t.Numerics().Sum(v => Math.Pow(v, 2));
 }
Пример #19
0
        private static object Subtotal(List<Expression> p)
        {
            var fId = (int)(Double)p[0];
            var tally = new Tally(p.Skip(1));

            switch (fId)
            {
                case 1:
                    return tally.Average();
                case 2:
                    return tally.Count();
                case 3:
                    return tally.CountA();
                case 4:
                    return tally.Max();
                case 5:
                    return tally.Min();
                case 6:
                    return tally.Product();
                case 7:
                    return tally.Std();
                case 8:
                    return tally.StdP();
                case 9:
                    return tally.Sum();
                case 10:
                    return tally.Var();
                case 11:
                    return tally.VarP();
                default:
                    throw new ArgumentException("Function not supported.");
            }
        }
Пример #20
0
        private static object Workday(List<Expression> p)
        {
            var startDate = (DateTime)p[0];
            var daysRequired = (int)p[1];

            if (daysRequired == 0) return startDate;
            if (daysRequired <  0) throw new ArgumentOutOfRangeException("DaysRequired must be >= 0.");

            var bankHolidays = new List<DateTime>();
            if (p.Count == 3)
            {
                var t = new Tally { p[2] };

                bankHolidays.AddRange(t.Select(XLHelper.GetDate));
            }
            var testDate = startDate.AddDays(((daysRequired / 7) + 2) * 7);
            return Workday(startDate, testDate, daysRequired, bankHolidays).NextWorkday(bankHolidays);
        }