Skip to content

Commit

Permalink
Add support for better test failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
metopa committed Mar 6, 2018
1 parent 23feb1a commit bf596e8
Show file tree
Hide file tree
Showing 686 changed files with 9,855 additions and 4,325 deletions.
3 changes: 3 additions & 0 deletions epi_judge_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

add_executable(a_b_sqrt2 a_b_sqrt2.cc)
add_executable(absent_value_array absent_value_array.cc)
add_executable(adding_credits adding_credits.cc)
add_executable(advance_by_offsets advance_by_offsets.cc)
Expand Down Expand Up @@ -60,6 +61,7 @@ add_executable(gcd gcd.cc)
add_executable(graph_clone graph_clone.cc)
add_executable(gray_code gray_code.cc)
add_executable(group_equal_entries group_equal_entries.cc)
add_executable(h_index h_index.cc)
add_executable(hanoi hanoi.cc)
add_executable(huffman_coding huffman_coding.cc)
add_executable(insert_in_list insert_in_list.cc)
Expand Down Expand Up @@ -120,6 +122,7 @@ add_executable(max_safe_height max_safe_height.cc)
add_executable(max_square_submatrix max_square_submatrix.cc)
add_executable(max_submatrix max_submatrix.cc)
add_executable(max_teams_in_photograph max_teams_in_photograph.cc)
add_executable(max_trapped_water max_trapped_water.cc)
add_executable(max_water_trappable max_water_trappable.cc)
add_executable(maximum_subarray_in_circular_array maximum_subarray_in_circular_array.cc)
add_executable(minimum_distance_3_sorted_arrays minimum_distance_3_sorted_arrays.cc)
Expand Down
1,102 changes: 681 additions & 421 deletions epi_judge_cpp/Makefile

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions epi_judge_cpp/a_b_sqrt2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <vector>

using std::vector;

vector<double> GenerateFirstKABSqrt2(int k) {
// Implement this placeholder.
return {};
}

#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"k"};
return GenericTestMain(args, timeout_seconds, "a_b_sqrt2.tsv",
&GenerateFirstKABSqrt2, DefaultComparator{},
param_names);
}
15 changes: 10 additions & 5 deletions epi_judge_cpp/absent_value_array.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdexcept>
#include <vector>

#include "test_framework/test_failure_exception.h"
#include "test_framework/test_failure.h"

using std::invalid_argument;
using std::vector;
Expand All @@ -16,16 +16,21 @@ int FindMissingElementWrapper(const vector<int>& stream) {
try {
return FindMissingElement(cbegin(stream), cend(stream));
} catch (invalid_argument&) {
throw TestFailureException("Unexpected no_missing_element exception");
throw TestFailure("Unexpected no_missing_element exception");
}
}

#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"stream"};
GenericTestMain(args, "absent_value_array.tsv", &FindMissingElementWrapper,
DefaultComparator{}, param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "absent_value_array.tsv",
&FindMissingElementWrapper, DefaultComparator{},
param_names);
}
49 changes: 35 additions & 14 deletions epi_judge_cpp/adding_credits.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>

#include "test_framework/test_failure_exception.h"
#include "test_framework/test_utils_serialization_traits.h"
#include "test_framework/fmt_print.h"
#include "test_framework/test_failure.h"

using std::string;

Expand Down Expand Up @@ -39,44 +39,65 @@ struct Operation {
int i_arg;
};

std::ostream& operator<<(std::ostream& out, const Operation& op) {
return out << FmtStr("{}({}, {})", op.op, op.s_arg, op.i_arg);
}

template <>
struct SerializationTraits<Operation>
: UserSerTraits<Operation, std::string, std::string, int> {};

