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

42
pom.xml
View File

@@ -6,7 +6,7 @@
<groupId>mattrixwv</groupId>
<artifactId>myClasses</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>myClasses</name>
<url>www.mattrixwv.com</url>
@@ -14,12 +14,13 @@
<properties>
<!--Compile-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<java.version>19</java.version>
<!--Sonarqube-->
<sonar.java.source>17</sonar.java.source>
<sonar.java.source>19</sonar.java.source>
<sonar.dependencyCheck.jsonReportPath>target/dependency-check-report.json</sonar.dependencyCheck.jsonReportPath>
<sonar.dependencyCheck.htmlReportPath>target/dependency-check-report.html</sonar.dependencyCheck.htmlReportPath>
</properties>
@@ -28,7 +29,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
@@ -39,7 +40,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.1.0</version>
<version>3.3.0</version>
<executions>
<execution>
<id>enforce-maven</id>
@@ -64,46 +65,51 @@
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<version>3.1.1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.0</version>
<version>3.12.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.3.0</version>
<version>3.4.2</version>
</plugin>
<!--Versions-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.11.0</version>
<version>2.15.0</version>
<configuration>
<rulesUri>file://${session.executionRootDirectory}/version-rules.xml</rulesUri>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.5.0</version>
</plugin>
<!--Sonarqube-->
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
@@ -113,7 +119,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<version>0.8.9</version>
<executions>
<execution>
<id>jacoco-initialize</id>
@@ -134,7 +140,7 @@
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>7.1.1</version>
<version>8.2.1</version>
<executions>
<execution>
<phase>none</phase>

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 06-25-22
//Modified: 04-13-23
//This class contains algorithms for numbers that I've found it useful to keep around
/*
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
@@ -277,7 +277,7 @@ public class NumberAlgorithms{
}
public static List<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
BigInteger topPossiblePrime = goalNumber.divide(BigInteger.TWO);
BigInteger topPossiblePrime = goalNumber.sqrt().add(BigInteger.ONE);
List<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> factors = new ArrayList<>();
@@ -303,7 +303,12 @@ public class NumberAlgorithms{
//If for some reason the goalNumber is not 1 throw an error
if(!goalNumber.equals(BigInteger.ONE)){
throw new InvalidResult("The factor was not 1: " + goalNumber);
if(isPrime(goalNumber)){
factors.add(goalNumber);
}
else{
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
}
//Return the list of factors

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/Stopwatch.java
//Matthew Ellison (Mattrixwv)
// Created: 03-01-19
//Modified: 06-26-22
//Modified: 04-13-23
//This file contains a class that is used to time the execution time of other programs
/*
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
@@ -122,6 +122,10 @@ public class Stopwatch{
}
}
if(duration < 0){
resolution = TIME_RESOLUTION.ERROR;
}
//Turn the number into a string
int durationFraction = (int)Math.round(((duration % 1) * 1000));
String time = String.format("%d.%03d", duration.intValue(), durationFraction);
@@ -143,11 +147,6 @@ public class Stopwatch{
@Override
public String toString(){
try{
return getStr();
}
catch(InvalidResult error){
return "There was an error in getStr(): " + error;
}
return getStr();
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 10-11-21
//Modified: 04-13-23
//This class contains algorithms for strings that I've found it useful to keep around
/*
Copyright (C) 2021 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
@@ -33,7 +33,7 @@ public class StringAlgorithms{
public static List<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, int num){
protected static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<>();
//Check if the number is out of bounds
if((num >= master.length()) || (num < 0)){

View File

@@ -1,17 +1,24 @@
//JavaClasses/src/main/java/mattrixwv/Exceptions/InvalidResult.java
//Matthew Ellison
// Created: 08-24-20
//Modified: 08-24-20
//Modified: 04-13-23
//This is an exception for an invalid result out of one of my algorithms
package com.mattrixwv.exceptions;
public class InvalidResult extends RuntimeException{
private static final long serialVersionUID = 1L;
public InvalidResult(){
super();
}
public InvalidResult(String msg){
super(msg);
}
public InvalidResult(Throwable cause){
super(cause);
}
public InvalidResult(String msg, Throwable cause){
super(msg, cause);
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
//Matthew Ellison
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates hexagonal numbers
/*
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
@@ -27,7 +27,7 @@ import java.util.NoSuchElementException;
public class HexagonalNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public HexagonalNumberGenerator(){
num = 1L;
@@ -35,23 +35,25 @@ public class HexagonalNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (2 * num * num) > 0;
return (2 * num * num) > num;
}
@Override
public Long next(){
Long newNum = ((2 * num * num) - num);
++num;
if(num > 0){
return newNum;
}
else{
//If the number will result in an overflow throw an exception
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
//Generate and return the hexagonal number
Long hexNum = ((2L * num * num) - num);
++num;
return hexNum;
}
public static boolean isHexagonal(Long x){
Long n = Math.round((Math.sqrt(1.0 + (8 * x)) + 1) / 4);
return ((2 * n * n) - n) == x;
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) + 1L) / 4L);
return ((2L * n * n) - n) == x;
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates pentagonal numbers
/*
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
@@ -27,7 +27,7 @@ import java.util.NoSuchElementException;
public class PentagonalNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public PentagonalNumberGenerator(){
num = 1L;
@@ -35,23 +35,22 @@ public class PentagonalNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (3 * num * num) > 0;
return (3L * num * num) > num;
}
@Override
public Long next(){
long newNum = ((3 * num * num) - num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
long pentNum = ((3L * num * num) - num) / 2L;
++num;
return pentNum;
}
public static boolean isPentagonal(Long x){
Long n = Math.round((Math.sqrt(1.0 + (24 * x)) + 1) / 6);
return (((3 * n * n) - n) / 2) == x;
Long n = Math.round((Math.sqrt(1.0D + (24L * x)) + 1L) / 6L);
return (((3L * n * n) - n) / 2L) == x;
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-25-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
@@ -31,37 +31,36 @@ import java.util.NoSuchElementException;
public class SieveOfEratosthenes implements Iterator<Long>{
long possiblePrime;
private Map<Long, ArrayList<Long>> dict;
protected long possiblePrime;
protected Map<Long, ArrayList<Long>> dict;
public SieveOfEratosthenes(){
dict = new HashMap<>();
possiblePrime = 2;
}
@Override
public boolean hasNext(){
return true;
}
@Override
public Long next(){
long prime;
//If this is the first run just return 2
if(possiblePrime <= 2){
if(possiblePrime == 2){
prime = possiblePrime++;
return prime;
}
//Loop until you find a prime number
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
if(possiblePrime < 0){
throw new NoSuchElementException("the next prime cannot be described by a long");
}
//Create the next entry for all entries in the map
for(long num : dict.get(possiblePrime)){
if(!dict.containsKey(possiblePrime + num + num)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(num));
dict.put(possiblePrime + num + num, tempArray);
dict.put(possiblePrime + num + num, new ArrayList<>(Arrays.asList(num)));
}
else{
dict.get(possiblePrime + num + num).add(num);
@@ -70,16 +69,14 @@ public class SieveOfEratosthenes implements Iterator<Long>{
//Delete the current entry
dict.remove(possiblePrime);
}
//Protect against overflows
if(possiblePrime < 0){
throw new NoSuchElementException("the next prime cannot be described by a long");
}
//Save that the number is a prime
prime = possiblePrime;
//Add the next entry to the prime
if(!dict.containsKey(prime * 3)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(prime));
dict.put(prime * 3, tempArray);
}
else{
dict.get(prime * 3).add(prime);
}
//Add the next entry to the prime dictionary
dict.put(prime * 3, new ArrayList<>(Arrays.asList(prime)));
//Move on to the next possible prime
possiblePrime += 2;

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-25-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
@@ -31,17 +31,20 @@ import java.util.Map;
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
BigInteger possiblePrime;
private Map<BigInteger, ArrayList<BigInteger>> dict;
protected BigInteger possiblePrime;
protected Map<BigInteger, ArrayList<BigInteger>> dict;
public SieveOfEratosthenesBig(){
dict = new HashMap<>();
possiblePrime = BigInteger.TWO;
}
@Override
public boolean hasNext(){
return true;
}
@Override
public BigInteger next(){
BigInteger prime;
@@ -71,14 +74,8 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
}
//Save that the number is a prime
prime = possiblePrime;
BigInteger loc = prime.multiply(BigInteger.valueOf(3));
if(!dict.containsKey(loc)){
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(prime));
dict.put(loc, tempArray);
}
else{
dict.get(loc).add(prime);
}
//Add the next entry to the prime dictionary
dict.put(prime.multiply(BigInteger.valueOf(3)), new ArrayList<>(Arrays.asList(prime)));
//Move on to the next possible prime
possiblePrime = possiblePrime.add(BigInteger.TWO);

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates triangular numbers
/*
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
@@ -27,7 +27,8 @@ import java.util.NoSuchElementException;
public class TriangularNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public TriangularNumberGenerator(){
num = 1L;
@@ -35,23 +36,22 @@ public class TriangularNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (num * num) > 0;
return ((num * num) + num) > num;
}
@Override
public Long next(){
Long newNum = ((num * num) + num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
Long newNum = ((num * num) + num) / 2L;
++num;
return newNum;
}
public static boolean isTriangular(Long x){
Long n = Math.round((Math.sqrt(1.0 + (8 * x)) - 1) / 2);
return (((n * n) + n) / 2) == x;
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) - 1L) / 2L);
return (((n * n) + n) / 2L) == x;
}
}

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));
}
}