mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 23:43:58 -05:00
Updated libraries and increased test coverage
This commit is contained in:
@@ -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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user