Skip to content

Commit

Permalink
Fix Java EpiTestComparator throws when user function returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunghsienlee committed Dec 23, 2017
1 parent 24440e4 commit 6b9c106
Show file tree
Hide file tree
Showing 41 changed files with 364 additions and 352 deletions.
13 changes: 6 additions & 7 deletions epi_judge_cpp/lowest_common_ancestor_in_bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@

using std::unique_ptr;

// clang-format off

// Input nodes are nonempty and the key at s is less than or equal to that at b.
// Input nodes are nonempty and the key at s is less than or equal to that at
// b.
BSTNode<int>* FindLCA(const unique_ptr<BSTNode<int>>& tree,
const unique_ptr<BSTNode<int>>& s,
const unique_ptr<BSTNode<int>>& b) {
// Implement this placeholder.
// Implement this placeholder.
return nullptr;
}
}

int LcaWrapper(TestTimer& timer, const std::unique_ptr<BSTNode<int>>& root,
int key1, int key2) {
Expand All @@ -30,10 +29,10 @@ int LcaWrapper(TestTimer& timer, const std::unique_ptr<BSTNode<int>>& root,
return result->data;
}


#include "test_framework/test_utils_generic_main.h"

int main(int argc, char* argv[]) {
generic_test_main(argc, argv, "lowest_common_ancestor_in_bst.tsv", &LcaWrapper);
generic_test_main(argc, argv, "lowest_common_ancestor_in_bst.tsv",
&LcaWrapper);
return 0;
}
22 changes: 11 additions & 11 deletions epi_judge_cpp/test_framework/json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ class Json final {
typedef std::map<std::string, Json> object;

// Constructors for the various types of JSON value.
Json() noexcept; // NUL
Json(std::nullptr_t) noexcept; // NUL
Json(double value); // NUMBER
Json(int value); // NUMBER
Json(bool value); // BOOL
Json() noexcept; // NUL
Json(std::nullptr_t) noexcept; // NUL
Json(double value); // NUMBER
Json(int value); // NUMBER
Json(bool value); // BOOL
Json(const std::string &value); // STRING
Json(std::string &&value); // STRING
Json(const char *value); // STRING
Json(const array &values); // ARRAY
Json(array &&values); // ARRAY
Json(const object &values); // OBJECT
Json(object &&values); // OBJECT
Json(std::string &&value); // STRING
Json(const char *value); // STRING
Json(const array &values); // ARRAY
Json(array &&values); // ARRAY
Json(const object &values); // OBJECT
Json(object &&values); // OBJECT

// Implicit constructor: anything with a to_json() function.
template <class T, class = decltype(&T::to_json)>
Expand Down
20 changes: 11 additions & 9 deletions epi_judge_java/epi/Anagrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ public static List<List<String>> findAnagrams(List<String> dictionary) {
}

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object a, Object b) -> {
List<List<String>> la = (List<List<String>>) a;
List<List<String>> lb = (List<List<String>>) b;
for (List<String> l : la) {
@SuppressWarnings("unchecked")
public static BiPredicate < List<List<String>>,
List < List<String>>> comp = (expected, result) -> {
if (result == null) {
return false;
}
for (List<String> l : expected) {
Collections.sort(l);
}
la.sort(new LexicographicalListComparator());
for (List<String> l : lb) {
expected.sort(new LexicographicalListComparator());
for (List<String> l : result) {
Collections.sort(l);
}
lb.sort(new LexicographicalListComparator());
return la.equals(lb);
result.sort(new LexicographicalListComparator());
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
19 changes: 7 additions & 12 deletions epi_judge_java/epi/Combinations.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@ public static List<List<Integer>> combinations(int n, int k) {
}

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object o1, Object o2) -> {
List<List<Integer>> la = (List<List<Integer>>) o1;
List<List<Integer>> lb = (List<List<Integer>>) o2;
for (List<Integer> l : la) {
Collections.sort(l);
public static BiPredicate < List<List<Integer>>,
List < List<Integer>>> comp = (expected, result) -> {
if (result == null) {
return false;
}
la.sort(new LexicographicalListComparator());
for (List<Integer> l : lb) {
Collections.sort(l);
}
lb.sort(new LexicographicalListComparator());
return la.equals(lb);
expected.sort(new LexicographicalListComparator<>());
result.sort(new LexicographicalListComparator<>());
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
14 changes: 7 additions & 7 deletions epi_judge_java/epi/CopyPostingList.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public static PostingListNode copyPostingsList(PostingListNode L) {
@EpiUserType(ctorParams = {int.class, int.class})
public static class SerializedNode {
public int order;
public int jump_index;
public int jumpIndex;

public SerializedNode(int order, int jump_index) {
public SerializedNode(int order, int jumpIndex) {
this.order = order;
this.jump_index = jump_index;
this.jumpIndex = jumpIndex;
}
}

Expand All @@ -34,11 +34,11 @@ public static PostingListNode createPostingList(List<SerializedNode> serialized)
head = new PostingListNode(serialized.get(i).order, head, null);
keyMapping.put(head.order, head);
}
PostingListNode list_it = head;
PostingListNode listIt = head;
for (SerializedNode x : serialized) {
if (x.jump_index != -1) {
list_it.jump = keyMapping.get(x.jump_index);
if (list_it.jump == null) {
if (x.jumpIndex != -1) {
listIt.jump = keyMapping.get(x.jumpIndex);
if (listIt.jump == null) {
throw new RuntimeException("Jump index out of range");
}
}
Expand Down
10 changes: 5 additions & 5 deletions epi_judge_java/epi/DoListsOverlap.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ public static void overlappingListsWrapper(TestTimer timer, ListNode<Integer> l1
last.next = it;
}

Set<Integer> common_nodes = new HashSet<>();
Set<Integer> commonNodes = new HashSet<>();
ListNode<Integer> it = common;
while (it != null && !common_nodes.contains(it.data)) {
common_nodes.add(it.data);
while (it != null && !commonNodes.contains(it.data)) {
commonNodes.add(it.data);
it = it.next;
}

timer.start();
ListNode<Integer> result = overlappingLists(l1, l2);
timer.stop();

if (!((common_nodes.isEmpty() && result == null)
|| (result != null && common_nodes.contains(result.data)))) {
if (!((commonNodes.isEmpty() && result == null)
|| (result != null && commonNodes.contains(result.data)))) {
throw new TestFailureException("Invalid result");
}
}
Expand Down
14 changes: 7 additions & 7 deletions epi_judge_java/epi/EnumerateBalancedParentheses.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public static List<String> generateBalancedParentheses(int numPairs) {
}

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object a, Object b) -> {
List<String> la = (List<String>) a;
List<String> lb = (List<String>) b;
Collections.sort(la);
Collections.sort(lb);
return la.equals(lb);
public static BiPredicate<List<String>, List<String>> comp = (expected, result) -> {
if (result == null) {
return false;
}
Collections.sort(expected);
Collections.sort(result);
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
16 changes: 9 additions & 7 deletions epi_judge_java/epi/EnumeratePalindromicDecompositions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public static List<List<String>> palindromeDecompositions(String input) {
}

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object expected, Object result) -> {
List<List<String>> a = (List<List<String>>) expected;
List<List<String>> b = (List<List<String>>) result;
a.sort(new LexicographicalListComparator());
b.sort(new LexicographicalListComparator());
return a.equals(b);
@SuppressWarnings("unchecked")
public static BiPredicate < List<List<String>>,
List < List<String>>> comp = (expected, result) -> {
if (result == null) {
return false;
}
expected.sort(new LexicographicalListComparator());
result.sort(new LexicographicalListComparator());
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
8 changes: 4 additions & 4 deletions epi_judge_java/epi/GrayCode.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package epi;
ackage epi;

import epi.test_framework.EpiTest;
import epi.test_framework.GenericTestHandler;
Expand Down Expand Up @@ -26,9 +26,9 @@ public static void grayCodeWrapper(TestTimer timer, int numBits) throws TestFail
List<Integer> result = grayCode(numBits);
timer.stop();

int expected_size = (1 << numBits);
if (result.size() != expected_size) {
throw new TestFailureException("Length mismatch: expected " + String.valueOf(expected_size)
int expectedSize = (1 << numBits);
if (result.size() != expectedSize) {
throw new TestFailureException("Length mismatch: expected " + String.valueOf(expectedSize)
+ ", got " + String.valueOf(result.size()));
}
for (int i = 1; i < result.size(); i++)
Expand Down
12 changes: 6 additions & 6 deletions epi_judge_java/epi/GroupEqualEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ public static void groupByAgeWrapper(TestTimer timer, List<Person> data)
groupByAge(data);
timer.stop();

Map<Person, Integer> new_values = buildMultiset(data);
if (!values.equals(new_values)) {
Map<Person, Integer> newValues = buildMultiset(data);
if (!values.equals(newValues)) {
throw new TestFailureException("Entry set changed");
}
int last_age = data.get(0).age;
int lastAge = data.get(0).age;
Set<Integer> ages = new HashSet<>();

for (Person p : data) {
if (ages.contains(p.age)) {
throw new TestFailureException("Entries are not grouped by age");
}
if (p.age != last_age) {
ages.add(last_age);
last_age = p.age;
if (p.age != lastAge) {
ages.add(lastAge);
lastAge = p.age;
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions epi_judge_java/epi/IsListCyclic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,38 @@ public static ListNode<Integer> hasCycle(ListNode<Integer> head) {
}

@EpiTest(testfile = "is_list_cyclic.tsv")
public static void HasCycleWrapper(TestTimer timer, ListNode<Integer> head, int cycle_idx)
public static void HasCycleWrapper(TestTimer timer, ListNode<Integer> head, int cycleIdx)
throws TestFailureException {
int cycle_length = 0;
if (cycle_idx != -1) {
int cycleLength = 0;
if (cycleIdx != -1) {
if (head == null) {
throw new RuntimeException("Can't cycle empty list");
}
ListNode<Integer> cycle_start = null, cursor = head;
ListNode<Integer> cycleStart = null, cursor = head;
while (cursor.next != null) {
if (cursor.data == cycle_idx) {
cycle_start = cursor;
if (cursor.data == cycleIdx) {
cycleStart = cursor;
}
cursor = cursor.next;
if (cycle_start != null) {
cycle_length++;
if (cycleStart != null) {
cycleLength++;
}
}
if (cursor.data == cycle_idx)
cycle_start = cursor;
if (cycle_start == null) {
if (cursor.data == cycleIdx) {
cycleStart = cursor;
}
if (cycleStart == null) {
throw new RuntimeException("Can't find a cycle start");
}
cursor.next = cycle_start;
cycle_length++;
cursor.next = cycleStart;
cycleLength++;
}

timer.start();
ListNode<Integer> result = hasCycle(head);
timer.stop();

if (cycle_idx == -1) {
if (cycleIdx == -1) {
if (result != null) {
throw new TestFailureException("Found a non-existing cycle");
}
Expand All @@ -54,14 +55,14 @@ public static void HasCycleWrapper(TestTimer timer, ListNode<Integer> head, int
ListNode<Integer> cursor = result;
do {
cursor = cursor.next;
cycle_length--;
if (cursor == null || cycle_length < 0) {
cycleLength--;
if (cursor == null || cycleLength < 0) {
throw new TestFailureException(
"Returned node does not belong to the cycle or is not the closest node to the head");
}
} while (cursor != result);

if (cycle_length != 0) {
if (cycleLength != 0) {
throw new TestFailureException(
"Returned node does not belong to the cycle or is not the closest node to the head");
}
Expand Down
4 changes: 1 addition & 3 deletions epi_judge_java/epi/KClosestStars.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public static List<Star> findClosestKStarsWrapper(List<Star> stars, int k) {

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object a, Object b) -> {
List<Double> expected = (List<Double>) a;
List<Star> result = (List<Star>) b;
public static BiPredicate<List<Double>, List<Star>> comp = (expected, result) -> {
if (expected.size() != result.size()) {
return false;
}
Expand Down
14 changes: 7 additions & 7 deletions epi_judge_java/epi/KLargestInHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public static List<Integer> kLargestInBinaryHeap(List<Integer> A, int k) {
}

@EpiTestComparator
@SuppressWarnings("unchecked")
public static BiPredicate<Object, Object> comp = (Object a, Object b) -> {
List<Integer> la = (List<Integer>) a;
List<Integer> lb = (List<Integer>) b;
Collections.sort(la);
Collections.sort(lb);
return la.equals(lb);
public static BiPredicate<List<Integer>, List<Integer>> comp = (expected, result) -> {
if (result == null) {
return false;
}
Collections.sort(expected);
Collections.sort(result);
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
11 changes: 7 additions & 4 deletions epi_judge_java/epi/KLargestValuesInBst.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public static List<Integer> findKLargestInBst(BstNode<Integer> tree, int k) {
}

@EpiTestComparator
public static BiPredicate<List<Integer>, List<Integer>> comp = (a, b) -> {
Collections.sort(a);
Collections.sort(b);
return a.equals(b);
public static BiPredicate<List<Integer>, List<Integer>> comp = (expected, result) -> {
if (result == null) {
return false;
}
Collections.sort(expected);
Collections.sort(result);
return expected.equals(result);
};

public static void main(String[] args) {
Expand Down
Loading

0 comments on commit 6b9c106

Please sign in to comment.