Updated libraries and increased test coverage

This commit is contained in:
2023-04-13 19:12:33 -04:00
parent 783fc7009c
commit f4dbf4e4dc
20 changed files with 560 additions and 227 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/test/java/mattrixwv/TestNumberAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 06-26-22
//Modified: 04-13-23
//This class contains tests for my number algorithms
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -115,94 +115,109 @@ public class TestNumberAlgorithms{
int num = 4;
boolean correctAnswer = false;
boolean answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 1 failed");
assertEquals(correctAnswer, answer);
//Test 2
num = 9;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 2 failed");
//Test 3
num = 15;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 3 failed");
//Test 4
assertEquals(correctAnswer, answer);
//Test 3
num = 97;
correctAnswer = true;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 4 failed");
//Test 5
num = 1000;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 5 failed");
//Test 6
assertEquals(correctAnswer, answer);
//Test 4
num = 1;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer, "isPrime int 6 failed");
assertEquals(correctAnswer, answer);
//Test 5
num = 2;
correctAnswer = true;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer);
//Test 6
num = 49;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer);
//Test 7
long longNum = 4;
num = 55;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 1 failed");
answer = NumberAlgorithms.isPrime(num);
assertEquals(correctAnswer, answer);
//Test 8
longNum = 9;
long longNum = 4L;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 2 failed");
assertEquals(correctAnswer, answer);
//Test 9
longNum = 15;
longNum = 15L;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 3 failed");
assertEquals(correctAnswer, answer);
//Test 10
longNum = 97;
longNum = 97L;
correctAnswer = true;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 4 failed");
assertEquals(correctAnswer, answer);
//Test 11
longNum = 1000;
longNum = 1L;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 5 failed");
assertEquals(correctAnswer, answer);
//Test 12
longNum = 1;
longNum = 2L;
correctAnswer = true;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer);
//Test 13
longNum = 49L;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer, "isPrime long 6 failed");
assertEquals(correctAnswer, answer);
//Test 14
longNum = 55L;
correctAnswer = false;
answer = NumberAlgorithms.isPrime(longNum);
assertEquals(correctAnswer, answer);
//Test 13
//Test 15
BigInteger bigNum = BigInteger.valueOf(4);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 1 failed");
//Test 14
bigNum = BigInteger.valueOf(9);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 2 failed");
//Test 15
assertEquals(correctAnswer, answer);
//Test 16
bigNum = BigInteger.valueOf(15);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 3 failed");
//Test 16
assertEquals(correctAnswer, answer);
//Test 17
bigNum = BigInteger.valueOf(97);
correctAnswer = true;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 4 failed");
//Test 17
bigNum = BigInteger.valueOf(1000);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 5 failed");
assertEquals(correctAnswer, answer);
//Test 18
bigNum = BigInteger.ONE;
bigNum = BigInteger.valueOf(1);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer, "isPrime BigInteger 6 failed");
assertEquals(correctAnswer, answer);
//Test 19
bigNum = BigInteger.valueOf(2);
correctAnswer = true;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer);
//Test 20
bigNum = BigInteger.valueOf(49);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer);
//Test 21
bigNum = BigInteger.valueOf(55);
correctAnswer = false;
answer = NumberAlgorithms.isPrime(bigNum);
assertEquals(correctAnswer, answer);
}
@Test
@@ -211,49 +226,66 @@ public class TestNumberAlgorithms{
List<Integer> correctAnswer = Arrays.asList(2, 2, 5, 5);
int number = 100;
List<Integer> answer = NumberAlgorithms.getFactors(number);
assertEquals(correctAnswer, answer, "getFactors int 1 failed");
assertEquals(correctAnswer, answer);
//Test 2
correctAnswer = Arrays.asList(2, 7, 7);
number = 98;
answer = NumberAlgorithms.getFactors(number);
assertEquals(correctAnswer, answer, "getFactors int 2 failed");
assertEquals(correctAnswer, answer);
//Test 3
correctAnswer = Arrays.asList(7);
number = 7;
answer = NumberAlgorithms.getFactors(number);
assertEquals(correctAnswer, answer, "getFactors int 3 failed");
assertEquals(correctAnswer, answer);
//Test 4
correctAnswer = Arrays.asList(2, 5);
number = 10;
answer = NumberAlgorithms.getFactors(number);
assertEquals(correctAnswer, answer);
//Test 5
List<Long> longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L);
long longNumber = 100L;
List<Long> longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 1 failed");
//Test 5
assertEquals(longCorrectAnswer, longAnswer);
//Test 6
longCorrectAnswer = Arrays.asList(2L, 7L, 7L);
longNumber = 98;
longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 2 failed");
//Test 6
assertEquals(longCorrectAnswer, longAnswer);
//Test 7
longCorrectAnswer = Arrays.asList(7L);
longNumber = 7;
longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals(longCorrectAnswer, longAnswer, "getFactors long 3 failed");
assertEquals(longCorrectAnswer, longAnswer);
//Test 8
longCorrectAnswer = Arrays.asList(2L, 5L);
longNumber = 10L;
longAnswer = NumberAlgorithms.getFactors(longNumber);
assertEquals(longCorrectAnswer, longAnswer);
//Test 7
//Test 9
List<BigInteger> bigCorrectAnswer = Arrays.asList(BigInteger.TWO, BigInteger.TWO, BigInteger.valueOf(5), BigInteger.valueOf(5));
BigInteger bigNumber = BigInteger.valueOf(100);
List<BigInteger> bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
//Test 8
assertEquals(bigCorrectAnswer, bigAnswer);
//Test 10
bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(7), BigInteger.valueOf(7));
bigNumber = BigInteger.valueOf(98);
bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
//Test 9
assertEquals(bigCorrectAnswer, bigAnswer);
//Test 11
bigCorrectAnswer = Arrays.asList(BigInteger.valueOf(7));
bigNumber = BigInteger.valueOf(7);
bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals(bigCorrectAnswer, bigAnswer, "getFactors BigInteger failed");
assertEquals(bigCorrectAnswer, bigAnswer);
//Test 12
bigCorrectAnswer = Arrays.asList(BigInteger.TWO, BigInteger.valueOf(5));
bigNumber = BigInteger.valueOf(10);
bigAnswer = NumberAlgorithms.getFactors(bigNumber);
assertEquals(bigCorrectAnswer, bigAnswer);
}
@Test

