1 ;;;; coverage.test --- Code coverage. -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-coverage)
20 #:use-module (test-suite lib)
21 #:use-module (system vm coverage)
22 #:use-module (system vm vm)
23 #:use-module (system base compile)
24 #:use-module (system foreign)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-11))
31 (let ((input (open-input-string snippet)))
32 (set-port-filename! input filename)
33 (read-enable 'positions)
34 (compile (read input))))))
36 (define test-procedure
43 (with-test-prefix "instrumented/executed-lines"
45 (pass-if "instr = exec"
46 (let ((proc (code "foo.scm" "(lambda (x y) ;; 0
48 (let-values (((data result)
50 (lambda () (proc 1 2)))))
51 (and (coverage-data? data)
53 (let-values (((instr exec)
54 (instrumented/executed-lines data "foo.scm")))
55 (and (= 2 instr) (= 2 exec)))))))
57 (pass-if "instr >= exec"
58 (let ((proc (code "foo.scm" "(lambda (x y) ;; 0
63 (let-values (((data result)
65 (lambda () (proc 1 2)))))
66 (and (coverage-data? data)
67 (let-values (((instr exec)
68 (instrumented/executed-lines data "foo.scm")))
69 (and (> instr 0) (>= instr exec))))))))
72 (with-test-prefix "line-execution-counts"
75 (let ((proc (code "bar.scm" "(lambda (x y) ;; 0
78 (let-values (((data result)
80 (lambda () (proc 1 2)))))
81 (let ((counts (line-execution-counts data "bar.scm")))
83 (every (lambda (line+count)
84 (let ((line (car line+count))
85 (count (cdr line+count)))
91 ;; Unhappily, lack of source location on identifiers combined with a
92 ;; block reordering change makes this test fail. The right solution
93 ;; is to fix the compiler, but really it should happen by fixing
94 ;; psyntax to have source location info for identifiers and immediate
96 (expect-fail "several times"
97 (let ((proc (code "fooz.scm" "(lambda (x) ;; 0
98 (format #f \"hello\") ;; 1
99 (let loop ((x x)) ;; 2
105 ((< x 0) 'never))))")))
106 (let-values (((data result)
108 (lambda () (proc 77)))))
109 (let ((counts (line-execution-counts data "fooz.scm")))
111 (every (lambda (line+count)
112 (let ((line (car line+count))
113 (count (cdr line+count)))
114 ;; The actual line counts for aliasing
115 ;; operations, like the loop header that
116 ;; initializes "x" to "x", are sensitive to
117 ;; whether there is an associated "mov"
118 ;; instruction, or whether the value is left
119 ;; in place. Currently there are no
120 ;; instructions for line 2, but we allow 1 as
126 ((4 5 6) (= count 77))
133 (let ((proc (code "baz.scm" "(lambda (x y) ;; 0
139 (let-values (((data result)
141 (lambda () (proc 1 2)))))
142 (let ((counts (line-execution-counts data "baz.scm")))
144 (every (lambda (line+count)
145 (let ((line (car line+count))
146 (count (cdr line+count)))
148 ((0 1 5) (= count 1))
150 ((4) #t) ;; the start of the `else' branch is
151 ;; attributed to line 4
155 ;; Same unfortunate caveat as above: block ordering and source
157 (expect-fail "one proc hit, one proc unused"
158 (let ((proc (code "baz.scm" "(letrec ((even? (lambda (x) ;; 0
160 (not (odd? (1- x))))))
161 (odd? (lambda (x) ;; 3
162 (not (even? (1- x)))))) ;; 4
164 (let-values (((data result)
166 (lambda () (proc 0)))))
167 (let ((counts (line-execution-counts data "baz.scm")))
169 (every (lambda (line+count)
170 (let ((line (car line+count))
171 (count (cdr line+count)))
174 ((2 3 4 5) (= count 0))
178 (pass-if "case-lambda"
179 (let ((proc (code "cl.scm" "(case-lambda ;; 0
181 ((x y) (+ x y))) ;; 2")))
182 (let-values (((data result)
185 (+ (proc 1) (proc 2 3))))))
186 (let ((counts (line-execution-counts data "cl.scm")))
188 (lset= equal? '((0 . 2) (1 . 1) (2 . 1)) counts))))))
190 (pass-if "all code on one line"
191 ;; There are several proc/IP pairs pointing to this source line, yet the hit
192 ;; count for the line should be 1.
193 (let ((proc (code "one-liner.scm"
194 "(lambda (x y) (+ x y (* x y) (if (> x y) 1 2) (quotient y x)))")))
195 (let-values (((data result)
197 (lambda () (proc 451 1884)))))
198 (let ((counts (line-execution-counts data "one-liner.scm")))
199 (equal? counts '((0 . 1))))))))
202 (with-test-prefix "procedure-execution-count"
204 (pass-if "several times"
205 (let ((proc (code "foo.scm" "(lambda (x y) x)")))
206 (let-values (((data result)
208 (lambda () (+ (proc 1 2) (proc 2 3))))))
209 (and (coverage-data? data)
211 (= (procedure-execution-count data proc) 2)))))
213 (pass-if "case-lambda"
214 (let ((proc (code "foo.scm" "(case-lambda ((x) x) ((x y) (+ x y)))")))
215 (let-values (((data result)
218 (+ (proc 1) (proc 2 3))))))
219 (and (coverage-data? data)
221 (= (procedure-execution-count data proc) 2)))))
224 (let ((proc (code "foo.scm" "(lambda (x y) x)")))
225 (let-values (((data result)
227 (lambda () (+ 1 2)))))
228 (and (coverage-data? data)
230 (zero? (procedure-execution-count data proc))))))
232 (pass-if "applicable struct"
233 (let* ((<box> (make-struct <applicable-struct-vtable> 0 'pw))
234 (proc (lambda args (length args)))
235 (b (make-struct <box> 0 proc)))
236 (let-values (((data result)
237 (with-code-coverage b)))
238 (and (coverage-data? data)
240 (= (procedure-execution-count data proc) 1)))))
242 (pass-if "called from C"
243 ;; The `scm_call_N' functions use the VM returned by `the-vm'. This
244 ;; test makes sure that their calls are traced.
245 (let ((proc (code "foo.scm" "(lambda (x y) (+ x y))"))
246 (call (false-if-exception ; can we resolve `scm_call_2'?
247 (pointer->procedure '*
248 (dynamic-func "scm_call_2"
252 (let-values (((data result)
255 (call (make-pointer (object-address proc))
256 (make-pointer (object-address 1))
257 (make-pointer (object-address 2)))))))
258 (and (coverage-data? data)
259 (= (object-address 3) (pointer-address result))
260 (= (procedure-execution-count data proc) 1)))
261 (throw 'unresolved))))
263 (pass-if "called from eval"
264 (let-values (((data result)
267 (eval '(test-procedure 123) (current-module))))))
268 (and (coverage-data? data)
269 (= (test-procedure 123) result)
270 (= (procedure-execution-count data test-procedure) 1)))))
273 (with-test-prefix "instrumented-source-files"
275 (pass-if "source files are listed as expected"
276 (let ((proc (code "chbouib.scm" "(lambda (x y) x)")))
277 (let-values (((data result)
279 (lambda () (proc 1 2)))))
281 (let ((files (map basename (instrumented-source-files data))))
282 (and (member "boot-9.scm" files)
283 (member "chbouib.scm" files)