//Java/JavaClasses/testAlgorithms.java //Matthew Ellison // Created: 03-02-19 //Modified: 03-02-19 //This program runs tests on all function in the Algorithms library /* Copyright (C) 2019 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ import mattrixwv.Algorithms; import mattrixwv.Stopwatch; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; public class testAlgorithms{ public static void main(String[] argv){ //Create a timer so you can time the execution time of the tests Stopwatch timer = new Stopwatch(); timer.start(); //Test getPrimes testGetPrimes(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getNumPrimes timer.start(); testGetNumPrimes(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getFactors timer.start(); testGetFactors(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getDivisors timer.start(); testGetDivisors(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getFib timer.start(); testGetFib(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getAllFib timer.start(); testGetAllFib(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Tets getSum timer.start(); testGetSum(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Test getProd timer.start(); testGetProd(); timer.stop(); System.out.println("It took " + timer.getStr() + " to run this test\n"); //Print a closing message System.out.println("Tests completed"); } //This function tests the getPrimes function private static void testGetPrimes(){ Boolean failed = false; //Holds whether a test was failed //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(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)); Integer topNum = 100; ArrayList answer = Algorithms.getPrimes(topNum); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getPrimes failed the first test"); failed = true; } //Test 2 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); BigInteger bigTopNum = BigInteger.valueOf(100); ArrayList bigAnswer = Algorithms.getPrimes(bigTopNum); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getPrimes failed the second test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getPrimes passed all tests"); } } //This function tests the getNumPrimes function private static void testGetNumPrimes(){ Boolean failed = false; //Holds whether a test was failed //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(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)); Integer numPrimes = 25; ArrayList answer = Algorithms.getNumPrimes(numPrimes); //Print an error message if the function return the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getNumPrimes failed at the first test"); failed = true; } //Test 2 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); BigInteger bigTopNum = BigInteger.valueOf(25); ArrayList bigAnswer = Algorithms.getNumPrimes(bigTopNum); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getPrimes failed the second test"); System.out.println("Correct Answer = " + bigCorrectAnswer.toString()); System.out.println("Answer = " + bigAnswer.toString()); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getNumPrimes passed all tests"); } } //This function tests the getFactors function private static void testGetFactors(){ Boolean failed = false; //Holds whether a test was failed //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 2, 5, 5)); Integer number = 100; ArrayList answer = Algorithms.getFactors(number); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getFactors failed the first test"); failed = true; } //Test 2 correctAnswer = new ArrayList(Arrays.asList(2, 7, 7)); number = 98; answer = Algorithms.getFactors(number); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getFactors failed the second test"); failed = true; } //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7))); BigInteger bigNumber = BigInteger.valueOf(98); ArrayList bigAnswer = Algorithms.getFactors(bigNumber); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getFactors failed the third test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getFactors passed all tests"); } } //This function tests the getDivisors function private static void testGetDivisors(){ Boolean failed = false; //Holds whether a test was failed //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100)); Integer topNum = 100; ArrayList answer = Algorithms.getDivisors(topNum); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getDivisors failed the first test"); failed = true; } //Test 2 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(10), BigInteger.valueOf(20), BigInteger.valueOf(25), BigInteger.valueOf(50), BigInteger.valueOf(100))); BigInteger bigTopNum = BigInteger.valueOf(100); ArrayList bigAnswer = Algorithms.getDivisors(bigTopNum); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getDivisors failed the second test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getDivisors passed all tests"); } } //This function tests the getFib function private static void testGetFib(){ Boolean failed = false; //Holds whether a test was failed //Test 1 Integer correctAnswer = 144; Integer number = 12; Integer answer = Algorithms.getFib(number); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getFib failed the first test"); failed = true; } //Test 2 correctAnswer = 6765; number = 20; answer = Algorithms.getFib(number); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getFib failed the seconds test"); failed = true; } //Test 3 BigInteger bigCorrectAnswer = new BigInteger("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816"); BigInteger bigNumber = BigInteger.valueOf(4782); BigInteger bigAnswer = Algorithms.getFib(bigNumber); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getFib failed the third test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getFib passed all tests"); } } //This function tests the getAllFib function private static void testGetAllFib(){ Boolean failed = false; //Holds whether a test was failed //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)); Integer highestNumber = 100; ArrayList answer = Algorithms.getAllFib(highestNumber); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getAllFib failed the first test"); failed = true; } //Test 2 correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987)); highestNumber = 1000; answer = Algorithms.getAllFib(highestNumber); //Print an error message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getAllFib failed the second test"); failed = true; } //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(13), BigInteger.valueOf(21), BigInteger.valueOf(34), BigInteger.valueOf(55), BigInteger.valueOf(89))); BigInteger bigHighestNumber = BigInteger.valueOf(100); ArrayList bigAnswer = Algorithms.getAllFib(bigHighestNumber); //Print an error message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getAllFib failed the third test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getAllFib passed all tests"); } } //This function tests the getSum function private static void testGetSum(){ Boolean failed = false; //Holds whether a test was failed //Test 1 Integer correctAnswer = 0; ArrayList numbers = new ArrayList(); Integer answer = Algorithms.getSum(numbers); //Print a message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getSum failed the first test"); failed = true; } //Test 2 correctAnswer = 118; numbers = new ArrayList(Arrays.asList(2, 2, 3, 3, 4, 4, 100)); answer = Algorithms.getSum(numbers); //Print a message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getSum failed the second test"); failed = true; } //Test 3 BigInteger bigCorrectAnswer = BigInteger.valueOf(118); ArrayList bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); BigInteger bigAnswer = Algorithms.getBigSum(bigNumbers); //Print a message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getSum failed the third test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getSum passed all tests"); } } //This function tests the getProd function private static void testGetProd(){ Boolean failed = false; //Holds whether a test was failed //Test 1 Integer correctAnswer = 0; ArrayList numbers = new ArrayList(); Integer answer = Algorithms.getProd(numbers); //Print a message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getProd failed the first test"); failed = true; } //Test 2 correctAnswer = 57600; numbers = new ArrayList(Arrays.asList(2, 2, 3, 3, 4, 4, 100)); answer = Algorithms.getProd(numbers); //Print a message if the function returned the wrong answer if(!correctAnswer.equals(answer)){ System.out.println("getProd failed the second test"); failed = true; } //Test 3 BigInteger bigCorrectAnswer = BigInteger.valueOf(57600); ArrayList bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); BigInteger bigAnswer = Algorithms.getBigProd(bigNumbers); //Print a message if the function returned the wrong answer if(!bigCorrectAnswer.equals(bigAnswer)){ System.out.println("getProd failed the third test"); failed = true; } //Print a message if all of the tests passed if(!failed){ System.out.println("getProd passed all tests"); } } }