mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//JavaClasses/src/test/java/mattrixwv/TestAlgorithms.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-07-20
|
||||
//Modified: 06-01-21
|
||||
//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
|
||||
/*
|
||||
@@ -476,4 +476,72 @@ public class TestAlgorithms{
|
||||
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);
|
||||
assertEquals("toBin 1 failed", correctAnswer, answer);
|
||||
//Test 2
|
||||
num = 0;
|
||||
correctAnswer = "0";
|
||||
answer = Algorithms.toBin(num);
|
||||
assertEquals("toBin 2 failed", correctAnswer, answer);
|
||||
//Test 3
|
||||
num = 1000000;
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = Algorithms.toBin(num);
|
||||
assertEquals("toBin 3 failed", correctAnswer, answer);
|
||||
|
||||
//Test 4
|
||||
long longNum = 7;
|
||||
correctAnswer = "111";
|
||||
answer = Algorithms.toBin(longNum);
|
||||
assertEquals("toBin long 1 failed", correctAnswer, answer);
|
||||
//Test 5
|
||||
longNum = 0;
|
||||
correctAnswer = "0";
|
||||
answer = Algorithms.toBin(longNum);
|
||||
assertEquals("toBin long 2 failed", correctAnswer, answer);
|
||||
//Test 6
|
||||
longNum = 1000000;
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = Algorithms.toBin(longNum);
|
||||
assertEquals("toBin long 3 failed", correctAnswer, answer);
|
||||
|
||||
//Test 7
|
||||
BigInteger bigNum = BigInteger.valueOf(7);
|
||||
correctAnswer = "111";
|
||||
answer = Algorithms.toBin(bigNum);
|
||||
assertEquals("toBin big 1 failed", correctAnswer, answer);
|
||||
//Test 8
|
||||
bigNum = BigInteger.ZERO;
|
||||
correctAnswer = "0";
|
||||
answer = Algorithms.toBin(bigNum);
|
||||
assertEquals("toBin big 2 failed", correctAnswer, answer);
|
||||
//Test 9
|
||||
bigNum = BigInteger.valueOf(1000000);
|
||||
correctAnswer = "11110100001001000000";
|
||||
answer = Algorithms.toBin(bigNum);
|
||||
assertEquals("toBin big 3 failed", correctAnswer, answer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user