From 3a1cf863db572d7bdc6cb9c59d8f46198df1183d Mon Sep 17 00:00:00 2001 From: Craig Cornelius Date: Thu, 7 Nov 2024 11:05:10 -0800 Subject: [PATCH] Update NodeJS generators to use .filter for subsampling (#334) * Update NodeJS generators to use .filter for subsampling. Issue #283 * Simplify sampling --- schema/message_fmt2/test_schema.json | 2 +- testgen/generators/common.js | 13 +++-- testgen/generators/datetime_fmt.py | 1 + testgen/generators/generate_test_hash.js | 2 +- testgen/generators/list_fmt.py | 10 +++- testgen/generators/list_fmt_gen.js | 32 +++++++++---- testgen/generators/rdt_fmt_gen.js | 55 ++++++++++------------ testgen/generators/relativedatetime_fmt.py | 11 ++++- 8 files changed, 76 insertions(+), 50 deletions(-) diff --git a/schema/message_fmt2/test_schema.json b/schema/message_fmt2/test_schema.json index e70e1edd..195d9b12 100644 --- a/schema/message_fmt2/test_schema.json +++ b/schema/message_fmt2/test_schema.json @@ -29,7 +29,7 @@ }, "description": { "type": "string", - "description": "what this is all about" + "description": "Tell about this test" }, "hexhash": { "description": "A hexadecimal hash code for the test without the label", diff --git a/testgen/generators/common.js b/testgen/generators/common.js index 06a3508e..4b8f80aa 100644 --- a/testgen/generators/common.js +++ b/testgen/generators/common.js @@ -1,3 +1,4 @@ +// Common functions for generation of test data from NodeJS function sample_tests(all_tests, run_limit) { // Gets a sampling of the data based on total and the expected number. @@ -6,11 +7,9 @@ function sample_tests(all_tests, run_limit) { return all_tests; } - let size_all = all_tests.length; - let increment = Math.floor(size_all / run_limit); - let samples = []; - for (let index = 0; index < size_all; index += increment) { - samples.push(all_tests[index]); - } - return samples; + const label_increment = Math.floor(all_tests.length / run_limit); + + return samples = all_tests.filter((e, i) => i % label_increment == 0); } + +module.exports = {sample_tests}; diff --git a/testgen/generators/datetime_fmt.py b/testgen/generators/datetime_fmt.py index 449af3fe..3b04bc9a 100644 --- a/testgen/generators/datetime_fmt.py +++ b/testgen/generators/datetime_fmt.py @@ -46,3 +46,4 @@ def process_test_data(self): result = subprocess.run(mv_command, shell=True) return result + diff --git a/testgen/generators/generate_test_hash.js b/testgen/generators/generate_test_hash.js index 933c00db..a8dd743e 100644 --- a/testgen/generators/generate_test_hash.js +++ b/testgen/generators/generate_test_hash.js @@ -22,7 +22,7 @@ function generate_hash_for_test(test_case) { // Computes a 32 byte hex hash code for the test case // Note that the test case should not include 'label'. - obj = remove_none(test_case) + obj = remove_none(test_case); json_str = JSON.stringify(obj); const hasher = crypto.createHash("sha1"); diff --git a/testgen/generators/list_fmt.py b/testgen/generators/list_fmt.py index 258b1c92..5d18f691 100644 --- a/testgen/generators/list_fmt.py +++ b/testgen/generators/list_fmt.py @@ -22,9 +22,14 @@ def process_test_data(self): 'icu71': '18.7.0', } + exec_list = ['node generators/list_fmt_gen.js'] + if self.run_limit > 0: + exec_list.append('-run_limit') + exec_list.append(str(self.run_limit)) + run_list = [ ['source ~/.nvm/nvm.sh; nvm install 21.6.0; nvm use 21.6.0 --silent'], - ['node generators/list_fmt_gen.js'], + exec_list, ['mv list_fmt*.json icu74'] ] @@ -34,7 +39,8 @@ def process_test_data(self): # Set up Node version and call the generator nvm_version = icu_nvm_versions[self.icu_version] - generate_command = 'source ~/.nvm/nvm.sh; nvm install %s; nvm use %s --silent; node generators/list_fmt_gen.js' % (nvm_version, nvm_version) + generate_command = 'source ~/.nvm/nvm.sh; nvm install %s; nvm use %s --silent; %s' %\ + (nvm_version, nvm_version, ' '.join(exec_list)) logging.debug('Running this command: %s', generate_command) result = result = subprocess.run(generate_command, shell=True) diff --git a/testgen/generators/list_fmt_gen.js b/testgen/generators/list_fmt_gen.js index ea2a8e2f..263623de 100644 --- a/testgen/generators/list_fmt_gen.js +++ b/testgen/generators/list_fmt_gen.js @@ -8,6 +8,7 @@ // Set up Node version to generate data specific to ICU/CLDR version // e.g., `nvm install 21.6.0;nvm use 21.6.0` (ICU 74) +const common_fns = require("./common.js"); const gen_hash = require("./generate_test_hash.js"); const fs = require('node:fs'); @@ -70,7 +71,8 @@ function generateAll() { const expected_count = locales.length * types.length * styles.length * lists.length; - console.log("Generating ", expected_count, " list_fmt tests for ", process.versions.icu); + console.log("Generating up to ", expected_count, " list_fmt tests for ", + process.versions.icu); for (const locale of locales) { @@ -156,25 +158,39 @@ function generateAll() { } - console.log('Number of list format tests generated for ', - process.versions.icu, ': ', label_num); + if (debug) { + console.log('Number of list format tests generated for ', + process.versions.icu, ': ', label_num); + console.log(' RUN LIMIT = ', run_limit); + } - test_obj['tests'] = test_cases; + test_obj['tests'] = common_fns.sample_tests(test_cases, run_limit); try { - fs.writeFileSync('list_fmt_test.json', JSON.stringify(test_obj, null)); + fs.writeFileSync('list_fmt_test.json', JSON.stringify(test_obj, null, 2)); // file written successfully } catch (err) { console.error(err); } - verify_obj['verifications'] = verify_cases; + verify_obj['verifications'] = common_fns.sample_tests(verify_cases, run_limit); try { - fs.writeFileSync('list_fmt_verify.json', JSON.stringify(verify_obj, null)); + fs.writeFileSync('list_fmt_verify.json', JSON.stringify(verify_obj, null, 2)); // file written successfully } catch (err) { console.error(err); } } +if (debug) { + console.log('LIST FORMAT argv: ', process.argv); +} + +let run_limit = -1; +if (process.argv.length >= 4) { + if (process.argv[2] == '-run_limit') { + run_limit = Number(process.argv[3]); + } +} + /* Call the generator */ -generateAll(); +generateAll(run_limit); diff --git a/testgen/generators/rdt_fmt_gen.js b/testgen/generators/rdt_fmt_gen.js index 4b5c92ee..3cfe5b94 100644 --- a/testgen/generators/rdt_fmt_gen.js +++ b/testgen/generators/rdt_fmt_gen.js @@ -12,6 +12,7 @@ // Set up Node version to generate data specific to ICU/CLDR version // e.g., `nvm install 21.6.0;nvm use 21.6.0` (ICU 74) +const common_fns = require("./common.js"); const gen_hash = require("./generate_test_hash.js"); const fs = require('node:fs'); @@ -48,22 +49,6 @@ const numeric = ['auto', 'always']; const counts = [-100, -4, -2, -1, 0, 1, 1.3, 2, 3, 4, 10]; -function sample_tests(all_tests, run_limit) { - // Gets a sampling of the data based on total and the expected number. - - if (run_limit < 0 || all_tests.length <= run_limit) { - return all_tests; - } - - let size_all = all_tests.length; - let increment = Math.floor(size_all / run_limit); - let samples = []; - for (let index = 0; index < size_all; index += increment) { - samples.push(all_tests[index]); - } - return samples; -} - // Create the test and verify JSON data for this case. function save_test(unit, count, locale, all_options, result, label_num, test_cases, verify_cases) { @@ -99,13 +84,13 @@ function save_test(unit, count, locale, all_options, result, label_num, console.log(' expected = ', result); } } catch (error) { - console.log('!!! error ', error, ' in label ', label_num); + console.log('!!! Problem pushing verify case. Error: ', + error, ' in label ', label_num); } } - function generateAll() { let test_obj = { @@ -166,7 +151,8 @@ function generateAll() { formatter_numeric_always = new Intl.RelativeTimeFormat(locale, all_options_numeric_always); } catch (error) { - console.log(error, ' with locale ', + console.log('Error creating RelativeTimeFormat: ', + error, ' with locale ', locale, ' and options: ', all_options_numeric_always); continue; } @@ -182,7 +168,8 @@ function generateAll() { } if (debug) { - console.log("resolved options: ", formatter.resolvedOptions()); + console.log("resolved options: ", + formatter_numeric_auto.resolvedOptions()); } for (const unit of units) { @@ -222,11 +209,13 @@ function generateAll() { } - console.log('Number of relative date/time tests generated for ', - process.versions.icu, ': ', label_num); - console.log(' %d tests are different between numeric auto and always', diff_count); + if (debug) { + console.log('Number of relative date/time tests generated for ', + process.versions.icu, ': ', label_num); + console.log(' %d tests are different between numeric auto and always', diff_count); + } - test_obj['tests'] = sample_tests(test_cases, run_limit); + test_obj['tests'] = common_fns.sample_tests(test_cases, run_limit); try { fs.writeFileSync('rdt_fmt_test.json', JSON.stringify(test_obj, null, 2)); // file written successfully @@ -234,7 +223,12 @@ function generateAll() { console.error(err); } - verify_obj['verifications'] = sample_tests(verify_cases, run_limit); + verify_obj['verifications'] = common_fns.sample_tests(verify_cases, run_limit); + if (debug) { + console.log('VERIFICATION COUNT: ', verify_obj['verifications'].length, + ' run_limit: ', run_limit); + } + try { fs.writeFileSync('rdt_fmt_verify.json', JSON.stringify(verify_obj, null, 2)); // file written successfully @@ -243,11 +237,14 @@ function generateAll() { } } -/* Call the generator */ + if (debug) { + console.log('RDT_FMT argv: ', process.argv); + } + let run_limit = -1; -if (process.argv.length >= 5) { - if (process.argv[3] == '-run_limit') { - run_limit = Number(process.argv[4]); +if (process.argv.length >= 4) { + if (process.argv[2] == '-run_limit') { + run_limit = Number(process.argv[3]); } } diff --git a/testgen/generators/relativedatetime_fmt.py b/testgen/generators/relativedatetime_fmt.py index 7480f5b7..1cc692f0 100644 --- a/testgen/generators/relativedatetime_fmt.py +++ b/testgen/generators/relativedatetime_fmt.py @@ -22,9 +22,15 @@ def process_test_data(self): 'icu71': '18.7.0', } + exec_list = ['node generators/rdt_fmt_gen.js'] + if self.run_limit: + exec_list.append('-run_limit') + exec_list.append(str(self.run_limit)) + print("RDTF generator: ", exec_list) + run_list = [ ['source ~/.nvm/nvm.sh; nvm install 21.6.0; nvm use 21.6.0 --silent'], - ['node generators/rdt_gen.js'], + exec_list, ['mv rdt_fmt*.json icu74'] ] @@ -34,7 +40,8 @@ def process_test_data(self): # Set up Node version and call the generator nvm_version = icu_nvm_versions[self.icu_version] - generate_command = 'source ~/.nvm/nvm.sh; nvm install %s; nvm use %s --silent; node generators/rdt_fmt_gen.js' % (nvm_version, nvm_version) + generate_command = 'source ~/.nvm/nvm.sh; nvm install %s; nvm use %s --silent; %s' %\ + (nvm_version, nvm_version, ' '.join(exec_list)) logging.debug('Running this command: %s', generate_command) result = subprocess.run(generate_command, shell=True)