View File

@@ -1,11 +1,11 @@
//JavaClasses/src/test/java/mattrixwv/TestStopwatch.java
//Matthew Ellison
// Created: 06-07-20
//Modified: 06-26-22
//Modified: 04-13-23
//This class holds many algorithms that I have found it useful to keep around
//As such all of the functions in here are static and meant to be used as stand alone functions
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -28,6 +28,7 @@ package com.mattrixwv;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
@@ -81,6 +82,8 @@ public class TestStopwatch{
//Test milliseconds
results = Stopwatch.getStr(1.0e6);
assertEquals("1.000 milliseconds", results);
results = Stopwatch.getStr(1.2e8);
assertEquals("120.000 milliseconds", results);
//Test seconds
results = Stopwatch.getStr(1.0e9);
assertEquals("1.000 seconds", results);
@@ -90,6 +93,11 @@ public class TestStopwatch{
//Test hours
results = Stopwatch.getStr(1.0e13);
assertEquals("2.778 hours", results);
//Test error
assertThrows(InvalidResult.class, () -> {
Stopwatch.getStr(-1.0);
});
}
@Test

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/test/java/mattrixwv/TestStringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 06-25-22
//Modified: 04-13-23
//This class contains tests for my number algorithms
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -24,6 +24,7 @@ package com.mattrixwv;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -38,6 +39,18 @@ public class TestStringAlgorithms{
List<String> correctAnswer = Arrays.asList("012", "021", "102", "120", "201", "210");
List<String> answer = StringAlgorithms.getPermutations(permString);
assertEquals(correctAnswer, answer, "getPermutations failed");
//Test 2
permString = "012";
correctAnswer = new ArrayList<>();
answer = StringAlgorithms.getPermutations(permString, 4);
assertEquals(correctAnswer, answer);
//Test 3
permString = "012";
correctAnswer = new ArrayList<>();
answer = StringAlgorithms.getPermutations(permString, -1);
assertEquals(correctAnswer, answer);
}
@Test
public void testFindNumOccurrence(){

View File

@@ -1,9 +1,9 @@
//JavaClasses/src/test/java/com/mattrixwv/TestTriple.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -22,24 +22,66 @@ package com.mattrixwv;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestTriple{
private Triple<Long, Long, Long> triple;
@BeforeEach
public void setup(){
triple = new Triple<>(1L, 2L, 3L);
}
@Test
public void testTriple(){
Triple<Long, Long, Long> longTriple1 = new Triple<Long, Long, Long>(1L, 2L, 3L);
Triple<Long, Long, Long> longTriple2 = new Triple<Long, Long, Long>(1L, 2L, 3L);
Triple<Long, Long, Long> longTriple3 = new Triple<Long, Long, Long>(3L, 2L, 1L);
assertEquals(1L, longTriple1.getA());
assertEquals(2L, longTriple1.getB());
assertEquals(3L, longTriple1.getC());
assertEquals(true, longTriple1.equals(longTriple1));
assertEquals(true, longTriple1.equals(longTriple2));
assertEquals(false, longTriple1.equals(longTriple3));
assertEquals(false, longTriple1.equals(1L));
assertEquals(Long.hashCode(1) + Long.hashCode(2) * Long.hashCode(3), longTriple1.hashCode());
assertEquals("[1, 2, 3]", longTriple1.toString());
public void testConstructor(){
assertEquals(1L, triple.getA());
assertEquals(2L, triple.getB());
assertEquals(3L, triple.getC());
}
@Test
public void testGetters(){
assertEquals(1L, triple.getA());
assertEquals(2L, triple.getB());
assertEquals(3L, triple.getC());
}
@Test
public void testEquals(){
Triple<Long, Long, Long> triple2 = new Triple<>(1L, 2L, 3L);
Triple<Long, Long, Long> triple3 = new Triple<>(3L, 2L, 3L);
Triple<Long, Long, Long> triple4 = new Triple<>(1L, 3L, 3L);
Triple<Long, Long, Long> triple5 = new Triple<>(1L, 2L, 1L);
Triple<Long, Long, Long> triple6 = new Triple<>(2L, 1L, 3L);
Triple<Long, Long, Long> triple7 = new Triple<>(3L, 2L, 1L);
Triple<Long, Long, Long> triple8 = new Triple<>(1L, 3L, 2L);
Triple<Long, Long, Long> triple9 = new Triple<>(3L, 1L, 2L);
assertEquals(triple, triple);
assertEquals(triple, triple2);
assertNotEquals(triple, triple3);
assertNotEquals(triple, triple4);
assertNotEquals(triple, triple5);
assertNotEquals(triple, triple6);
assertNotEquals(triple, triple7);
assertNotEquals(triple, triple8);
assertNotEquals(triple, triple9);
assertNotEquals(triple, 1L);
}
@Test
public void testHashCode(){
assertEquals(Long.hashCode(1) + Long.hashCode(2) * Long.hashCode(3), triple.hashCode());
}
@Test
public void testToString(){
assertEquals("[1, 2, 3]", triple.toString());
}
}

View File

@@ -0,0 +1,37 @@
//JavaClasses/src/test/java/com/mattrixwv/exceptions/TestInvalidResult.java
//Mattrixwv
// Created: 04-13-23
//Modified: 04-13-23
package com.mattrixwv.exceptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.Test;
public class TestInvalidResult{
private String message = "message";
private Throwable cause = new Exception();
@Test
public void testConstructor(){
InvalidResult exception = new InvalidResult();
assertNull(exception.getMessage());
assertNull(exception.getCause());
exception = new InvalidResult(message);
assertEquals(message, exception.getMessage());
assertNull(exception.getCause());
exception = new InvalidResult(cause);
assertEquals(cause.toString(), exception.getMessage());
assertEquals(cause, exception.getCause());
exception = new InvalidResult(message, cause);
assertEquals(message, exception.getMessage());
assertEquals(cause, exception.getCause());
}
}

View File

@@ -1,9 +1,9 @@
//JavaClasses/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -22,26 +22,62 @@ package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestHexagonalNumberGenerator{
private HexagonalNumberGenerator gen;
@BeforeEach
public void setup(){
gen = new HexagonalNumberGenerator();
}
@Test
public void hexagonalNumberGeneratorTest(){
HexagonalNumberGenerator gen = new HexagonalNumberGenerator();
public void testConstructor(){
gen = new HexagonalNumberGenerator();
assertNotNull(gen);
assertEquals(1L, gen.num);
}
@Test
public void testHasNext(){
assertTrue(gen.hasNext());
gen.num = Long.MAX_VALUE;
assertFalse(gen.hasNext());
}
@Test
public void testNext(){
List<Long> nums = Arrays.asList(1L, 6L, 15L, 28L, 45L, 66L, 91L, 120L, 153L);
ArrayList<Long> generatedNums = new ArrayList<>();
for(int cnt = 0;cnt < nums.size();++cnt){
generatedNums.add(gen.next());
}
assertEquals(nums, generatedNums);
assertEquals(true, gen.hasNext());
assertEquals(true, HexagonalNumberGenerator.isHexagonal(153L));
assertEquals(false, HexagonalNumberGenerator.isHexagonal(154L));
gen.num = Long.MAX_VALUE;
assertThrows(NoSuchElementException.class, () -> {
gen.next();
});
}
@Test
public void testIsHexagonal(){
assertTrue(HexagonalNumberGenerator.isHexagonal(153L));
assertFalse(HexagonalNumberGenerator.isHexagonal(154L));
}
}

View File

@@ -1,9 +1,9 @@
//JavaClasses/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -22,26 +22,62 @@ package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestPentagonalNumberGenerator{
private PentagonalNumberGenerator gen;
@BeforeEach
public void setup(){
gen = new PentagonalNumberGenerator();
}
@Test
public void pentagonalNumberGeneratorTest(){
PentagonalNumberGenerator gen = new PentagonalNumberGenerator();
public void testConstructor(){
assertNotNull(gen);
assertEquals(1L, gen.num);
}
@Test
public void testHasNext(){
assertTrue(gen.hasNext());
gen.num = Long.MAX_VALUE;
assertFalse(gen.hasNext());
}
@Test
public void testNext(){
List<Long> nums = Arrays.asList(1L, 5L, 12L, 22L, 35L, 51L, 70L, 92L, 117L);
ArrayList<Long> generatedNums = new ArrayList<>();
for(int cnt = 0;cnt < nums.size();++cnt){
generatedNums.add(gen.next());
}
assertEquals(nums, generatedNums);
assertEquals(true, gen.hasNext());
assertEquals(true, PentagonalNumberGenerator.isPentagonal(117L));
assertEquals(false, PentagonalNumberGenerator.isPentagonal(118L));
gen.num = Long.MAX_VALUE;
assertThrows(NoSuchElementException.class, () -> {
gen.next();
});
}
@Test
public void testIsPentagonal(){
assertTrue(PentagonalNumberGenerator.isPentagonal(117L));
assertFalse(PentagonalNumberGenerator.isPentagonal(118L));
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/test/java/mattrixwv/TestSieveOfEratosthenes.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-26-22
//Modified: 04-13-23
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -23,41 +23,55 @@ package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestSieveOfEratosthenes{
private SieveOfEratosthenes sieve;
@BeforeEach
public void setup(){
sieve = new SieveOfEratosthenes();
}
@Test
public void testSieveOfEratosthenes(){
//Test 1
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
public void testConstructor(){
assertEquals(2L, sieve.possiblePrime);
assertEquals(new HashMap<>(), sieve.dict);
}
@Test
public void testHasNext(){
assertTrue(sieve.hasNext());
}
@Test
public void testNext(){
List<Long> correctAnswer = Arrays.asList(2L, 3L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L, 59L, 61L, 67L, 71L, 73L, 79L, 83L, 89L, 97L);
ArrayList<Long> answer = new ArrayList<Long>();
ArrayList<Long> answer = new ArrayList<>();
for(int cnt = 0;cnt < 25;++cnt){
long prime = sieve.next();
answer.add(prime);
}
assertEquals(correctAnswer, answer, "SieveOfEratosthenes failed");
assertTrue(sieve.hasNext());
}
@Test
public void testSieveOfEratosthenesBig(){
//Test 1
SieveOfEratosthenesBig sieve = new SieveOfEratosthenesBig();
List<BigInteger> correctAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97));
ArrayList<BigInteger> answer = new ArrayList<BigInteger>();
for(int cnt = 0;cnt < 25;++cnt){
BigInteger prime = sieve.next();
answer.add(prime);
}
assertEquals(correctAnswer, answer, "SieveOfEratosthenesBig failed");
assertTrue(sieve.hasNext());
assertEquals(correctAnswer, answer);
sieve.possiblePrime = Long.MAX_VALUE;
sieve.dict.put(Long.MAX_VALUE, new ArrayList<>());
sieve.dict.put(Long.MAX_VALUE + 2, new ArrayList<>());
assertThrows(NoSuchElementException.class, () -> {
sieve.next();
});
}
}

View File

@@ -0,0 +1,69 @@
//JavaClasses/src/test/java/mattrixwv/TestSieveOfEratosthenesBig.java
//Matthew Ellison
// Created: 04-13-23
//Modified: 04-13-23
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
/*
Copyright (C) 2023 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestSieveOfEratostheneseBig{
private SieveOfEratosthenesBig sieve;
@BeforeEach
public void setup(){
sieve = new SieveOfEratosthenesBig();
}
@Test
public void testConstructor(){
assertEquals(BigInteger.TWO, sieve.possiblePrime);
assertEquals(new HashMap<>(), sieve.dict);
}
@Test
public void testHasNext(){
assertTrue(sieve.hasNext());
}
@Test
public void testNext(){
List<BigInteger> correctAnswer = Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(5), BigInteger.valueOf(7), BigInteger.valueOf(11), BigInteger.valueOf(13), BigInteger.valueOf(17), BigInteger.valueOf(19), BigInteger.valueOf(23), BigInteger.valueOf(29), BigInteger.valueOf(31), BigInteger.valueOf(37), BigInteger.valueOf(41), BigInteger.valueOf(43), BigInteger.valueOf(47), BigInteger.valueOf(53), BigInteger.valueOf(59), BigInteger.valueOf(61), BigInteger.valueOf(67), BigInteger.valueOf(71), BigInteger.valueOf(73), BigInteger.valueOf(79), BigInteger.valueOf(83), BigInteger.valueOf(89), BigInteger.valueOf(97));
ArrayList<BigInteger> answer = new ArrayList<>();
for(int cnt = 0;cnt < 25;++cnt){
BigInteger prime = sieve.next();
answer.add(prime);
}
assertEquals(correctAnswer, answer);
}
}

View File

@@ -1,9 +1,9 @@
//JavaClasses/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -22,26 +22,60 @@ package com.mattrixwv.generators;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class TestTriangularNumberGenerator{
private TriangularNumberGenerator gen;
@BeforeEach
public void setup(){
gen = new TriangularNumberGenerator();
}
@Test
public void triangularNumberGeneratorTest(){
TriangularNumberGenerator gen = new TriangularNumberGenerator();
public void testConstructor(){
gen = new TriangularNumberGenerator();
}
@Test
public void testHasNext(){
assertTrue(gen.hasNext());
gen.num = Long.MAX_VALUE;
assertFalse(gen.hasNext());
}
@Test
public void testNext(){
List<Long> nums = Arrays.asList(1L, 3L, 6L, 10L, 15L, 21L, 28L, 36L, 45L);
ArrayList<Long> generatedNums = new ArrayList<>();
for(int cnt = 0;cnt < nums.size();++cnt){
generatedNums.add(gen.next());
}
assertEquals(nums, generatedNums);
assertEquals(true, gen.hasNext());
assertEquals(true, TriangularNumberGenerator.isTriangular(55L));
assertEquals(false, TriangularNumberGenerator.isTriangular(54L));
gen.num = Long.MAX_VALUE;
assertThrows(NoSuchElementException.class, () -> {
gen.next();
});
}
@Test
public void testIsTriangular(){
assertTrue(TriangularNumberGenerator.isTriangular(55L));
assertFalse(TriangularNumberGenerator.isTriangular(54L));
}
}