void ClientsCreditsInfoTester(const std::vector<Operation>& ops) {
ClientsCreditsInfo credits;
ClientsCreditsInfo cr;
int op_idx = 0;
for (auto& op : ops) {
if (op.op == "ClientsCreditsInfo") {
continue;
} else if (op.op == "remove") {
bool result = credits.Remove(op.s_arg);
bool result = cr.Remove(op.s_arg);
if (result != op.i_arg) {
throw TestFailureException("Remove: return value mismatch");
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
}
} else if (op.op == "max") {
auto result = credits.Max();
auto result = cr.Max();
if (result != op.s_arg) {
throw TestFailureException("Max: return value mismatch");
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
}
} else if (op.op == "insert") {
credits.Insert(op.s_arg, op.i_arg);
cr.Insert(op.s_arg, op.i_arg);
} else if (op.op == "add_all") {
credits.AddAll(op.i_arg);
cr.AddAll(op.i_arg);
} else if (op.op == "lookup") {
auto result = credits.Lookup(op.s_arg);
auto result = cr.Lookup(op.s_arg);
if (result != op.i_arg) {
throw TestFailureException("Lookup: return value mismatch");
throw TestFailure()
.WithProperty(PropertyName::STATE, cr)
.WithProperty(PropertyName::COMMAND, op)
.WithMismatchInfo(op_idx, op.i_arg, result);
;
}
}
op_idx++;
}
}

#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"ops"};
GenericTestMain(args, "adding_credits.tsv", &ClientsCreditsInfoTester,
DefaultComparator{}, param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "adding_credits.tsv",
&ClientsCreditsInfoTester, DefaultComparator{},
param_names);
}
10 changes: 7 additions & 3 deletions epi_judge_cpp/advance_by_offsets.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ bool CanReachEnd(const vector<int>& max_advance_steps) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"max_advance_steps"};
GenericTestMain(args, "advance_by_offsets.tsv", &CanReachEnd,
DefaultComparator{}, param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "advance_by_offsets.tsv",
&CanReachEnd, DefaultComparator{}, param_names);
}
45 changes: 31 additions & 14 deletions epi_judge_cpp/alternating_array.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <string>
#include <vector>

#include "test_framework/test_failure_exception.h"
#include "test_framework/test_timer.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"

using std::vector;

Expand All @@ -14,42 +15,58 @@ void CheckAnswer(const vector<int>& A) {
for (int i = 0; i < A.size(); ++i) {
if (i % 2) {
if (A[i] < A[i - 1]) {
throw TestFailureException("");
throw TestFailure(
std::to_string(i) + "th element (" + std::to_string(A[i]) +
") shall be greater than or equal to " + std::to_string(i - 1) +
"th element (" + std::to_string(A[i - 1]) + ")");
}
if (i + 1 < A.size()) {
if (A[i] < A[i + 1]) {
throw TestFailureException("");
throw TestFailure(
std::to_string(i) + "th element (" + std::to_string(A[i]) +
") shall be greater than or equal to " + std::to_string(i + 1) +
"th element (" + std::to_string(A[i + 1]) + ")");
}
}
} else {
if (i > 0) {
if (A[i - 1] < A[i]) {
throw TestFailureException("");
throw TestFailure(std::to_string(i - 1) + "th element (" +
std::to_string(A[i - 1]) +
") shall be greater than or equal to " +
std::to_string(i) + "th element (" +
std::to_string(A[i]) + ")");
}
}
if (i + 1 < A.size()) {
if (A[i + 1] < A[i]) {
throw TestFailureException("");
throw TestFailure(std::to_string(i + 1) + "th element (" +
std::to_string(A[i + 1]) +
") shall be greater than or equal to " +
std::to_string(i) + "th element (" +
std::to_string(A[i]) + ")");
}
}
}
}
}

