From ee9b256b671ed5f2016be273d4381a2ae09312ba Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sat, 3 Jul 2021 15:39:56 -0400 Subject: [PATCH] Split algorithms to separate files --- src/main/java/mattrixwv/ArrayAlgorithms.java | 146 ++++++++ ...{Algorithms.java => NumberAlgorithms.java} | 222 +----------- src/main/java/mattrixwv/StringAlgorithms.java | 90 +++++ .../java/mattrixwv/TestArrayAlgorithms.java | 114 ++++++ ...orithms.java => TestNumberAlgorithms.java} | 326 ++++-------------- .../java/mattrixwv/TestStringAlgorithms.java | 79 +++++ 6 files changed, 502 insertions(+), 475 deletions(-) create mode 100644 src/main/java/mattrixwv/ArrayAlgorithms.java rename src/main/java/mattrixwv/{Algorithms.java => NumberAlgorithms.java} (79%) create mode 100644 src/main/java/mattrixwv/StringAlgorithms.java create mode 100644 src/test/java/mattrixwv/TestArrayAlgorithms.java rename src/test/java/mattrixwv/{TestAlgorithms.java => TestNumberAlgorithms.java} (53%) create mode 100644 src/test/java/mattrixwv/TestStringAlgorithms.java diff --git a/src/main/java/mattrixwv/ArrayAlgorithms.java b/src/main/java/mattrixwv/ArrayAlgorithms.java new file mode 100644 index 0000000..3f51191 --- /dev/null +++ b/src/main/java/mattrixwv/ArrayAlgorithms.java @@ -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 . +*/ +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 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 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 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 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 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 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 String printList(ArrayList 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(); + } +} diff --git a/src/main/java/mattrixwv/Algorithms.java b/src/main/java/mattrixwv/NumberAlgorithms.java similarity index 79% rename from src/main/java/mattrixwv/Algorithms.java rename to src/main/java/mattrixwv/NumberAlgorithms.java index 247d639..9a9ba66 100644 --- a/src/main/java/mattrixwv/Algorithms.java +++ b/src/main/java/mattrixwv/NumberAlgorithms.java @@ -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 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 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 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 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 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 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 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 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 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 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 getPermutations(String master){ - return getPermutations(master, 0); - } - private static ArrayList getPermutations(String master, int num){ - ArrayList perms = new ArrayList(); - //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 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 String printList(ArrayList 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(); - } } diff --git a/src/main/java/mattrixwv/StringAlgorithms.java b/src/main/java/mattrixwv/StringAlgorithms.java new file mode 100644 index 0000000..3e4fbc6 --- /dev/null +++ b/src/main/java/mattrixwv/StringAlgorithms.java @@ -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 . +*/ +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 getPermutations(String master){ + return getPermutations(master, 0); + } + private static ArrayList getPermutations(String master, int num){ + ArrayList perms = new ArrayList(); + //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 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; + } + } + +} diff --git a/src/test/java/mattrixwv/TestArrayAlgorithms.java b/src/test/java/mattrixwv/TestArrayAlgorithms.java new file mode 100644 index 0000000..b156fd0 --- /dev/null +++ b/src/test/java/mattrixwv/TestArrayAlgorithms.java @@ -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 . +*/ +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 numbers = new ArrayList(); + Integer answer = ArrayAlgorithms.getSum(numbers); + assertEquals("getSum Integer 1 failed", correctAnswer, answer); + //Test 2 + correctAnswer = 118; + numbers = new ArrayList(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 longNumbers = new ArrayList(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 bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); + BigInteger bigAnswer = ArrayAlgorithms.getBigSum(bigNumbers); + assertEquals("getSum BigInteger failed", bigCorrectAnswer, bigAnswer); + } + @Test + public void testGetProd(){ + //Test 1 + Integer correctAnswer = 0; + ArrayList numbers = new ArrayList(); + Integer answer = ArrayAlgorithms.getProd(numbers); + assertEquals("getProd Integer 1 failed", correctAnswer, answer); + //Test 2 + correctAnswer = 57600; + numbers = new ArrayList(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 longNumbers = new ArrayList(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 bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); + BigInteger bigAnswer = ArrayAlgorithms.getBigProd(bigNumbers); + assertEquals("getProd BigInteger failed", bigCorrectAnswer, bigAnswer); + } + @Test + public void testPrintList(){ + //Test 1 + ArrayList nums = new ArrayList(); + String correctAnswer = "[]"; + String answer = ArrayAlgorithms.printList(nums); + assertEquals("printList 1 failed", correctAnswer, answer); + //Test 2 + nums = new ArrayList(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 2 failed", correctAnswer, answer); + //Test 3 + nums = new ArrayList(Arrays.asList(-3, -2, -1, 0, 1, 2, 3)); + correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; + answer = ArrayAlgorithms.printList(nums); + assertEquals("printList 3 failed", correctAnswer, answer); + + //Test 4 + ArrayList strings = new ArrayList(Arrays.asList("A", "B", "C")); + correctAnswer = "[A, B, C]"; + answer = ArrayAlgorithms.printList(strings); + assertEquals("printList 1 failed", correctAnswer, answer); + //Test 5 + strings = new ArrayList(Arrays.asList("abc", "def", "ghi")); + correctAnswer = "[abc, def, ghi]"; + answer = ArrayAlgorithms.printList(strings); + assertEquals("printList 2 failed", correctAnswer, answer); + } +} diff --git a/src/test/java/mattrixwv/TestAlgorithms.java b/src/test/java/mattrixwv/TestNumberAlgorithms.java similarity index 53% rename from src/test/java/mattrixwv/TestAlgorithms.java rename to src/test/java/mattrixwv/TestNumberAlgorithms.java index e737baf..92060b6 100644 --- a/src/test/java/mattrixwv/TestAlgorithms.java +++ b/src/test/java/mattrixwv/TestNumberAlgorithms.java @@ -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 . */ - - 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 correctAnswer = new ArrayList(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)); Integer topNum = 100; - ArrayList answer = Algorithms.getPrimes(topNum); + ArrayList answer = NumberAlgorithms.getPrimes(topNum); assertEquals("getPrimes Integer failed", correctAnswer, answer); //Test 2 ArrayList longCorrectAnswer = new ArrayList(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 longAnswer = Algorithms.getPrimes(longTopNum); + ArrayList longAnswer = NumberAlgorithms.getPrimes(longTopNum); assertEquals("getPrimes Long failed", longCorrectAnswer, longAnswer); //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); BigInteger bigTopNum = BigInteger.valueOf(100); - ArrayList bigAnswer = Algorithms.getPrimes(bigTopNum); + ArrayList bigAnswer = NumberAlgorithms.getPrimes(bigTopNum); assertEquals("getPrimes BigInteger failed", bigCorrectAnswer, bigAnswer); } @Test @@ -61,19 +59,19 @@ public class TestAlgorithms{ //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)); Integer numPrimes = 25; - ArrayList answer = Algorithms.getNumPrimes(numPrimes); + ArrayList answer = NumberAlgorithms.getNumPrimes(numPrimes); assertEquals("getNumPrimes Integer failed", correctAnswer, answer); //Test 2 ArrayList longCorrectAnswer = new ArrayList(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 longAnswer = Algorithms.getNumPrimes(longNumPrimes); + ArrayList longAnswer = NumberAlgorithms.getNumPrimes(longNumPrimes); assertEquals("getNumPrimes Long failed", longCorrectAnswer, longAnswer); //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97))); BigInteger bigNumPrimes = BigInteger.valueOf(25); - ArrayList bigAnswer = Algorithms.getNumPrimes(bigNumPrimes); + ArrayList 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 correctAnswer = new ArrayList(Arrays.asList(2, 2, 5, 5)); Integer number = 100; - ArrayList answer = Algorithms.getFactors(number); + ArrayList answer = NumberAlgorithms.getFactors(number); assertEquals("getFactors Integer 1 failed", correctAnswer, answer); //Test 2 correctAnswer = new ArrayList(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 longCorrectAnswer = new ArrayList(Arrays.asList(2L, 2L, 5L, 5L)); Long longNumber = 100L; - ArrayList longAnswer = Algorithms.getFactors(longNumber); + ArrayList longAnswer = NumberAlgorithms.getFactors(longNumber); assertEquals("getFactors Long failed", longCorrectAnswer, longAnswer); //Test 4 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7))); BigInteger bigNumber = BigInteger.valueOf(98); - ArrayList bigAnswer = Algorithms.getFactors(bigNumber); + ArrayList bigAnswer = NumberAlgorithms.getFactors(bigNumber); assertEquals("getFactors BigInteger failed", bigCorrectAnswer, bigAnswer); } @Test @@ -171,19 +169,19 @@ public class TestAlgorithms{ //Test 1 ArrayList correctAnswer = new ArrayList(Arrays.asList(1, 2, 4, 5, 10, 20, 25, 50, 100)); Integer topNum = 100; - ArrayList answer = Algorithms.getDivisors(topNum); + ArrayList answer = NumberAlgorithms.getDivisors(topNum); assertEquals("getDivisors Integer failed", correctAnswer, answer); //Test 2 ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 2L, 4L, 5L, 10L, 20L, 25L, 50L, 100L)); Long longTopNum = 100L; - ArrayList longAnswer = Algorithms.getDivisors(longTopNum); + ArrayList longAnswer = NumberAlgorithms.getDivisors(longTopNum); assertEquals("getDivisors Long failed", longCorrectAnswer, longAnswer); //Test 3 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(4), BigInteger.valueOf(5), BigInteger.valueOf(10), BigInteger.valueOf(20), BigInteger.valueOf(25), BigInteger.valueOf(50), BigInteger.valueOf(100))); BigInteger bigTopNum = BigInteger.valueOf(100); - ArrayList bigAnswer = Algorithms.getDivisors(bigTopNum); + ArrayList 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 correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)); Integer highestNumber = 100; - ArrayList answer = Algorithms.getAllFib(highestNumber); + ArrayList answer = NumberAlgorithms.getAllFib(highestNumber); assertEquals("getAllFib Integer 1 failed", correctAnswer, answer); //Test 2 correctAnswer = new ArrayList(Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987)); highestNumber = 1000; - answer = Algorithms.getAllFib(highestNumber); + answer = NumberAlgorithms.getAllFib(highestNumber); assertEquals("getAllFib Integer 2 failed", correctAnswer, answer); //Test 3 ArrayList longCorrectAnswer = new ArrayList(Arrays.asList(1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L, 55L, 89L, 144L, 233L, 377L, 610L, 987L)); Long longHighestNumber = 1000L; - ArrayList longAnswer = Algorithms.getAllFib(longHighestNumber); + ArrayList longAnswer = NumberAlgorithms.getAllFib(longHighestNumber); assertEquals("getAllFib Long failed", longCorrectAnswer, longAnswer); //Test 4 ArrayList bigCorrectAnswer = new ArrayList(Arrays.asList(BigInteger.valueOf(1), BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(8), BigInteger.valueOf(13), BigInteger.valueOf(21), BigInteger.valueOf(34), BigInteger.valueOf(55), BigInteger.valueOf(89))); BigInteger bigHighestNumber = BigInteger.valueOf(100); - ArrayList bigAnswer = Algorithms.getAllFib(bigHighestNumber); + ArrayList 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 numbers = new ArrayList(); - Integer answer = Algorithms.getSum(numbers); - assertEquals("getSum Integer 1 failed", correctAnswer, answer); - //Test 2 - correctAnswer = 118; - numbers = new ArrayList(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 longNumbers = new ArrayList(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 bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); - BigInteger bigAnswer = Algorithms.getBigSum(bigNumbers); - assertEquals("getSum BigInteger failed", bigCorrectAnswer, bigAnswer); - } - @Test - public void testGetProd(){ - //Test 1 - Integer correctAnswer = 0; - ArrayList numbers = new ArrayList(); - Integer answer = Algorithms.getProd(numbers); - assertEquals("getProd Integer 1 failed", correctAnswer, answer); - //Test 2 - correctAnswer = 57600; - numbers = new ArrayList(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 longNumbers = new ArrayList(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 bigNumbers = new ArrayList(Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(3), BigInteger.valueOf(4), BigInteger.valueOf(4), BigInteger.valueOf(100))); - BigInteger bigAnswer = Algorithms.getBigProd(bigNumbers); - assertEquals("getProd BigInteger failed", bigCorrectAnswer, bigAnswer); - } - @Test - public void testIsFound(){ - //Integer - //Test 1 - Boolean correctAnswer = true; - ArrayList numbers = new ArrayList(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(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(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(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 longNumbers = new ArrayList(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(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(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(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 bigNumbers = new ArrayList(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(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(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(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 correctAnswer = new ArrayList(Arrays.asList("012", "021", "102", "120", "201", "210")); - ArrayList 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 nums = new ArrayList(); - String correctAnswer = "[]"; - String answer = Algorithms.printList(nums); - assertEquals("printList 1 failed", correctAnswer, answer); - //Test 2 - nums = new ArrayList(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 2 failed", correctAnswer, answer); - //Test 3 - nums = new ArrayList(Arrays.asList(-3, -2, -1, 0, 1, 2, 3)); - correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]"; - answer = Algorithms.printList(nums); - assertEquals("printList 3 failed", correctAnswer, answer); - - //Test 4 - ArrayList strings = new ArrayList(Arrays.asList("A", "B", "C")); - correctAnswer = "[A, B, C]"; - answer = Algorithms.printList(strings); - assertEquals("printList 1 failed", correctAnswer, answer); - //Test 5 - strings = new ArrayList(Arrays.asList("abc", "def", "ghi")); - correctAnswer = "[abc, def, ghi]"; - answer = Algorithms.printList(strings); - assertEquals("printList 2 failed", correctAnswer, answer); - } } diff --git a/src/test/java/mattrixwv/TestStringAlgorithms.java b/src/test/java/mattrixwv/TestStringAlgorithms.java new file mode 100644 index 0000000..21ebe1e --- /dev/null +++ b/src/test/java/mattrixwv/TestStringAlgorithms.java @@ -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 . +*/ +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 correctAnswer = new ArrayList(Arrays.asList("012", "021", "102", "120", "201", "210")); + ArrayList 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); + } +}