Skip to content

Commit 3b397a3

Browse files
committed
test: Improve test framework
Various improvements to lcov's self-test framework: - Add test case for lcov --diff - Add new verbosity level - Enable normalization of coverage data files from stdin - Fix lcov_add_concatenated4 test name Signed-off-by: Peter Oberparleiter <[email protected]>
1 parent 53a6ce8 commit 3b397a3

File tree

12 files changed

+157
-9
lines changed

12 files changed

+157
-9
lines changed

test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ info:
1111
echo ""
1212
echo "Available make variables:"
1313
echo " SIZE : specify size of test data (small, medium, large)"
14-
echo " V : specify level of verbosity (0, 1)"
14+
echo " V : specify level of verbosity (0, 1, 2)"
1515

1616
test:
1717
for TEST in $(TESTDIRS) ; do \

test/bin/norminfo

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,16 @@ sub main()
178178
my $tn = "";
179179
my %allfiles;
180180

181+
$multi = 1 if (!defined($multi));
181182
if (!defined($infofile)) {
182-
die("Usage: $0 <coverage-data-file> [<multiplier>]\n");
183+
$infofile = "standard input";
184+
warn("$0: Reading data from standard input\n");
185+
open($fd, "<&STDIN") or
186+
die("$0: Could not duplicated stdin: $!\n");
187+
} else {
188+
open($fd, "<", $infofile) or
189+
die("$0: Could not open $infofile: $!\n");
183190
}
184-
$multi = 1 if (!defined($multi));
185-
186-
open($fd, "<", $infofile) or die("$0: Could not open $infofile: $!\n");
187191

188192
# Register starting positions of data sets
189193
while (my $line = <$fd>) {

test/bin/test_run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ echo
8383
# Show log excerpt on failure or if requested
8484
if [ $RC -ne 0 -o "$V" == "1" ] ; then
8585
LEN=$(tail -c "+$POS" "$LOGFILE" | wc -l)
86-
if [ "$LEN" -gt "$EXCERPTLEN" ] ; then
86+
if [ "$LEN" -gt "$EXCERPTLEN" -a "$V" != "1" ] ; then
8787
tail -c "+$POS" "$LOGFILE" | head -n $EXCERPTLEN | t_indent
8888
let LEN=$LEN-$EXCERPTLEN
8989
echo " ..."

test/common.mak

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ COUNTFILES := $(ZEROCOUNTS) $(FULLCOUNTS) $(TARGETCOUNTS) $(PART1COUNTS) \
1818
LCOVRC := $(TOPDIR)lcovrc
1919
LCOVFLAGS := --config-file $(LCOVRC)
2020
SIZE := small
21+
CC := gcc
2122

2223
export LCOV := lcov $(LCOVFLAGS)
2324
export PATH := $(TOPDIR)/../bin:$(TOPDIR)/bin:$(PATH)
@@ -41,6 +42,8 @@ clean_common:
4142
$(INFOFILES) $(COUNTFILES):
4243
cd $(TOPDIR) && mkinfo profiles/$(SIZE)
4344

45+
ifneq ($(V),2)
4446
.SILENT:
47+
endif
4548

4649
.PHONY: all init exit prepare clean clean_common

test/lcov_add_files/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include ../common.mak
33
ADDTEST := ./add_test
44

55
TESTS := lcov_add_zero lcov_add_zero2 lcov_add_full lcov_add_full2 \
6-
lcov_add_part lcov_add_part2 lcov_add_concatenated
6+
lcov_add_part lcov_add_part2 lcov_add_concatenated4
77

88

99
test: $(TESTS)
@@ -35,7 +35,7 @@ lcov_add_part2:
3535
# should be same as target file
3636
test_run lcov_add_part2 $(ADDTEST) 1 "$(TARGETINFO)" "$(PART1INFO)" "$(PART2INFO)"
3737

38-
lcov_add_concatenated:
38+
lcov_add_concatenated4:
3939
# Add coverage file that consists of 4 concatenation of target files
4040
# and reduce counts to 1/4 - output should be the same as input
4141
cat $(TARGETINFO) $(TARGETINFO) $(TARGETINFO) $(TARGETINFO) >concatenated.info

test/lcov_add_files/add_test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ fi
4141

4242
echo "Comparing with reference..."
4343
if ! diff -u "$REFFILE" "$SORTFILE" ; then
44-
echo "Error: Result of addition differs from reference file" >&2
44+
echo "Error: Result of combination differs from reference file" >&2
4545
exit 1
4646
fi

test/lcov_diff/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include ../common.mak
2+
3+
test:
4+
test_run lcov_diff_apply ./diff_test
5+
6+
clean:
7+
make -C old clean
8+
make -C new clean
9+
rm -f *.info diff

test/lcov_diff/diff_test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
#
3+
# Copyright IBM Corp. 2017
4+
#
5+
# Usage: diff_test
6+
#
7+
# Check lcov's diff function:
8+
# - Compile two slightly different test programs
9+
# - Run the programs and collect coverage data
10+
# - Generate a patch containing the difference between the source code
11+
# - Apply the patch to the coverage data
12+
# - Compare the resulting patched coverage data file with the data from the
13+
# patched source file
14+
#
15+
16+
function die()
17+
{
18+
echo "Error: $@" >&2
19+
exit 1
20+
}
21+
22+
make -C old || die "Failed to compile old source"
23+
make -C new || die "Failed to compile new source"
24+
diff -u $PWD/old/prog.c $PWD/new/prog.c > diff
25+
26+
$LCOV --diff old/prog.info diff --convert-filenames -o patched.info -t bla || \
27+
die "Failed to apply patch to coverage data file"
28+
norminfo new/prog.info > new_normalized.info
29+
norminfo patched.info > patched_normalized.info
30+
sed -i -e 's/^TN:.*$/TN:/' patched_normalized.info
31+
32+
diff -u patched_normalized.info new_normalized.info || \
33+
die "Mismatch in patched coverage data file"
34+
35+
echo "Patched coverage data file matches expected file"

test/lcov_diff/new/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
prog.info:
2+
3+
include ../../common.mak
4+
5+
prog.info: prog.gcda
6+
$(LCOV) -c -d . -o prog.info
7+
8+
prog.gcda: prog
9+
./prog || true
10+
11+
prog: prog.c
12+
$(CC) prog.c -o prog --coverage
13+
14+
clean:
15+
rm -f prog prog.gcda prog.gcno prog.info
16+
17+
.PHONY: all clean

test/lcov_diff/new/prog.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
4+
int fn(int x)
5+
{
6+
switch (x) {
7+
case -1: return 0;
8+
9+
10+
case 0: return 2;
11+
case 2: return 3;
12+
13+
14+
case 12: return 7;
15+
default: return 255;
16+
}
17+
18+
19+
20+
}
21+
22+
int fn2()
23+
{
24+
25+
26+
return 7;
27+
}
28+
29+
30+
31+
int main(int argc, char *argv[])
32+
{
33+
34+
35+
if (argc > 1)
36+
return fn(argc);
37+
38+
return fn2();
39+
40+
41+
}

0 commit comments

Comments
 (0)