File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
core-java/src/test/java/com/baeldung/modulo Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .baeldung .modulo ;
2+
3+ import org .junit .Test ;
4+ import static org .assertj .core .api .Java6Assertions .*;
5+
6+ public class ModuloUnitTest {
7+
8+ @ Test
9+ public void whenIntegerDivision_thenLosesRemainder (){
10+ assertThat (11 / 4 ).isEqualTo (2 );
11+ }
12+
13+ @ Test
14+ public void whenDoubleDivision_thenKeepsRemainder (){
15+ assertThat (11 / 4.0 ).isEqualTo (2.75 );
16+ }
17+
18+ @ Test
19+ public void whenModulo_thenReturnsRemainder (){
20+ assertThat (11 % 4 ).isEqualTo (3 );
21+ }
22+
23+ @ Test (expected = ArithmeticException .class )
24+ public void whenDivisionByZero_thenArithmeticException (){
25+ double result = 1 / 0 ;
26+ }
27+
28+ @ Test (expected = ArithmeticException .class )
29+ public void whenModuloByZero_thenArithmeticException (){
30+ double result = 1 % 0 ;
31+ }
32+
33+ @ Test
34+ public void whenDivisorIsOddAndModulusIs2_thenResultIs1 (){
35+ assertThat (3 % 2 ).isEqualTo (1 );
36+ }
37+
38+ @ Test
39+ public void whenDivisorIsEvenAndModulusIs2_thenResultIs0 (){
40+ assertThat (4 % 2 ).isEqualTo (0 );
41+ }
42+
43+ @ Test
44+ public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds (){
45+ int QUEUE_CAPACITY = 10 ;
46+ int [] circularQueue = new int [QUEUE_CAPACITY ];
47+ int itemsInserted = 0 ;
48+ for (int value = 0 ; value < 1000 ; value ++) {
49+ int writeIndex = ++itemsInserted % QUEUE_CAPACITY ;
50+ circularQueue [writeIndex ] = value ;
51+ }
52+ }
53+
54+ }
You can’t perform that action at this time.
0 commit comments