Skip to content

Commit b7ff540

Browse files
committed
compiler: add coverage to tests
Add coverage measurements to tests. Also add them to CI.
1 parent b827b6e commit b7ff540

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

.github/workflows/build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ jobs:
322322
# Use more time to pass UBSAN and disable leaks in JIT mode because we exit using exit(), and do not call destructors.
323323
[ "${{ env.das_llvm_disabled }}" = "ON" ] || ASAN_OPTIONS=detect_leaks=0 ./daslang _dasroot_/dastest/dastest.das -jit -- --color --test ../tests || ASAN_OPTIONS=detect_leaks=0 ./daslang _dasroot_/dastest/dastest.das -jit -- --color --isolated-mode --timeout 4800 --test ../tests
324324
./daslang _dasroot_/dastest/dastest.das -- --color --test ../tests || ./daslang _dasroot_/dastest/dastest.das -- --color --isolated-mode --timeout 3600 --test ../tests
325+
326+
# Run tests with coverage and print report.
327+
./daslang _dasroot_/dastest/dastest.das -- --cov-path coverage.txt --color --test ../tests
328+
lcov --summary cov --rc lcov_branch_coverage=1
325329
;;
326330
windows32)
327331
cd bin/"${{ matrix.cmake_preset }}"

daslib/archive.das

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def public serialize(var arch : Archive; var value : array<auto(TT)>) {
210210
delete value
211211
}
212212
arch |> read_raw(len)
213-
value |> resize(len)
213+
unsafe(value |> resize(len))
214214
for (element in value) {
215215
arch |> _::serialize(element)
216216
}

dastest/suite.das

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct SuiteCtx {
2626
verbose : bool = false
2727
compile_only : bool = false
2828
use_aot : bool = false
29+
cov_path : string = ""
2930

3031
// Testing-related options.
3132
testNames : array<string>
@@ -91,6 +92,7 @@ def SuiteCtx(args : array<string>) : SuiteCtx {
9192
ctx.projectPath = args |> get_str_arg("--test-project", "")
9293
ctx.compile_only = args |> has_value("--compile-only")
9394
ctx.use_aot = args |> has_value("--use-aot") || is_in_aot()
95+
ctx.cov_path = args |> get_str_arg("--cov-path", "")
9496
collect_tests_names(args, ctx.testNames)
9597

9698
ctx.bench_enabled = args |> has_value("--bench")
@@ -168,6 +170,11 @@ def test_file(file_name : string; var ctx : SuiteCtx; var file_ctx : FileCtx) :
168170
if (jit_enabled()) {
169171
access |> add_extra_module("just_in_time", "{get_das_root()}/daslib/just_in_time.das")
170172
}
173+
if (!ctx.cov_path |> empty()) {
174+
cop.export_all = true
175+
cop.rtti = true
176+
access |> add_extra_module("coverage", "{get_das_root()}/daslib/coverage.das")
177+
}
171178
compile_file(file_name, access, unsafe(addr(mg)), cop) $(ok, program, output) {
172179
var expectedErrors : table<CompilationError; int>
173180
if (program != null) {
@@ -242,6 +249,21 @@ def test_file(file_name : string; var ctx : SuiteCtx; var file_ctx : FileCtx) :
242249
var mod = program |> get_this_module()
243250
if (mod != null && context != null) {
244251
let module_result = test_module(*mod, *context, ctx, file_ctx)
252+
if (module_result.total > 0 && !ctx.cov_path |> empty()) {
253+
var report = ""
254+
unsafe(invoke_in_context(*context, "fill_report", unsafe(addr(report)), ""))
255+
// Clone to current context.
256+
report := report
257+
fopen(ctx.cov_path, "ab") <| $(fw) {
258+
if (fw != null) {
259+
to_log(LOG_INFO, "Coverage to {ctx.cov_path}\n")
260+
fwrite(fw, report)
261+
} else {
262+
to_log(LOG_ERROR, "Couldn't create output file {ctx.cov_path}\n")
263+
}
264+
}
265+
266+
}
245267
if (module_result.failed + module_result.errors == 0) {
246268
unsafe {
247269
file_ctx.context = addr(*context)

tests/language/failed_const_and_block_folding.das

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ expect 40209:2
77
//options log_optimization_passes=true
88
// options log=true
99

10+
[marker(no_coverage)]
1011
def two_plus_two : int {
1112
return 2 + 2
1213
}
1314

15+
[marker(no_coverage)]
1416
def var_two_plus_two : int {
1517
let x, y : int = 2
1618
return x + y
1719
}
1820

21+
[marker(no_coverage)]
1922
def var_op3_two_plus_two(cond : bool) : int {
2023
let x, y : int = 2
2124
return (cond ? x : y) + 2
2225
}
2326

27+
[marker(no_coverage)]
2428
def var_op3_arg_two_plus_two(cond : bool) : int {
2529
let x, y : int = 2
2630
let cc : bool = cond
2731
return (cc ? x : y) + 2
2832
}
2933

34+
[marker(no_coverage)]
3035
def test_dead_code(X : int) : int {
3136
var x : int = X
3237
while (x < 5) {
@@ -42,6 +47,7 @@ def test_dead_code(X : int) : int {
4247
}
4348
}
4449

50+
[marker(no_coverage)]
4551
def test_empty_if(cond : bool) {
4652
if (cond) {
4753
assert(false)
@@ -50,7 +56,7 @@ def test_empty_if(cond : bool) {
5056
}
5157
}
5258

53-
[export]
59+
[export, marker(no_coverage)]
5460
def test : bool {
5561
if ((3 + (2 > 3 ? 0 : 1)) != 4) {
5662
3 + (2 > 3 ? 0 : 1)

tests/language/failed_static_assert_in_infer.das

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ options gen2
33

44
expect 40100
55

6+
[marker(no_coverage)]
67
def fold(a : array<auto(numt)>; var from : numt) {
78
for (x in a) {
89
from += x
@@ -11,17 +12,20 @@ def fold(a : array<auto(numt)>; var from : numt) {
1112
return from
1213
}
1314

15+
[marker(no_coverage)]
1416
def join(a : array<auto(numt)>) {
1517
var b = numt(0)
1618
return fold(a, b)
1719
}
1820

21+
[marker(no_coverage)]
1922
def test(msg) {
2023
let a : array<uint64>
2124
let q = join(a)
2225
return string(q) + msg
2326
}
2427

28+
[marker(no_coverage)]
2529
def test : bool {
2630
feint(test("this"))
2731
return true

0 commit comments

Comments
 (0)