//ok, but new method is better //private byte[] ConvertStringToItsHash(string s) //{ // //if range or limit, not the count!!! // var primeNumbers = PrimeNumbersGenerator.Generate(s.Length / 2); // int arraysCount = (primeNumbers.Count + 1) * 2; // char[][] result = new char[arraysCount][]; // char[] charArray = s.ToCharArray(); // result[0] = new char[charArray.Length]; // Array.Copy(charArray, result[0], charArray.Length); // for (int i = 0; i < primeNumbers.Count; i++) // { // result[i + 1] = new char[(charArray.Length - 1) / primeNumbers[i]]; // for(int k = 0; k < result[i + 1].Length; k++) // { // result[i + 1][k] = charArray[primeNumbers[i] * (k + 1)]; // } // } // Array.Reverse(charArray); // result[arraysCount - 1] = new char[charArray.Length]; // Array.Copy(charArray, result[arraysCount - 1], charArray.Length); // for (int i = 0; i < primeNumbers.Count; i++) // { // result[i + 1] = new char[(charArray.Length -1) / primeNumbers[i]]; // for (int k = 0; k < result[i + 1].Length; k++) // { // result[i + 1][k] = charArray[primeNumbers[i] * (k + 1)]; // } // } // byte[] byteResult = new byte[32 * arraysCount]; // using (HashAlgorithm algorithm = SHA256.Create()) // { // for (int i = 0; i < result.Length; i++) // { // var str = new string(result[i]); // var bytes = algorithm.ComputeHash(Encoding.UTF8.GetBytes(str)); // Array.Copy(bytes, 0, byteResult, 32 * i, bytes.Length); // } // } // return byteResult; //} private byte[] GetHash(string s) { int arraysCount; if (s.Length <= 30) { var primeNumbers = PrimeNumbersGenerator.Generate(s.Length / 2); arraysCount = 2 * s.Length - primeNumbers[(primeNumbers.Count - 1) / 2]; } else { arraysCount = 60; } byte[] bytesResult = new byte[32 * arraysCount]; using (HashAlgorithm algorithm = SHA256.Create()) { var bytes = algorithm.ComputeHash(Encoding.UTF8.GetBytes(s)); Array.Copy(bytes, 0, bytesResult, 0, bytes.Length); for (int i = 0; i < arraysCount - 1; i++) { bytes = algorithm.ComputeHash(bytes); Array.Copy(bytes, 0, bytesResult, 32 * (i + 1), bytes.Length); } } return(bytesResult); }
public void GenerateListUpUntilLimit(int limit, int[] expectedPrimeNumbers) { var generator = new PrimeNumbersGenerator(); var resultPrimeNumbers = generator.Generate(limit); Assert.Equal(expectedPrimeNumbers, resultPrimeNumbers); }
public void IsTypeOfPrimeNumbersGenerator(int value) { Assert.IsInstanceOfType(new PrimeNumbersGenerator(), typeof(PrimeNumbersGenerator)); Assert.IsInstanceOfType(PrimeNumbersGenerator.GetSingleton(), typeof(PrimeNumbersGenerator)); List <int> lista = new List <int>(); Assert.IsInstanceOfType(PrimeNumbersGenerator.GetSingleton().GetValuesTo( out lista, value), typeof(PrimeNumbersGenerator)); Assert.IsInstanceOfType(new PrimeNumbersGenerator().FindPrimesTo(value), typeof(PrimeNumbersGenerator)); }
public void IsExpectedNumberOfPrimes(int value, int exptected) { int metodaZwracajacaIloscLiczbPierwszychDoWartosci(int x) { PrimeNumbersGenerator.GetSingleton().GetValuesTo(out List <int> listOfInts, x); return(listOfInts.Count()); } Assert.AreEqual(exptected, metodaZwracajacaIloscLiczbPierwszychDoWartosci(value)); }
public void IsFalseExpectedNumberOfPrimes(int value, int expected) { CheckIsExpectedNumberOfPrimes metodaZwracajacaIloscLiczbPierwszychDoWartosci = delegate(int x) { PrimeNumbersGenerator.GetSingleton().GetValuesTo(out List <int> listOfInts, x); return(listOfInts.Count()); }; Assert.IsFalse(expected.Equals(metodaZwracajacaIloscLiczbPierwszychDoWartosci(value))); Assert.AreNotEqual(expected, metodaZwracajacaIloscLiczbPierwszychDoWartosci(value)); }
public void Should_Not_Be_Prime_If_Even(int NumToCheck) { //Arrange var generator = new PrimeNumbersGenerator(); //Act var result = generator.GetPrimeNumber(1); //Assert Assert.IsTrue(NumToCheck % 2 == 0); }
public void Should_Throw_Exception_User_Input_Is_Negative(int NumToCheck) { //Arrange var generator = new PrimeNumbersGenerator(); //act var result = generator.GetPrimeNumber(-1); Assert.IsTrue(NumToCheck > 2); }
public void DoesSpiralaUlamaHaveRightAmountOfPoints(int generatePrimesToValue, int expectedNumberOfPrimesAndPoints) { Tuple <List <Tuple <int, int> >, double> tuple = new Tuple <List <Tuple <int, int> >, double>(new List <Tuple <int, int> >(), 0); PrimeNumbersGenerator.GetSingleton().FindPrimesTo(generatePrimesToValue); SpiralaUlamaGenerator spirala = new SpiralaUlamaGenerator().GetValues(100, 100, out tuple); Assert.Equals(tuple.Item1.Count, expectedNumberOfPrimesAndPoints); Assert.IsTrue(tuple.Item2 > 0); }
/// private async void ButtonZmienZakres_Click(object sender, RoutedEventArgs e) { string s = textBoxTuWpiszLiczbe.Text; if (Int32.TryParse(s, out int result)) { await Task.Run(() => PrimeNumbersGenerator.GetSingleton().FindPrimesTo(result)); await textBoxInfoOZakresie.Dispatcher.InvokeAsync( () => textBoxInfoOZakresie.Text = $"Aktualny zakres Liczb Pierwszych do: {result})"); } }
public void Should_Throw_Exception_User_Input_Equals_Zero() { //Arrange var generator = new PrimeNumbersGenerator(); try { //act var result = generator.GetPrimeNumber(0); } catch (Exception ex) { Assert.AreEqual("Gotta be greater than 0", ex.Message); } }
public void ArePrimesNumbersToStringEqualsToExpectedStringOfPrimes() { Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(1).ToString().Equals("")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(2).ToString().Equals("2")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(3).ToString().Equals("2 3")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(4).ToString().Equals("2 3")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(5).ToString().Equals("2 3 5")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(6).ToString().Equals("2 3 5")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(7).ToString().Equals("2 3 5 7")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(8).ToString().Equals("2 3 5 7")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(9).ToString().Equals("2 3 5 7")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(10).ToString().Equals("2 3 5 7")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(11).ToString().Equals("2 3 5 7 11")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(12).ToString().Equals("2 3 5 7 11")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(13).ToString().Equals("2 3 5 7 11 13")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(30).ToString().Equals( "2 3 5 7 11 13 17 19 23 29")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(100).ToString().Equals( "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(200).ToString().Equals( "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 " + "101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199")); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(500).ToString().Equals( "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 " + "101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 " + "211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 " + "337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 " + "461 463 467 479 487 491 499" )); Assert.IsTrue(PrimeNumbersGenerator.GetSingleton().FindPrimesTo(1000).ToString().Equals( "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 " + "101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 " + "211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 " + "337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 " + "461 463 467 479 487 491 499 " + "503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 " + "643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 " + "797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 " + "947 953 967 971 977 983 991 997")); }
private async void WyswietlLiczbyPierwsze() { await Task.Run(() => textBoxDoWyswietlaniaLiczbPierwszych.Dispatcher.InvokeAsync( () => textBoxDoWyswietlaniaLiczbPierwszych.Text = PrimeNumbersGenerator.GetSingleton().ToString())); }
public void GenerateEmptyListUpUntilOne(int limit) { var generator = new PrimeNumbersGenerator(); Assert.Empty(generator.Generate(limit)); }
public void GenerateListUpUntilLimit(int limit, int[] expected) { var generator = new PrimeNumbersGenerator(); Assert.Equal(expected, generator.Generate(limit)); }