summaryrefslogtreecommitdiff
path: root/test-suite/tests/coverage.test
blob: 5f393b6b0aea6d0b79795e69ef5734a78d9984b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
;;;; coverage.test --- Code coverage.    -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2017 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-coverage)
  #:use-module (test-suite lib)
  #:use-module (system vm coverage)
  #:use-module (system vm vm)
  #:use-module (system base compile)
  #:use-module (system foreign)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-11))

(define-syntax code
  (syntax-rules ()
    ((_ filename snippet)
     (let ((input (open-input-string snippet)))
       (set-port-filename! input filename)
       (read-enable 'positions)
       (compile (read input))))))

(define test-procedure
  (compile '(lambda (x)
              (if (> x 2)
                  (- x 2)
                  (+ x 2)))))


(with-test-prefix "instrumented/executed-lines"

  (pass-if "instr = exec"
    (let ((proc (code "foo.scm" "(lambda (x y)  ;; 0
                                   (+ x y))     ;; 1")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 1 2)))))
        (and (coverage-data? data)
             (= 3 result)
             (let-values (((instr exec)
                           (instrumented/executed-lines data "foo.scm")))
               (and (= 2 instr) (= 2 exec)))))))

  (pass-if "instr >= exec"
    (let ((proc (code "foo.scm" "(lambda (x y)       ;; 0
                                   (if (> x y)       ;; 1
                                       (begin        ;; 2
                                         (display x) ;; 3
                                         (+ x y))))  ;; 4")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 1 2)))))
        (and (coverage-data? data)
             (let-values (((instr exec)
                           (instrumented/executed-lines data "foo.scm")))
               (and (> instr 0) (>= instr exec))))))))


(with-test-prefix "line-execution-counts"

  (pass-if "once"
    (let ((proc (code "bar.scm" "(lambda (x y)   ;; 0
                                   (+ (/ x y)    ;; 1
                                      (* x y)))  ;; 2")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 1 2)))))
        (let ((counts (line-execution-counts data "bar.scm")))
          (and (pair? counts)
               (every (lambda (line+count)
                        (let ((line  (car line+count))
                              (count (cdr line+count)))
                          (and (>= line 0)
                               (<= line 2)
                               (= count 1))))
                      counts))))))

  ;; Unhappily, lack of source location on identifiers combined with a
  ;; block reordering change makes this test fail.  The right solution
  ;; is to fix the compiler, but really it should happen by fixing
  ;; psyntax to have source location info for identifiers and immediate
  ;; values.
  (expect-fail "several times"
    (let ((proc (code "fooz.scm" "(lambda (x)                   ;; 0
                                    (format #f \"hello\")       ;; 1
                                    (let loop ((x x))           ;; 2
                                      (cond ((> x 0)            ;; 3
                                             (begin             ;; 4
                                               (format #f \"~a\" x)
                                               (loop (1- x))))  ;; 6
                                            ((= x 0) #t)        ;; 7
                                            ((< x 0) 'never))))")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 77)))))
        (let ((counts (line-execution-counts data "fooz.scm")))
          (and (pair? counts)
               (every (lambda (line+count)
                        (let ((line  (car line+count))
                              (count (cdr line+count)))
                          ;; The actual line counts for aliasing
                          ;; operations, like the loop header that
                          ;; initializes "x" to "x", are sensitive to
                          ;; whether there is an associated "mov"
                          ;; instruction, or whether the value is left
                          ;; in place.  Currently there are no
                          ;; instructions for line 2, but we allow 1 as
                          ;; well.
                          (case line
                            ((0 1)   (= count 1))
                            ((2)     (<= 0 count 1))
                            ((3)     (= count 78))
                            ((4 5 6) (= count 77))
                            ((7)     (= count 1))
                            ((8)     (= count 0))
                            (else    #f))))
                      counts))))))

  (pass-if "some"
    (let ((proc (code "baz.scm" "(lambda (x y)       ;; 0
                                   (if (> x y)       ;; 1
                                       (begin        ;; 2
                                         (display x) ;; 3
                                         (+ x y))    ;; 4
                                       (+ x y)))     ;; 5")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 1 2)))))
        (let ((counts (line-execution-counts data "baz.scm")))
          (and (pair? counts)
               (every (lambda (line+count)
                        (let ((line  (car line+count))
                              (count (cdr line+count)))
                          (case line
                            ((0 1 5) (= count 1))
                            ((2 3)   (= count 0))
                            ((4)     #t) ;; the start of the `else' branch is
                                         ;; attributed to line 4
                            (else    #f))))
                      counts))))))

  ;; Same unfortunate caveat as above: block ordering and source
  ;; locations :(
  (expect-fail "one proc hit, one proc unused"
    (let ((proc (code "baz.scm" "(letrec ((even? (lambda (x)               ;; 0
                                                   (or (= x 0)             ;; 1
                                                       (not (odd? (1- x))))))
                                          (odd?  (lambda (x)               ;; 3
                                                   (not (even? (1- x)))))) ;; 4
                                   even?)")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 0)))))
        (let ((counts (line-execution-counts data "baz.scm")))
          (and (pair? counts)
               (every (lambda (line+count)
                        (let ((line  (car line+count))
                              (count (cdr line+count)))
                          (case line
                            ((0 1)     (= count 1))
                            ((2 3 4 5) (= count 0))
                            (else      #f))))
                      counts))))))

  (pass-if "case-lambda"
    (let ((proc (code "cl.scm"  "(case-lambda       ;; 0
                                   ((x)   (+ x 3))  ;; 1
                                   ((x y) (+ x y))) ;; 2")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda ()
                        (+ (proc 1) (proc 2 3))))))
        (let ((counts (line-execution-counts data "cl.scm")))
          (and (pair? counts)
               (lset= equal? '((0 . 2) (1 . 1) (2 . 1)) counts))))))

  (pass-if "all code on one line"
    ;; There are several proc/IP pairs pointing to this source line, yet the hit
    ;; count for the line should be 1.
    (let ((proc (code "one-liner.scm"
            "(lambda (x y) (+ x y (* x y) (if (> x y) 1 2) (quotient y x)))")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 451 1884)))))
        (let ((counts (line-execution-counts data "one-liner.scm")))
          (equal? counts '((0 . 1)))))))

  (pass-if "tail calls"
    (let ((proc (code "tail-calls.scm"
                      "(begin
                         (define (tail-call-test)
                           (display \"foo\\n\")
                           (tail-call-target))

                         (define (tail-call-target)
                           (display \"bar\\n\"))

                         tail-call-test)")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (with-output-to-string proc)))))
        (let ((counts (line-execution-counts data "tail-calls.scm")))
          (define (lset-contains? eq? a b)
            (lset= eq? b (lset-intersection eq? a b)))
          ;; Due to top-level binding optimization, the target may be
          ;; inlined or into the caller.  All we can say is that the
          ;; entry was seen, and the two displays were called.
          (lset-contains? equal? counts '((1 . 1) (2 . 1) (6 . 1))))))))


(with-test-prefix "procedure-execution-count"

  (pass-if "several times"
    (let ((proc (code "foo.scm" "(lambda (x y) x)")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (+ (proc 1 2) (proc 2 3))))))
        (and (coverage-data? data)
             (= 3 result)
             (= (procedure-execution-count data proc) 2)))))

  (pass-if "case-lambda"
    (let ((proc (code "foo.scm" "(case-lambda ((x) x) ((x y) (+ x y)))")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda ()
                        (+ (proc 1) (proc 2 3))))))
        (and (coverage-data? data)
             (= 6 result)
             (= (procedure-execution-count data proc) 2)))))

  (pass-if "never"
    (let ((proc (code "foo.scm" "(lambda (x y) x)")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (+ 1 2)))))
        (and (coverage-data? data)
             (= 3 result)
             (zero? (procedure-execution-count data proc))))))

  (pass-if "applicable struct"
    (let* ((<box> (make-struct/no-tail <applicable-struct-vtable> 'pw))
           (proc  (lambda args (length args)))
           (b     (make-struct/no-tail <box> proc)))
      (let-values (((data result)
                    (with-code-coverage b)))
        (and (coverage-data? data)
             (= 0 result)
             (= (procedure-execution-count data proc) 1)))))

  (pass-if "called from C"
    ;; The `scm_call_N' functions use the VM returned by `the-vm'.  This
    ;; test makes sure that their calls are traced.
    (let ((proc (code "foo.scm" "(lambda (x y) (+ x y))"))
          (call (false-if-exception       ; can we resolve `scm_call_2'?
                 (pointer->procedure '*
                                     (dynamic-func "scm_call_2"
                                                   (dynamic-link))
                                     '(* * *)))))
      (if call
          (let-values (((data result)
                        (with-code-coverage
                          (lambda ()
                            (call (make-pointer (object-address proc))
                                  (make-pointer (object-address 1))
                                  (make-pointer (object-address 2)))))))
            (and (coverage-data? data)
                 (= (object-address 3) (pointer-address result))
                 (= (procedure-execution-count data proc) 1)))
          (throw 'unresolved))))

  (pass-if "called from eval"
    (let-values (((data result)
                  (with-code-coverage
                    (lambda ()
                      (eval '(test-procedure 123) (current-module))))))
      (and (coverage-data? data)
           (= (test-procedure 123) result)
           (= (procedure-execution-count data test-procedure) 1)))))


(with-test-prefix "instrumented-source-files"

  (pass-if "source files are listed as expected"
    (let ((proc (code "chbouib.scm" "(lambda (x y) x)")))
      (let-values (((data result)
                    (with-code-coverage
                      (lambda () (proc 1 2)))))

        (let ((files (map basename (instrumented-source-files data))))
          (and (member "boot-9.scm" files)
               (member "chbouib.scm" files)
               #t))))))