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); }); }
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)); } }); }
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); }); }
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."); }); }