From f4dbf4e4dcbbbe2a442326a6c1f927c86f76fac3 Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 13 Apr 2023 19:12:33 -0400 Subject: [PATCH] Updated libraries and increased test coverage --- pom.xml | 42 +++-- .../java/com/mattrixwv/NumberAlgorithms.java | 13 +- src/main/java/com/mattrixwv/Stopwatch.java | 15 +- .../java/com/mattrixwv/StringAlgorithms.java | 6 +- .../mattrixwv/exceptions/InvalidResult.java | 9 +- .../generators/HexagonalNumberGenerator.java | 26 +-- .../generators/PentagonalNumberGenerator.java | 23 ++- .../generators/SieveOfEratosthenes.java | 33 ++-- .../generators/SieveOfEratosthenesBig.java | 21 +-- .../generators/TriangularNumberGenerator.java | 24 +-- .../com/mattrixwv/TestNumberAlgorithms.java | 162 +++++++++++------- .../java/com/mattrixwv/TestStopwatch.java | 12 +- .../com/mattrixwv/TestStringAlgorithms.java | 17 +- src/test/java/com/mattrixwv/TestTriple.java | 72 ++++++-- .../exceptions/TestInvalidResult.java | 37 ++++ .../TestHexagonalNumberGenerator.java | 50 +++++- .../TestPentagonalNumberGenerator.java | 50 +++++- .../generators/TestSieveOfEratosthenes.java | 58 ++++--- .../TestSieveOfEratostheneseBig.java | 69 ++++++++ .../TestTriangularNumberGenerator.java | 48 +++++- 20 files changed, 560 insertions(+), 227 deletions(-) create mode 100644 src/test/java/com/mattrixwv/exceptions/TestInvalidResult.java create mode 100644 src/test/java/com/mattrixwv/generators/TestSieveOfEratostheneseBig.java diff --git a/pom.xml b/pom.xml index e05eb66..34d5a3b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ mattrixwv myClasses - 1.2.0 + 1.2.1 myClasses www.mattrixwv.com @@ -14,12 +14,13 @@ UTF-8 - 17 - 17 - 17 + UTF-8 + 19 + 19 + 19 - 17 + 19 target/dependency-check-report.json target/dependency-check-report.html @@ -28,7 +29,7 @@ org.junit.jupiter junit-jupiter-api - 5.9.1 + 5.9.2 test @@ -39,7 +40,7 @@ org.apache.maven.plugins maven-enforcer-plugin - 3.1.0 + 3.3.0 enforce-maven @@ -64,46 +65,51 @@ maven-resources-plugin - 3.2.0 + 3.3.1 maven-compiler-plugin - 3.10.1 + 3.11.0 maven-surefire-plugin - 3.0.0-M5 + 3.0.0 maven-jar-plugin - 3.2.2 + 3.3.0 maven-install-plugin - 3.0.0-M1 + 3.1.1 maven-deploy-plugin - 3.0.0-M1 + 3.1.1 maven-site-plugin - 3.12.0 + 3.12.1 maven-project-info-reports-plugin - 3.3.0 + 3.4.2 org.codehaus.mojo versions-maven-plugin - 2.11.0 + 2.15.0 file://${session.executionRootDirectory}/version-rules.xml + + org.apache.maven.plugins + maven-dependency-plugin + 3.5.0 + org.sonarsource.scanner.maven @@ -113,7 +119,7 @@ org.jacoco jacoco-maven-plugin - 0.8.8 + 0.8.9 jacoco-initialize @@ -134,7 +140,7 @@ org.owasp dependency-check-maven - 7.1.1 + 8.2.1 none diff --git a/src/main/java/com/mattrixwv/NumberAlgorithms.java b/src/main/java/com/mattrixwv/NumberAlgorithms.java index 0db0f1b..50daacb 100644 --- a/src/main/java/com/mattrixwv/NumberAlgorithms.java +++ b/src/main/java/com/mattrixwv/NumberAlgorithms.java @@ -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 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 primes = getPrimes(topPossiblePrime); ArrayList 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 diff --git a/src/main/java/com/mattrixwv/Stopwatch.java b/src/main/java/com/mattrixwv/Stopwatch.java index d6968a6..1537737 100644 --- a/src/main/java/com/mattrixwv/Stopwatch.java +++ b/src/main/java/com/mattrixwv/Stopwatch.java @@ -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(); } } diff --git a/src/main/java/com/mattrixwv/StringAlgorithms.java b/src/main/java/com/mattrixwv/StringAlgorithms.java index 089a9a5..89cff8c 100644 --- a/src/main/java/com/mattrixwv/StringAlgorithms.java +++ b/src/main/java/com/mattrixwv/StringAlgorithms.java @@ -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 getPermutations(String master){ return getPermutations(master, 0); } - private static ArrayList getPermutations(String master, int num){ + protected 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)){ diff --git a/src/main/java/com/mattrixwv/exceptions/InvalidResult.java b/src/main/java/com/mattrixwv/exceptions/InvalidResult.java index 3133a71..a7ee316 100644 --- a/src/main/java/com/mattrixwv/exceptions/InvalidResult.java +++ b/src/main/java/com/mattrixwv/exceptions/InvalidResult.java @@ -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); + } } diff --git a/src/main/java/com/mattrixwv/generators/HexagonalNumberGenerator.java b/src/main/java/com/mattrixwv/generators/HexagonalNumberGenerator.java index 9429a2f..56bcb22 100644 --- a/src/main/java/com/mattrixwv/generators/HexagonalNumberGenerator.java +++ b/src/main/java/com/mattrixwv/generators/HexagonalNumberGenerator.java @@ -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{ - private Long num; + protected long num; public HexagonalNumberGenerator(){ num = 1L; @@ -35,23 +35,25 @@ public class HexagonalNumberGenerator implements Iterator{ @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; } } diff --git a/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java b/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java index 5394ad0..0c88f95 100644 --- a/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java +++ b/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java @@ -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{ - private Long num; + protected long num; public PentagonalNumberGenerator(){ num = 1L; @@ -35,23 +35,22 @@ public class PentagonalNumberGenerator implements Iterator{ @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; } } diff --git a/src/main/java/com/mattrixwv/generators/SieveOfEratosthenes.java b/src/main/java/com/mattrixwv/generators/SieveOfEratosthenes.java index 724b151..ccbdb05 100644 --- a/src/main/java/com/mattrixwv/generators/SieveOfEratosthenes.java +++ b/src/main/java/com/mattrixwv/generators/SieveOfEratosthenes.java @@ -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 possiblePrime; - private Map> dict; + protected long possiblePrime; + protected Map> 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 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{ //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 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; diff --git a/src/main/java/com/mattrixwv/generators/SieveOfEratosthenesBig.java b/src/main/java/com/mattrixwv/generators/SieveOfEratosthenesBig.java index 3828a69..0aa9760 100644 --- a/src/main/java/com/mattrixwv/generators/SieveOfEratosthenesBig.java +++ b/src/main/java/com/mattrixwv/generators/SieveOfEratosthenesBig.java @@ -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 possiblePrime; - private Map> dict; + protected BigInteger possiblePrime; + protected Map> 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{ } //Save that the number is a prime prime = possiblePrime; - BigInteger loc = prime.multiply(BigInteger.valueOf(3)); - if(!dict.containsKey(loc)){ - ArrayList 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); diff --git a/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java b/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java index d871d69..e567e3a 100644 --- a/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java +++ b/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java @@ -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{ - private Long num; + protected long num; + public TriangularNumberGenerator(){ num = 1L; @@ -35,23 +36,22 @@ public class TriangularNumberGenerator implements Iterator{ @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; } } diff --git a/src/test/java/com/mattrixwv/TestNumberAlgorithms.java b/src/test/java/com/mattrixwv/TestNumberAlgorithms.java index 2a862ed..d002051 100644 --- a/src/test/java/com/mattrixwv/TestNumberAlgorithms.java +++ b/src/test/java/com/mattrixwv/TestNumberAlgorithms.java @@ -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 correctAnswer = Arrays.asList(2, 2, 5, 5); int number = 100; List 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 longCorrectAnswer = Arrays.asList(2L, 2L, 5L, 5L); long longNumber = 100L; List 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 bigCorrectAnswer = Arrays.asList(BigInteger.TWO, BigInteger.TWO, BigInteger.valueOf(5), BigInteger.valueOf(5)); BigInteger bigNumber = BigInteger.valueOf(100); List 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 diff --git a/src/test/java/com/mattrixwv/TestStopwatch.java b/src/test/java/com/mattrixwv/TestStopwatch.java index b27de1c..33f3cbe 100644 --- a/src/test/java/com/mattrixwv/TestStopwatch.java +++ b/src/test/java/com/mattrixwv/TestStopwatch.java @@ -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 diff --git a/src/test/java/com/mattrixwv/TestStringAlgorithms.java b/src/test/java/com/mattrixwv/TestStringAlgorithms.java index 43fc25c..a274c62 100644 --- a/src/test/java/com/mattrixwv/TestStringAlgorithms.java +++ b/src/test/java/com/mattrixwv/TestStringAlgorithms.java @@ -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 correctAnswer = Arrays.asList("012", "021", "102", "120", "201", "210"); List 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(){ diff --git a/src/test/java/com/mattrixwv/TestTriple.java b/src/test/java/com/mattrixwv/TestTriple.java index 7f96ae7..74be5e3 100644 --- a/src/test/java/com/mattrixwv/TestTriple.java +++ b/src/test/java/com/mattrixwv/TestTriple.java @@ -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 triple; + + + @BeforeEach + public void setup(){ + triple = new Triple<>(1L, 2L, 3L); + } + + @Test - public void testTriple(){ - Triple longTriple1 = new Triple(1L, 2L, 3L); - Triple longTriple2 = new Triple(1L, 2L, 3L); - Triple longTriple3 = new Triple(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 triple2 = new Triple<>(1L, 2L, 3L); + Triple triple3 = new Triple<>(3L, 2L, 3L); + Triple triple4 = new Triple<>(1L, 3L, 3L); + Triple triple5 = new Triple<>(1L, 2L, 1L); + Triple triple6 = new Triple<>(2L, 1L, 3L); + Triple triple7 = new Triple<>(3L, 2L, 1L); + Triple triple8 = new Triple<>(1L, 3L, 2L); + Triple 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()); } } diff --git a/src/test/java/com/mattrixwv/exceptions/TestInvalidResult.java b/src/test/java/com/mattrixwv/exceptions/TestInvalidResult.java new file mode 100644 index 0000000..843ee59 --- /dev/null +++ b/src/test/java/com/mattrixwv/exceptions/TestInvalidResult.java @@ -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()); + } +} diff --git a/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java b/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java index 00d743c..bfa65c0 100644 --- a/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java +++ b/src/test/java/com/mattrixwv/generators/TestHexagonalNumberGenerator.java @@ -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 nums = Arrays.asList(1L, 6L, 15L, 28L, 45L, 66L, 91L, 120L, 153L); ArrayList 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)); } } diff --git a/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java b/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java index 619cab4..3317918 100644 --- a/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java +++ b/src/test/java/com/mattrixwv/generators/TestPentagonalNumberGenerator.java @@ -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 nums = Arrays.asList(1L, 5L, 12L, 22L, 35L, 51L, 70L, 92L, 117L); ArrayList 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)); } } diff --git a/src/test/java/com/mattrixwv/generators/TestSieveOfEratosthenes.java b/src/test/java/com/mattrixwv/generators/TestSieveOfEratosthenes.java index 4142eda..f529f12 100644 --- a/src/test/java/com/mattrixwv/generators/TestSieveOfEratosthenes.java +++ b/src/test/java/com/mattrixwv/generators/TestSieveOfEratosthenes.java @@ -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 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 answer = new ArrayList(); + ArrayList 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 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 answer = new ArrayList(); - 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(); + }); } } diff --git a/src/test/java/com/mattrixwv/generators/TestSieveOfEratostheneseBig.java b/src/test/java/com/mattrixwv/generators/TestSieveOfEratostheneseBig.java new file mode 100644 index 0000000..6f71750 --- /dev/null +++ b/src/test/java/com/mattrixwv/generators/TestSieveOfEratostheneseBig.java @@ -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 . +*/ +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 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 answer = new ArrayList<>(); + for(int cnt = 0;cnt < 25;++cnt){ + BigInteger prime = sieve.next(); + answer.add(prime); + } + assertEquals(correctAnswer, answer); + } +} diff --git a/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java b/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java index df76db9..f1c2b8e 100644 --- a/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java +++ b/src/test/java/com/mattrixwv/generators/TestTriangularNumberGenerator.java @@ -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 nums = Arrays.asList(1L, 3L, 6L, 10L, 15L, 21L, 28L, 36L, 45L); ArrayList 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)); } }