Skip to content

Instantly share code, notes, and snippets.

@Fabinout
Created February 8, 2021 10:54
Show Gist options
  • Save Fabinout/abc8725fc810c2abdc0f1445d9204284 to your computer and use it in GitHub Desktop.
Save Fabinout/abc8725fc810c2abdc0f1445d9204284 to your computer and use it in GitHub Desktop.
package code.kata.gildedrose;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
class kata1Test {
public static List<Integer> processSum(List<Integer> list) {
if (list.size() == 0) return Collections.emptyList();
ArrayList<Integer> result = new ArrayList<>();
result.add(list.get(0));
if (list.size() == 1) return result;
for (int i = 1, listSize = list.size(); i < listSize; i++) {
Integer integer = list.get(i);
List<Integer> tail = list.subList(i + 1, Math.min(i + (i + 1), list.size()));
int tailSum = tail.stream().mapToInt(Integer::intValue).sum();
result.add(integer + tailSum);
}
return result;
}
void processSumAssert(List<Integer> inputList, List<Integer> expectedList) {
List<Integer> result = processSum(inputList);
assertThat(result).hasSize(inputList.size());
assertThat(result).isEqualTo(expectedList);
}
@Test
void processSumTests_8_elements() {
processSumAssert(List.of(2, 5, 1, -9, 24, 3, 6, -8),
List.of(2, 6, 16, 24, 25, 1, -2, -8));
}
@Test
void processSumTests_1_element() {
processSumAssert(List.of(2), List.of(2));
processSumAssert(List.of(3), List.of(3));
}
@Test
void processSumTests_1_other_element() {
processSumAssert(Collections.emptyList(), Collections.emptyList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment