コード例 #1
0
        public void stringHashvRollingTest1()
        {
            int sLength = 50;

            SaveHumanity.InitPowers(sLength);
            SaveHumanity.BigPrime = 536870923;

            StringBuilder s1 = new StringBuilder(sLength);

            for (int ii = 0; ii < sLength; ii++)
            {
                s1.Append('a');
            }
            StringBuilder s2 = new StringBuilder(s1.ToString());

            s2.Append('z');

            long s1HashCalculated = SaveHumanity.CalculateStringHash(s1.ToString());

            s1[s1.Length - 1] = 'z';
            long s1ShiftedHashCalculated = SaveHumanity.CalculateStringHash(s1.ToString());

            long s2ShiftedHashCalculated = SaveHumanity.CalculateNewRollingHash(s1HashCalculated, s2.ToString(), sLength - 1);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(s1ShiftedHashCalculated, s2ShiftedHashCalculated);
            });
        }
コード例 #2
0
        public void RollingHashHackerrankTests(int testNum)
        {
            string          inFile = string.Format("{0}hackerrank{1}_input.txt", _sTestDataRootDir, testNum);
            List <string[]> oData  = GetInputData(inFile);

            int testCounter = 0;

            Assert.Multiple(() =>
            {
                foreach (string[] arr in oData)
                {
                    int mismatchHashCount = 0;

                    string p = arr[0];
                    string v = arr[1];

                    SaveHumanity.InitPowers(v.Length);
                    SaveHumanity.BigPrime = 536870923;

                    long rollingHash = SaveHumanity.CalculateStringHash(p.Substring(0, v.Length));

                    Stopwatch swCalcHash = new Stopwatch();
                    swCalcHash.Start();
                    for (int ii = 0; ii <= p.Length - v.Length; ii++)
                    {
                        string sub = p.Substring(ii, v.Length);

                        if (!swCalcHash.IsRunning)
                        {
                            swCalcHash.Start();
                        }
                        long subHash = SaveHumanity.CalculateStringHash(sub);
                        swCalcHash.Stop();

                        if (subHash != rollingHash)
                        {
                            mismatchHashCount++;
                        }
                        if (ii < p.Length - v.Length)
                        {
                            rollingHash = SaveHumanity.CalculateNewRollingHash(rollingHash, p.Substring(ii, v.Length + 1), v.Length - 1);
                        }
                    }

                    Debug.WriteLine("");

                    testCounter++;
                    Debug.WriteLine(string.Format("Test case {0} : Total hash calc time (s): {1}", testCounter, (float)swCalcHash.ElapsedMilliseconds / 1000.0));
                    Debug.WriteLine("");
                    Assert.AreEqual(0, mismatchHashCount, string.Format("Test case {0} : found {1} instances of the rolling hash not matching the real hash", testCounter, mismatchHashCount));
                }
            });
        }
コード例 #3
0
        public void PerformanceHackerrankTests(int testNum)
        {
            string          inFile = string.Format("{0}hackerrank{1}_input.txt", _sTestDataRootDir, testNum);
            List <string[]> oData  = GetInputData(inFile);

            Assert.Multiple(() =>
            {
                int testCounter  = 0;
                long elapsedTime = 0;
                foreach (string[] arr in oData)
                {
                    Stopwatch swInitPowers      = new Stopwatch();
                    Stopwatch swCalcRollingHash = new Stopwatch();
                    Stopwatch swCalcVirusHash   = new Stopwatch();

                    string p = arr[0];
                    string v = arr[1];

                    swInitPowers.Start();
                    SaveHumanity.InitPowers(v.Length);
                    swInitPowers.Stop();

                    SaveHumanity.BigPrime = 536870923;

                    swCalcVirusHash.Start();
                    long rollingHash = SaveHumanity.CalculateStringHash(p.Substring(0, v.Length));
                    swCalcVirusHash.Stop();

                    swCalcRollingHash.Start();
                    for (int ii = 0; ii <= p.Length - v.Length; ii++)
                    {
                        string sub = p.Substring(ii, v.Length);
                        if (ii < p.Length - v.Length)
                        {
                            rollingHash = SaveHumanity.CalculateNewRollingHash(rollingHash, p.Substring(ii, v.Length + 1), v.Length - 1);
                        }
                    }
                    swCalcRollingHash.Stop();

                    Debug.WriteLine(string.Format("Test case {0} : InitPowers (ms): {1}", testCounter, swInitPowers.ElapsedMilliseconds));
                    Debug.WriteLine(string.Format("Test case {0} : Calc Virus Hash (ms): {1}", testCounter, swCalcVirusHash.ElapsedMilliseconds));
                    Debug.WriteLine(string.Format("Test case {0} : Total rollinghash calc time (s): {1}", testCounter, (float)swCalcRollingHash.ElapsedMilliseconds / 1000.0));
                    Debug.WriteLine("");

                    testCounter++;

                    elapsedTime = swCalcRollingHash.ElapsedMilliseconds;
                }
                Assert.IsTrue(elapsedTime <= 10000);
            });
        }
コード例 #4
0
        public void rollingStringHashTest1()
        {
            SaveHumanity.InitPowers(5);
            SaveHumanity.BigPrime = 536870923;

            Assert.Multiple(() =>
            {
                Assert.AreEqual(48128797, SaveHumanity.CalculateNewRollingHash(SaveHumanity.CalculateStringHash("hello"), "hellow", 4), "hash 1 is jacked.");
                Assert.AreEqual(51329857, SaveHumanity.CalculateNewRollingHash(48128797, "ellowo", 4), "hash 2 is jacked.");
                Assert.AreEqual(51387788, SaveHumanity.CalculateNewRollingHash(51329857, "llowor", 4), "hash 3 is jacked.");
                Assert.AreEqual(52893988, SaveHumanity.CalculateNewRollingHash(51387788, "loworl", 4), "hash 4 is jacked.");
                Assert.AreEqual(56411052, SaveHumanity.CalculateNewRollingHash(52893988, "oworld", 4), "hash 5 is jacked.");
            });
        }
コード例 #5
0
        public void stringHashTest1()
        {
            SaveHumanity.InitPowers(5);
            SaveHumanity.BigPrime = 536870923;

            Assert.Multiple(() =>
            {
                Assert.AreEqual(49376607, SaveHumanity.CalculateStringHash("hello"), "string hash 1 is messed up");
                Assert.AreEqual(48128797, SaveHumanity.CalculateStringHash("ellow"), "string hash 2 is messed up");
                Assert.AreEqual(51329857, SaveHumanity.CalculateStringHash("llowo"), "string hash 3 is messed up");
                Assert.AreEqual(51387788, SaveHumanity.CalculateStringHash("lowor"), "string hash 4 is messed up");
                Assert.AreEqual(52893988, SaveHumanity.CalculateStringHash("oworl"), "string hash 5 is messed up");
                Assert.AreEqual(56411052, SaveHumanity.CalculateStringHash("world"), "string hash 6 is messed up");
            });
        }
コード例 #6
0
        public void doHashVariances()
        {
            string s           = "virus";
            char   variantChar = 'a';

            SaveHumanity.InitPowers(5);
            SaveHumanity.BigPrime = 536870923;

            for (int jj = s.Length - 1; jj >= 0; jj--)
            {
                char[] sArr = s.ToArray();
                for (int ii = 0; ii < 26; ii++)
                {
                    sArr[jj] = (char)(variantChar + ii);
                    string foo = new string(sArr);
                    Debug.WriteLine(string.Format("variant char: {0}, hash = {1}", foo, SaveHumanity.CalculateStringHash(foo)));
                }
            }
        }