Created
May 15, 2015 09:31
-
-
Save yusufaytas/eb008fd1515f149336ef to your computer and use it in GitHub Desktop.
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
package com.yusufaytas.test.intercom; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
public class ArbitrarilyNestedArraysTest { | |
@Test | |
public void testflattenWithZeroElement() { | |
List<Object> arrays = createArbitraryList(new int[]{}, 0, 0); | |
List<Integer> actualArray = new ArbitrarilyNestedArrays().flatten(arrays); | |
assertEquals(0, actualArray.size()); | |
} | |
@Test | |
public void testflattenWithElements() { | |
int[] expectedElements = { 1, 2, 3 , 4, 5}; | |
for (int i = 0; i < expectedElements.length; i++) { | |
List<Object> arrays = createArbitraryList(expectedElements, 0, expectedElements.length); | |
List<Integer> actualArray = new ArbitrarilyNestedArrays().flatten(arrays); | |
for (int j = 0; j < expectedElements.length; j++) { | |
assertEquals(expectedElements[j], (int) actualArray.get(j)); | |
} | |
} | |
} | |
public List<Object> createArbitraryList(int[] elements, int begin, int end) { | |
List<Object> list = new ArrayList<Object>(); | |
while (begin < end) { | |
int tempEnd = (int) (Math.random() * (end - begin + 1)) + begin; | |
if (Math.random() > 0.5) { | |
for (int i = begin; i < tempEnd; i++) { | |
list.add(elements[i]); | |
} | |
} else { | |
List<Object> subList = createArbitraryList(elements, begin, tempEnd); | |
list.add(subList); | |
} | |
begin = tempEnd; | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment