BAEL-1586 Sum and Average in Java Array (#3718)

* Evaluation Article

* CR comments resolved

* BAEL-1586 Find Sum and Average in Java Array

* Revert "CR comments resolved"

This reverts commit c9bf88fcd5.

* Revert "Evaluation Article"

This reverts commit 41ad30313a.

* BAEL-1586 Removed Author Description

* BAEL-1586 Find Sum And Avergae In Array

* remove excess whitespace
This commit is contained in:
raghav-jha
2018-03-07 21:29:50 +05:30
committed by pauljervis
parent 52d257a545
commit 4526bb6add
6 changed files with 161 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class Find2ndLargestInArrayTest {
@Test
public void givenAnIntArray_thenFind2ndLargestElement() {
int[] array = { 1, 3, 24, 16, 87, 20 };
int expected2ndLargest = 24;
int actualSecondLargestElement = Find2ndLargestInArray.find2ndLargestElement(array);
Assert.assertEquals(expected2ndLargest, actualSecondLargestElement);
}
}

View File

@@ -0,0 +1,35 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class FindElementInArrayTest {
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindAnElement() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int element = 19;
boolean expectedResult = true;
boolean actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
element = 78;
expectedResult = false;
actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindAnElement() {
int[] array = { 15, 16, 12, 18 };
int element = 16;
boolean expectedResult = true;
boolean actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
element = 20;
expectedResult = false;
actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
Assert.assertEquals(expectedResult, actualResult);
}
}

View File

@@ -0,0 +1,41 @@
package com.baeldung.array;
import org.junit.Assert;
import org.junit.Test;
public class SumAndAverageInArrayTest {
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindSum() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int expectedSumOfArray = 55;
int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array);
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindSum() {
int[] array = { 1, 3, 4, 8, 19, 20 };
int expectedSumOfArray = 55;
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
}
@Test
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
int[] array = { 1, 3, 4, 8, 19, 20 };
double expectedAvgOfArray = 9.17;
double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array);
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
}
@Test
public void givenAnIntArray_whenUsingStream_thenFindAverage() {
int[] array = { 1, 3, 4, 8, 19, 20 };
double expectedAvgOfArray = 9.17;
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
}
}