Skip to content

Commit

Permalink
Fix abbreviated translators (#2)
Browse files Browse the repository at this point in the history
* Fix abbreviated translators

* Add translation tests

* Change formatting
  • Loading branch information
MasloMaslane authored Sep 6, 2024
1 parent e8e0b8e commit 9f19bc0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
47 changes: 47 additions & 0 deletions tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,52 @@ main ()
++index;
}

index = 0;
for (const auto &test_case : test_translation_cases)
{
// Prevent the compiler from optimizing the test
std::string first_copy (test_case.first.size (), '\0');
std::memcpy (first_copy.data (), test_case.first.data (),
test_case.first.size ());
std::string second_copy (test_case.second.size (), '\0');
std::memcpy (second_copy.data (), test_case.second.data (),
test_case.second.size ());

auto result = oicompare::compare (
first_copy.c_str (), first_copy.c_str () + first_copy.size (),
second_copy.c_str (), second_copy.c_str () + second_copy.size ());

// Replace stdout.
fflush (stdout);
int stdout_fd = dup (fileno (stdout));
std::FILE *capture_file = tmpfile ();
dup2 (fileno (capture_file), fileno (stdout));

// Print the result.
test_case.translator (result);

// Restore stdout.
fflush (stdout);
dup2 (stdout_fd, fileno (stdout));
close (stdout_fd);

char buffer[1024];
std::string result_str;
rewind (capture_file);
while (fgets (buffer, sizeof (buffer), capture_file) != nullptr)
result_str += buffer;
fclose (capture_file);

if (result_str != test_case.result)
{
fmt::println ("Test {} failed", index);
fmt::println ("Expected: {}", test_case.result);
fmt::println ("Got: {}", result_str);
return 1;
}

++index;
}

return 0;
}
44 changes: 44 additions & 0 deletions tests.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <variant>

#include "oicompare.hh"
#include "translations.hh"

namespace oicompare::tests
{
Expand Down Expand Up @@ -53,6 +54,14 @@ struct test_case
std::string_view second;
};

struct test_translation_case
{
oicompare::translations::translation translator;
std::string_view first;
std::string_view second;
std::string_view result;
};

#define REP10(X) X X X X X X X X X X
#define REP100(X) REP10 (REP10 (X))

Expand Down Expand Up @@ -102,6 +111,41 @@ constexpr auto test_cases = std::array{
REP100 ("A"sv) "B"sv, REP100 ("A"sv) " B"sv},
};

constexpr auto test_translation_cases = std::array{
test_translation_case{
translations::english_translation<translations::kind::full>::print,
""sv, ""sv, "OK\n"sv},
test_translation_case{
translations::english_translation<translations::kind::full>::print,
"ABC"sv, "ABC"sv, "OK\n"sv},
test_translation_case{
translations::english_translation<translations::kind::full>::print,
"ABC\nDEF\n"sv, "ABC\nABC\n"sv,
"WRONG: line 2: expected \"DEF\", got \"ABC\"\n"sv},
test_translation_case{
translations::english_translation<translations::kind::terse>::print,
"ABC"sv, "AB"sv, "WRONG\n"sv},
test_translation_case{translations::english_translation<
translations::kind::abbreviated>::print,
"25"sv, "2"sv,
"WRONG: line 1: expected \"25\", got \"2\"\n"sv},
test_translation_case{
translations::english_translation<
translations::kind::abbreviated>::print,
"10001"sv REP100 ("0"sv), "10000"sv REP100 ("0"sv),
"WRONG: line 1: expected \"1000100000000000000000000000000000000000000000000000000000000000000000000000000000000…\", got \"1000000000000000000000000000000000000000000000000000000000000000000000000000000000000…\"\n"sv},
test_translation_case{
translations::english_translation<
translations::kind::abbreviated>::print,
"1"sv REP100 ("0"sv) "0"sv REP100 ("0"sv),
"1"sv REP100 ("0"sv) "1"sv REP100 ("0"sv),
"WRONG: line 1: expected \"1000000000000000000000000000000000000000000000000000000000000000000000000000000…000…\", got \"1000000000000000000000000000000000000000000000000000000000000000000000000000000…010…\"\n"sv},
test_translation_case{
translations::english_translation<
translations::kind::abbreviated>::print,
"1"sv REP100 ("0"sv) "0"sv, "1"sv REP100 ("0"sv) "1"sv,
"WRONG: line 1: expected \"10000000000000000000000000000000000000000000000000000000000000000000000000000000000…00\", got \"10000000000000000000000000000000000000000000000000000000000000000000000000000000000…01\"\n"sv}};

#undef REP100
#undef REP10

Expand Down
14 changes: 9 additions & 5 deletions translations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <fmt/format.h>

#include "oicompare.hh"
#include "print_format.hh"

namespace oicompare::translations
{
Expand Down Expand Up @@ -108,7 +109,7 @@ template <> struct represent_word<true>
using namespace std::string_view_literals;

assert (!word.empty ());
assert (first_difference < word.size ());
assert (first_difference <= word.size ());

std::size_t used_chars
= char_length ('"') + char_length (word[first_difference]);
Expand All @@ -134,8 +135,10 @@ template <> struct represent_word<true>

assert (used_chars <= abbreviated_max);

auto append_char_helper
= [&] (char ch) constexpr { out = append_char (std::move (out), ch); };
auto append_char_helper = [&](char ch) constexpr
{
out = append_char (std::move (out), ch);
};

*out++ = '"';

Expand Down Expand Up @@ -188,7 +191,8 @@ template <> struct represent_word<true>
append_char_helper (word[first_difference - 1]);
}

append_char_helper (word[first_difference]);
if (first_difference < word.size ())
append_char_helper (word[first_difference]);

if (first_difference < word.size () - 1)
{
Expand Down Expand Up @@ -243,7 +247,7 @@ template <kind Kind> struct english_translation
{
using namespace std::string_view_literals;

return print_format ([&] (auto &ctx) {
return print_format ([&token, mismatch] (auto &ctx) {
auto out = ctx.out ();
switch (token.type)
{
Expand Down

0 comments on commit 9f19bc0

Please sign in to comment.