1 ;;;; coverage.test --- Code coverage. -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2010, 2011, 2012, 2013, 2014 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 (pass-if "several times"
92 (let ((proc (code "fooz.scm" "(lambda (x) ;; 0
93 (format #f \"hello\") ;; 1
94 (let loop ((x x)) ;; 2
100 ((< x 0) 'never))))")))
101 (let-values (((data result)
103 (lambda () (proc 77)))))
104 (let ((counts (line-execution-counts data "fooz.scm")))
106 (every (lambda (line+count)
107 (let ((line (car line+count))
108 (count (cdr line+count)))
109 ;; The actual line counts for aliasing
110 ;; operations, like the loop header that
111 ;; initializes "x" to "x", are sensitive to
112 ;; whether there is an associated "mov"
113 ;; instruction, or whether the value is left
114 ;; in place. Currently there are no
115 ;; instructions for line 2, but we allow 1 as
121 ((4 5 6) (= count 77))
128 (let ((proc (code "baz.scm" "(lambda (x y) ;; 0
134 (let-values (((data result)
136 (lambda () (proc 1 2)))))
137 (let ((counts (line-execution-counts data "baz.scm")))
139 (every (lambda (line+count)
140 (let ((line (car line+count))
141 (count (cdr line+count)))
143 ((0 1 5) (= count 1))
145 ((4) #t) ;; the start of the `else' branch is
146 ;; attributed to line 4
150 (pass-if "one proc hit, one proc unused"
151 (let ((proc (code "baz.scm" "(letrec ((even? (lambda (x) ;; 0
153 (not (odd? (1- x))))))
154 (odd? (lambda (x) ;; 3
155 (not (even? (1- x)))))) ;; 4
157 (let-values (((data result)
159 (lambda () (proc 0)))))
160 (let ((counts (line-execution-counts data "baz.scm")))
162 (every (lambda (line+count)
163 (let ((line (car line+count))
164 (count (cdr line+count)))
167 ((2 3 4 5) (= count 0))
171 (pass-if "case-lambda"
172 (let ((proc (code "cl.scm" "(case-lambda ;; 0
174 ((x y) (+ x y))) ;; 2")))
175 (let-values (((data result)
178 (+ (proc 1) (proc 2 3))))))
179 (let ((counts (line-execution-counts data "cl.scm")))
181 (lset= equal? '((0 . 2) (1 . 1) (2 . 1)) counts))))))
183 (pass-if "all code on one line"
184 ;; There are several proc/IP pairs pointing to this source line, yet the hit
185 ;; count for the line should be 1.
186 (let ((proc (code "one-liner.scm"
187 "(lambda (x y) (+ x y (* x y) (if (> x y) 1 2) (quotient y x)))")))
188 (let-values (((data result)
190 (lambda () (proc 451 1884)))))
191 (let ((counts (line-execution-counts data "one-liner.scm")))
192 (equal? counts '((0 . 1))))))))
195 (with-test-prefix "procedure-execution-count"
197 (pass-if "several times"
198 (let ((proc (code "foo.scm" "(lambda (x y) x)")))
199 (let-values (((data result)
201 (lambda () (+ (proc 1 2) (proc 2 3))))))
202 (and (coverage-data? data)
204 (= (procedure-execution-count data proc) 2)))))
206 (pass-if "case-lambda"
207 (let ((proc (code "foo.scm" "(case-lambda ((x) x) ((x y) (+ x y)))")))
208 (let-values (((data result)
211 (+ (proc 1) (proc 2 3))))))
212 (and (coverage-data? data)
214 (= (procedure-execution-count data proc) 2)))))
217 (let ((proc (code "foo.scm" "(lambda (x y) x)")))
218 (let-values (((data result)
220 (lambda () (+ 1 2)))))
221 (and (coverage-data? data)
223 (zero? (procedure-execution-count data proc))))))
225 (pass-if "applicable struct"
226 (let* ((<box> (make-struct <applicable-struct-vtable> 0 'pw))
227 (proc (lambda args (length args)))
228 (b (make-struct <box> 0 proc)))
229 (let-values (((data result)
230 (with-code-coverage b)))
231 (and (coverage-data? data)
233 (= (procedure-execution-count data proc) 1)))))
235 (pass-if "called from C"
236 ;; The `scm_call_N' functions use the VM returned by `the-vm'. This
237 ;; test makes sure that their calls are traced.
238 (let ((proc (code "foo.scm" "(lambda (x y) (+ x y))"))
239 (call (false-if-exception ; can we resolve `scm_call_2'?
240 (pointer->procedure '*
241 (dynamic-func "scm_call_2"
245 (let-values (((data result)
248 (call (make-pointer (object-address proc))
249 (make-pointer (object-address 1))
250 (make-pointer (object-address 2)))))))
251 (and (coverage-data? data)
252 (= (object-address 3) (pointer-address result))
253 (= (procedure-execution-count data proc) 1)))
254 (throw 'unresolved))))
256 (pass-if "called from eval"
257 (let-values (((data result)
260 (eval '(test-procedure 123) (current-module))))))
261 (and (coverage-data? data)
262 (= (test-procedure 123) result)
263 (= (procedure-execution-count data test-procedure) 1)))))
266 (with-test-prefix "instrumented-source-files"
268 (pass-if "source files are listed as expected"
269 (let ((proc (code "chbouib.scm" "(lambda (x y) x)")))
270 (let-values (((data result)
272 (lambda () (proc 1 2)))))
274 (let ((files (map basename (instrumented-source-files data))))
275 (and (member "boot-9.scm" files)
276 (member "chbouib.scm" files)