mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-06 23:13:57 -05:00
Added number generators
This commit is contained in:
142
src/test/java/com/mattrixwv/TestArrayAlgorithms.java
Normal file
142
src/test/java/com/mattrixwv/TestArrayAlgorithms.java
Normal file
@@ -0,0 +1,142 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestArrayAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 06-26-22
|
||||
//This class contains tests for my array algorithms
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestArrayAlgorithms{
|
||||
@Test
|
||||
public void testGetSum(){
|
||||
//Test 1
|
||||
int correctAnswer = 0;
|
||||
List<Integer> numbers = new ArrayList<>();
|
||||
int answer = ArrayAlgorithms.getSum(numbers);
|
||||
assertEquals(correctAnswer, answer, "getSum int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = 118;
|
||||
numbers = Arrays.asList(2, 2, 3, 3, 4, 4, 100);
|
||||
answer = ArrayAlgorithms.getSum(numbers);
|
||||
assertEquals(correctAnswer, answer, "getSum int 2 failed");
|
||||
|
||||
//Test 3
|
||||
long longCorrectAnswer = 0;
|
||||
List<Long> longNumbers = new ArrayList<>();
|
||||
long longAnswer = ArrayAlgorithms.getLongSum(longNumbers);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getSum long 1 failed");
|
||||
//Test 4
|
||||
longCorrectAnswer = 118;
|
||||
longNumbers = Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L);
|
||||
longAnswer = ArrayAlgorithms.getLongSum(longNumbers);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getSum long 2 failed");
|
||||
|
||||
//Test 5
|
||||
BigInteger bigCorrectAnswer = BigInteger.ZERO;
|
||||
List<BigInteger> bigNumbers = new ArrayList<>();
|
||||
BigInteger bigAnswer = ArrayAlgorithms.getBigSum(bigNumbers);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getSum BigInteger 1 failed");
|
||||
//Test 6
|
||||
bigCorrectAnswer = BigInteger.valueOf(118);
|
||||
bigNumbers = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100));
|
||||
bigAnswer = ArrayAlgorithms.getBigSum(bigNumbers);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getSum BigInteger 2 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetProd(){
|
||||
//Test 1
|
||||
int correctAnswer = 0;
|
||||
List<Integer> numbers = new ArrayList<>();
|
||||
int answer = ArrayAlgorithms.getProd(numbers);
|
||||
assertEquals(correctAnswer, answer, "getProd int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = 57600;
|
||||
numbers = Arrays.asList(2, 2, 3, 3, 4, 4, 100);
|
||||
answer = ArrayAlgorithms.getProd(numbers);
|
||||
assertEquals(correctAnswer, answer, "getProd int 2 failed");
|
||||
|
||||
//Test 3
|
||||
long longCorrectAnswer = 0;
|
||||
List<Long> longNumbers = new ArrayList<>();
|
||||
long longAnswer = ArrayAlgorithms.getLongProd(longNumbers);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getProd long 1 failed");
|
||||
//Test 4
|
||||
longCorrectAnswer = 57600L;
|
||||
longNumbers = Arrays.asList(2L, 2L, 3L, 3L, 4L, 4L, 100L);
|
||||
longAnswer = ArrayAlgorithms.getLongProd(longNumbers);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getProd long 2 failed");
|
||||
|
||||
//Test 5
|
||||
BigInteger bigCorrectAnswer = BigInteger.ZERO;
|
||||
List<BigInteger> bigNumbers = new ArrayList<BigInteger>();
|
||||
BigInteger bigAnswer = ArrayAlgorithms.getBigProd(bigNumbers);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getProd BigInteger 1 failed");
|
||||
//Test 6
|
||||
bigCorrectAnswer = BigInteger.valueOf(57600);
|
||||
bigNumbers = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100));
|
||||
bigAnswer = ArrayAlgorithms.getBigProd(bigNumbers);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getProd BigInteger 2 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrintList(){
|
||||
//Test 1
|
||||
List<Integer> nums = new ArrayList<Integer>();
|
||||
String correctAnswer = "[]";
|
||||
String answer = ArrayAlgorithms.printList(nums);
|
||||
assertEquals(correctAnswer, answer, "printList<Integer> 1 failed");
|
||||
//Test 2
|
||||
nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
|
||||
answer = ArrayAlgorithms.printList(nums);
|
||||
assertEquals(correctAnswer, answer, "printList<Integer> 2 failed");
|
||||
//Test 3
|
||||
nums = Arrays.asList(-3, -2, -1, 0, 1, 2, 3);
|
||||
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
|
||||
answer = ArrayAlgorithms.printList(nums);
|
||||
assertEquals(correctAnswer, answer, "printList<Integer> 3 failed");
|
||||
|
||||
//Test 4
|
||||
List<String> strings = new ArrayList<String>();
|
||||
correctAnswer = "[]";
|
||||
answer = ArrayAlgorithms.printList(strings);
|
||||
assertEquals(correctAnswer, answer, "printList<String> 1 failed");
|
||||
//Test 5
|
||||
strings = Arrays.asList("A", "B", "C");
|
||||
correctAnswer = "[A, B, C]";
|
||||
answer = ArrayAlgorithms.printList(strings);
|
||||
assertEquals(correctAnswer, answer, "printList<String> 2 failed");
|
||||
//Test 6
|
||||
strings = new ArrayList<String>(Arrays.asList("abc", "def", "ghi"));
|
||||
correctAnswer = "[abc, def, ghi]";
|
||||
answer = ArrayAlgorithms.printList(strings);
|
||||
assertEquals(correctAnswer, answer, "printList<String> 3 failed");
|
||||
}
|
||||
}
|
||||
594
src/test/java/com/mattrixwv/TestNumberAlgorithms.java
Normal file
594
src/test/java/com/mattrixwv/TestNumberAlgorithms.java
Normal file
@@ -0,0 +1,594 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestNumberAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 06-26-22
|
||||
//This class contains tests for my number algorithms
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
public class TestNumberAlgorithms{
|
||||
@Test
|
||||
public void testGetPrimes(){
|
||||
//Test 1
|
||||
List<Integer> correctAnswer = new ArrayList<>();
|
||||
int topNum = 1;
|
||||
List<Integer> answer = NumberAlgorithms.getPrimes(topNum);
|
||||
assertEquals(correctAnswer, answer, "getPrimes int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = 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);
|
||||
topNum = 100;
|
||||
answer = NumberAlgorithms.getPrimes(topNum);
|
||||
assertEquals(correctAnswer, answer, "getPrimes int 2 failed");
|
||||
|
||||
//Test 3
|
||||
List<Long> longCorrectAnswer = new ArrayList<>();
|
||||
long longTopNum = 0;
|
||||
List<Long> longAnswer = NumberAlgorithms.getPrimes(longTopNum);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getPrimes long 1 failed");
|
||||
//Test 4
|
||||
longCorrectAnswer = 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);
|
||||
longTopNum = 100;
|
||||
longAnswer = NumberAlgorithms.getPrimes(longTopNum);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getPrimes long 2 failed");
|
||||
|
||||
//Test 5
|
||||
List<BigInteger> bigCorrectAnswer = new ArrayList<>();
|
||||
BigInteger bigTopNum = BigInteger.ZERO;
|
||||
List<BigInteger> bigAnswer = NumberAlgorithms.getPrimes(bigTopNum);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getPrimes BigInteger 1 failed");
|
||||
//Test 6
|
||||
bigCorrectAnswer = 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));
|
||||
bigTopNum = BigInteger.valueOf(100);
|
||||
bigAnswer = NumberAlgorithms.getPrimes(bigTopNum);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getPrimes BigInteger 2 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNumPrimes(){
|
||||
//Test 1
|
||||
List<Integer> correctAnswer = new ArrayList<>();
|
||||
int numPrimes = 0;
|
||||
List<Integer> answer = NumberAlgorithms.getNumPrimes(numPrimes);
|
||||
assertEquals(correctAnswer, answer, "getNumPrimes int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = 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);
|
||||
numPrimes = 25;
|
||||
answer = NumberAlgorithms.getNumPrimes(numPrimes);
|
||||
assertEquals(correctAnswer, answer, "getNumPrimes int 2 failed");
|
||||
|
||||
//Test 3
|
||||
List<Long> longCorrectAnswer = new ArrayList<>();
|
||||
long longNumPrimes = 0;
|
||||
List<Long> longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getNumPrimes long 1 failed");
|
||||
//Test 4
|
||||
longCorrectAnswer = 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);
|
||||
longNumPrimes = 25;
|
||||
longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getNumPrimes long 2 failed");
|
||||
|
||||
//Test 5
|
||||
List<BigInteger> bigCorrectAnswer = new ArrayList<>();
|
||||
BigInteger bigNumPrimes = BigInteger.ZERO;
|
||||
List<BigInteger> bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getNumPrimes BigInteger 1 failed");
|
||||
//Test 6
|
||||
bigCorrectAnswer = 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));
|
||||
bigNumPrimes = BigInteger.valueOf(25);
|
||||
bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getNumPrimes BigInteger 2 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPrime(){
|
||||
//Test 1
|
||||
int num = 4;
|
||||
boolean correctAnswer = false;
|
||||
boolean answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 1 failed");
|
||||
//Test 2
|
||||
num = 9;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 2 failed");
|
||||
//Test 3
|
||||
num = 15;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 3 failed");
|
||||
//Test 4
|
||||
num = 97;
|
||||
correctAnswer = true;
|
||||
answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 4 failed");
|
||||
//Test 5
|
||||
num = 1000;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 5 failed");
|
||||
//Test 6
|
||||
num = 1;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(num);
|
||||
assertEquals(correctAnswer, answer, "isPrime int 6 failed");
|
||||
|
||||
//Test 7
|
||||
long longNum = 4;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 1 failed");
|
||||
//Test 8
|
||||
longNum = 9;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 2 failed");
|
||||
//Test 9
|
||||
longNum = 15;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 3 failed");
|
||||
//Test 10
|
||||
longNum = 97;
|
||||
correctAnswer = true;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 4 failed");
|
||||
//Test 11
|
||||
longNum = 1000;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 5 failed");
|
||||
//Test 12
|
||||
longNum = 1;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(longNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime long 6 failed");
|
||||
|
||||
//Test 13
|
||||
BigInteger bigNum = BigInteger.valueOf(4);
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 1 failed");
|
||||
//Test 14
|
||||
bigNum = BigInteger.valueOf(9);
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 2 failed");
|
||||
//Test 15
|
||||
bigNum = BigInteger.valueOf(15);
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 3 failed");
|
||||
//Test 16
|
||||
bigNum = BigInteger.valueOf(97);
|
||||
correctAnswer = true;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 4 failed");
|
||||
//Test 17
|
||||
bigNum = BigInteger.valueOf(1000);
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 5 failed");
|
||||
//Test 18
|
||||
bigNum = BigInteger.ONE;
|
||||
correctAnswer = false;
|
||||
answer = NumberAlgorithms.isPrime(bigNum);
|
||||
assertEquals(correctAnswer, answer, "isPrime BigInteger 6 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFactors() throws InvalidResult{
|
||||
//Test 1
|
||||
List<Integer> correctAnswer = Arrays.asList(2, 2, 5, 5);
|
||||
int number = 100;
|
||||
List<Integer> answer = NumberAlgorithms.getFactors(number);
|
||||
assertEquals(correctAnswer, answer, "getFactors int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = Arrays.asList(2, 7, 7);
|
||||
number = 98;
|
||||
answer = NumberAlgorithms.getFactors(number);
|
||||
assertEquals(correctAnswer, answer, "getFactors int 2 failed");
|
||||
//Test 3
|
||||
correctAnswer = Arrays.asList(7);
|
||||
number = 7;
|
||||
answer = NumberAlgorithms.getFactors(number);
|
||||
assertEquals(correctAnswer, answer, "getFactors int 3 failed");
|
||||
|
||||
//Test 4
|
||||
List<Long> longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L);
|
||||
long longNumber = 100L;
|
||||
List<Long> longAnswer = NumberAlgorithms.getFactors(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 1 failed");
|
||||
//Test 5
|
||||
longCorrectAnswer = Arrays.asList(2L, 7L, 7L);
|
||||
longNumber = 98;
|
||||
longAnswer = NumberAlgorithms.getFactors(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 2 failed");
|
||||
//Test 6
|
||||
longCorrectAnswer = Arrays.asList(7L);
|
||||
longNumber = 7;
|
||||
longAnswer = NumberAlgorithms.getFactors(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 3 failed");
|
||||
|
||||
//Test 7
|
||||
List<BigInteger> bigCorrectAnswer = Arrays.asList(BigInteger.TWO, BigInteger.TWO, BigInteger.valueOf(5), BigInteger.valueOf(5));
|
||||
BigInteger bigNumber = BigInteger.valueOf(100);
|
||||
List<BigInteger> bigAnswer = NumberAlgorithms.getFactors(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
|
||||
//Test 8
|
||||
bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7));
|
||||
bigNumber = BigInteger.valueOf(98);
|
||||
bigAnswer = NumberAlgorithms.getFactors(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
|
||||
//Test 9
|
||||
bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(7));
|
||||
bigNumber = BigInteger.valueOf(7);
|
||||
bigAnswer = NumberAlgorithms.getFactors(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDivisors(){
|
||||
//Test 1
|
||||
List<Integer> correctAnswer = Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100);
|
||||
int topNum = 100;
|
||||
List<Integer> answer = NumberAlgorithms.getDivisors(topNum);
|
||||
assertEquals(correctAnswer, answer, "getDivisors int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = Arrays.asList(1, 2, 3, 6);
|
||||
topNum = 6;
|
||||
answer = NumberAlgorithms.getDivisors(topNum);
|
||||
assertEquals(correctAnswer, answer, "getDivisors int 2 failed");
|
||||
//Test 3
|
||||
correctAnswer = new ArrayList<>();
|
||||
topNum = -1;
|
||||
answer = NumberAlgorithms.getDivisors(topNum);
|
||||
assertEquals(correctAnswer, answer, "getDivisors int 3 failed");
|
||||
|
||||
//Test 4
|
||||
List<Long> longCorrectAnswer = Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L);
|
||||
long longTopNum = 100;
|
||||
List<Long> longAnswer = NumberAlgorithms.getDivisors(longTopNum);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getDivisors long 1 failed");
|
||||
//Test 5
|
||||
longCorrectAnswer = Arrays.asList(1L, 2L, 3L, 6L);
|
||||
longTopNum = 6;
|
||||
longAnswer = NumberAlgorithms.getDivisors(longTopNum);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getDivisors long 2 failed");
|
||||
//Test 6
|
||||
longCorrectAnswer = new ArrayList<>();
|
||||
longTopNum = -1;
|
||||
longAnswer = NumberAlgorithms.getDivisors(longTopNum);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getDivisors long 3 failed");
|
||||
|
||||
//Test 7
|
||||
List<BigInteger> bigCorrectAnswer = 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);
|
||||
List<BigInteger> bigAnswer = NumberAlgorithms.getDivisors(bigTopNum);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getDivisors BigInteger 1 failed");
|
||||
//Test 8
|
||||
bigCorrectAnswer = Arrays.asList(BigInteger.ONE, BigInteger.TWO, BigInteger.valueOf(3), BigInteger.valueOf(6));
|
||||
bigTopNum = BigInteger.valueOf(6);
|
||||
bigAnswer = NumberAlgorithms.getDivisors(bigTopNum);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getDivisors BigInteger 2 failed");
|
||||
//Test 9
|
||||
bigCorrectAnswer = new ArrayList<>();
|
||||
bigTopNum = BigInteger.valueOf(-1);
|
||||
bigAnswer = NumberAlgorithms.getDivisors(bigTopNum);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getDivisors BigInteger 3 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFib(){
|
||||
//Test 1
|
||||
int correctAnswer = 0;
|
||||
int number = 0;
|
||||
int answer = NumberAlgorithms.getFib(number);
|
||||
assertEquals(correctAnswer, answer, "getFib int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = 144;
|
||||
number = 12;
|
||||
answer = NumberAlgorithms.getFib(number);
|
||||
assertEquals(correctAnswer, answer, "getFib int 2 failed");
|
||||
//Test 3
|
||||
correctAnswer = 6765;
|
||||
number = 20;
|
||||
answer = NumberAlgorithms.getFib(number);
|
||||
assertEquals(correctAnswer, answer, "getFib int 3 failed");
|
||||
|
||||
//Test 4
|
||||
long longCorrectAnswer = 0;
|
||||
long longNumber = 0;
|
||||
long longAnswer = NumberAlgorithms.getFib(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFib long 1 failed");
|
||||
//Test 5
|
||||
longCorrectAnswer = 144;
|
||||
longNumber = 12;
|
||||
longAnswer = NumberAlgorithms.getFib(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFib long 2 failed");
|
||||
//Test 6
|
||||
longCorrectAnswer = 6765;
|
||||
longNumber = 20;
|
||||
longAnswer = NumberAlgorithms.getFib(longNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getFib long 3 failed");
|
||||
|
||||
//Test 7
|
||||
BigInteger bigCorrectAnswer = BigInteger.ZERO;
|
||||
BigInteger bigNumber = BigInteger.ZERO;
|
||||
BigInteger bigAnswer = NumberAlgorithms.getFib(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFib BigInteger 1 failed");
|
||||
//Test 8
|
||||
bigCorrectAnswer = BigInteger.valueOf(144);
|
||||
bigNumber = BigInteger.valueOf(12);
|
||||
bigAnswer = NumberAlgorithms.getFib(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFib BigInteger 2 failed");
|
||||
//Test 9
|
||||
bigCorrectAnswer = BigInteger.valueOf(6765);
|
||||
bigNumber = BigInteger.valueOf(20);
|
||||
bigAnswer = NumberAlgorithms.getFib(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFib BigInteger 3 failed");
|
||||
//Test 10
|
||||
bigCorrectAnswer = new BigInteger("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816");
|
||||
bigNumber = BigInteger.valueOf(4782);
|
||||
bigAnswer = NumberAlgorithms.getFib(bigNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getFib BigInteger 4 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllFib(){
|
||||
//Test 1
|
||||
List<Integer> correctAnswer = new ArrayList<>();
|
||||
int highestNumber = 0;
|
||||
List<Integer> answer = NumberAlgorithms.getAllFib(highestNumber);
|
||||
assertEquals(correctAnswer, answer, "getAllFib int 1 failed");
|
||||
//Test 2
|
||||
correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89);
|
||||
highestNumber = 100;
|
||||
answer = NumberAlgorithms.getAllFib(highestNumber);
|
||||
assertEquals(correctAnswer, answer, "getAllFib int 2 failed");
|
||||
//Test 3
|
||||
correctAnswer = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987);
|
||||
highestNumber = 1000;
|
||||
answer = NumberAlgorithms.getAllFib(highestNumber);
|
||||
assertEquals(correctAnswer, answer, "getAllFib int 3 failed");
|
||||
|
||||
//Test 4
|
||||
List<Long> longCorrectAnswer = new ArrayList<>();
|
||||
long longHighestNumber = 0;
|
||||
List<Long> longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getAllFib long 1 failed");
|
||||
//Test 5
|
||||
longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L);
|
||||
longHighestNumber = 100;
|
||||
longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getAllFib long 2 failed");
|
||||
//Test 6
|
||||
longCorrectAnswer = Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L);
|
||||
longHighestNumber = 1000L;
|
||||
longAnswer = NumberAlgorithms.getAllFib(longHighestNumber);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "getAllFib long 3 failed");
|
||||
|
||||
//Test 7
|
||||
List<BigInteger> bigCorrectAnswer = new ArrayList<>();
|
||||
BigInteger bigHighestNumber = BigInteger.ZERO;
|
||||
List<BigInteger> bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "gitAllFib BigInteger 1 failed");
|
||||
//Test 8
|
||||
bigCorrectAnswer = 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));
|
||||
bigHighestNumber = BigInteger.valueOf(100);
|
||||
bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getAllFib BigInteger 2 failed");
|
||||
//Test 9
|
||||
bigCorrectAnswer = Arrays.asList(BigInteger.ONE, BigInteger.ONE, BigInteger.TWO, 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.valueOf(144), BigInteger.valueOf(233), BigInteger.valueOf(377), BigInteger.valueOf(610), BigInteger.valueOf(987));
|
||||
bigHighestNumber = BigInteger.valueOf(1000);
|
||||
bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "getAllFib BigInteger 3 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactorial(){
|
||||
//Test 1
|
||||
Exception error = assertThrows(InvalidParameterException.class, () -> {
|
||||
NumberAlgorithms.factorial(-1);
|
||||
});
|
||||
assertEquals(NumberAlgorithms.FACTORIAL_NEGATIVE_MESSAGE, error.getMessage());
|
||||
//Test 2
|
||||
int correctAnswer = 720;
|
||||
int number = 6;
|
||||
int answer = NumberAlgorithms.factorial(number);
|
||||
assertEquals(correctAnswer, answer, "factorial int 1 failed");
|
||||
//Test 3
|
||||
correctAnswer = 479001600;
|
||||
number = 12;
|
||||
answer = NumberAlgorithms.factorial(number);
|
||||
assertEquals(correctAnswer, answer, "factorial int 2 failed");
|
||||
|
||||
//Test 4
|
||||
error = assertThrows(InvalidParameterException.class, () -> {
|
||||
NumberAlgorithms.factorial(-1L);
|
||||
});
|
||||
assertEquals(NumberAlgorithms.FACTORIAL_NEGATIVE_MESSAGE, error.getMessage());
|
||||
//Test 5
|
||||
long correctAnswerLong = 720L;
|
||||
long numberLong = 6L;
|
||||
long answerLong = NumberAlgorithms.factorial(numberLong);
|
||||
assertEquals(correctAnswerLong, answerLong, "factorial long 1 failed");
|
||||
//Test 6
|
||||
correctAnswerLong = 479001600L;
|
||||
numberLong = 12L;
|
||||
answerLong = NumberAlgorithms.factorial(numberLong);
|
||||
assertEquals(correctAnswerLong, answerLong, "factorial long 2 failed");
|
||||
//Test 7
|
||||
correctAnswerLong = 2432902008176640000L;
|
||||
numberLong = 20L;
|
||||
answerLong = NumberAlgorithms.factorial(numberLong);
|
||||
assertEquals(correctAnswerLong, answerLong, "factorial long 3 failed");
|
||||
|
||||
//Test 8
|
||||
final BigInteger exceptionNumberBig = BigInteger.valueOf(-1);
|
||||
error = assertThrows(InvalidParameterException.class, () -> {
|
||||
NumberAlgorithms.factorial(exceptionNumberBig);
|
||||
});
|
||||
assertEquals(NumberAlgorithms.FACTORIAL_NEGATIVE_MESSAGE, error.getMessage());
|
||||
//Test 9
|
||||
BigInteger correctAnswerBig = BigInteger.valueOf(720L);
|
||||
BigInteger numberBig = BigInteger.valueOf(6);
|
||||
BigInteger answerBig = NumberAlgorithms.factorial(numberBig);
|
||||
assertEquals(correctAnswerBig, answerBig, "factorial BigInteger 1 failed");
|
||||
//Test 10
|
||||
correctAnswerBig = BigInteger.valueOf(479001600L);
|
||||
numberBig = BigInteger.valueOf(12);
|
||||
answerBig = NumberAlgorithms.factorial(numberBig);
|
||||
assertEquals(correctAnswerBig, answerBig, "factorial BigInteger 2 failed");
|
||||
//Test 11
|
||||
correctAnswerBig = BigInteger.valueOf(2432902008176640000L);
|
||||
numberBig = BigInteger.valueOf(20L);
|
||||
answerBig = NumberAlgorithms.factorial(numberBig);
|
||||
assertEquals(correctAnswerBig, answerBig, "factorial BigInteger 3 failed");
|
||||
//Test 12
|
||||
correctAnswerBig = new BigInteger("265252859812191058636308480000000");
|
||||
numberBig = BigInteger.valueOf(30L);
|
||||
answerBig = NumberAlgorithms.factorial(numberBig);
|
||||
assertEquals(correctAnswerBig, answerBig, "factorial BigInteger 4 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGCD(){
|
||||
//Test 1
|
||||
int num1 = 2;
|
||||
int num2 = 3;
|
||||
int correctAnswer = 1;
|
||||
int answer = NumberAlgorithms.gcd(num1, num2);
|
||||
assertEquals(correctAnswer, answer, "GCD int 1 failed");
|
||||
//Test 2
|
||||
num1 = 1000;
|
||||
num2 = 575;
|
||||
correctAnswer = 25;
|
||||
answer = NumberAlgorithms.gcd(num1, num2);
|
||||
assertEquals(correctAnswer, answer, "GCD int 2 failed");
|
||||
//Test 3
|
||||
num1 = 1000;
|
||||
num2 = 1000;
|
||||
correctAnswer = 1000;
|
||||
answer = NumberAlgorithms.gcd(num1, num2);
|
||||
assertEquals(correctAnswer, answer, "GCD int 3 failed");
|
||||
|
||||
//Test 4
|
||||
long longNum1 = 2;
|
||||
long longNum2 = 3;
|
||||
long longCorrectAnswer = 1;
|
||||
long longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "GCD long 1 failed");
|
||||
//Test 5
|
||||
longNum1 = 1000;
|
||||
longNum2 = 575;
|
||||
longCorrectAnswer = 25;
|
||||
longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "GCD long 2 failed");
|
||||
//Test 6
|
||||
longNum1 = 1000;
|
||||
longNum2 = 1000;
|
||||
longCorrectAnswer = 1000;
|
||||
longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
|
||||
assertEquals(longCorrectAnswer, longAnswer, "GCD long 3 failed");
|
||||
|
||||
//Test 7
|
||||
BigInteger bigNum1 = BigInteger.TWO;
|
||||
BigInteger bigNum2 = BigInteger.valueOf(3);
|
||||
BigInteger bigCorrectAnswer = BigInteger.ONE;
|
||||
BigInteger bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "GCD BigInteger 1 failed");
|
||||
//Test 8
|
||||
bigNum1 = BigInteger.valueOf(1000);
|
||||
bigNum2 = BigInteger.valueOf(575);
|
||||
bigCorrectAnswer = BigInteger.valueOf(25);
|
||||
bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "GCD BigInteger 2 failed");
|
||||
//Test 9
|
||||
bigNum1 = BigInteger.valueOf(1000);
|
||||
bigNum2 = BigInteger.valueOf(1000);
|
||||
bigCorrectAnswer = BigInteger.valueOf(1000);
|
||||
bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
|
||||
assertEquals(bigCorrectAnswer, bigAnswer, "GCD BigInteger 3 failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBin(){
|
||||
//Test 1
|
||||
int num = 7;
|
||||
String correctAnswer = "111";
|
||||
String answer = NumberAlgorithms.toBin(num);
|
||||
assertEquals(correctAnswer, answer, "toBin 1 failed");
|
||||
//Test 2
|
||||
num = 0;
|
||||
correctAnswer = "0";
|
||||
answer = NumberAlgorithms.toBin(num);
|
||||
assertEquals(correctAnswer, answer, "toBin 2 failed");
|
||||
//Test 3
|
||||
num = 1000000;
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = NumberAlgorithms.toBin(num);
|
||||
assertEquals(correctAnswer, answer, "toBin 3 failed");
|
||||
|
||||
//Test 4
|
||||
long longNum = 7;
|
||||
correctAnswer = "111";
|
||||
answer = NumberAlgorithms.toBin(longNum);
|
||||
assertEquals(correctAnswer, answer, "toBin long 1 failed");
|
||||
//Test 5
|
||||
longNum = 0;
|
||||
correctAnswer = "0";
|
||||
answer = NumberAlgorithms.toBin(longNum);
|
||||
assertEquals(correctAnswer, answer, "toBin long 2 failed");
|
||||
//Test 6
|
||||
longNum = 1000000;
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = NumberAlgorithms.toBin(longNum);
|
||||
assertEquals(correctAnswer, answer, "toBin long 3 failed");
|
||||
|
||||
//Test 7
|
||||
BigInteger bigNum = BigInteger.valueOf(7);
|
||||
correctAnswer = "111";
|
||||
answer = NumberAlgorithms.toBin(bigNum);
|
||||
assertEquals(correctAnswer, answer, "toBin big 1 failed");
|
||||
//Test 8
|
||||
bigNum = BigInteger.ZERO;
|
||||
correctAnswer = "0";
|
||||
answer = NumberAlgorithms.toBin(bigNum);
|
||||
assertEquals(correctAnswer, answer, "toBin big 2 failed");
|
||||
//Test 9
|
||||
bigNum = BigInteger.valueOf(1000000);
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = NumberAlgorithms.toBin(bigNum);
|
||||
assertEquals(correctAnswer, answer, "toBin big 3 failed");
|
||||
}
|
||||
}
|
||||
109
src/test/java/com/mattrixwv/TestStopwatch.java
Normal file
109
src/test/java/com/mattrixwv/TestStopwatch.java
Normal file
@@ -0,0 +1,109 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestStopwatch.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-07-20
|
||||
//Modified: 06-26-22
|
||||
//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) 2022 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 com.mattrixwv;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.mattrixwv.exceptions.InvalidResult;
|
||||
|
||||
|
||||
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();
|
||||
assertNotNull(timer.toString());
|
||||
//If it gets to here without throwing an exception everything went well
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConversion() throws InvalidResult{
|
||||
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(sum, Integer.valueOf(0), "You really messed up");
|
||||
//Check that the different resolutions work out correctly
|
||||
Double nano = timer.getNano();
|
||||
assertEquals(timer.getMicro(), (nano / 1000D), ALLOWANCE, "Micro resolution test failed");
|
||||
assertEquals(timer.getMilli(), (nano / 1000000D), ALLOWANCE, "Milli resolution test failed");
|
||||
assertEquals(timer.getSecond(), (nano / 1000000000D), ALLOWANCE, "Second resolution test failed");
|
||||
assertEquals(timer.getMinute(), (nano / 60000000000D), ALLOWANCE, "Minute resolution test failed");
|
||||
assertEquals(timer.getHour(), (nano / 3600000000000D), ALLOWANCE, "Hour resolution test failed");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringConversion() throws InvalidResult{
|
||||
//Test nanoseconds
|
||||
String results = Stopwatch.getStr(1.0);
|
||||
assertEquals("1.000 nanoseconds", results);
|
||||
//Test microseconds
|
||||
results = Stopwatch.getStr(1.0e3);
|
||||
assertEquals("1.000 microseconds", results);
|
||||
//Test milliseconds
|
||||
results = Stopwatch.getStr(1.0e6);
|
||||
assertEquals("1.000 milliseconds", results);
|
||||
//Test seconds
|
||||
results = Stopwatch.getStr(1.0e9);
|
||||
assertEquals("1.000 seconds", results);
|
||||
//Test minutes
|
||||
results = Stopwatch.getStr(1.0e12);
|
||||
assertEquals("16.667 minutes", results);
|
||||
//Test hours
|
||||
results = Stopwatch.getStr(1.0e13);
|
||||
assertEquals("2.778 hours", results);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testErrors(){
|
||||
Stopwatch timer = new Stopwatch();
|
||||
assertEquals(0.0, timer.getNano(), "Stopwatch failed not started test");
|
||||
|
||||
timer.stop();
|
||||
assertEquals(0.0, timer.getNano(), "Stopwatch failed stop before start test");
|
||||
|
||||
timer.start();
|
||||
assertNotEquals(0.0, timer.getNano(), "Stopwatch failed not stopped test");
|
||||
|
||||
timer.reset();
|
||||
assertEquals(0.0, timer.getNano(), "Stopwatch failed reset test");
|
||||
}
|
||||
}
|
||||
117
src/test/java/com/mattrixwv/TestStringAlgorithms.java
Normal file
117
src/test/java/com/mattrixwv/TestStringAlgorithms.java
Normal file
@@ -0,0 +1,117 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-03-21
|
||||
//Modified: 06-25-22
|
||||
//This class contains tests for my number algorithms
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestStringAlgorithms{
|
||||
@Test
|
||||
public void testGetPermutations(){
|
||||
//Test 1
|
||||
String permString = "012";
|
||||
List<String> correctAnswer = Arrays.asList("012", "021", "102", "120", "201", "210");
|
||||
List<String> answer = StringAlgorithms.getPermutations(permString);
|
||||
assertEquals(correctAnswer, answer, "getPermutations failed");
|
||||
}
|
||||
@Test
|
||||
public void testFindNumOccurrence(){
|
||||
//Test 1
|
||||
String testString = "abcdefgdd";
|
||||
char testChar = 'a';
|
||||
long correctAnswer = 1;
|
||||
long answer = StringAlgorithms.findNumOccurrence(testString, testChar);
|
||||
assertEquals(correctAnswer, answer, "FindNumOccurrence 1 failed");
|
||||
//Test 2
|
||||
testChar = 'd';
|
||||
correctAnswer = 3;
|
||||
answer = StringAlgorithms.findNumOccurrence(testString, testChar);
|
||||
assertEquals(correctAnswer, answer, "FindNumOccurrence 2 failed");
|
||||
//Test 3
|
||||
testChar = 'h';
|
||||
correctAnswer = 0;
|
||||
answer = StringAlgorithms.findNumOccurrence(testString, testChar);
|
||||
assertEquals(correctAnswer, answer, "FindNumOccurrence 3 failed");
|
||||
}
|
||||
@Test
|
||||
public void testIsPalindrome(){
|
||||
//Test 1
|
||||
String str = "101";
|
||||
boolean correctAnswer = true;
|
||||
boolean answer = StringAlgorithms.isPalindrome(str);
|
||||
assertEquals(correctAnswer, answer, "isPalindrome 1 failed");
|
||||
//Test 2
|
||||
str = "100";
|
||||
correctAnswer = false;
|
||||
answer = StringAlgorithms.isPalindrome(str);
|
||||
assertEquals(correctAnswer, answer, "isPalindrome 2 failed");
|
||||
//Test 3
|
||||
str = "";
|
||||
correctAnswer = true;
|
||||
answer = StringAlgorithms.isPalindrome(str);
|
||||
assertEquals(correctAnswer, answer, "isPalindrome 3 failed");
|
||||
}
|
||||
@Test
|
||||
public void testIsPandigital(){
|
||||
//Test 1
|
||||
String num = "123456789";
|
||||
boolean correctAnswer = true;
|
||||
boolean answer = StringAlgorithms.isPandigital(num);
|
||||
assertEquals(correctAnswer, answer, "isPandigital 1 failed");
|
||||
|
||||
//Test 2
|
||||
num = "123";
|
||||
correctAnswer = true;
|
||||
answer = StringAlgorithms.isPandigital(num, '1', '3');
|
||||
assertEquals(correctAnswer, answer, "isPandigital 2 failed");
|
||||
|
||||
//Test 3
|
||||
num = "123";
|
||||
correctAnswer = false;
|
||||
answer = StringAlgorithms.isPandigital(num);
|
||||
assertEquals(correctAnswer, answer, "isPandigital 3 failed");
|
||||
|
||||
//Test 4
|
||||
num = "123";
|
||||
correctAnswer = false;
|
||||
answer = StringAlgorithms.isPandigital(num, '3', '1');
|
||||
assertEquals(correctAnswer, answer, "isPandigital 4 failed");
|
||||
|
||||
//Test 5
|
||||
num = "1";
|
||||
correctAnswer = true;
|
||||
answer = StringAlgorithms.isPandigital(num, '1', '1');
|
||||
assertEquals(correctAnswer, answer, "isPandigital 5 failed");
|
||||
|
||||
//Test 6
|
||||
num = "112";
|
||||
correctAnswer = false;
|
||||
answer = StringAlgorithms.isPandigital(num, '1', '3');
|
||||
assertEquals(correctAnswer, answer, "isPandigital 6 failed");
|
||||
}
|
||||
}
|
||||
45
src/test/java/com/mattrixwv/TestTriple.java
Normal file
45
src/test/java/com/mattrixwv/TestTriple.java
Normal file
@@ -0,0 +1,45 @@
|
||||
//JavaClasses/src/test/java/com/mattrixwv/TestTriple.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 08-20-22
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestTriple{
|
||||
@Test
|
||||
public void testTriple(){
|
||||
Triple<Long, Long, Long> longTriple1 = new Triple<Long, Long, Long>(1L, 2L, 3L);
|
||||
Triple<Long, Long, Long> longTriple2 = new Triple<Long, Long, Long>(1L, 2L, 3L);
|
||||
Triple<Long, Long, Long> longTriple3 = new Triple<Long, Long, Long>(3L, 2L, 1L);
|
||||
assertEquals(1L, longTriple1.getA());
|
||||
assertEquals(2L, longTriple1.getB());
|
||||
assertEquals(3L, longTriple1.getC());
|
||||
assertEquals(true, longTriple1.equals(longTriple1));
|
||||
assertEquals(true, longTriple1.equals(longTriple2));
|
||||
assertEquals(false, longTriple1.equals(longTriple3));
|
||||
assertEquals(false, longTriple1.equals(1L));
|
||||
assertEquals(Long.hashCode(1) + Long.hashCode(2) * Long.hashCode(3), longTriple1.hashCode());
|
||||
assertEquals("[1, 2, 3]", longTriple1.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//JavaClasses/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 08-20-22
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv.generators;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestHexagonalNumberGenerator{
|
||||
@Test
|
||||
public void hexagonalNumberGeneratorTest(){
|
||||
HexagonalNumberGenerator gen = new HexagonalNumberGenerator();
|
||||
List<Long> nums = Arrays.asList(1L, 6L, 15L, 28L, 45L, 66L, 91L, 120L, 153L);
|
||||
ArrayList<Long> generatedNums = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < nums.size();++cnt){
|
||||
generatedNums.add(gen.next());
|
||||
}
|
||||
assertEquals(nums, generatedNums);
|
||||
assertEquals(true, gen.hasNext());
|
||||
assertEquals(true, HexagonalNumberGenerator.isHexagonal(153L));
|
||||
assertEquals(false, HexagonalNumberGenerator.isHexagonal(154L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//JavaClasses/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 08-20-22
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv.generators;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestPentagonalNumberGenerator{
|
||||
@Test
|
||||
public void pentagonalNumberGeneratorTest(){
|
||||
PentagonalNumberGenerator gen = new PentagonalNumberGenerator();
|
||||
List<Long> nums = Arrays.asList(1L, 5L, 12L, 22L, 35L, 51L, 70L, 92L, 117L);
|
||||
ArrayList<Long> generatedNums = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < nums.size();++cnt){
|
||||
generatedNums.add(gen.next());
|
||||
}
|
||||
assertEquals(nums, generatedNums);
|
||||
assertEquals(true, gen.hasNext());
|
||||
assertEquals(true, PentagonalNumberGenerator.isPentagonal(117L));
|
||||
assertEquals(false, PentagonalNumberGenerator.isPentagonal(118L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestSieveOfEratosthenes.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-26-22
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv.generators;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestSieveOfEratosthenes{
|
||||
@Test
|
||||
public void testSieveOfEratosthenes(){
|
||||
//Test 1
|
||||
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
|
||||
List<Long> correctAnswer = 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);
|
||||
ArrayList<Long> answer = new ArrayList<Long>();
|
||||
for(int cnt = 0;cnt < 25;++cnt){
|
||||
long prime = sieve.next();
|
||||
answer.add(prime);
|
||||
}
|
||||
assertEquals(correctAnswer, answer, "SieveOfEratosthenes failed");
|
||||
assertTrue(sieve.hasNext());
|
||||
}
|
||||
@Test
|
||||
public void testSieveOfEratosthenesBig(){
|
||||
//Test 1
|
||||
SieveOfEratosthenesBig sieve = new SieveOfEratosthenesBig();
|
||||
List<BigInteger> correctAnswer = 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));
|
||||
ArrayList<BigInteger> answer = new ArrayList<BigInteger>();
|
||||
for(int cnt = 0;cnt < 25;++cnt){
|
||||
BigInteger prime = sieve.next();
|
||||
answer.add(prime);
|
||||
}
|
||||
assertEquals(correctAnswer, answer, "SieveOfEratosthenesBig failed");
|
||||
assertTrue(sieve.hasNext());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
//JavaClasses/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 08-20-22
|
||||
/*
|
||||
Copyright (C) 2022 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 com.mattrixwv.generators;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
public class TestTriangularNumberGenerator{
|
||||
@Test
|
||||
public void triangularNumberGeneratorTest(){
|
||||
TriangularNumberGenerator gen = new TriangularNumberGenerator();
|
||||
List<Long> nums = Arrays.asList(1L, 3L, 6L, 10L, 15L, 21L, 28L, 36L, 45L);
|
||||
ArrayList<Long> generatedNums = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < nums.size();++cnt){
|
||||
generatedNums.add(gen.next());
|
||||
}
|
||||
assertEquals(nums, generatedNums);
|
||||
assertEquals(true, gen.hasNext());
|
||||
assertEquals(true, TriangularNumberGenerator.isTriangular(55L));
|
||||
assertEquals(false, TriangularNumberGenerator.isTriangular(54L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user