From ce570370f0f1e2aaf8c58d4b6629a627b7e7085b Mon Sep 17 00:00:00 2001 From: mame Date: Thu, 14 Sep 2017 03:25:36 +0000 Subject: Add branch coverage for if statement git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59876 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/coverage/coverage.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'ext/coverage') diff --git a/ext/coverage/coverage.c b/ext/coverage/coverage.c index a286cf9222..e4845484e0 100644 --- a/ext/coverage/coverage.c +++ b/ext/coverage/coverage.c @@ -67,6 +67,31 @@ rb_coverage_start(int argc, VALUE *argv, VALUE klass) return Qnil; } +static VALUE +branch_coverage(VALUE branches) +{ + VALUE ret = rb_hash_new(); + VALUE structure = rb_ary_dup(RARRAY_AREF(branches, 0)); + VALUE counters = rb_ary_dup(RARRAY_AREF(branches, 1)); + int i, j, id = 0; + + for (i = 0; i < RARRAY_LEN(structure); i++) { + VALUE branches = RARRAY_AREF(structure, i); + VALUE base_type = RARRAY_AREF(branches, 0); + VALUE base_lineno = RARRAY_AREF(branches, 1); + VALUE children = rb_hash_new(); + rb_hash_aset(ret, rb_ary_new_from_args(3, base_type, INT2FIX(id++), base_lineno), children); + for (j = 2; j < RARRAY_LEN(branches); j += 3) { + VALUE target_label = RARRAY_AREF(branches, j); + VALUE target_lineno = RARRAY_AREF(branches, j + 1); + int idx = FIX2INT(RARRAY_AREF(branches, j + 2)); + rb_hash_aset(children, rb_ary_new_from_args(3, target_label, INT2FIX(id++), target_lineno), RARRAY_AREF(counters, idx)); + } + } + + return ret; +} + static int coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h) { @@ -92,7 +117,7 @@ coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h) } if (branches) { - rb_hash_aset(h, ID2SYM(rb_intern("branches")), branches); ++ rb_hash_aset(h, ID2SYM(rb_intern("branches")), branch_coverage(branches)); } if (methods) { -- cgit v1.2.3