Skip to content

Commit 9f19bc0

Browse files
authored
Fix abbreviated translators (#2)
* Fix abbreviated translators * Add translation tests * Change formatting
1 parent e8e0b8e commit 9f19bc0

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

tester.cc

+47
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,52 @@ main ()
9393
++index;
9494
}
9595

96+
index = 0;
97+
for (const auto &test_case : test_translation_cases)
98+
{
99+
// Prevent the compiler from optimizing the test
100+
std::string first_copy (test_case.first.size (), '\0');
101+
std::memcpy (first_copy.data (), test_case.first.data (),
102+
test_case.first.size ());
103+
std::string second_copy (test_case.second.size (), '\0');
104+
std::memcpy (second_copy.data (), test_case.second.data (),
105+
test_case.second.size ());
106+
107+
auto result = oicompare::compare (
108+
first_copy.c_str (), first_copy.c_str () + first_copy.size (),
109+
second_copy.c_str (), second_copy.c_str () + second_copy.size ());
110+
111+
// Replace stdout.
112+
fflush (stdout);
113+
int stdout_fd = dup (fileno (stdout));
114+
std::FILE *capture_file = tmpfile ();
115+
dup2 (fileno (capture_file), fileno (stdout));
116+
117+
// Print the result.
118+
test_case.translator (result);
119+
120+
// Restore stdout.
121+
fflush (stdout);
122+
dup2 (stdout_fd, fileno (stdout));
123+
close (stdout_fd);
124+
125+
char buffer[1024];
126+
std::string result_str;
127+
rewind (capture_file);
128+
while (fgets (buffer, sizeof (buffer), capture_file) != nullptr)
129+
result_str += buffer;
130+
fclose (capture_file);
131+
132+
if (result_str != test_case.result)
133+
{
134+
fmt::println ("Test {} failed", index);
135+
fmt::println ("Expected: {}", test_case.result);
136+
fmt::println ("Got: {}", result_str);
137+
return 1;
138+
}
139+
140+
++index;
141+
}
142+
96143
return 0;
97144
}

tests.hh

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <variant>
55

66
#include "oicompare.hh"
7+
#include "translations.hh"
78

89
namespace oicompare::tests
910
{
@@ -53,6 +54,14 @@ struct test_case
5354
std::string_view second;
5455
};
5556

57+
struct test_translation_case
58+
{
59+
oicompare::translations::translation translator;
60+
std::string_view first;
61+
std::string_view second;
62+
std::string_view result;
63+
};
64+
5665
#define REP10(X) X X X X X X X X X X
5766
#define REP100(X) REP10 (REP10 (X))
5867

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

114+
constexpr auto test_translation_cases = std::array{
115+
test_translation_case{
116+
translations::english_translation<translations::kind::full>::print,
117+
""sv, ""sv, "OK\n"sv},
118+
test_translation_case{
119+
translations::english_translation<translations::kind::full>::print,
120+
"ABC"sv, "ABC"sv, "OK\n"sv},
121+
test_translation_case{
122+
translations::english_translation<translations::kind::full>::print,
123+
"ABC\nDEF\n"sv, "ABC\nABC\n"sv,
124+
"WRONG: line 2: expected \"DEF\", got \"ABC\"\n"sv},
125+
test_translation_case{
126+
translations::english_translation<translations::kind::terse>::print,
127+
"ABC"sv, "AB"sv, "WRONG\n"sv},
128+
test_translation_case{translations::english_translation<
129+
translations::kind::abbreviated>::print,
130+
"25"sv, "2"sv,
131+
"WRONG: line 1: expected \"25\", got \"2\"\n"sv},
132+
test_translation_case{
133+
translations::english_translation<
134+
translations::kind::abbreviated>::print,
135+
"10001"sv REP100 ("0"sv), "10000"sv REP100 ("0"sv),
136+
"WRONG: line 1: expected \"1000100000000000000000000000000000000000000000000000000000000000000000000000000000000…\", got \"1000000000000000000000000000000000000000000000000000000000000000000000000000000000000…\"\n"sv},
137+
test_translation_case{
138+
translations::english_translation<
139+
translations::kind::abbreviated>::print,
140+
"1"sv REP100 ("0"sv) "0"sv REP100 ("0"sv),
141+
"1"sv REP100 ("0"sv) "1"sv REP100 ("0"sv),
142+
"WRONG: line 1: expected \"1000000000000000000000000000000000000000000000000000000000000000000000000000000…000…\", got \"1000000000000000000000000000000000000000000000000000000000000000000000000000000…010…\"\n"sv},
143+
test_translation_case{
144+
translations::english_translation<
145+
translations::kind::abbreviated>::print,
146+
"1"sv REP100 ("0"sv) "0"sv, "1"sv REP100 ("0"sv) "1"sv,
147+
"WRONG: line 1: expected \"10000000000000000000000000000000000000000000000000000000000000000000000000000000000…00\", got \"10000000000000000000000000000000000000000000000000000000000000000000000000000000000…01\"\n"sv}};
148+
105149
#undef REP100
106150
#undef REP10
107151

translations.hh

+9-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <fmt/format.h>
1313

1414
#include "oicompare.hh"
15+
#include "print_format.hh"
1516

1617
namespace oicompare::translations
1718
{
@@ -108,7 +109,7 @@ template <> struct represent_word<true>
108109
using namespace std::string_view_literals;
109110

110111
assert (!word.empty ());
111-
assert (first_difference < word.size ());
112+
assert (first_difference <= word.size ());
112113

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

135136
assert (used_chars <= abbreviated_max);
136137

137-
auto append_char_helper
138-
= [&] (char ch) constexpr { out = append_char (std::move (out), ch); };
138+
auto append_char_helper = [&](char ch) constexpr
139+
{
140+
out = append_char (std::move (out), ch);
141+
};
139142

140143
*out++ = '"';
141144

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

191-
append_char_helper (word[first_difference]);
194+
if (first_difference < word.size ())
195+
append_char_helper (word[first_difference]);
192196

193197
if (first_difference < word.size () - 1)
194198
{
@@ -243,7 +247,7 @@ template <kind Kind> struct english_translation
243247
{
244248
using namespace std::string_view_literals;
245249

246-
return print_format ([&] (auto &ctx) {
250+
return print_format ([&token, mismatch] (auto &ctx) {
247251
auto out = ctx.out ();
248252
switch (token.type)
249253
{

0 commit comments

Comments
 (0)