File: math.primes.scm

package info (click to toggle)
guile-lib 0.2.7-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,904 kB
  • sloc: lisp: 7,014; sh: 3,986; makefile: 191
file content (94 lines) | stat: -rw-r--r-- 3,564 bytes parent folder | download | duplicates (8)
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
;;; ----------------------------------------------------------------------
;;;    unit test
;;;    Copyright (C) 2003 Richard Todd
;;;
;;;    This program is free software; you can redistribute it and/or modify
;;;    it under the terms of the GNU General Public License as published by
;;;    the Free Software Foundation; either version 2 of the License, or
;;;    (at your option) any later version.
;;;
;;;    This program 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 General Public License for more details.
;;;
;;;    You should have received a copy of the GNU General Public License
;;;    along with this program; if not, write to the Free Software
;;;    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;;; ----------------------------------------------------------------------
(use-modules (math primes)
             (unit-test)
             (oop goops))


;; **********************************************************************
;; Test of factorization 
;; **********************************************************************
(define-class <test-factorization> (<test-case>))

(define-method (test-factor-function (tc <test-factorization>))
  (for-each
   (lambda (lst)
     (assert-equal (sort lst                    <)
                   (sort (factor (apply * lst)) <)))
   '((2) 
     (2 3) 
     (2 3 23)
     (7 13 29 43))))

;; **********************************************************************
;; Test of primes listings... 
;; **********************************************************************
(define-class <test-prime-lists> (<test-case>)
  (first-hundred #:accessor first100))

(define-method (set-up-test (tpl <test-prime-lists>))
  (set! (first100 tpl)
        (list 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 
              53 59 61 67 71 73 79 83 89 97 101 103 107 
              109 113 127 131 137 139 149 151 157 163 
              167 173 179 181 191 193 197 199 211 223 
              227 229 233 239 241 251 257 263 269 271 
              277 281 283 293 307 311 313 317 331 337 
              347 349 353 359 367 373 379 383 389 397 
              401 409 419 421 431 433 439 443 449 457 
              461 463 467 479 487 491 499 503 509 521 
              523 541)))

(define-method (test-recognizes-first100 (tpl <test-prime-lists>))
  (for-each (lambda (n)
              (assert-true (prime? n)))
            (first100 tpl)))

(define-method (test-prime> (tpl <test-prime-lists>))
  (let loop ((lst (first100 tpl)))
    (if (>= (length lst) 2)
        (begin
          (assert-equal (cadr lst)
                        (prime> (car lst)))
          (loop (cdr lst))))))

(define-method (test-prime< (tpl <test-prime-lists>))
  (let loop ((lst (first100 tpl)))
    (if (>= (length lst) 2)
        (begin
          (assert-equal (car lst)
                        (prime< (cadr lst)))
          (loop (cdr lst)))))
  (assert-equal #f
                (prime< 2)))

(define-method (test-primes> (tpl <test-prime-lists>))
  (assert-equal (first100 tpl)
                (primes> 0 100)))

(define-method (test-primes< (tpl <test-prime-lists>))
  (assert-equal (first100 tpl)
                (primes< 542 100))
  ;; it returns a shorter list if you ask for more than there are...
  (assert-equal 1
                (length (primes< 3 100))))

(exit-with-summary (run-all-defined-test-cases))

;;; arch-tag: 6014a90f-7104-4807-815a-17325809b3ad