v2: support -stats for _test.v files

pull/4161/head
Delyan Angelov 2020-03-30 18:21:32 +03:00 committed by GitHub
parent 3440d7edd8
commit 07de351546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 37 deletions

View File

@ -85,20 +85,8 @@ fn (b mut BenchedTests) end_testing() {
// /////////////////////////////////////////////////////////////////// // ///////////////////////////////////////////////////////////////////
fn nasserts(n int) string { fn nasserts(n int) string {
if n == 0 {
return '${n:2d} asserts | '
}
if n == 1 { if n == 1 {
return '${n:2d} assert | ' return '${n:5d} 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} asserts | ' return '${n:5d} asserts | '
} }

View File

@ -197,7 +197,7 @@ pub fn (b &Benchmark) total_duration() i64 {
fn (b &Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string { fn (b &Benchmark) tdiff_in_ms(s string, sticks i64, eticks i64) string {
if b.verbose { if b.verbose {
tdiff := (eticks - sticks) tdiff := (eticks - sticks)
return '${tdiff:6lld} ms $s' return '${tdiff:6d} ms $s'
} }
return s return s
} }

View File

@ -50,7 +50,7 @@ pub fn (b mut Builder) gen_c(v_files []string) string {
exit(1) exit(1)
} }
// println('starting cgen...') // println('starting cgen...')
res := gen.cgen(b.parsed_files, b.table) res := gen.cgen(b.parsed_files, b.table, b.pref)
t3 := time.ticks() t3 := time.ticks()
gen_time := t3 - t2 gen_time := t3 - t2
println('C GEN: ${gen_time}ms') println('C GEN: ${gen_time}ms')

View File

@ -6,6 +6,7 @@ import (
v.table v.table
v.depgraph v.depgraph
v.token v.token
v.pref
term term
) )
@ -24,6 +25,7 @@ struct Gen {
definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file) definitions strings.Builder // typedefs, defines etc (everything that goes to the top of the file)
inits strings.Builder // contents of `void _vinit(){}` inits strings.Builder // contents of `void _vinit(){}`
table &table.Table table &table.Table
pref &pref.Preferences
mut: mut:
file ast.File file ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0 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'] '\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') // println('start cgen2')
mut g := Gen{ mut g := Gen{
out: strings.new_builder(100) out: strings.new_builder(100)
@ -61,6 +63,7 @@ pub fn cgen(files []ast.File, table &table.Table) string {
definitions: strings.new_builder(100) definitions: strings.new_builder(100)
inits: strings.new_builder(100) inits: strings.new_builder(100)
table: table table: table
pref: pref
fn_decl: 0 fn_decl: 0
autofree: true autofree: true
indent: -1 indent: -1
@ -554,19 +557,21 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
if g.is_test { if g.is_test {
g.writeln('{') g.writeln('{')
g.writeln(' g_test_oks++;') 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(' println(_STR("OK ${g.file.path}:${a.pos.line_nr}: fn ${g.fn_decl.name}(): assert $s_assertion"));')
g.writeln('}else{') g.writeln('}else{')
g.writeln(' g_test_fails++;') 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(' 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('}')
}
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(' exit(1);') g.writeln(' exit(1);')
g.writeln(' // TODO')
g.writeln(' // Maybe print all vars in a test function if it fails?')
g.writeln('}') 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) { 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.definitions.writeln('int g_test_fails = 0;')
g.writeln('int main() {') g.writeln('int main() {')
g.writeln('\t_vinit();') 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 tfuncs := []string
mut tsuite_begin := '' mut tsuite_begin := ''
mut tsuite_end := '' mut tsuite_end := ''
@ -2506,17 +2535,15 @@ pub fn (g mut Gen) write_tests_main() {
} }
tfuncs << f.name tfuncs << f.name
} }
mut all_tfuncs := []string
if tsuite_begin.len > 0 { if tsuite_begin.len > 0 {
g.writeln('\t${tsuite_begin}();\n') all_tfuncs << tsuite_begin
}
for t in tfuncs {
g.writeln('\t${t}();')
} }
all_tfuncs << tfuncs
if tsuite_end.len > 0 { if tsuite_end.len > 0 {
g.writeln('\t${tsuite_end}();\n') all_tfuncs << tsuite_end
} }
g.writeln('\treturn 0;') return all_tfuncs
g.writeln('}')
} }
fn (g &Gen) is_importing_os() bool { fn (g &Gen) is_importing_os() bool {

View File

@ -5,7 +5,7 @@ import (
v.gen v.gen
v.table v.table
v.checker v.checker
v.eval // v.eval
v.pref v.pref
term term
) )
@ -77,10 +77,11 @@ x := 10
8+4 8+4
' '
table := &table.Table{} 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) mut checker := checker.new_checker(table)
checker.check(prog) checker.check(prog)
res := gen.cgen([prog], table) res := gen.cgen([prog], table, vpref)
println(res) println(res)
} }
@ -97,7 +98,8 @@ fn test_one() {
] ]
expected := 'int a = 10;int b = -a;int c = 20;' expected := 'int a = 10;int b = -a;int c = 20;'
table := table.new_table() table := table.new_table()
mut scope := &ast.Scope{ vpref := &pref.Preferences{}
scope := &ast.Scope{
start_pos: 0 start_pos: 0
parent: 0 parent: 0
} }
@ -111,7 +113,7 @@ fn test_one() {
} }
mut checker := checker.new_checker(table) mut checker := checker.new_checker(table)
checker.check(program) 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) println(res)
ok := expected == res ok := expected == res
println(res) println(res)
@ -193,8 +195,9 @@ fn test_parse_expr() {
] ]
mut e := []ast.Stmt mut e := []ast.Stmt
table := table.new_table() table := table.new_table()
vpref := &pref.Preferences{}
mut checker := checker.new_checker(table) mut checker := checker.new_checker(table)
mut scope := &ast.Scope{ scope := &ast.Scope{
start_pos: 0 start_pos: 0
parent: 0 parent: 0
} }
@ -207,7 +210,7 @@ fn test_parse_expr() {
scope: scope scope: scope
} }
checker.check(program) checker.check(program)
res := gen.cgen([program], table).after('#endif') res := gen.cgen([program], table, vpref).after('#endif')
println('========') println('========')
println(res) println(res)
println('========') println('========')