Skip to content

Commit

Permalink
Updated tests. Corrected stdout and sterr discrepencies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Jan 3, 2024
1 parent 00cb2be commit 9c4ebc8
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 95 deletions.
2 changes: 2 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ project "tests"

libdirs { "../sigma/output/compiler/bin/%{cfg.buildcfg}" }

debugargs { "run", "../../tests" }

includedirs {
"source"
}
Expand Down
4 changes: 2 additions & 2 deletions source/compiler/compiler/compilation_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace sigma {
default: break; // suppress unhandled enumeration warnings
}

utility::console::println();
utility::console::print("\n");
});
}

Expand All @@ -63,7 +63,7 @@ namespace sigma {
symbol_value = utility::detail::escape_string(syntax.string_table.get(info.symbol_key));
}

utility::console::println("{:<20} {}", info.tok.to_string(), symbol_value);
utility::console::print("{:<20} {}\n", info.tok.to_string(), symbol_value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/compiler/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace sigma {
: m_description(description) {}

auto compiler::compile() const -> utility::result<void> {
utility::console::println("compiling file: {}", m_description.path.string());
utility::console::print("compiling file: {}\n", m_description.path.string());

ASSERT(m_description.emit != emit_target::EXECUTABLE, "executable support not implemented");
TRY(verify_file(m_description.path));
Expand Down
5 changes: 5 additions & 0 deletions source/compiler/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace sigma {
} // namespace sigma::ir

enum emit_target : u8 {
NONE,
OBJECT,
EXECUTABLE
};
Expand Down Expand Up @@ -87,6 +88,10 @@ struct parametric::options_parser<sigma::ir::system> {
template<>
struct parametric::options_parser<sigma::emit_target> {
static auto parse(const std::string& value) -> sigma::emit_target {
if (value == "none") {
return sigma::emit_target::NONE;
}

if (value == "object") {
return sigma::emit_target::OBJECT;
}
Expand Down
2 changes: 1 addition & 1 deletion source/compiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ i32 compile(const parametric::parameters& params) {
const auto result = sigma::compiler::compile(description);

if (result.has_error()) {
utility::console::println(result.get_error_message());
utility::console::printerr(result.get_error_message());
return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion source/intermediate_representation/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace sigma::ir {
}

// DEBUG
utility::console::println("{}", assembly.get_underlying());
utility::console::print("{}\n", assembly.get_underlying());
}

auto module::generate_object_file() -> utility::object_file {
Expand Down
144 changes: 81 additions & 63 deletions source/tests/main.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,86 @@
#include <utility/diagnostics.h>
#include <utility/filesystem/file.h>
#include <utility/string_helper.h>
#include <utility/parametric/parametric.h>
#include <utility/shell.h>

// Runs all tests in sigma/tests and checks against a database of expected results. Should only
// be invoked via the run_tests.bat file.

//#define WIN_COMPILER_PATH utility::fs::get_canonical_path("../../../../output/compiler/bin/Debug/compiler.exe").string()
//#define ERR_FILE_PATH "err.txt"
//
using namespace utility::types;
//
//auto compile_file(const filepath& path) -> utility::result<bool> {
// const std::string command = WIN_COMPILER_PATH + " " + utility::fs::get_canonical_path(path).string() + " NO_EMIT" + " 1> err.txt";
// const bool has_error = system(command.c_str());
//
// if(has_error) {
// TRY(std::string message, utility::file::read_text_file(ERR_FILE_PATH));
//
// // message = utility::detail::remove_first_line(message);
// // message.erase(message.size() - 1); // remove the trailing newline
// // utility::console::println("\033[31m'{}'\033[0m", message);
// }
//
// return has_error;
//}

//i32 main() {
// std::queue<filepath> paths({ utility::fs::get_canonical_path("../../../../tests") });
//
// std::cout << paths.front() << '\n';
//
// utility::console::println("running tests...");
// utility::console::println("{}", std::string(32, '-'));
//
// while(!paths.empty()) {
// const filepath current = paths.front();
// paths.pop();
//
// utility::directory::for_all_directory_items(current, [&](const filepath& path) {
// if(utility::fs::is_directory(path)) {
// paths.push(path);
// return;
// }
//
// static const char* result_codes[] = {
// "OK",
// "ERROR",
// };
//
// const auto result = compile_file(path);
//
// if(result.has_error()) {
// utility::console::println(result.get_error_message());
// return;
// }
//
// utility::console::println("{:<30}{}", path.stem().string(), result_codes[result.get_value()]);
// });
// }
//
// utility::fs::remove(ERR_FILE_PATH);
// return 0;
//}

#define STOUD_FILE "STDOUT.txt"
#define STERR_FILE "STDERR.txt"

auto get_compiler_path() -> std::string {
#ifdef SYSTEM_WINDOWS
return utility::fs::get_canonical_path("../../output/compiler/bin/Debug/compiler.exe").string();
#elif defined SYSTEM_LINUX
return utility::fs::get_canonical_path("../../output/compiler/bin/Debug/compiler").string();
#else
PANIC("unhandled system path");
return std::string();
#endif
}

bool test_file(const filepath& path) {
const std::string command = std::format("{} compile {} -e none > {} 2> {}", get_compiler_path(), path.string(), STOUD_FILE, STERR_FILE);
const i32 return_code = utility::shell::execute(command);

if(return_code == 0) {
utility::console::print("{:<20} OK\n", path.filename().string());
return false;
}

utility::console::printerr("{:<20} ERROR\n", path.filename().string());

const auto file_result = utility::file::read_text_file(STERR_FILE);
if(file_result.has_error()) {
throw std::exception(std::format("cannot open file {}", STERR_FILE).c_str());
}

utility::console::printerr("'{}'\n", file_result.get_value());

return true;
}

i32 run_all_tests(const parametric::parameters& params) {
const auto test_directory = params.get<std::string>("directory");
std::queue<filepath> paths({ utility::fs::get_canonical_path(test_directory) });
bool encountered_error = false;

// run our tests
try {
while (!paths.empty()) {
const filepath current = paths.front();
paths.pop();

utility::directory::for_all_directory_items(current, [&](const filepath& path) {
if (utility::fs::is_directory(path)) {
paths.push(path);
return;
}

encountered_error |= test_file(path);
});
}
} catch(const std::exception& exception) {
utility::console::printerr("error: {}\n", exception.what());
encountered_error = true;
}

// cleanup
try {
utility::fs::remove(STOUD_FILE);
utility::fs::remove(STERR_FILE);
} catch(const std::exception& exception) {
utility::console::printerr("error: {}\n", exception.what());
encountered_error = true;
}

return encountered_error ? 1 : 0;
}

i32 main(i32 argc, char* argv[]) {
//
}
parametric::program program;

auto& run_all = program.add_command("run", "run all tests in the specified directory", run_all_tests);
run_all.add_positional_argument<std::string>("directory", "test directory");

return program.parse(argc, argv);
}
16 changes: 8 additions & 8 deletions source/utility/diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ namespace utility {
std::cout << format_str(std::move(fmt), std::forward<arguments>(args)...);
}

static void println(const std::string& message) {
std::cout << message << '\n';
static void printerr(const std::string& message) {
std::cerr << message;
}

template<typename... arguments>
static void println(std::format_string<arguments...> fmt, arguments&&... args) {
std::cout << format_str(std::move(fmt), std::forward<arguments>(args)...) << '\n';
}

static void println() {
std::cout << '\n';
static void printerr(std::format_string<arguments...> fmt, arguments&&... args) {
std::cerr << format_str(std::move(fmt), std::forward<arguments>(args)...);
}

static void flush() {
std::cout << std::flush;
}

static void errflush() {
std::cerr << std::flush;
}
private:
template<typename... arguments>
static auto format_str(std::format_string<arguments...> fmt, arguments&&... args) -> std::string {
Expand Down
4 changes: 4 additions & 0 deletions source/utility/filesystem/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace utility {
return std::filesystem::is_regular_file(path);
}

static auto get_working_directory() -> filepath {
return std::filesystem::current_path();
}

static auto get_canonical_path(const filepath& path) -> filepath {
try {
return canonical(path);
Expand Down
40 changes: 21 additions & 19 deletions source/utility/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@
#endif

#ifdef DEBUG
#define ASSERT(__condition, __fmt, ...) \
do { \
if(!(__condition)) { \
utility::console::print("ASSERTION FAILED: ({}:{}): ", __FILE__, __LINE__); \
utility::console::println((__fmt), ##__VA_ARGS__); \
utility::console::flush(); \
DEBUG_BREAK(); \
} \
#define ASSERT(__condition, __fmt, ...) \
do { \
if(!(__condition)) { \
utility::console::printerr("ASSERTION FAILED: ({}:{}): ", __FILE__, __LINE__); \
utility::console::printerr((__fmt), ##__VA_ARGS__); \
utility::console::printerr("\n"); \
utility::console::flush(); \
DEBUG_BREAK(); \
} \
} while(false)

#define NOT_IMPLEMENTED() \
do { \
utility::console::println("ASSERTION FAILED: ({}:{}): NOT IMPLEMENTED", __FILE__, __LINE__); \
utility::console::flush(); \
DEBUG_BREAK(); \
#define NOT_IMPLEMENTED() \
do { \
utility::console::printerr("ASSERTION FAILED: ({}:{}): NOT IMPLEMENTED\n", __FILE__, __LINE__); \
utility::console::errflush(); \
DEBUG_BREAK(); \
} while(false)

#define PANIC( __fmt, ...) \
do { \
utility::console::print("ASSERTION FAILED: ({}:{}): ", __FILE__, __LINE__); \
utility::console::println((__fmt), ##__VA_ARGS__); \
utility::console::flush(); \
DEBUG_BREAK(); \
#define PANIC( __fmt, ...) \
do { \
utility::console::printerr("ASSERTION FAILED: ({}:{}): ", __FILE__, __LINE__); \
utility::console::printerr((__fmt), ##__VA_ARGS__); \
utility::console::printerr("\n"); \
utility::console::errflush(); \
DEBUG_BREAK(); \
} while(false)
#else
#define ASSERT(__condition, __fmt, ...)
Expand Down

0 comments on commit 9c4ebc8

Please sign in to comment.