Skip to content

Commit

Permalink
tests: factor out looping into macro (#398)
Browse files Browse the repository at this point in the history
Also fix some cases where random generator was not re-seeded in new trials.
  • Loading branch information
WojciechMula committed Apr 9, 2024
1 parent d6735e0 commit c9e9d51
Show file tree
Hide file tree
Showing 47 changed files with 311 additions and 897 deletions.
12 changes: 3 additions & 9 deletions tests/convert_latin1_to_utf16be_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ namespace {
constexpr int trials = 1000;
}

TEST(convert_all_latin) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_all_latin) {
// range for 2 UTF-16 bytes
simdutf::tests::helpers::RandomIntRanges random({
{0x00, 0xff}}, 0);
simdutf::tests::helpers::RandomIntRanges random({{0x00, 0xff}}, seed);

auto procedure = [&implementation](const char* latin1, size_t size, char16_t* utf16le) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -39,9 +36,6 @@ TEST(convert_all_latin) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
11 changes: 3 additions & 8 deletions tests/convert_latin1_to_utf16le_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ namespace {
constexpr int trials = 1000;
}

TEST(convert_all_latin) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_all_latin) {
// range for 2 UTF-16 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x00, 0xff}}, 0);
simdutf::tests::helpers::RandomIntRanges random({{0x00, 0xff}}, seed);

auto procedure = [&implementation](const char* latin1, size_t size, char16_t* utf16) -> size_t {
return implementation.convert_latin1_to_utf16le(latin1, size, utf16);
Expand All @@ -34,9 +32,6 @@ TEST(convert_all_latin) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
9 changes: 2 additions & 7 deletions tests/convert_latin1_to_utf32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ namespace {
constexpr size_t trials = 10000;
}

TEST(convert_all_latin1) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_all_latin1) {
size_t counter = 0;
auto generator = [&counter]() -> uint32_t {
return counter++ & 0xFF;
Expand All @@ -31,9 +29,6 @@ TEST(convert_all_latin1) {
simdutf::tests::helpers::transcode_latin1_to_utf32_test_base test(generator, 256);
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
9 changes: 2 additions & 7 deletions tests/convert_latin1_to_utf8_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ namespace {
constexpr size_t trials = 10000;
}

TEST(convert_all_latin1) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_all_latin1) {
size_t counter = 0;
auto generator = [&counter]() -> uint8_t {
return counter++ & 0xFF;
Expand All @@ -32,9 +30,6 @@ TEST(convert_all_latin1) {
simdutf::tests::helpers::transcode_latin1_to_utf8_test_base test(generator, 256);
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
17 changes: 3 additions & 14 deletions tests/convert_utf16be_to_latin1_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ namespace {


// For invalid inputs, we expect the conversion to fail (return 0)
TEST(convert_random_inputs) {
for(size_t trial = 0; trial < trials; trial ++) {
uint32_t seed{1234+uint32_t(trial)};
TEST_LOOP(trials, convert_random_inputs) {
simdutf::tests::helpers::RandomInt r(0x00, 0xffff, seed);

if((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }

for (size_t size: input_size) {
std::vector<char16_t> utf16(size);
for(size_t i = 0; i < size; i++) {
Expand All @@ -43,13 +39,9 @@ TEST(convert_random_inputs) {
ASSERT_EQUAL(0, actual_size);
}
}
}
}

TEST(convert_2_UTF16_bytes) {
int seed = {1234};
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_2_UTF16_bytes) {
// range for 1, 2 or 3 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x00ff},}, seed);

Expand All @@ -66,7 +58,6 @@ TEST(convert_2_UTF16_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_fails_if_input_too_large) {
Expand Down Expand Up @@ -104,6 +95,4 @@ TEST(convert_fails_if_input_too_large) {
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
13 changes: 3 additions & 10 deletions tests/convert_utf16be_to_latin1_tests_with_errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ namespace {
constexpr int trials = 1000;
}

TEST(convert_2_UTF16_bytes) {
int seed = {1234};
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_2_UTF16_bytes) {
// range for 1, 2 or 3 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x00ff},
}, seed);
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x00ff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char* latin1) -> size_t {

Expand All @@ -41,7 +37,6 @@ TEST(convert_2_UTF16_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_fails_if_input_too_large) {
Expand Down Expand Up @@ -85,6 +80,4 @@ TEST(convert_fails_if_input_too_large) {
}
}

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
18 changes: 5 additions & 13 deletions tests/convert_utf16be_to_utf32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ namespace {
constexpr int trials = 1000;
}

TEST(convert_2_UTF16_bytes) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_2_UTF16_bytes) {
// range for 1, 2 or 3 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x007f},
{0x0080, 0x07ff},
{0x0800, 0xd7ff},
{0xe000, 0xffff}}, 0);
{0xe000, 0xffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char32_t* utf32) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -42,15 +40,12 @@ TEST(convert_2_UTF16_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_with_surrogates) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_with_surrogates) {
// range for 3 or 4 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0800, 0xd800-1},
{0xe000, 0x10ffff}}, 0);
{0xe000, 0x10ffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char32_t* utf32) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -67,7 +62,6 @@ TEST(convert_with_surrogates) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

#if SIMDUTF_IS_BIG_ENDIAN
Expand Down Expand Up @@ -275,6 +269,4 @@ TEST(all_possible_8_codepoint_combinations) {
}
#endif

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
18 changes: 5 additions & 13 deletions tests/convert_utf16be_to_utf32_with_errors_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ namespace {
constexpr int trials = 1000;
}

TEST(convert_2_UTF16_bytes) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_2_UTF16_bytes) {
// range for 1, 2 or 3 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x007f},
{0x0080, 0x07ff},
{0x0800, 0xd7ff},
{0xe000, 0xffff}}, 0);
{0xe000, 0xffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char32_t* utf32) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -42,14 +40,11 @@ TEST(convert_2_UTF16_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_with_surrogates) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_with_surrogates) {
simdutf::tests::helpers::RandomIntRanges random({{0x0800, 0xd800-1},
{0xe000, 0x10ffff}}, 0);
{0xe000, 0x10ffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char32_t* utf32) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -66,7 +61,6 @@ TEST(convert_with_surrogates) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

#if SIMDUTF_IS_BIG_ENDIAN
Expand Down Expand Up @@ -182,6 +176,4 @@ TEST(convert_fails_if_there_is_surrogate_pair_is_followed_by_high_surrogate) {
}
#endif

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
24 changes: 6 additions & 18 deletions tests/convert_utf16be_to_utf8_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ TEST(convert_pure_ASCII) {
}
}

TEST(convert_into_1_or_2_UTF8_bytes) {
for(size_t trial = 0; trial < trials; trial ++) {
uint32_t seed{1234+uint32_t(trial)};
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_into_1_or_2_UTF8_bytes) {
simdutf::tests::helpers::RandomInt random(0x0000, 0x07ff, seed); // range for 1 or 2 UTF-8 bytes

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char* utf8) -> size_t {
Expand All @@ -63,17 +60,14 @@ TEST(convert_into_1_or_2_UTF8_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_into_1_or_2_or_3_UTF8_bytes) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_into_1_or_2_or_3_UTF8_bytes) {
// range for 1, 2 or 3 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0000, 0x007f},
{0x0080, 0x07ff},
{0x0800, 0xd7ff},
{0xe000, 0xffff}}, 0);
{0xe000, 0xffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char* utf8) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -90,15 +84,12 @@ TEST(convert_into_1_or_2_or_3_UTF8_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

TEST(convert_into_3_or_4_UTF8_bytes) {
for(size_t trial = 0; trial < trials; trial ++) {
if ((trial % 100) == 0) { std::cout << "."; std::cout.flush(); }
TEST_LOOP(trials, convert_into_3_or_4_UTF8_bytes) {
// range for 3 or 4 UTF-8 bytes
simdutf::tests::helpers::RandomIntRanges random({{0x0800, 0xd800-1},
{0xe000, 0x10ffff}}, 0);
{0xe000, 0x10ffff}}, seed);

auto procedure = [&implementation](const char16_t* utf16le, size_t size, char* utf8) -> size_t {
std::vector<char16_t> utf16be(size);
Expand All @@ -115,7 +106,6 @@ TEST(convert_into_3_or_4_UTF8_bytes) {
ASSERT_TRUE(test(procedure));
ASSERT_TRUE(test.check_size(size_procedure));
}
}
}

#if SIMDUTF_IS_BIG_ENDIAN
Expand Down Expand Up @@ -322,6 +312,4 @@ TEST(all_possible_8_codepoint_combinations) {
}
#endif

int main(int argc, char* argv[]) {
return simdutf::test::main(argc, argv);
}
TEST_MAIN
Loading

0 comments on commit c9e9d51

Please sign in to comment.