Skip to content

Commit

Permalink
chore(core): Unicode version test
Browse files Browse the repository at this point in the history
- load versions from node and ICU4C++ and Blocks.txt

Fixes: #10183
  • Loading branch information
srl295 committed May 11, 2024
1 parent 5103a48 commit c7b51ba
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions core/tests/unit/ldml/test_unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,31 @@
#include <unicode/uchar.h>
#include "json.hpp"

#include <test_assert.h>
#include <test_color.h>

#ifdef assert_basic_equal
#undef assert_basic_equal
#endif
#define assert_basic_equal(actual, expected) { \
if ((actual) != (expected)) { \
std::cerr \
<< "Test failed at " << __FILE__ << ":" << __LINE__ << ":" \
<< std::endl \
<< "expected: " << (expected) << std::endl \
<< "actual: " << (actual) << std::endl; \
std::exit(EXIT_FAILURE); \
} \
}

//-------------------------------------------------------------------------------------
// Unicode version tests
//-------------------------------------------------------------------------------------

std::string arg_path;

nlohmann::json load_json(const km::core::path &jsonpath) {
std::cout << "== " << __FUNCTION__ << " " << jsonpath << std::endl;
std::cout << "== " << __FUNCTION__ << " loading " << jsonpath << std::endl;
std::ifstream json_file(jsonpath.native());
if (!json_file) {
return -1; // no file
Expand All @@ -39,22 +56,77 @@ nlohmann::json load_json(const km::core::path &jsonpath) {
return data;
}

void test_unicode_versions(const nlohmann::json versions) {
std::cout << "== " << __FUNCTION__ << std::endl;
/** @returns the major version of 'ver', skipping initial '^'. empty on err */
std::string get_major(const std::string& ver) {
assert(!ver.empty());
auto start = 0;
// skip leading '^'
if (ver[start] == '^') {
start++;
}
// find first '.'
auto end = ver.find('.', start);
assert(end != std::string::npos);
return ver.substr(start, end - start);
}

std::cout << "ICU: " << U_ICU_VERSION << std::endl;
std::cout << "Unicode: " << U_UNICODE_VERSION << std::endl;
void test_unicode_versions(const nlohmann::json &versions, const nlohmann::json &package) {
std::cout << "== " << __FUNCTION__ << std::endl;

const std::string cxx_icu(U_ICU_VERSION),
cxx_icu_unicode(U_UNICODE_VERSION),
node_icu_unicode(versions["/unicode"_json_pointer].template get<std::string>()),
node(versions["/node"_json_pointer].template get<std::string>()),
node_icu(versions["/icu"_json_pointer].template get<std::string>()),
node_engine(package["/engines/node"_json_pointer].template get<std::string>());
// fetch Blocks.txt
std::ifstream blocks_file(km::core::path("../../../../resources/standards-data/unicode-character-database/Blocks.txt"));
assert(blocks_file.good());
std::string block_line;
assert(std::getline(blocks_file, block_line)); // first line
const std::string prefix = "# Blocks-";
assert(block_line.length() > prefix.length());
const auto block_unicode_ver = block_line.substr(prefix.length());

// calculations
auto block_ver_major = get_major(block_unicode_ver);
auto node_engine_major = get_major(node_engine);
auto node_major = get_major(node);
auto cxx_icu_major = get_major(cxx_icu);
auto node_icu_major = get_major(node_icu);
auto cxx_icu_unicode_major = get_major(cxx_icu_unicode);
auto node_icu_unicode_major = get_major(node_icu_unicode);
#define SHOW_VAR(x) (std::cout << #x << "\t" << (x) << std::endl)
SHOW_VAR(cxx_icu);
SHOW_VAR(cxx_icu_unicode);
SHOW_VAR(node_icu_unicode);
SHOW_VAR(node);
SHOW_VAR(node_icu);
SHOW_VAR(node_engine);
SHOW_VAR(block_unicode_ver);
#undef SHOW_VAR

assert_basic_equal(node_major, node_engine_major);
assert_basic_equal(node_icu_unicode_major, cxx_icu_unicode_major);
assert_basic_equal(node_icu_unicode_major, block_ver_major);

// seems less important if the Unicode verison matches.
//assert_basic_equal(cxx_icu_major, node_icu_major);

std::cout << "All OK!" << std::endl;

std::cout << std::endl;
}

int test_all(const char *jsonpath) {
std::cout << "= " << __FUNCTION__ << std::endl;

auto versions = load_json(km::core::path(jsonpath));
auto package = load_json(km::core::path("../../../../package.json"));

assert(!versions.empty());

test_unicode_versions(versions);
test_unicode_versions(versions, package);

return EXIT_SUCCESS;
}
Expand Down

0 comments on commit c7b51ba

Please sign in to comment.