Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For loop TDD #19

Merged
merged 1 commit into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/com/ordestiny/tdd/loop/ForLoop.java
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;
}
}
36 changes: 36 additions & 0 deletions src/test/java/com/ordestiny/tdd/loop/ForLoopTest.java
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);
}
}