From 07de351546b0bd1fc5317a9a60c2b886019b0dd7 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 30 Mar 2020 18:21:32 +0300 Subject: [PATCH] v2: support -stats for _test.v files --- cmd/tools/preludes/tests_with_stats.v | 14 +------ vlib/benchmark/benchmark.v | 2 +- vlib/v/builder/builder.v | 2 +- vlib/v/gen/cgen.v | 57 ++++++++++++++++++++------- vlib/v/parser/parser_test.v | 17 ++++---- 5 files changed, 55 insertions(+), 37 deletions(-) diff --git a/cmd/tools/preludes/tests_with_stats.v b/cmd/tools/preludes/tests_with_stats.v index bcbfa890eb..3e0d2c17dc 100644 --- a/cmd/tools/preludes/tests_with_stats.v +++ b/cmd/tools/preludes/tests_with_stats.v @@ -85,20 +85,8 @@ fn (b mut BenchedTests) end_testing() { // /////////////////////////////////////////////////////////////////// fn nasserts(n int) string { - if n == 0 { - return '${n:2d} asserts | ' - } if n == 1 { - return '${n:2d} assert | ' - } - if n < 10 { - return '${n:2d} asserts | ' - } - if n < 100 { - return '${n:3d} asserts | ' - } - if n < 1000 { - return '${n:4d} asserts | ' + return '${n:5d} assert | ' } return '${n:5d} asserts | ' } diff --git a/vlib/benchmark/benchmark.v b/vlib/benchmark/benchmark.v index 6629685955..18232d297a 100644 --- a/vlib/benchmark/benchmark.v +++ b/vlib/benchmark/benchmark.v @@ -197,7 +197,7 @@ pub fn (b &Benchmark) total_duration() i64 { fn (b &Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string { if b.verbose { tdiff := (eticks - sticks) - return '${tdiff:6lld} ms $s' + return '${tdiff:6d} ms $s' } return s } diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index aecad70c50..234a89598a 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -50,7 +50,7 @@ pub fn (b mut Builder) gen_c(v_files []string) string { exit(1) } // println('starting cgen...') - res := gen.cgen(b.parsed_files, b.table) + res := gen.cgen(b.parsed_files, b.table, b.pref) t3 := time.ticks() gen_time := t3 - t2 println('C GEN: ${gen_time}ms') diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e8f93a3822..4ea70d113a 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -6,6 +6,7 @@ import ( v.table v.depgraph v.token + v.pref term ) @@ -24,6 +25,7 @@ struct Gen { definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) inits strings.Builder // contents of `void _vinit(){}` table &table.Table + pref &pref.Preferences mut: file ast.File fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 @@ -53,7 +55,7 @@ const ( '\t\t\t\t\t\t\t\t'] ) -pub fn cgen(files []ast.File, table &table.Table) string { +pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string { // println('start cgen2') mut g := Gen{ out: strings.new_builder(100) @@ -61,6 +63,7 @@ pub fn cgen(files []ast.File, table &table.Table) string { definitions: strings.new_builder(100) inits: strings.new_builder(100) table: table + pref: pref fn_decl: 0 autofree: true indent: -1 @@ -554,19 +557,21 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) { if g.is_test { g.writeln('{') g.writeln(' g_test_oks++;') + g.writeln(' cb_assertion_ok( _STR("${g.file.path}"), ${a.pos.line_nr}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );') // g.writeln(' println(_STR("OK ${g.file.path}:${a.pos.line_nr}: fn ${g.fn_decl.name}(): assert $s_assertion"));') g.writeln('}else{') g.writeln(' g_test_fails++;') - g.writeln(' eprintln(_STR("${g.file.path}:${a.pos.line_nr}: FAIL: fn ${g.fn_decl.name}(): assert $s_assertion"));') - g.writeln(' exit(1);') - g.writeln('}') - } - else { - g.writeln('{}else{') - g.writeln(' eprintln(_STR("${g.file.path}:${a.pos.line_nr}: FAIL: fn ${g.fn_decl.name}(): assert $s_assertion"));') + g.writeln(' cb_assertion_failed( _STR("${g.file.path}"), ${a.pos.line_nr}, _STR("assert ${s_assertion}"), _STR("${g.fn_decl.name}()") );') g.writeln(' exit(1);') + g.writeln(' // TODO') + g.writeln(' // Maybe print all vars in a test function if it fails?') g.writeln('}') + return } + g.writeln('{}else{') + g.writeln(' eprintln(_STR("${g.file.path}:${a.pos.line_nr}: FAIL: fn ${g.fn_decl.name}(): assert $s_assertion"));') + g.writeln(' exit(1);') + g.writeln('}') } fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { @@ -2491,6 +2496,30 @@ pub fn (g mut Gen) write_tests_main() { g.definitions.writeln('int g_test_fails = 0;') g.writeln('int main() {') g.writeln('\t_vinit();') + g.writeln('') + all_tfuncs := g.get_all_test_function_names() + if g.pref.is_stats { + g.writeln('\tBenchedTests bt = start_testing(${all_tfuncs.len}, tos3("$g.pref.path"));') + } + for t in all_tfuncs { + if g.pref.is_stats { + g.writeln('\tBenchedTests_testing_step_start(&bt, tos3("$t"));') + } + g.writeln('\t${t}();') + if g.pref.is_stats { + g.writeln('\tBenchedTests_testing_step_end(&bt);') + } + } + if g.pref.is_stats { + g.writeln('\tBenchedTests_end_testing(&bt);') + } + g.writeln('') + g.writeln('_vcleanup();') + g.writeln('\treturn g_test_fails > 0;') + g.writeln('}') +} + +fn (g &Gen) get_all_test_function_names() []string { mut tfuncs := []string mut tsuite_begin := '' mut tsuite_end := '' @@ -2506,17 +2535,15 @@ pub fn (g mut Gen) write_tests_main() { } tfuncs << f.name } + mut all_tfuncs := []string if tsuite_begin.len > 0 { - g.writeln('\t${tsuite_begin}();\n') - } - for t in tfuncs { - g.writeln('\t${t}();') + all_tfuncs << tsuite_begin } + all_tfuncs << tfuncs if tsuite_end.len > 0 { - g.writeln('\t${tsuite_end}();\n') + all_tfuncs << tsuite_end } - g.writeln('\treturn 0;') - g.writeln('}') + return all_tfuncs } fn (g &Gen) is_importing_os() bool { diff --git a/vlib/v/parser/parser_test.v b/vlib/v/parser/parser_test.v index 94d802c587..eb76083b03 100644 --- a/vlib/v/parser/parser_test.v +++ b/vlib/v/parser/parser_test.v @@ -5,7 +5,7 @@ import ( v.gen v.table v.checker - v.eval + // v.eval v.pref term ) @@ -77,10 +77,11 @@ x := 10 8+4 ' table := &table.Table{} - prog := parse_file(s, table, .skip_comments, &pref.Preferences{}) + vpref := &pref.Preferences{} + prog := parse_file(s, table, .skip_comments, vpref) mut checker := checker.new_checker(table) checker.check(prog) - res := gen.cgen([prog], table) + res := gen.cgen([prog], table, vpref) println(res) } @@ -97,7 +98,8 @@ fn test_one() { ] expected := 'int a = 10;int b = -a;int c = 20;' table := table.new_table() - mut scope := &ast.Scope{ + vpref := &pref.Preferences{} + scope := &ast.Scope{ start_pos: 0 parent: 0 } @@ -111,7 +113,7 @@ fn test_one() { } mut checker := checker.new_checker(table) checker.check(program) - res := gen.cgen([program], table).replace('\n', '').trim_space().after('#endif') + res := gen.cgen([program], table, vpref).replace('\n', '').trim_space().after('#endif') println(res) ok := expected == res println(res) @@ -193,8 +195,9 @@ fn test_parse_expr() { ] mut e := []ast.Stmt table := table.new_table() + vpref := &pref.Preferences{} mut checker := checker.new_checker(table) - mut scope := &ast.Scope{ + scope := &ast.Scope{ start_pos: 0 parent: 0 } @@ -207,7 +210,7 @@ fn test_parse_expr() { scope: scope } checker.check(program) - res := gen.cgen([program], table).after('#endif') + res := gen.cgen([program], table, vpref).after('#endif') println('========') println(res) println('========')