Changeset View
Changeset View
Standalone View
Standalone View
java/src/test/java/org/softwareheritage/graph/GraphTest.java
Show All 16 Lines | |||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import com.github.luben.zstd.ZstdInputStream; | import com.github.luben.zstd.ZstdInputStream; | ||||
import it.unimi.dsi.big.webgraph.LazyLongIterator; | import it.unimi.dsi.big.webgraph.LazyLongIterator; | ||||
import it.unimi.dsi.big.webgraph.LazyLongIterators; | import it.unimi.dsi.big.webgraph.LazyLongIterators; | ||||
import org.junit.jupiter.api.BeforeAll; | import org.junit.jupiter.api.BeforeAll; | ||||
import static org.junit.Assert.assertEquals; | import static org.junit.Assert.assertEquals; | ||||
import static org.junit.Assert.fail; | |||||
public class GraphTest { | public class GraphTest { | ||||
static SwhBidirectionalGraph graph; | static SwhBidirectionalGraph graph; | ||||
final protected String TEST_ORIGIN_ID = "swh:1:ori:83404f995118bd25774f4ac14422a8f175e7a054"; | final protected String TEST_ORIGIN_ID = "swh:1:ori:83404f995118bd25774f4ac14422a8f175e7a054"; | ||||
@BeforeAll | @BeforeAll | ||||
public static void setUp() throws IOException { | public static void setUp() throws IOException { | ||||
Show All 15 Lines | public class GraphTest { | ||||
public static <T> void assertEqualsAnyOrder(Collection<T> expected, Collection<T> actual) { | public static <T> void assertEqualsAnyOrder(Collection<T> expected, Collection<T> actual) { | ||||
ArrayList<T> expectedList = new ArrayList<>(expected); | ArrayList<T> expectedList = new ArrayList<>(expected); | ||||
ArrayList<T> actualList = new ArrayList<>(actual); | ArrayList<T> actualList = new ArrayList<>(actual); | ||||
expectedList.sort(Comparator.comparing(Object::toString)); | expectedList.sort(Comparator.comparing(Object::toString)); | ||||
actualList.sort(Comparator.comparing(Object::toString)); | actualList.sort(Comparator.comparing(Object::toString)); | ||||
assertEquals(expectedList, actualList); | assertEquals(expectedList, actualList); | ||||
} | } | ||||
public static <T> void assertContainsAll(Collection<T> expected, Collection<T> actual) { | |||||
ArrayList<T> expectedList = new ArrayList<>(expected); | |||||
ArrayList<T> actualList = new ArrayList<>(actual); | |||||
expectedList.sort(Comparator.comparing(Object::toString)); | |||||
Iterator<T> expectedIterator = expectedList.iterator(); | |||||
actualList.sort(Comparator.comparing(Object::toString)); | |||||
for (T actualItem : actualList) { | |||||
boolean found = false; | |||||
while (expectedIterator.hasNext()) { | |||||
if (expectedIterator.next().equals(actualItem)) { | |||||
found = true; | |||||
break; | |||||
} | |||||
} | |||||
if (!found) { | |||||
// TODO: better message when actualItem is present twice in actualList, | |||||
// but only once in expectedList | |||||
fail(String.format("%s not found in %s", actualItem, expectedList)); | |||||
} | |||||
} | |||||
} | |||||
public static <T> void assertLength(int expected, Collection<T> actual) { | |||||
assertEquals(String.format("Size of collection %s:", actual), expected, actual.size()); | |||||
} | |||||
public static ArrayList<Long> lazyLongIteratorToList(LazyLongIterator input) { | public static ArrayList<Long> lazyLongIteratorToList(LazyLongIterator input) { | ||||
ArrayList<Long> inputList = new ArrayList<>(); | ArrayList<Long> inputList = new ArrayList<>(); | ||||
Iterator<Long> inputIt = LazyLongIterators.eager(input); | Iterator<Long> inputIt = LazyLongIterators.eager(input); | ||||
inputIt.forEachRemaining(inputList::add); | inputIt.forEachRemaining(inputList::add); | ||||
return inputList; | return inputList; | ||||
} | } | ||||
public static String[] readZstFile(Path zstFile) throws IOException { | public static String[] readZstFile(Path zstFile) throws IOException { | ||||
ZstdInputStream zis = new ZstdInputStream(new FileInputStream(zstFile.toFile())); | ZstdInputStream zis = new ZstdInputStream(new FileInputStream(zstFile.toFile())); | ||||
return (new String(zis.readAllBytes())).split("\n"); | return (new String(zis.readAllBytes())).split("\n"); | ||||
} | } | ||||
} | } |