void RearrangeWrapper(TestTimer& timer, vector<int> A) {
timer.Start();
Rearrange(&A);
timer.Stop();
void RearrangeWrapper(TimedExecutor& executor, vector<int> A) {
executor.Run([&] { Rearrange(&A); });

CheckAnswer(A);
}

#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"timer", "A"};
GenericTestMain(args, "alternating_array.tsv", &RearrangeWrapper,
DefaultComparator{}, param_names);
return 0;
std::vector<std::string> param_names{"executor", "A"};
return GenericTestMain(args, timeout_seconds, "alternating_array.tsv",
&RearrangeWrapper, DefaultComparator{}, param_names);
}
12 changes: 8 additions & 4 deletions epi_judge_cpp/anagrams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ vector<vector<string>> FindAnagrams(const vector<string>& dictionary) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"dictionary"};
GenericTestMain(args, "anagrams.tsv", &FindAnagrams,
&UnorderedComparator<std::vector<std::vector<std::string>>>,
param_names);
return 0;
return GenericTestMain(
args, timeout_seconds, "anagrams.tsv", &FindAnagrams,
&UnorderedComparator<std::vector<std::vector<std::string>>>, param_names);
}
11 changes: 8 additions & 3 deletions epi_judge_cpp/apply_permutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ vector<int> ApplyPermutationWrapper(vector<int> perm, vector<int> A) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"perm", "A"};
GenericTestMain(args, "apply_permutation.tsv", &ApplyPermutationWrapper,
DefaultComparator{}, param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "apply_permutation.tsv",
&ApplyPermutationWrapper, DefaultComparator{},
param_names);
}
10 changes: 7 additions & 3 deletions epi_judge_cpp/arbitrage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ bool IsArbitrageExist(vector<vector<double>> graph) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"graph"};
GenericTestMain(args, "arbitrage.tsv", &IsArbitrageExist, DefaultComparator{},
param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "arbitrage.tsv",
&IsArbitrageExist, DefaultComparator{}, param_names);
}
4 changes: 1 addition & 3 deletions epi_judge_cpp/binary_tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ struct BinaryTreeNode {
: data(data), left(std::move(left)), right(std::move(right)) {}
};

template <typename KeyT>
struct SerializationTraits<unique_ptr<BinaryTreeNode<KeyT>>>
: BinaryTreeSerializationTraits<unique_ptr<BinaryTreeNode<KeyT>>, false> {};
DECLARE_BINARY_TREE_TYPE(KeyT, std::unique_ptr<BinaryTreeNode<KeyT>>, false)
4 changes: 1 addition & 3 deletions epi_judge_cpp/binary_tree_with_parent_prototype.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ struct BinaryTreeNode {
explicit BinaryTreeNode(const T& data) : data(data), parent(nullptr) {}
};

template <typename KeyT>
struct SerializationTraits<unique_ptr<BinaryTreeNode<KeyT>>>
: BinaryTreeSerializationTraits<unique_ptr<BinaryTreeNode<KeyT>>, true> {};
DECLARE_BINARY_TREE_TYPE(KeyT, std::unique_ptr<BinaryTreeNode<KeyT>>, true)
12 changes: 8 additions & 4 deletions epi_judge_cpp/binomial_coefficients.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ int ComputeBinomialCoefficient(int n, int k) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"n", "k"};
GenericTestMain(args, "binomial_coefficients.tsv",
&ComputeBinomialCoefficient, DefaultComparator{},
param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "binomial_coefficients.tsv",
&ComputeBinomialCoefficient, DefaultComparator{},
param_names);
}
10 changes: 7 additions & 3 deletions epi_judge_cpp/bonus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ int CalculateBonus(const vector<int>& productivity) {
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"productivity"};
GenericTestMain(args, "bonus.tsv", &CalculateBonus, DefaultComparator{},
param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "bonus.tsv", &CalculateBonus,
DefaultComparator{}, param_names);
}
11 changes: 8 additions & 3 deletions epi_judge_cpp/bst_from_preorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ unique_ptr<BstNode<int>> RebuildBSTFromPreorder(
#include "test_framework/generic_test.h"

int main(int argc, char* argv[]) {
// The timeout is set to 15 seconds for each test case.
// If your program ends with TIMEOUT error, and you want to try longer time
// limit, you can extend the limit by changing the following line.
std::chrono::seconds timeout_seconds{15};

std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"preorder_sequence"};
GenericTestMain(args, "bst_from_preorder.tsv", &RebuildBSTFromPreorder,
DefaultComparator{}, param_names);
return 0;
return GenericTestMain(args, timeout_seconds, "bst_from_preorder.tsv",
&RebuildBSTFromPreorder, DefaultComparator{},
param_names);
}
Loading

0 comments on commit bf596e8

Please sign in to comment.