-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from btirumala1219/main
For loop TDD
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.ordestiny.tdd.loop; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ForLoop { | ||
public List<Integer> standardForLoop(int times) { | ||
List<Integer> list = new ArrayList<>(times); | ||
|
||
for(int i = 0; i<times; i++){ | ||
list.add(i); | ||
} | ||
return list; | ||
} | ||
|
||
public List<Integer> forEachLoop(int times) { | ||
List<Integer> input = standardForLoop(times); | ||
List<Integer> list = new ArrayList<>(times); | ||
|
||
input.forEach(i -> list.add(i)); | ||
return list; | ||
} | ||
|
||
public List<Integer> forXinYLoop(int times) { | ||
List<Integer> input = standardForLoop(times); | ||
List<Integer> list = new ArrayList<>(times); | ||
|
||
for(int i : input) { | ||
list.add(i); | ||
} | ||
|
||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.ordestiny.tdd.loop; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ForLoopTest { | ||
List<Integer> sample = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4)); | ||
ForLoop loop = new ForLoop(); | ||
|
||
@Test | ||
void standardForLoopTest() { | ||
List<Integer> list = loop.standardForLoop(5); | ||
assertTrue(areListsSame(list, sample)); | ||
} | ||
|
||
@Test | ||
void forEachLoopTest() { | ||
List<Integer> list = loop.forEachLoop(5); | ||
assertTrue(areListsSame(list, sample)); | ||
} | ||
|
||
@Test | ||
void forXinYLoopTest() { | ||
List<Integer> list = loop.forXinYLoop(5); | ||
assertTrue(areListsSame(list, sample)); | ||
} | ||
|
||
private boolean areListsSame(List<Integer> list1, List<Integer> list2){ | ||
return list1.containsAll(list2); | ||
} | ||
} |