Split algorithms to separate files

This commit is contained in:
2021-07-03 15:39:56 -04:00
parent bf12be25ac
commit ee9b256b67
6 changed files with 502 additions and 475 deletions

View File

@@ -0,0 +1,146 @@
//JavaClasses/src/main/java/mattrixwv/ArrayAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//This class contains algorithms for vectors that I've found it useful to keep around
/*
Copyright (C) 2021 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 java.math.BigInteger;
import java.util.ArrayList;
public class ArrayAlgorithms{
//This function returns the sum of all elements in the list
public static int getSum(ArrayList<Integer> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return 0;
}
//Setup the variables
int sum = 0;
//Loop through every element in the list and add them together
for(int num : nums){
sum += num;
}
//Return the sum of all elements
return sum;
}
public static long getLongSum(ArrayList<Long> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return 0L;
}
//Setup the variables
long sum = 0L;
//Loop through every element in the list and add them together
for(long num : nums){
sum += num;
}
//Return the sum of all elements
return sum;
}
public static BigInteger getBigSum(ArrayList<BigInteger> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return BigInteger.valueOf(0);
}
//Setup the variables
BigInteger sum = BigInteger.valueOf(0);
//Loop through every element in the list and add them together
for(BigInteger num : nums){
sum = sum.add(num);
}
//Return the sum of all elements
return sum;
}
//This function returns the product of all elements in the list
public static int getProd(ArrayList<Integer> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return 0;
}
//Setup the variables
int product = 1; //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(int num : nums){
product *= num;
}
//Return the product of all elements
return product;
}
public static long getLongProd(ArrayList<Long> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return 0L;
}
//Setup the variables
long product = 1L; //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(long num : nums){
product *= num;
}
//Return the product of all elements
return product;
}
public static BigInteger getBigProd(ArrayList<BigInteger> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return BigInteger.valueOf(0);
}
//Setup the variables
BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(BigInteger num : nums){
product = product.multiply(num);
}
//Return the product of all elements
return product;
}
//Print a list
public static <T> String printList(ArrayList<T> list){
StringBuilder listString = new StringBuilder("[");
for(int cnt = 0;cnt < list.size();++cnt){
listString.append(list.get(cnt));
if(cnt < list.size() - 1){
listString.append(", ");
}
}
listString.append("]");
return listString.toString();
}
}

View File

@@ -1,11 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/Algorithms.java
//JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 06-29-21
//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
// Created: 07-03-21
//Modified: 07-03-21
//This class contains algorithms for numbers that I've found it useful to keep around
/*
Copyright (C) 2021 Matthew Ellison
Copyright (C) 2021 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
@@ -31,7 +30,7 @@ import java.util.Collections;
import mattrixwv.exceptions.InvalidResult;
public class Algorithms{
public class NumberAlgorithms{
//?This is here just to prove that templates exist and for a possible rewrite at a later time
public static <T> T getNum(T num1){
return num1;
@@ -756,203 +755,6 @@ public class Algorithms{
}
return num1.or(num2);
}
//This function returns the sum of all elements in the list
public static int getSum(ArrayList<Integer> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return 0;
}
//Setup the variables
int sum = 0;
//Loop through every element in the list and add them together
for(int num : nums){
sum += num;
}
//Return the sum of all elements
return sum;
}
public static long getLongSum(ArrayList<Long> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return 0L;
}
//Setup the variables
long sum = 0L;
//Loop through every element in the list and add them together
for(long num : nums){
sum += num;
}
//Return the sum of all elements
return sum;
}
public static BigInteger getBigSum(ArrayList<BigInteger> nums){
//If a blank list was passed to the function return 0 as the sum
if(nums.size() == 0){
return BigInteger.valueOf(0);
}
//Setup the variables
BigInteger sum = BigInteger.valueOf(0);
//Loop through every element in the list and add them together
for(BigInteger num : nums){
sum = sum.add(num);
}
//Return the sum of all elements
return sum;
}
//This function returns the product of all elements in the list
public static int getProd(ArrayList<Integer> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return 0;
}
//Setup the variables
int product = 1; //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(int num : nums){
product *= num;
}
//Return the product of all elements
return product;
}
public static long getLongProd(ArrayList<Long> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return 0L;
}
//Setup the variables
long product = 1L; //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(long num : nums){
product *= num;
}
//Return the product of all elements
return product;
}
public static BigInteger getBigProd(ArrayList<BigInteger> nums){
//If a blank list was passed tot he fuction return 0 as the product
if(nums.size() == 0){
return BigInteger.valueOf(0);
}
//Setup the variables
BigInteger product = BigInteger.valueOf(1); //Start at 1 because x * 1 = x
//Loop through every element in the list and multiply them together
for(BigInteger num : nums){
product = product.multiply(num);
}
//Return the product of all elements
return product;
}
//This function returns true if key is found in ary
public static Boolean isFound(ArrayList<Integer> ary, Integer key){
//Look through every element in the array, looing for the key element
for(Integer num : ary){
//If there is an element in the array that is the same as key return true
if(num.equals(key)){
return true;
}
}
//If you made it to the end of the array without finding a match return false because the element was not found
return false;
}
public static Boolean isLongFound(ArrayList<Long> ary, Long key){
//Look through every element in the array, looing for the key element
for(Long num : ary){
//If there is an element in the array that is the same as key return true
if(num.equals(key)){
return true;
}
}
//If you made it to the end of the array without finding a match return false because the element was not found
return false;
}
public static Boolean isBigFound(ArrayList<BigInteger> ary, BigInteger key){
//Look through every element in the array, looing for the key element
for(BigInteger num : ary){
//If there is an element in the array that is the same as key return true
if(num.equals(key)){
return true;
}
}
//If you made it to the end of the array without finding a match return false because the element was not found
return false;
}
//This is a function that creates all permutations of a string and returns a vector of those permutations.
public static ArrayList<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<String>();
//Check if the number is out of bounds
if((num >= master.length()) || (num < 0)){
//Do nothing and return an empty arraylist
}
//If this is the last possible recurse just return the current string
else if(num == (master.length() - 1)){
perms.add(master);
}
//If there are more possible recurses, recurse with the current permutation
else{
ArrayList<String> temp = getPermutations(master, num + 1);
perms.addAll(temp);
//You need to swap the current letter with every possible letter after it
//The ones needed to swap before will happen automatically when the function recurses
for(int cnt = 1;(num + cnt) < master.length();++cnt){
master = swapString(master, num, (num + cnt));
temp = getPermutations(master, num + 1);
perms.addAll(temp);
master = swapString(master, num, (num + cnt));
}
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
if(num == 0){
Collections.sort(perms);
}
}
//Return the arraylist that was built
return perms;
}
private static String swapString(String str, int first, int second){
char[] tempStr = str.toCharArray();
char temp = tempStr[first];
tempStr[first] = tempStr[second];
tempStr[second] = temp;
String swappedString = new String(tempStr);
return swappedString;
}
//This function returns the number of times the character occurs in the string
public static long findNumOccurrence(String str, char c){
return str.chars().filter(ch -> ch == c).count();
}
//Returns true if the string passed in is a palindrome
public static boolean isPalindrome(String str){
String rev = new StringBuilder(str).reverse().toString();
if(str.equals(rev)){
return true;
}
else{
return false;
}
}
//Converts a number to its binary equivalent
public static String toBin(int num){
//Convert the number to a binary string
@@ -966,16 +768,4 @@ public class Algorithms{
//Conver the number to binary string
return num.toString(2);
}
//Print a list
public static <T> String printList(ArrayList<T> list){
StringBuilder listString = new StringBuilder("[");
for(int cnt = 0;cnt < list.size();++cnt){
listString.append(list.get(cnt));
if(cnt < list.size() - 1){
listString.append(", ");
}
}
listString.append("]");
return listString.toString();
}
}

View File

@@ -0,0 +1,90 @@
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//This class contains algorithms for strings that I've found it useful to keep around
/*
Copyright (C) 2021 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 java.util.ArrayList;
import java.util.Collections;
public class StringAlgorithms{
//This is a function that creates all permutations of a string and returns a vector of those permutations.
public static ArrayList<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<String>();
//Check if the number is out of bounds
if((num >= master.length()) || (num < 0)){
//Do nothing and return an empty arraylist
}
//If this is the last possible recurse just return the current string
else if(num == (master.length() - 1)){
perms.add(master);
}
//If there are more possible recurses, recurse with the current permutation
else{
ArrayList<String> temp = getPermutations(master, num + 1);
perms.addAll(temp);
//You need to swap the current letter with every possible letter after it
//The ones needed to swap before will happen automatically when the function recurses
for(int cnt = 1;(num + cnt) < master.length();++cnt){
master = swapString(master, num, (num + cnt));
temp = getPermutations(master, num + 1);
perms.addAll(temp);
master = swapString(master, num, (num + cnt));
}
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
if(num == 0){
Collections.sort(perms);
}
}
//Return the arraylist that was built
return perms;
}
private static String swapString(String str, int first, int second){
char[] tempStr = str.toCharArray();
char temp = tempStr[first];
tempStr[first] = tempStr[second];
tempStr[second] = temp;
String swappedString = new String(tempStr);
return swappedString;
}
//This function returns the number of times the character occurs in the string
public static long findNumOccurrence(String str, char c){
return str.chars().filter(ch -> ch == c).count();
}
//Returns true if the string passed in is a palindrome
public static boolean isPalindrome(String str){
String rev = new StringBuilder(str).reverse().toString();
if(str.equals(rev)){
return true;
}
else{
return false;
}
}
}

View File

@@ -0,0 +1,114 @@
//JavaClasses/src/test/java/mattrixwv/TestArrayAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//This class contains tests for my array algorithms
/*
Copyright (C) 2021 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 TestArrayAlgorithms{
@Test
public void testGetSum(){
//Test 1
Integer correctAnswer = 0;
ArrayList<Integer> numbers = new ArrayList<Integer>();
Integer answer = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.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 = ArrayAlgorithms.getBigProd(bigNumbers);
assertEquals("getProd BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
public void testPrintList(){
//Test 1
ArrayList<Integer> nums = new ArrayList<Integer>();
String correctAnswer = "[]";
String answer = ArrayAlgorithms.printList(nums);
assertEquals("printList<Integer> 1 failed", correctAnswer, answer);
//Test 2
nums = new ArrayList<Integer>(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("printList<Integer> 2 failed", correctAnswer, answer);
//Test 3
nums = new ArrayList<Integer>(Arrays.asList(-3, -2, -1, 0, 1, 2, 3));
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
answer = ArrayAlgorithms.printList(nums);
assertEquals("printList<Integer> 3 failed", correctAnswer, answer);
//Test 4
ArrayList<String> strings = new ArrayList<String>(Arrays.asList("A", "B", "C"));
correctAnswer = "[A, B, C]";
answer = ArrayAlgorithms.printList(strings);
assertEquals("printList<String> 1 failed", correctAnswer, answer);
//Test 5
strings = new ArrayList<String>(Arrays.asList("abc", "def", "ghi"));
correctAnswer = "[abc, def, ghi]";
answer = ArrayAlgorithms.printList(strings);
assertEquals("printList<String> 2 failed", correctAnswer, answer);
}
}

View File

@@ -1,11 +1,10 @@
//JavaClasses/src/test/java/mattrixwv/TestAlgorithms.java
//JavaClasses/src/test/java/mattrixwv/TestNumberAlgorithms.java
//Matthew Ellison
// Created: 06-07-20
//Modified: 06-29-21
//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
// Created: 07-03-21
//Modified: 07-03-21
//This class contains tests for my number algorithms
/*
Copyright (C) 2021 Matthew Ellison
Copyright (C) 2021 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
@@ -20,10 +19,9 @@ Copyright (C) 2021 Matthew Ellison
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;
@@ -35,25 +33,25 @@ import org.junit.Test;
import mattrixwv.exceptions.InvalidResult;
public class TestAlgorithms{
public class TestNumberAlgorithms{
@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);
ArrayList<Integer> answer = NumberAlgorithms.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);
ArrayList<Long> longAnswer = NumberAlgorithms.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);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getPrimes(bigTopNum);
assertEquals("getPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -61,19 +59,19 @@ public class TestAlgorithms{
//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);
ArrayList<Integer> answer = NumberAlgorithms.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);
ArrayList<Long> longAnswer = NumberAlgorithms.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 bigNumPrimes = BigInteger.valueOf(25);
ArrayList<BigInteger> bigAnswer = Algorithms.getNumPrimes(bigNumPrimes);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getNumPrimes(bigNumPrimes);
assertEquals("getNumPrimes BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -81,64 +79,64 @@ public class TestAlgorithms{
//Test 1
int num = 2;
boolean correctAnswer = true;
boolean answer = Algorithms.isPrime(num);
boolean answer = NumberAlgorithms.isPrime(num);
assertEquals("isPrime Integer 1 failed", correctAnswer, answer);
//Test 2
num = 97;
correctAnswer = true;
answer = Algorithms.isPrime(num);
answer = NumberAlgorithms.isPrime(num);
assertEquals("isPrime Integer 2 failed", correctAnswer, answer);
//Test 3
num = 1000;
correctAnswer = false;
answer = Algorithms.isPrime(num);
answer = NumberAlgorithms.isPrime(num);
assertEquals("isPrime Integer 3 failed", correctAnswer, answer);
//Test 4
num = 1;
correctAnswer = false;
answer = Algorithms.isPrime(num);
answer = NumberAlgorithms.isPrime(num);
assertEquals("isPrime Integer 4 failed", correctAnswer, answer);
//Test 5
long longNum = 2;
correctAnswer = true;
answer = Algorithms.isPrime(longNum);
answer = NumberAlgorithms.isPrime(longNum);
assertEquals("isPrime Long 1 failed", correctAnswer, answer);
//Test 6
longNum = 97;
correctAnswer = true;
answer = Algorithms.isPrime(longNum);
answer = NumberAlgorithms.isPrime(longNum);
assertEquals("isPrime Long 2 failed", correctAnswer, answer);
//Test 7
longNum = 1000;
correctAnswer = false;
answer = Algorithms.isPrime(longNum);
answer = NumberAlgorithms.isPrime(longNum);
assertEquals("isPrime Long 3 failed", correctAnswer, answer);
//Test 8
longNum = 1;
correctAnswer = false;
answer = Algorithms.isPrime(longNum);
answer = NumberAlgorithms.isPrime(longNum);
assertEquals("isPrime Long 4 failed", correctAnswer, answer);
//Test 9
BigInteger bigNum = BigInteger.TWO;
correctAnswer = true;
answer = Algorithms.isPrime(bigNum);
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals("isPrime BigInteger 1 failed", correctAnswer, answer);
//Test 10
bigNum = BigInteger.valueOf(97);
correctAnswer = true;
answer = Algorithms.isPrime(bigNum);
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals("isPrime BigInteger 2 failed", correctAnswer, answer);
//Test 11
bigNum = BigInteger.valueOf(1000);
correctAnswer = false;
answer = Algorithms.isPrime(bigNum);
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals("isPrime BigInteger 3 failed", correctAnswer, answer);
//Test 12
bigNum = BigInteger.ONE;
correctAnswer = false;
answer = Algorithms.isPrime(bigNum);
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals("isPrime BigInteger 4 failed", correctAnswer, answer);
}
@Test
@@ -146,24 +144,24 @@ public class TestAlgorithms{
//Test 1
ArrayList<Integer> correctAnswer = new ArrayList<Integer>(Arrays.asList(2, 2, 5, 5));
Integer number = 100;
ArrayList<Integer> answer = Algorithms.getFactors(number);
ArrayList<Integer> answer = NumberAlgorithms.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);
answer = NumberAlgorithms.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);
ArrayList<Long> longAnswer = NumberAlgorithms.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);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals("getFactors BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -171,19 +169,19 @@ public class TestAlgorithms{
//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);
ArrayList<Integer> answer = NumberAlgorithms.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);
ArrayList<Long> longAnswer = NumberAlgorithms.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);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getDivisors(bigTopNum);
assertEquals("getDivisors BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -191,24 +189,24 @@ public class TestAlgorithms{
//Test 1
Integer correctAnswer = 144;
Integer number = 12;
Integer answer = Algorithms.getFib(number);
Integer answer = NumberAlgorithms.getFib(number);
assertEquals("getFib Integer 1 failed", correctAnswer, answer);
//Test 2
correctAnswer = 6765;
number = 20;
answer = Algorithms.getFib(number);
answer = NumberAlgorithms.getFib(number);
assertEquals("getFib Integer 2 failed", correctAnswer, answer);
//Test 3
Long longCorrectAnswer = 6765L;
Long longNumber = 20L;
Long longAnswer = Algorithms.getFib(longNumber);
Long longAnswer = NumberAlgorithms.getFib(longNumber);
assertEquals("getFib Long failed", longCorrectAnswer, longAnswer);
//Test 4
BigInteger bigCorrectAnswer = new BigInteger("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816");
BigInteger bigNumber = BigInteger.valueOf(4782);
BigInteger bigAnswer = Algorithms.getFib(bigNumber);
BigInteger bigAnswer = NumberAlgorithms.getFib(bigNumber);
assertEquals("getFib BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -216,24 +214,24 @@ public class TestAlgorithms{
//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);
ArrayList<Integer> answer = NumberAlgorithms.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);
answer = NumberAlgorithms.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);
ArrayList<Long> longAnswer = NumberAlgorithms.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);
ArrayList<BigInteger> bigAnswer = NumberAlgorithms.getAllFib(bigHighestNumber);
assertEquals("getAllFib BigInteger failed", bigCorrectAnswer, bigAnswer);
}
@Test
@@ -242,51 +240,51 @@ public class TestAlgorithms{
//Test 1
Integer correctAnswer = 720;
Integer number = 6;
Integer answer = Algorithms.factorial(number);
Integer answer = NumberAlgorithms.factorial(number);
assertEquals("factorial Integer 1 failed", correctAnswer, answer);
//Test 2
correctAnswer = 479001600;
number = 12;
answer = Algorithms.factorial(number);
answer = NumberAlgorithms.factorial(number);
assertEquals("factorial Integer 2 failed", correctAnswer, answer);
//Long
//Test 3
Long correctAnswerLong = 720L;
Long numberLong = 6L;
Long answerLong = Algorithms.factorial(numberLong);
Long answerLong = NumberAlgorithms.factorial(numberLong);
assertEquals("factorial Long 1 failed", correctAnswerLong, answerLong);
//Test 4
correctAnswerLong = 479001600L;
numberLong = 12L;
answerLong = Algorithms.factorial(numberLong);
answerLong = NumberAlgorithms.factorial(numberLong);
assertEquals("factorial Long 2 failed", correctAnswerLong, answerLong);
//Test 5
correctAnswerLong = 2432902008176640000L;
numberLong = 20L;
answerLong = Algorithms.factorial(numberLong);
answerLong = NumberAlgorithms.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);
BigInteger answerBig = NumberAlgorithms.factorial(numberBig);
assertEquals("factorial BigInteger 1 failed", correctAnswerBig, answerBig);
//Test 7
correctAnswerBig = BigInteger.valueOf(479001600L);
numberBig = BigInteger.valueOf(12);
answerBig = Algorithms.factorial(numberBig);
answerBig = NumberAlgorithms.factorial(numberBig);
assertEquals("factorial BigInteger 2 failed", correctAnswerBig, answerBig);
//Test 8
correctAnswerBig = BigInteger.valueOf(2432902008176640000L);
numberBig = BigInteger.valueOf(20L);
answerBig = Algorithms.factorial(numberBig);
answerBig = NumberAlgorithms.factorial(numberBig);
assertEquals("factorial BigInteger 3 failed", correctAnswerBig, answerBig);
//Test 9
correctAnswerBig = new BigInteger("265252859812191058636308480000000");
numberBig = BigInteger.valueOf(30L);
answerBig = Algorithms.factorial(numberBig);
answerBig = NumberAlgorithms.factorial(numberBig);
assertEquals("factorial BigInteger 4 failed", correctAnswerBig, answerBig);
}
@Test
@@ -295,297 +293,107 @@ public class TestAlgorithms{
int num1 = 2;
int num2 = 3;
int correctAnswer = 1;
int answer = Algorithms.gcd(num1, num2);
int answer = NumberAlgorithms.gcd(num1, num2);
assertEquals("GCD Integer 1 failed", correctAnswer, answer);
//Test 2
num1 = 1000;
num2 = 575;
correctAnswer = 25;
answer = Algorithms.gcd(num1, num2);
answer = NumberAlgorithms.gcd(num1, num2);
assertEquals("GCD Integer 2 failed", correctAnswer, answer);
//Test 3
num1 = 1000;
num2 = 1000;
correctAnswer = 1000;
answer = Algorithms.gcd(num1, num2);
answer = NumberAlgorithms.gcd(num1, num2);
assertEquals("GCD Integer 3 failed", correctAnswer, answer);
//Test 4
long longNum1 = 2;
long longNum2 = 3;
long longCorrectAnswer = 1;
long longAnswer = Algorithms.gcd(longNum1, longNum2);
long longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
assertEquals("GCD Long 1 failed", longCorrectAnswer, longAnswer);
//Test 5
longNum1 = 1000;
longNum2 = 575;
longCorrectAnswer = 25;
longAnswer = Algorithms.gcd(longNum1, longNum2);
longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
assertEquals("GCD Long 2 failed", longCorrectAnswer, longAnswer);
//Test 6
longNum1 = 1000;
longNum2 = 1000;
longCorrectAnswer = 1000;
longAnswer = Algorithms.gcd(longNum1, longNum2);
longAnswer = NumberAlgorithms.gcd(longNum1, longNum2);
assertEquals("GCD Long 3 failed", longCorrectAnswer, longAnswer);
//Test 7
BigInteger bigNum1 = BigInteger.TWO;
BigInteger bigNum2 = BigInteger.valueOf(3);
BigInteger bigCorrectAnswer = BigInteger.ONE;
BigInteger bigAnswer = Algorithms.gcd(bigNum1, bigNum2);
BigInteger bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
assertEquals("GCD BigInteger 1 failed", bigCorrectAnswer, bigAnswer);
//Test 8
bigNum1 = BigInteger.valueOf(1000);
bigNum2 = BigInteger.valueOf(575);
bigCorrectAnswer = BigInteger.valueOf(25);
bigAnswer = Algorithms.gcd(bigNum1, bigNum2);
bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
assertEquals("GCD BigInteger 2 failed", bigCorrectAnswer, bigAnswer);
//Test 9
bigNum1 = BigInteger.valueOf(1000);
bigNum2 = BigInteger.valueOf(1000);
bigCorrectAnswer = BigInteger.valueOf(1000);
bigAnswer = Algorithms.gcd(bigNum1, bigNum2);
bigAnswer = NumberAlgorithms.gcd(bigNum1, bigNum2);
assertEquals("GCD BigInteger 3 failed", bigCorrectAnswer, bigAnswer);
}
@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);
}
@Test
public void testFindNumOccurrence(){
//Test 1
String testString = "abcdefgdd";
char testChar = 'a';
long correctAnswer = 1;
long answer = Algorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 1 failed", correctAnswer, answer);
//Test 2
testChar = 'd';
correctAnswer = 3;
answer = Algorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 2 failed", correctAnswer, answer);
//Test 3
testChar = 'h';
correctAnswer = 0;
answer = Algorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 3 failed", correctAnswer, answer);
}
@Test
public void testIsPalindrome(){
//Test 1
String str = "101";
boolean correctAnswer = true;
boolean answer = Algorithms.isPalindrome(str);
assertEquals("isPalindrome 1 failed", correctAnswer, answer);
//Test 2
str = "100";
correctAnswer = false;
answer = Algorithms.isPalindrome(str);
assertEquals("isPalindrome 2 failed", correctAnswer, answer);
//Test 3
str = "";
correctAnswer = true;
answer = Algorithms.isPalindrome(str);
assertEquals("isPalindrome 3 failed", correctAnswer, answer);
}
@Test
public void testToBin(){
//Test 1
int num = 7;
String correctAnswer = "111";
String answer = Algorithms.toBin(num);
String answer = NumberAlgorithms.toBin(num);
assertEquals("toBin 1 failed", correctAnswer, answer);
//Test 2
num = 0;
correctAnswer = "0";
answer = Algorithms.toBin(num);
answer = NumberAlgorithms.toBin(num);
assertEquals("toBin 2 failed", correctAnswer, answer);
//Test 3
num = 1000000;
correctAnswer = "11110100001001000000";
answer = Algorithms.toBin(num);
answer = NumberAlgorithms.toBin(num);
assertEquals("toBin 3 failed", correctAnswer, answer);
//Test 4
long longNum = 7;
correctAnswer = "111";
answer = Algorithms.toBin(longNum);
answer = NumberAlgorithms.toBin(longNum);
assertEquals("toBin long 1 failed", correctAnswer, answer);
//Test 5
longNum = 0;
correctAnswer = "0";
answer = Algorithms.toBin(longNum);
answer = NumberAlgorithms.toBin(longNum);
assertEquals("toBin long 2 failed", correctAnswer, answer);
//Test 6
longNum = 1000000;
correctAnswer = "11110100001001000000";
answer = Algorithms.toBin(longNum);
answer = NumberAlgorithms.toBin(longNum);
assertEquals("toBin long 3 failed", correctAnswer, answer);
//Test 7
BigInteger bigNum = BigInteger.valueOf(7);
correctAnswer = "111";
answer = Algorithms.toBin(bigNum);
answer = NumberAlgorithms.toBin(bigNum);
assertEquals("toBin big 1 failed", correctAnswer, answer);
//Test 8
bigNum = BigInteger.ZERO;
correctAnswer = "0";
answer = Algorithms.toBin(bigNum);
answer = NumberAlgorithms.toBin(bigNum);
assertEquals("toBin big 2 failed", correctAnswer, answer);
//Test 9
bigNum = BigInteger.valueOf(1000000);
correctAnswer = "11110100001001000000";
answer = Algorithms.toBin(bigNum);
answer = NumberAlgorithms.toBin(bigNum);
assertEquals("toBin big 3 failed", correctAnswer, answer);
}
@Test
public void testPrintList(){
//Test 1
ArrayList<Integer> nums = new ArrayList<Integer>();
String correctAnswer = "[]";
String answer = Algorithms.printList(nums);
assertEquals("printList<Integer> 1 failed", correctAnswer, answer);
//Test 2
nums = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
answer = Algorithms.printList(nums);
assertEquals("printList<Integer> 2 failed", correctAnswer, answer);
//Test 3
nums = new ArrayList<Integer>(Arrays.asList(-3, -2, -1, 0, 1, 2, 3));
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
answer = Algorithms.printList(nums);
assertEquals("printList<Integer> 3 failed", correctAnswer, answer);
//Test 4
ArrayList<String> strings = new ArrayList<String>(Arrays.asList("A", "B", "C"));
correctAnswer = "[A, B, C]";
answer = Algorithms.printList(strings);
assertEquals("printList<String> 1 failed", correctAnswer, answer);
//Test 5
strings = new ArrayList<String>(Arrays.asList("abc", "def", "ghi"));
correctAnswer = "[abc, def, ghi]";
answer = Algorithms.printList(strings);
assertEquals("printList<String> 2 failed", correctAnswer, answer);
}
}

View File

@@ -0,0 +1,79 @@
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 07-03-21
//This class contains tests for my number algorithms
/*
Copyright (C) 2021 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.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
public class TestStringAlgorithms{
@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 = StringAlgorithms.getPermutations(permString);
assertEquals("getPermutations failed", correctAnswer, answer);
}
@Test
public void testFindNumOccurrence(){
//Test 1
String testString = "abcdefgdd";
char testChar = 'a';
long correctAnswer = 1;
long answer = StringAlgorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 1 failed", correctAnswer, answer);
//Test 2
testChar = 'd';
correctAnswer = 3;
answer = StringAlgorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 2 failed", correctAnswer, answer);
//Test 3
testChar = 'h';
correctAnswer = 0;
answer = StringAlgorithms.findNumOccurrence(testString, testChar);
assertEquals("FindNumOccurrence 3 failed", correctAnswer, answer);
}
@Test
public void testIsPalindrome(){
//Test 1
String str = "101";
boolean correctAnswer = true;
boolean answer = StringAlgorithms.isPalindrome(str);
assertEquals("isPalindrome 1 failed", correctAnswer, answer);
//Test 2
str = "100";
correctAnswer = false;
answer = StringAlgorithms.isPalindrome(str);
assertEquals("isPalindrome 2 failed", correctAnswer, answer);
//Test 3
str = "";
correctAnswer = true;
answer = StringAlgorithms.isPalindrome(str);
assertEquals("isPalindrome 3 failed", correctAnswer, answer);
}
}