cleanup and testing work
This commit is contained in:
parent
ffd17c1b21
commit
791142c67e
|
@ -8,29 +8,28 @@ import com.baeldung.algorithms.slope_one.SlopeOne;
|
|||
|
||||
public class RunAlgorithm {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.println("Run algorithm:");
|
||||
System.out.println("1 - Simulated Annealing");
|
||||
System.out.println("2 - Slope One");
|
||||
System.out.println("3 - Simple Genetic Algorithm");
|
||||
int decision = in.nextInt();
|
||||
switch (decision) {
|
||||
case 1:
|
||||
System.out.println(
|
||||
"Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
||||
break;
|
||||
case 2:
|
||||
SlopeOne.slopeOne(3);
|
||||
break;
|
||||
case 3:
|
||||
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown option");
|
||||
break;
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
System.out.println("Run algorithm:");
|
||||
System.out.println("1 - Simulated Annealing");
|
||||
System.out.println("2 - Slope One");
|
||||
System.out.println("3 - Simple Genetic Algorithm");
|
||||
int decision = in.nextInt();
|
||||
switch (decision) {
|
||||
case 1:
|
||||
System.out.println("Optimized distance for travel: " + SimulatedAnnealing.simulateAnnealing(10, 10000, 0.9995));
|
||||
break;
|
||||
case 2:
|
||||
SlopeOne.slopeOne(3);
|
||||
break;
|
||||
case 3:
|
||||
SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown option");
|
||||
break;
|
||||
}
|
||||
in.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,40 +5,40 @@ import lombok.Data;
|
|||
@Data
|
||||
public class Individual {
|
||||
|
||||
protected int defaultGeneLength = 64;
|
||||
private byte[] genes = new byte[defaultGeneLength];
|
||||
private int fitness = 0;
|
||||
protected int defaultGeneLength = 64;
|
||||
private byte[] genes = new byte[defaultGeneLength];
|
||||
private int fitness = 0;
|
||||
|
||||
public Individual() {
|
||||
for (int i = 0; i < genes.length; i++) {
|
||||
byte gene = (byte) Math.round(Math.random());
|
||||
genes[i] = gene;
|
||||
}
|
||||
}
|
||||
public Individual() {
|
||||
for (int i = 0; i < genes.length; i++) {
|
||||
byte gene = (byte) Math.round(Math.random());
|
||||
genes[i] = gene;
|
||||
}
|
||||
}
|
||||
|
||||
protected byte getSingleGene(int index) {
|
||||
return genes[index];
|
||||
}
|
||||
protected byte getSingleGene(int index) {
|
||||
return genes[index];
|
||||
}
|
||||
|
||||
protected void setSingleGene(int index, byte value) {
|
||||
genes[index] = value;
|
||||
fitness = 0;
|
||||
}
|
||||
protected void setSingleGene(int index, byte value) {
|
||||
genes[index] = value;
|
||||
fitness = 0;
|
||||
}
|
||||
|
||||
public int getFitness() {
|
||||
if (fitness == 0) {
|
||||
fitness = SimpleGeneticAlgorithm.getFitness(this);
|
||||
}
|
||||
return fitness;
|
||||
}
|
||||
public int getFitness() {
|
||||
if (fitness == 0) {
|
||||
fitness = SimpleGeneticAlgorithm.getFitness(this);
|
||||
}
|
||||
return fitness;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String geneString = "";
|
||||
for (int i = 0; i < genes.length; i++) {
|
||||
geneString += getSingleGene(i);
|
||||
}
|
||||
return geneString;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
String geneString = "";
|
||||
for (int i = 0; i < genes.length; i++) {
|
||||
geneString += getSingleGene(i);
|
||||
}
|
||||
return geneString;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,33 +8,33 @@ import lombok.Data;
|
|||
@Data
|
||||
public class Population {
|
||||
|
||||
private List<Individual> individuals;
|
||||
private List<Individual> individuals;
|
||||
|
||||
public Population(int size, boolean createNew) {
|
||||
individuals = new ArrayList<>();
|
||||
if (createNew) {
|
||||
createNewPopulation(size);
|
||||
}
|
||||
}
|
||||
public Population(int size, boolean createNew) {
|
||||
individuals = new ArrayList<>();
|
||||
if (createNew) {
|
||||
createNewPopulation(size);
|
||||
}
|
||||
}
|
||||
|
||||
protected Individual getIndividual(int index) {
|
||||
return individuals.get(index);
|
||||
}
|
||||
protected Individual getIndividual(int index) {
|
||||
return individuals.get(index);
|
||||
}
|
||||
|
||||
protected Individual getFittest() {
|
||||
Individual fittest = individuals.get(0);
|
||||
for (int i = 0; i < individuals.size(); i++) {
|
||||
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
|
||||
fittest = getIndividual(i);
|
||||
}
|
||||
}
|
||||
return fittest;
|
||||
}
|
||||
protected Individual getFittest() {
|
||||
Individual fittest = individuals.get(0);
|
||||
for (int i = 0; i < individuals.size(); i++) {
|
||||
if (fittest.getFitness() <= getIndividual(i).getFitness()) {
|
||||
fittest = getIndividual(i);
|
||||
}
|
||||
}
|
||||
return fittest;
|
||||
}
|
||||
|
||||
private void createNewPopulation(int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
Individual newIndividual = new Individual();
|
||||
individuals.add(i, newIndividual);
|
||||
}
|
||||
}
|
||||
private void createNewPopulation(int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
Individual newIndividual = new Individual();
|
||||
individuals.add(i, newIndividual);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,115 +5,113 @@ import lombok.Data;
|
|||
@Data
|
||||
public class SimpleGeneticAlgorithm {
|
||||
|
||||
private static final double uniformRate = 0.5;
|
||||
private static final double mutationRate = 0.025;
|
||||
private static final int tournamentSize = 5;
|
||||
private static final boolean elitism = true;
|
||||
private static byte[] solution = new byte[64];
|
||||
private static final double uniformRate = 0.5;
|
||||
private static final double mutationRate = 0.025;
|
||||
private static final int tournamentSize = 5;
|
||||
private static final boolean elitism = true;
|
||||
private static byte[] solution = new byte[64];
|
||||
|
||||
public static boolean runAlgorithm(int populationSize, String solution) {
|
||||
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
||||
throw new RuntimeException(
|
||||
"The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
||||
}
|
||||
SimpleGeneticAlgorithm.setSolution(solution);
|
||||
Population myPop = new Population(populationSize, true);
|
||||
public static boolean runAlgorithm(int populationSize, String solution) {
|
||||
if (solution.length() != SimpleGeneticAlgorithm.solution.length) {
|
||||
throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes");
|
||||
}
|
||||
SimpleGeneticAlgorithm.setSolution(solution);
|
||||
Population myPop = new Population(populationSize, true);
|
||||
|
||||
int generationCount = 1;
|
||||
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
|
||||
System.out.println(
|
||||
"Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
||||
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
|
||||
generationCount++;
|
||||
}
|
||||
System.out.println("Solution found!");
|
||||
System.out.println("Generation: " + generationCount);
|
||||
System.out.println("Genes: ");
|
||||
System.out.println(myPop.getFittest());
|
||||
return true;
|
||||
}
|
||||
int generationCount = 1;
|
||||
while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) {
|
||||
System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness());
|
||||
myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop);
|
||||
generationCount++;
|
||||
}
|
||||
System.out.println("Solution found!");
|
||||
System.out.println("Generation: " + generationCount);
|
||||
System.out.println("Genes: ");
|
||||
System.out.println(myPop.getFittest());
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Population evolvePopulation(Population pop) {
|
||||
int elitismOffset;
|
||||
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
||||
public static Population evolvePopulation(Population pop) {
|
||||
int elitismOffset;
|
||||
Population newPopulation = new Population(pop.getIndividuals().size(), false);
|
||||
|
||||
if (elitism) {
|
||||
newPopulation.getIndividuals().add(0, pop.getFittest());
|
||||
elitismOffset = 1;
|
||||
} else {
|
||||
elitismOffset = 0;
|
||||
}
|
||||
if (elitism) {
|
||||
newPopulation.getIndividuals().add(0, pop.getFittest());
|
||||
elitismOffset = 1;
|
||||
} else {
|
||||
elitismOffset = 0;
|
||||
}
|
||||
|
||||
for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
|
||||
Individual indiv1 = tournamentSelection(pop);
|
||||
Individual indiv2 = tournamentSelection(pop);
|
||||
Individual newIndiv = crossover(indiv1, indiv2);
|
||||
newPopulation.getIndividuals().add(i, newIndiv);
|
||||
}
|
||||
for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) {
|
||||
Individual indiv1 = tournamentSelection(pop);
|
||||
Individual indiv2 = tournamentSelection(pop);
|
||||
Individual newIndiv = crossover(indiv1, indiv2);
|
||||
newPopulation.getIndividuals().add(i, newIndiv);
|
||||
}
|
||||
|
||||
for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
|
||||
mutate(newPopulation.getIndividual(i));
|
||||
}
|
||||
for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) {
|
||||
mutate(newPopulation.getIndividual(i));
|
||||
}
|
||||
|
||||
return newPopulation;
|
||||
}
|
||||
return newPopulation;
|
||||
}
|
||||
|
||||
private static Individual crossover(Individual indiv1, Individual indiv2) {
|
||||
Individual newSol = new Individual();
|
||||
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
||||
if (Math.random() <= uniformRate) {
|
||||
newSol.setSingleGene(i, indiv1.getSingleGene(i));
|
||||
} else {
|
||||
newSol.setSingleGene(i, indiv2.getSingleGene(i));
|
||||
}
|
||||
}
|
||||
return newSol;
|
||||
}
|
||||
private static Individual crossover(Individual indiv1, Individual indiv2) {
|
||||
Individual newSol = new Individual();
|
||||
for (int i = 0; i < newSol.getDefaultGeneLength(); i++) {
|
||||
if (Math.random() <= uniformRate) {
|
||||
newSol.setSingleGene(i, indiv1.getSingleGene(i));
|
||||
} else {
|
||||
newSol.setSingleGene(i, indiv2.getSingleGene(i));
|
||||
}
|
||||
}
|
||||
return newSol;
|
||||
}
|
||||
|
||||
private static void mutate(Individual indiv) {
|
||||
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
||||
if (Math.random() <= mutationRate) {
|
||||
byte gene = (byte) Math.round(Math.random());
|
||||
indiv.setSingleGene(i, gene);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static void mutate(Individual indiv) {
|
||||
for (int i = 0; i < indiv.getDefaultGeneLength(); i++) {
|
||||
if (Math.random() <= mutationRate) {
|
||||
byte gene = (byte) Math.round(Math.random());
|
||||
indiv.setSingleGene(i, gene);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Individual tournamentSelection(Population pop) {
|
||||
Population tournament = new Population(tournamentSize, false);
|
||||
for (int i = 0; i < tournamentSize; i++) {
|
||||
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
||||
tournament.getIndividuals().add(i, pop.getIndividual(randomId));
|
||||
}
|
||||
Individual fittest = tournament.getFittest();
|
||||
return fittest;
|
||||
}
|
||||
private static Individual tournamentSelection(Population pop) {
|
||||
Population tournament = new Population(tournamentSize, false);
|
||||
for (int i = 0; i < tournamentSize; i++) {
|
||||
int randomId = (int) (Math.random() * pop.getIndividuals().size());
|
||||
tournament.getIndividuals().add(i, pop.getIndividual(randomId));
|
||||
}
|
||||
Individual fittest = tournament.getFittest();
|
||||
return fittest;
|
||||
}
|
||||
|
||||
protected static int getFitness(Individual individual) {
|
||||
int fitness = 0;
|
||||
for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
|
||||
if (individual.getSingleGene(i) == solution[i]) {
|
||||
fitness++;
|
||||
}
|
||||
}
|
||||
return fitness;
|
||||
}
|
||||
protected static int getFitness(Individual individual) {
|
||||
int fitness = 0;
|
||||
for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) {
|
||||
if (individual.getSingleGene(i) == solution[i]) {
|
||||
fitness++;
|
||||
}
|
||||
}
|
||||
return fitness;
|
||||
}
|
||||
|
||||
protected static int getMaxFitness() {
|
||||
int maxFitness = solution.length;
|
||||
return maxFitness;
|
||||
}
|
||||
protected static int getMaxFitness() {
|
||||
int maxFitness = solution.length;
|
||||
return maxFitness;
|
||||
}
|
||||
|
||||
protected static void setSolution(String newSolution) {
|
||||
solution = new byte[newSolution.length()];
|
||||
for (int i = 0; i < newSolution.length(); i++) {
|
||||
String character = newSolution.substring(i, i + 1);
|
||||
if (character.contains("0") || character.contains("1")) {
|
||||
solution[i] = Byte.parseByte(character);
|
||||
} else {
|
||||
solution[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
protected static void setSolution(String newSolution) {
|
||||
solution = new byte[newSolution.length()];
|
||||
for (int i = 0; i < newSolution.length(); i++) {
|
||||
String character = newSolution.substring(i, i + 1);
|
||||
if (character.contains("0") || character.contains("1")) {
|
||||
solution[i] = Byte.parseByte(character);
|
||||
} else {
|
||||
solution[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,6 @@ public class SquareCalculator {
|
|||
return executor.submit(() -> {
|
||||
Thread.sleep(1000);
|
||||
return input * input;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,55 +8,55 @@ public class CharArrayToStringUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = new String(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString() {
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = new String(charArray, 4, 3);
|
||||
String expectedValue = "act";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString() {
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = String.copyValueOf(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString() {
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = String.copyValueOf(charArray, 0, 4);
|
||||
String expectedValue = "char";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString() {
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = String.valueOf(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString() {
|
||||
char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' };
|
||||
String result = String.valueOf(charArray, 3, 4);
|
||||
String expectedValue = "ract";
|
||||
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,15 +6,15 @@ import org.junit.Test;
|
|||
|
||||
public class StringToCharArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
|
||||
String givenString = "characters";
|
||||
@Test
|
||||
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
|
||||
String givenString = "characters";
|
||||
|
||||
char[] result = givenString.toCharArray();
|
||||
char[] result = givenString.toCharArray();
|
||||
|
||||
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
|
||||
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
|
||||
|
||||
assertArrayEquals(expectedCharArray, result);
|
||||
}
|
||||
assertArrayEquals(expectedCharArray, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||
|
||||
public class BinaryGeneticAlgorithmTest {
|
||||
|
||||
@Test
|
||||
public void testGA() {
|
||||
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50,
|
||||
"1011000100000100010000100000100111001000000100000100000000001111"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||
|
||||
public class BinaryGeneticAlgorithmUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGA() {
|
||||
Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"));
|
||||
}
|
||||
|
||||
}
|
|
@ -32,13 +32,14 @@ public class PriorityBlockingQueueUnitTest {
|
|||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||
|
||||
final Thread thread = new Thread(() -> {
|
||||
System.out.println("Polling...");
|
||||
while (true) {
|
||||
try {
|
||||
Integer poll = queue.take();
|
||||
System.out.println("Polled: " + poll);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
System.out.println("Polling...");
|
||||
while (true) {
|
||||
try {
|
||||
Integer poll = queue.take();
|
||||
System.out.println("Polled: " + poll);
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
|
||||
|
|
|
@ -18,9 +18,7 @@ public class ConcurrentNavigableMapTests {
|
|||
|
||||
updateMapConcurrently(skipListMap, 4);
|
||||
|
||||
Iterator<Integer> skipListIter = skipListMap
|
||||
.keySet()
|
||||
.iterator();
|
||||
Iterator<Integer> skipListIter = skipListMap.keySet().iterator();
|
||||
int previous = skipListIter.next();
|
||||
while (skipListIter.hasNext()) {
|
||||
int current = skipListIter.next();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
package org.baeldung.java.streams;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -14,28 +14,25 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadPoolInParallelStreamTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal()
|
||||
throws InterruptedException, ExecutionException {
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException {
|
||||
long firstNum = 1;
|
||||
long lastNum = 1_000_000;
|
||||
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed()
|
||||
.collect(Collectors.toList());
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream()
|
||||
.reduce(0L, Long::sum)).get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream(){
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream() {
|
||||
List<Long> aList = new ArrayList<>();
|
||||
Stream<Long> parallelStream = aList.parallelStream();
|
||||
|
||||
|
||||
assertTrue(parallelStream.isParallel());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>logmdc</artifactId>
|
||||
<artifactId>log-mdc</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>logmdc</name>
|
||||
<name>log-mdc</name>
|
||||
<packaging>war</packaging>
|
||||
<description>tutorial on logging with MDC and NDC</description>
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@Ignore("manual only")
|
||||
public class ExternalPropertiesWithXmlIntegrationTest {
|
||||
public class ExternalPropertiesWithXmlManualTest {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
|
@ -12,7 +12,7 @@ import java.sql.SQLException;
|
|||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringRetryTest {
|
||||
public class SpringRetryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MyService myService;
|
|
@ -8,7 +8,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
|||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ThreadPoolTaskSchedulerTest {
|
||||
public class ThreadPoolTaskSchedulerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException {
|
||||
Thread.sleep(2550);
|
|
@ -5,7 +5,7 @@ import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest;
|
|||
import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
|
||||
import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
|
||||
import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
|
||||
import org.baeldung.properties.external.ExternalPropertiesWithXmlIntegrationTest;
|
||||
import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
@ -15,7 +15,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
|||
PropertiesWithXmlIntegrationTest.class,
|
||||
ExternalPropertiesWithJavaIntegrationTest.class,
|
||||
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
|
||||
ExternalPropertiesWithXmlIntegrationTest.class,
|
||||
ExternalPropertiesWithXmlManualTest.class,
|
||||
ExtendedPropertiesWithJavaIntegrationTest.class,
|
||||
PropertiesWithMultipleXmlsIntegrationTest.class,
|
||||
})// @formatter:on
|
||||
|
|
|
@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
|
||||
public class BeanNameMappingConfigTest {
|
||||
public class BeanNameMappingConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
|
@ -22,7 +22,7 @@ import com.baeldung.config.ControllerClassNameHandlerMappingConfig;
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
|
||||
public class ControllerClassNameHandlerMappingTest {
|
||||
public class ControllerClassNameHandlerMappingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
|
@ -1,7 +1,10 @@
|
|||
package com.baeldung.handlermappings;
|
||||
|
||||
import com.baeldung.config.HandlerMappingDefaultConfig;
|
||||
import com.baeldung.config.HandlerMappingPrioritiesConfig;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
@ -14,15 +17,12 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
import com.baeldung.config.HandlerMappingDefaultConfig;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = HandlerMappingDefaultConfig.class)
|
||||
public class HandlerMappingDefaultConfigTest {
|
||||
public class HandlerMappingDefaultConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
|
@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
|
||||
public class HandlerMappingPriorityConfigTest {
|
||||
public class HandlerMappingPriorityConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
|
@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
|
||||
public class SimpleUrlMappingConfigTest {
|
||||
public class SimpleUrlMappingConfigIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
Loading…
Reference in New Issue