Skip to content

Commit

Permalink
Code reformatting and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metopa committed May 3, 2018
1 parent f7bef7d commit 7fada56
Show file tree
Hide file tree
Showing 180 changed files with 802 additions and 1,047 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ __pycache__/

# Generated by make
cpp_build

epi_judge_java/java_build/
15 changes: 6 additions & 9 deletions epi_judge_cpp/test_framework/any.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// @library
#pragma once

#include <memory>
#include <typeindex>
#include <utility>

#include "fmt_print.h"
#include "test_utils_meta.h"
#include "serialization_traits.h"
#include "test_utils_meta.h"

#pragma once

namespace any {
class AnyBase {
Expand All @@ -31,7 +29,8 @@ class AnyBase {
const T& Cast() const {
const T* ptr = TryCast<T>();
if (!ptr) {
throw std::runtime_error(FmtStr("Any: expected type {}, got {}", TypeName(), typeid(T).name()));
throw std::runtime_error(FmtStr("Any: expected type {}, got {}",
TypeName(), typeid(T).name()));
}
return *ptr;
}
Expand All @@ -47,9 +46,7 @@ class AnySpecialization : public AnyBase {
explicit AnySpecialization(FwdT&& value)
: value_(std::forward<FwdT>(value)) {}

std::type_index Typeid() const override {
return std::type_index(typeid(T));
}
std::type_index Typeid() const override { return std::type_index(typeid(T)); }

std::string TypeName() const override { return typeid(T).name(); }

Expand Down
13 changes: 6 additions & 7 deletions epi_judge_cpp/test_framework/binary_tree_utils.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// @library
#pragma once

#include <algorithm>
#include <memory>
#include <queue>
#include <sstream>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>

#include "fmt_print_fwd.h"
#include "serialization_traits.h"

#pragma once

template <typename Node, typename T>
void TreeGenerateHelper(const Node& tree, std::vector<T>* result, int order) {
Expand Down Expand Up @@ -144,8 +143,8 @@ int BinaryTreeHeight(const Node& tree) {
if (!tree) {
return -1;
}
return 1 + std::max(BinaryTreeHeight(tree->left),
BinaryTreeHeight(tree->right));
return 1 +
std::max(BinaryTreeHeight(tree->left), BinaryTreeHeight(tree->right));
}

template <typename Node>
Expand Down
12 changes: 5 additions & 7 deletions epi_judge_cpp/test_framework/console_color.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// @library
#pragma once

#include <functional>
#include <iostream>

#ifdef PLATFORM_WIN
#define NOMINMAX
#include <Windows.h>
#endif

#include "platform.h"

#pragma once

#ifdef PLATFORM_WIN
#endif

namespace console_color {

enum class ConsoleColor { FG_RED, FG_GREEN, FG_BLUE, FG_YELLOW, FG_DEFAULT };
Expand Down
6 changes: 2 additions & 4 deletions epi_judge_cpp/test_framework/fmt_print.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// @library
#pragma once

#include <ostream>
#include <string>
#include <tuple>

#include "fmt_print_fwd.h"
#include "serialization_traits.h"

#pragma once

namespace detail {
template <typename T>
void PrintToImpl(std::ostream& out, const T& x, HasOStreamOpTag) {
Expand Down
5 changes: 2 additions & 3 deletions epi_judge_cpp/test_framework/fmt_print_fwd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @library
#pragma once

#include <iosfwd>

#pragma once

template <typename T>
std::ostream& PrintTo(std::ostream& out, const T& x);

Expand Down
21 changes: 9 additions & 12 deletions epi_judge_cpp/test_framework/generic_test.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// @library
#pragma once

#include <chrono>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <vector>

#include "fmt_print.h"
#include "generic_test_handler.h"
#include "json_parser.h"
Expand All @@ -17,6 +13,8 @@
#include "test_timer.h"
#include "test_utils.h"

#pragma once

template <typename Function, typename Comparator>
TestResult RunTests(GenericTestHandler<Function, Comparator>& handler,
const TestConfig& config);
Expand Down Expand Up @@ -50,8 +48,8 @@ TestResult GenericTestMain(const std::vector<std::string>& commandline_args,

platform::SetOutputOpts(config.tty_mode, config.color_mode);

GenericTestHandler<Function, Comparator> test_handler(
test_func, comparator, param_names);
GenericTestHandler<Function, Comparator> test_handler(test_func, comparator,
param_names);
return RunTests(test_handler, config);
} catch (std::runtime_error& e) {
std::cerr << std::endl << "Critical error: " << e.what() << std::endl;
Expand All @@ -68,8 +66,8 @@ void UpdateTestPassed(std::string test_file, int tests_passed) {
std::string err;
std::string js_file_str = buffer.str();
const std::string kJsBeginPattern = "run(";
js_file_str.replace(js_file_str.find(kJsBeginPattern),
kJsBeginPattern.size(), "");
js_file_str.replace(js_file_str.find(kJsBeginPattern), kJsBeginPattern.size(),
"");
const std::string kJsEndPattern = ");";
js_file_str.replace(js_file_str.find(kJsEndPattern), kJsEndPattern.size(),
"");
Expand All @@ -85,16 +83,15 @@ void UpdateTestPassed(std::string test_file, int tests_passed) {
for (const auto& language : problem.second.object_items()) {
if (test_file == language.first) {
const std::string format = "\"{}\": {{\"passed\": {},";
const std::string pattern = FmtStr(
format, test_file, language.second["passed"].int_value());
const std::string pattern =
FmtStr(format, test_file, language.second["passed"].int_value());
const std::string replacement =
FmtStr(format, test_file, tests_passed);
serialized_problem_mapping.replace(
serialized_problem_mapping.find(pattern), pattern.size(),
replacement);
std::ofstream ofs(problem_mapping_file_path);
ofs << kJsBeginPattern << serialized_problem_mapping
<< kJsEndPattern;
ofs << kJsBeginPattern << serialized_problem_mapping << kJsEndPattern;
ofs.close();
return;
}
Expand Down
15 changes: 6 additions & 9 deletions epi_judge_cpp/test_framework/generic_test_handler.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// @library
#pragma once

#include <iterator>
#include <numeric>
#include <regex>
#include <string>
#include <tuple>
#include <vector>

#include "serialization_traits.h"
#include "test_output.h"
#include "test_utils.h"
#include "test_utils_meta.h"
#include "serialization_traits.h"
#include "timed_executor.h"

#pragma once

/**
* The central class in generic test runner framework.
* It is responsible for asserting that the function signature matches
Expand Down Expand Up @@ -66,8 +64,7 @@ class GenericTestHandler {
const std::vector<std::string>& param_names)
: func_(func), comp_(comp), param_names_(param_names) {
if (func_traits::HasExecutorHook()) {
param_names_.erase(
param_names_.begin()); // Remove "executor" parameter
param_names_.erase(param_names_.begin()); // Remove "executor" parameter
}
}

Expand Down Expand Up @@ -202,8 +199,8 @@ class GenericTestHandler {
template <typename ArgTuple, std::size_t... I>
decltype(auto) Invoke(TimedExecutor& executor, ArgTuple& args,
std::index_sequence<I...> /*unused*/) const {
return InvokeWithExecutor(typename func_traits::executor_hook_tag(),
func_, executor, std::get<I>(args)...);
return InvokeWithExecutor(typename func_traits::executor_hook_tag(), func_,
executor, std::get<I>(args)...);
};

template <typename T, typename U>
Expand Down
29 changes: 10 additions & 19 deletions epi_judge_cpp/test_framework/json_parser.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @library
#pragma once

#include <cassert>
#include <cinttypes>
#include <cmath>
Expand All @@ -13,15 +10,17 @@
#include <memory>
#include <string>
#include <vector>
#define noexcept throw()
#define snprintf _snprintf_s

#pragma once

#ifdef _MSC_VER
#if _MSC_VER <= 1800 // VS 2013
#ifndef noexcept
#define noexcept throw()
#endif

#ifndef snprintf
#define snprintf _snprintf_s
#endif
#endif
#endif
Expand Down Expand Up @@ -426,8 +425,7 @@ Json::Json(bool value) : m_ptr(value ? statics().t : statics().f) {}
Json::Json(const string &value) : m_ptr(make_shared<JsonString>(value)) {}
Json::Json(string &&value) : m_ptr(make_shared<JsonString>(move(value))) {}
Json::Json(const char *value) : m_ptr(make_shared<JsonString>(value)) {}
Json::Json(const Json::array &values)
: m_ptr(make_shared<JsonArray>(values)) {}
Json::Json(const Json::array &values) : m_ptr(make_shared<JsonArray>(values)) {}
Json::Json(Json::array &&values)
: m_ptr(make_shared<JsonArray>(move(values))) {}
Json::Json(const Json::object &values)
Expand All @@ -449,16 +447,12 @@ const map<string, Json> &Json::object_items() const {
return m_ptr->object_items();
}
const Json &Json::operator[](size_t i) const { return (*m_ptr)[i]; }
const Json &Json::operator[](const string &key) const {
return (*m_ptr)[key];
}
const Json &Json::operator[](const string &key) const { return (*m_ptr)[key]; }

double JsonValue::number_value() const { return 0; }
int64_t JsonValue::int_value() const { return 0; }
bool JsonValue::bool_value() const { return false; }
const string &JsonValue::string_value() const {
return statics().empty_string;
}
const string &JsonValue::string_value() const { return statics().empty_string; }
const vector<Json> &JsonValue::array_items() const {
return statics().empty_vector;
}
Expand Down Expand Up @@ -553,8 +547,7 @@ struct JsonParser final {
* Advance until the current character is non-whitespace.
*/
void consume_whitespace() {
while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' ||
str[i] == '\t')
while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' || str[i] == '\t')
i++;
}

Expand Down Expand Up @@ -657,8 +650,7 @@ struct JsonParser final {
string out;
long last_escaped_codepoint = -1;
while (true) {
if (i == str.size())
return fail("unexpected end of input in string", "");
if (i == str.size()) return fail("unexpected end of input in string", "");

char ch = str[i++];

Expand All @@ -679,8 +671,7 @@ struct JsonParser final {
}

// Handle escapes
if (i == str.size())
return fail("unexpected end of input in string", "");
if (i == str.size()) return fail("unexpected end of input in string", "");

ch = str[i++];

Expand Down
21 changes: 9 additions & 12 deletions epi_judge_cpp/test_framework/platform.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
// @library
#define PLATFORM_WIN
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h> //must precede sys/stat.h
#include <unistd.h>
#include <cstdio>
#include <iostream>
#include "tri_bool.h"

#pragma once

/**
Expand All @@ -7,23 +15,12 @@
*/

#if _WINDOWS || __MINGW__ || __CYGWIN__
#define PLATFORM_WIN
#endif

#include <iostream>

#ifdef PLATFORM_WIN
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h> //must precede sys/stat.h
#include <cstdio>
#else // UNIX
#include <sys/stat.h>
#include <unistd.h>
#endif

#include "tri_bool.h"

namespace platform {

bool ENABLE_TTY_OUTPUT = false;
Expand Down
13 changes: 5 additions & 8 deletions epi_judge_cpp/test_framework/random_sequence_checker.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @library
#pragma once

#include <algorithm>
#include <array>
#include <cmath>
Expand All @@ -11,9 +8,10 @@
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include "test_failure.h"

#pragma once

int ComputeDeviationMultiplier(double allowed_false_negative, int num_rvs) {
const double individual_rv_error = allowed_false_negative / num_rvs;
std::array<double, 7> kErrorBounds = {
Expand Down Expand Up @@ -53,8 +51,7 @@ bool CheckFrequencies(const std::vector<int>& seq, int n,
// roughly we mean the difference is less than k_sigma.
return std::all_of(std::begin(indiv_freqs), std::end(indiv_freqs),
[avg, k_sigma_indiv](const auto& key_freq) {
return std::abs(avg - key_freq.second) <=
k_sigma_indiv;
return std::abs(avg - key_freq.second) <= k_sigma_indiv;
});
}

Expand Down Expand Up @@ -127,8 +124,8 @@ int BinomialCoefficient(int n, int k) {

// Get the mth combination in lexicographical order from A (n elements) chosen
// k at a time.
std::vector<int> ComputeCombinationIdx(const std::vector<int>& A, int n,
int k, int m) {
std::vector<int> ComputeCombinationIdx(const std::vector<int>& A, int n, int k,
int m) {
std::vector<int> comb;
int a = n, b = k, x = (BinomialCoefficient(n, k) - 1) - m;
for (int i = 0; i < k; ++i) {
Expand Down
Loading

0 comments on commit 7fada56

Please sign in to comment.