mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 15:03:58 -05:00
Added junit testing
This commit is contained in:
350
src/test/java/mattrixwv/TestAlgorithms.java
Normal file
350
src/test/java/mattrixwv/TestAlgorithms.java
Normal file
@@ -0,0 +1,350 @@
|
||||
//src/test/java/mattrixwv/TestAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-07-20
|
||||
//Modified: 06-07-20
|
||||
//This class holds many algorithms that I have found it useful to keep around
|
||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||
/*
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
package mattrixwv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestAlgorithms{
|
||||
@Test
|
||||
public void testGetPrimes(){
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> answer = Algorithms.getPrimes(topNum);
|
||||
assertEquals("getPrimes Integer failed", correctAnswer, answer);
|
||||
|
||||
//Test 2
|
||||
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L));
|
||||
Long longTopNum = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getPrimes(longTopNum);
|
||||
assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> bigAnswer = Algorithms.getPrimes(bigTopNum);
|
||||
assertEquals("getPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetNumPrimes(){
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> answer = Algorithms.getNumPrimes(numPrimes);
|
||||
assertEquals("getNumPrimes Integer failed", correctAnswer, answer);
|
||||
|
||||
//Test 2
|
||||
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L));
|
||||
Long longNumPrimes = 25L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getNumPrimes(longNumPrimes);
|
||||
assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> bigAnswer = Algorithms.getNumPrimes(bigTopNum);
|
||||
assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetFactors(){
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
|
||||
Integer number = 100;
|
||||
ArrayList<Integer> answer = Algorithms.getFactors(number);
|
||||
assertEquals("getFactors Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 7, 7));
|
||||
number = 98;
|
||||
answer = Algorithms.getFactors(number);
|
||||
assertEquals("getFactors Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Test 3
|
||||
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 2L, 5L, 5L));
|
||||
Long longNumber = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getFactors(longNumber);
|
||||
assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 4
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7)));
|
||||
BigInteger bigNumber = BigInteger.valueOf(98);
|
||||
ArrayList<BigInteger> bigAnswer = Algorithms.getFactors(bigNumber);
|
||||
assertEquals("getFactors BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetDivisors(){
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100));
|
||||
Integer topNum = 100;
|
||||
ArrayList<Integer> answer = Algorithms.getDivisors(topNum);
|
||||
assertEquals("getDivisors Integer failed", correctAnswer, answer);
|
||||
|
||||
//Test 2
|
||||
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L));
|
||||
Long longTopNum = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getDivisors(longTopNum);
|
||||
assertEquals("getDivisors Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> bigAnswer = Algorithms.getDivisors(bigTopNum);
|
||||
assertEquals("getDivisors BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetFib(){
|
||||
//Test 1
|
||||
Integer correctAnswer = 144;
|
||||
Integer number = 12;
|
||||
Integer answer = Algorithms.getFib(number);
|
||||
assertEquals("getDivisors Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = 6765;
|
||||
number = 20;
|
||||
answer = Algorithms.getFib(number);
|
||||
assertEquals("getDivisors Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Test 3
|
||||
Long longCorrectAnswer = 6765L;
|
||||
Long longNumber = 20L;
|
||||
Long longAnswer = Algorithms.getFib(longNumber);
|
||||
assertEquals("getDivisors Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 4
|
||||
BigInteger bigCorrectAnswer = new BigInteger("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816");
|
||||
BigInteger bigNumber = BigInteger.valueOf(4782);
|
||||
BigInteger bigAnswer = Algorithms.getFib(bigNumber);
|
||||
assertEquals("getDivisors BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetAllFib(){
|
||||
//Test 1
|
||||
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89));
|
||||
Integer highestNumber = 100;
|
||||
ArrayList<Integer> answer = Algorithms.getAllFib(highestNumber);
|
||||
assertEquals("getAllFib Integer 1 failed", correctAnswer, answer);
|
||||
|
||||
//Test 2
|
||||
correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987));
|
||||
highestNumber = 1000;
|
||||
answer = Algorithms.getAllFib(highestNumber);
|
||||
assertEquals("getAllFib Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Test 3
|
||||
ArrayList<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L));
|
||||
Long longHighestNumber = 1000L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getAllFib(longHighestNumber);
|
||||
assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 4
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> bigAnswer = Algorithms.getAllFib(bigHighestNumber);
|
||||
assertEquals("getAllFib BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testFactorial(){
|
||||
//Integer
|
||||
//Test 1
|
||||
Integer correctAnswer = 720;
|
||||
Integer number = 6;
|
||||
Integer answer = Algorithms.factorial(number);
|
||||
assertEquals("factorial Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = 479001600;
|
||||
number = 12;
|
||||
answer = Algorithms.factorial(number);
|
||||
assertEquals("factorial Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Long
|
||||
//Test 3
|
||||
Long correctAnswerLong = 720L;
|
||||
Long numberLong = 6L;
|
||||
Long answerLong = Algorithms.factorial(numberLong);
|
||||
assertEquals("factorial Long 1 failed", correctAnswerLong, answerLong);
|
||||
//Test 4
|
||||
correctAnswerLong = 479001600L;
|
||||
numberLong = 12L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
assertEquals("factorial Long 2 failed", correctAnswerLong, answerLong);
|
||||
//Test 5
|
||||
correctAnswerLong = 2432902008176640000L;
|
||||
numberLong = 20L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
assertEquals("factorial Long 3 failed", correctAnswerLong, answerLong);
|
||||
|
||||
//BigInteger
|
||||
//Test 6
|
||||
BigInteger correctAnswerBig = BigInteger.valueOf(720L);
|
||||
BigInteger numberBig = BigInteger.valueOf(6);
|
||||
BigInteger answerBig = Algorithms.factorial(numberBig);
|
||||
assertEquals("factorial BigInteger 1 failed", correctAnswerBig, answerBig);
|
||||
//Test 7
|
||||
correctAnswerBig = BigInteger.valueOf(479001600L);
|
||||
numberBig = BigInteger.valueOf(12);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
assertEquals("factorial BigInteger 2 failed", correctAnswerBig, answerBig);
|
||||
//Test 8
|
||||
correctAnswerBig = BigInteger.valueOf(2432902008176640000L);
|
||||
numberBig = BigInteger.valueOf(20L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
assertEquals("factorial BigInteger 3 failed", correctAnswerBig, answerBig);
|
||||
//Test 9
|
||||
correctAnswerBig = new BigInteger("265252859812191058636308480000000");
|
||||
numberBig = BigInteger.valueOf(30L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
assertEquals("factorial BigInteger 4 failed", correctAnswerBig, answerBig);
|
||||
}
|
||||
@Test
|
||||
public void testGetSum(){
|
||||
//Test 1
|
||||
Integer correctAnswer = 0;
|
||||
ArrayList<Integer> numbers = new ArrayList<Integer>();
|
||||
Integer answer = Algorithms.getSum(numbers);
|
||||
assertEquals("getSum Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = 118;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(2, 2, 3, 3, 4, 4, 100));
|
||||
answer = Algorithms.getSum(numbers);
|
||||
assertEquals("getSum Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Test 3
|
||||
Long longCorrectAnswer = 118L;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L));
|
||||
Long longAnswer = Algorithms.getLongSum(longNumbers);
|
||||
assertEquals("getSum Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 4
|
||||
BigInteger bigCorrectAnswer = BigInteger.valueOf(118);
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(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);
|
||||
assertEquals("getSum BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testGetProd(){
|
||||
//Test 1
|
||||
Integer correctAnswer = 0;
|
||||
ArrayList<Integer> numbers = new ArrayList<Integer>();
|
||||
Integer answer = Algorithms.getProd(numbers);
|
||||
assertEquals("getProd Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = 57600;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(2, 2, 3, 3, 4, 4, 100));
|
||||
answer = Algorithms.getProd(numbers);
|
||||
assertEquals("getProd Integer 2 failed", correctAnswer, answer);
|
||||
|
||||
//Test 3
|
||||
Long longCorrectAnswer = 57600L;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L));
|
||||
Long longAnswer = Algorithms.getLongProd(longNumbers);
|
||||
assertEquals("getProd Long failed", longCorrectAnswer, longAnswer);
|
||||
|
||||
//Test 4
|
||||
BigInteger bigCorrectAnswer = BigInteger.valueOf(57600);
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(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);
|
||||
assertEquals("getProd BigInteger failed", bigCorrectAnswer, bigAnswer);
|
||||
}
|
||||
@Test
|
||||
public void testIsFound(){
|
||||
//Integer
|
||||
//Test 1
|
||||
Boolean correctAnswer = true;
|
||||
ArrayList<Integer> numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
Boolean answer = Algorithms.isFound(numbers, 0);
|
||||
assertEquals("isFound Integer 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
correctAnswer = true;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 9);
|
||||
assertEquals("isFound Integer 2 failed", correctAnswer, answer);
|
||||
//Test 3
|
||||
correctAnswer = true;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 5);
|
||||
assertEquals("isFound Integer 3 failed", correctAnswer, answer);
|
||||
//Test 4
|
||||
correctAnswer = false;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 10);
|
||||
assertEquals("isFound Integer 4 failed", correctAnswer, answer);
|
||||
|
||||
//Test 5
|
||||
correctAnswer = true;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 0L);
|
||||
assertEquals("isFound Long 1 failed", correctAnswer, answer);
|
||||
//Test 6
|
||||
correctAnswer = true;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 9L);
|
||||
assertEquals("isFound Long 2 failed", correctAnswer, answer);
|
||||
//Test 7
|
||||
correctAnswer = true;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 5L);
|
||||
assertEquals("isFound Long 3 failed", correctAnswer, answer);
|
||||
//Test 8
|
||||
correctAnswer = false;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 10L);
|
||||
assertEquals("isFound Long 4 failed", correctAnswer, answer);
|
||||
|
||||
//Test 9
|
||||
correctAnswer = true;
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(0L));
|
||||
assertEquals("isFound BigInteger 1 failed", correctAnswer, answer);
|
||||
//Test 10
|
||||
correctAnswer = true;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(9L));
|
||||
assertEquals("isFound BigInteger 2 failed", correctAnswer, answer);
|
||||
//Test 11
|
||||
correctAnswer = true;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(5L));
|
||||
assertEquals("isFound BigInteger 3 failed", correctAnswer, answer);
|
||||
//Test 12
|
||||
correctAnswer = false;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(10L));
|
||||
assertEquals("isFound BigInteger 4 failed", correctAnswer, answer);
|
||||
}
|
||||
@Test
|
||||
public void testGetPermutations(){
|
||||
//Test 1
|
||||
String permString = "012";
|
||||
ArrayList<String> correctAnswer = new ArrayList<String>(Arrays.asList("012", "021", "102", "120", "201", "210"));
|
||||
ArrayList<String> answer = Algorithms.getPermutations(permString);
|
||||
assertEquals("getPermutations failed", correctAnswer, answer);
|
||||
}
|
||||
}
|
||||
64
src/test/java/mattrixwv/TestStopwatch.java
Normal file
64
src/test/java/mattrixwv/TestStopwatch.java
Normal file
@@ -0,0 +1,64 @@
|
||||
//src/test/java/mattrixwv/TestStopwatch.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-07-20
|
||||
//Modified: 06-07-20
|
||||
//This class holds many algorithms that I have found it useful to keep around
|
||||
//As such all of the functions in here are static and meant to be used as stand alone functions
|
||||
/*
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
package mattrixwv;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestStopwatch{
|
||||
private static final Integer NUM_TO_RUN = 100000;
|
||||
private static final Double ALLOWANCE = .0000000001;
|
||||
@Test
|
||||
public void testStartStop(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
timer.stop();
|
||||
//If it gets to here without throwing an exception everything went well
|
||||
}
|
||||
@Test
|
||||
public void testConversion(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
Integer sum = 0;
|
||||
//Start the timer
|
||||
timer.start();
|
||||
//Do something to run some time
|
||||
for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){
|
||||
sum += cnt;
|
||||
}
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
//Assert something so the sum isn't ignored during compile
|
||||
assertNotEquals("You really messed up", sum, Integer.valueOf(0));
|
||||
//Check that the different resolutions work out correctly
|
||||
Double nano = timer.getNano();
|
||||
assertEquals("Micro resolution test failed", timer.getMicro(), (nano / 1000D), ALLOWANCE);
|
||||
assertEquals("Milli resolution test failed", timer.getMilli(), (nano / 1000000D), ALLOWANCE);
|
||||
assertEquals("Second resolution test failed", timer.getSecond(), (nano / 1000000000D), ALLOWANCE);
|
||||
assertEquals("Minute resolution test failed", timer.getMinute(), (nano / 60000000000D), ALLOWANCE);
|
||||
assertEquals("Hour resolution test failed", timer.getHour(), (nano / 3600000000000D), ALLOWANCE);
|
||||
}
|
||||
}
|
||||
@@ -1,734 +0,0 @@
|
||||
//Java/JavaClasses/testAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 03-02-19
|
||||
//Modified: 03-24-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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
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");
|
||||
|
||||
//Test factorial
|
||||
timer.start();
|
||||
testFactorial();
|
||||
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");
|
||||
|
||||
//Test getProd
|
||||
timer.start();
|
||||
testIsFound();
|
||||
timer.stop();
|
||||
System.out.println("It took " + timer.getStr() + " to run this test\n");
|
||||
|
||||
timer.start();
|
||||
testGetPermutations();
|
||||
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<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> 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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L));
|
||||
Long longTopNum = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getPrimes(longTopNum);
|
||||
//Print an error message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getPrimes failed the second test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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 third 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<Integer> correctAnswer = new ArrayList<Integer>(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<Integer> 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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L));
|
||||
Long longNumPrimes = 25L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getNumPrimes(longNumPrimes);
|
||||
//Print an error message if the function return the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getNumPrimes failed at the second test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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 third test");
|
||||
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<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
|
||||
Integer number = 100;
|
||||
ArrayList<Integer> 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<Integer>(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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(2L, 2L, 5L, 5L));
|
||||
Long longNumber = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getFactors(longNumber);
|
||||
//Print an error message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getFactors failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7)));
|
||||
BigInteger bigNumber = BigInteger.valueOf(98);
|
||||
ArrayList<BigInteger> 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 fourth 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<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100));
|
||||
Integer topNum = 100;
|
||||
ArrayList<Integer> 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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L));
|
||||
Long longTopNum = 100L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getDivisors(longTopNum);
|
||||
//Print an error message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getDivisors failed the second test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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 third 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
|
||||
Long longCorrectAnswer = 6765L;
|
||||
Long longNumber = 20L;
|
||||
Long longAnswer = Algorithms.getFib(longNumber);
|
||||
//Print an error message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getFib failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
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 fourth 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<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89));
|
||||
Integer highestNumber = 100;
|
||||
ArrayList<Integer> 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<Integer>(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<Long> longCorrectAnswer = new ArrayList<Long>(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L));
|
||||
Long longHighestNumber = 1000L;
|
||||
ArrayList<Long> longAnswer = Algorithms.getAllFib(longHighestNumber);
|
||||
//Print an error message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getAllFib failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
ArrayList<BigInteger> bigCorrectAnswer = new ArrayList<BigInteger>(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<BigInteger> 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 fourth 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 factorial function
|
||||
private static void testFactorial(){
|
||||
Boolean failed = false; //Holds whether a test was failed
|
||||
|
||||
//Test 1
|
||||
Integer correctAnswer = 720;
|
||||
Integer number = 6;
|
||||
Integer answer = Algorithms.factorial(number);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswer.equals(answer)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 2
|
||||
correctAnswer = 479001600;
|
||||
number = 12;
|
||||
answer = Algorithms.factorial(number);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswer.equals(answer)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
Long correctAnswerLong = 720L;
|
||||
Long numberLong = 6L;
|
||||
Long answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 4
|
||||
correctAnswerLong = 479001600L;
|
||||
numberLong = 12L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 5
|
||||
correctAnswerLong = 2432902008176640000L;
|
||||
numberLong = 20L;
|
||||
answerLong = Algorithms.factorial(numberLong);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerLong.equals(answerLong)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 6
|
||||
BigInteger correctAnswerBig = BigInteger.valueOf(720L);
|
||||
BigInteger numberBig = BigInteger.valueOf(6);
|
||||
BigInteger answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 7
|
||||
correctAnswerBig = BigInteger.valueOf(479001600L);
|
||||
numberBig = BigInteger.valueOf(12);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 8
|
||||
correctAnswerBig = BigInteger.valueOf(2432902008176640000L);
|
||||
numberBig = BigInteger.valueOf(20L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
//Test 9
|
||||
correctAnswerBig = new BigInteger("265252859812191058636308480000000");
|
||||
numberBig = BigInteger.valueOf(30L);
|
||||
answerBig = Algorithms.factorial(numberBig);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!correctAnswerBig.equals(answerBig)){
|
||||
System.out.println("factorial failed the first test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Print a message if all of the tests passed
|
||||
if(!failed){
|
||||
System.out.println("factorial 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<Integer> numbers = new ArrayList<Integer>();
|
||||
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<Integer>(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
|
||||
Long longCorrectAnswer = 118L;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L));
|
||||
Long longAnswer = Algorithms.getLongSum(longNumbers);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getSum failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
BigInteger bigCorrectAnswer = BigInteger.valueOf(118);
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(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 fourth 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<Integer> numbers = new ArrayList<Integer>();
|
||||
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<Integer>(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
|
||||
Long longCorrectAnswer = 57600L;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L));
|
||||
Long longAnswer = Algorithms.getLongProd(longNumbers);
|
||||
//Print a message if the function returned the wrong answer
|
||||
if(!longCorrectAnswer.equals(longAnswer)){
|
||||
System.out.println("getProd failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
BigInteger bigCorrectAnswer = BigInteger.valueOf(57600);
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(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 fourth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Print a message if all of the tests passed
|
||||
if(!failed){
|
||||
System.out.println("getProd passed all tests");
|
||||
}
|
||||
}
|
||||
//This function tests the isFound functions
|
||||
private static void testIsFound(){
|
||||
Boolean failed = false; //Holds whether a test was failed
|
||||
|
||||
//Test 1
|
||||
Boolean correctAnswer = true;
|
||||
ArrayList<Integer> numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
Boolean answer = Algorithms.isFound(numbers, 0);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the first test");
|
||||
System.out.println("compare = " + answer.compareTo(correctAnswer));
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 2
|
||||
correctAnswer = true;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 9);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the second test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 3
|
||||
correctAnswer = true;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 5);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the third test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 4
|
||||
correctAnswer = false;
|
||||
numbers = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
|
||||
answer = Algorithms.isFound(numbers, 10);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the fourth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 5
|
||||
correctAnswer = true;
|
||||
ArrayList<Long> longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 0L);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the fifth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 6
|
||||
correctAnswer = true;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 9L);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the sixth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 7
|
||||
correctAnswer = true;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 5L);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the seventh test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 8
|
||||
correctAnswer = false;
|
||||
longNumbers = new ArrayList<Long>(Arrays.asList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L));
|
||||
answer = Algorithms.isLongFound(longNumbers, 10L);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the eighth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 9
|
||||
correctAnswer = true;
|
||||
ArrayList<BigInteger> bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(0L));
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the nineth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 10
|
||||
correctAnswer = true;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(9L));
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the tenth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 11
|
||||
correctAnswer = true;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(5L));
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the eleventh test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Test 12
|
||||
correctAnswer = false;
|
||||
bigNumbers = new ArrayList<BigInteger>(Arrays.asList(BigInteger.valueOf(0L), BigInteger.valueOf(1L), BigInteger.valueOf(2L), BigInteger.valueOf(3L), BigInteger.valueOf(4L), BigInteger.valueOf(5L), BigInteger.valueOf(6L), BigInteger.valueOf(7L), BigInteger.valueOf(8L), BigInteger.valueOf(9L)));
|
||||
answer = Algorithms.isBigFound(bigNumbers, BigInteger.valueOf(10L));
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("isFound failed the twelth test");
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Print a message if all of the tests passed
|
||||
if(!failed){
|
||||
System.out.println("isFound passed all tests");
|
||||
}
|
||||
}
|
||||
//This function tests the getPermutations function
|
||||
private static void testGetPermutations(){
|
||||
Boolean failed = false;
|
||||
//Test 1
|
||||
String permString = "012";
|
||||
ArrayList<String> correctAnswer = new ArrayList<String>(Arrays.asList("012", "021", "102", "120", "201", "210"));
|
||||
ArrayList<String> answer = Algorithms.getPermutations(permString);
|
||||
if(!answer.equals(correctAnswer)){
|
||||
System.out.println("getPermutations failed the first test");
|
||||
System.out.println(answer.toString());
|
||||
failed = true;
|
||||
}
|
||||
|
||||
//Print a message if all of the tests passed
|
||||
if(!failed){
|
||||
System.out.println("getPermutations passed all tests");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
getPrimes passed all tests
|
||||
It took 5.209 milliseconds to run this test
|
||||
|
||||
getNumPrimes passed all tests
|
||||
It took 1.464 milliseconds to run this test
|
||||
|
||||
getFactors passed all tests
|
||||
It took 456.699 microseconds to run this test
|
||||
|
||||
getDivisors passed all tests
|
||||
It took 428.300 microseconds to run this test
|
||||
|
||||
getFib passed all tests
|
||||
It took 6.107 milliseconds to run this test
|
||||
|
||||
getAllFib passed all tests
|
||||
It took 708.100 microseconds to run this test
|
||||
|
||||
factorial passed all tests
|
||||
It took 522.200 microseconds to run this test
|
||||
|
||||
getSum passed all tests
|
||||
It took 1.736 milliseconds to run this test
|
||||
|
||||
getProd passed all tests
|
||||
It took 541.600 microseconds to run this test
|
||||
|
||||
isFound passed all tests
|
||||
It took 537.700 microseconds to run this test
|
||||
|
||||
getPermutations passed all tests
|
||||
It took 336.500 microseconds to run this test
|
||||
|
||||
Tests completed
|
||||
*/
|
||||
@@ -1,69 +0,0 @@
|
||||
//Java/JavaClasses/testStopwatch.java
|
||||
//Matthew Ellison
|
||||
// Created: 03-01-19
|
||||
//Modified: 03-01-19
|
||||
//This class is used to test that stopwatch does what it is supposed to do (approximately)
|
||||
|
||||
|
||||
import mattrixwv.Stopwatch;
|
||||
|
||||
|
||||
public class testStopwatch{
|
||||
private static final int NUM_TO_RUN = 100000;
|
||||
public static void main(String[] argv){
|
||||
Boolean failed = false; //A flag to determine if all tests were passed
|
||||
//Print the test begin message
|
||||
System.out.println("Begin test");
|
||||
//Describe which test is being performed
|
||||
System.out.println("Starting loop");
|
||||
//Start the timer
|
||||
Stopwatch timer = new Stopwatch();
|
||||
timer.start();
|
||||
for(int cnt = 0;cnt < NUM_TO_RUN;++cnt){
|
||||
System.out.print(cnt);
|
||||
}
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
System.out.println("\nLoop completed");
|
||||
|
||||
//Check that the different resolutions work out correctly
|
||||
System.out.println("Checking that resolutions line up correctly");
|
||||
Double nano = timer.getNano();
|
||||
if(timer.getMicro() != (nano.doubleValue() / 1000D)){
|
||||
System.out.println("Error on microsecond resolution");
|
||||
failed = true;
|
||||
}
|
||||
else if(timer.getMilli() != (nano.doubleValue() / 1000000D)){
|
||||
System.out.println("Error on millisecond resolution");
|
||||
failed = true;
|
||||
}
|
||||
else if(timer.getSecond() != (nano.doubleValue() / 1000000000D)){
|
||||
System.out.println("Error on second resolution");
|
||||
failed = true;
|
||||
}
|
||||
else if(timer.getMinute() != (nano.doubleValue() / 60000000000D)){
|
||||
System.out.println("Error on minute resolution");
|
||||
failed = true;
|
||||
}
|
||||
else if(timer.getHour() != (nano.doubleValue() / 3600000000000D)){
|
||||
System.out.println("Error on hour resolution");
|
||||
failed = true;
|
||||
}
|
||||
else{
|
||||
System.out.println("All time resolution tests completed successfully");
|
||||
}
|
||||
System.out.println("Completed resolution checking");
|
||||
|
||||
//Print the results
|
||||
System.out.printf("The timer results in: %s\n", timer.getStr());
|
||||
System.out.printf("The timer results in: " + timer + "\n");
|
||||
System.out.printf("The timer results in: %f milliseconds\n", timer.getMilli());
|
||||
|
||||
if(!failed){
|
||||
System.out.println("All tests completed successfully");
|
||||
}
|
||||
else{
|
||||
System.out.println("Test failed. Try again!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user