Merge pull request #5467 from eugenp/bael-2123-modulo

Bael 2123 modulo
This commit is contained in:
Tom Hombergs 2018-10-15 23:15:52 +02:00 committed by GitHub
commit a8f3ef312b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 93 deletions

View File

@ -1 +0,0 @@
/bin/

View File

@ -1,4 +1,4 @@
package findItems;
package com.baeldung.findItems;
import static org.junit.Assert.assertEquals;
@ -10,24 +10,20 @@ import java.util.stream.Collectors;
import org.junit.Test;
public class FindItemsBasedOnValues {
public class FindItemsBasedOnOtherStreamUnitTest {
List<Employee> EmplList = new ArrayList<Employee>();
List<Department> deptList = new ArrayList<Department>();
private List<Employee> employeeList = new ArrayList<Employee>();
public static void main(String[] args) throws ParseException {
FindItemsBasedOnValues findItems = new FindItemsBasedOnValues();
findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly();
}
private List<Department> departmentList = new ArrayList<Department>();
@Test
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
Integer expectedId = 1002;
populate(EmplList, deptList);
populate(employeeList, departmentList);
List<Employee> filteredList = EmplList.stream()
.filter(empl -> deptList.stream()
List<Employee> filteredList = employeeList.stream()
.filter(empl -> departmentList.stream()
.anyMatch(dept -> dept.getDepartment()
.equals("sales") && empl.getEmployeeId()
.equals(dept.getEmployeeId())))
@ -53,16 +49,16 @@ public class FindItemsBasedOnValues {
}
class Employee {
Integer employeeId;
String employeeName;
private Integer employeeId;
private String employeeName;
public Employee(Integer employeeId, String employeeName) {
Employee(Integer employeeId, String employeeName) {
super();
this.employeeId = employeeId;
this.employeeName = employeeName;
}
public Integer getEmployeeId() {
Integer getEmployeeId() {
return employeeId;
}
@ -73,20 +69,20 @@ class Employee {
}
class Department {
Integer employeeId;
String department;
private Integer employeeId;
private String department;
public Department(Integer employeeId, String department) {
Department(Integer employeeId, String department) {
super();
this.employeeId = employeeId;
this.department = department;
}
public Integer getEmployeeId() {
Integer getEmployeeId() {
return employeeId;
}
public String getDepartment() {
String getDepartment() {
return department;
}

View File

@ -0,0 +1,54 @@
package com.baeldung.modulo;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;
public class ModuloUnitTest {
@Test
public void whenIntegerDivision_thenLosesRemainder(){
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder(){
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder(){
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException(){
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException(){
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
int QUEUE_CAPACITY= 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
}
}
}