File: search.basic.scm

package info (click to toggle)
guile-lib 0.2.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,868 kB
  • sloc: lisp: 6,986; sh: 3,962; makefile: 195
file content (87 lines) | stat: -rw-r--r-- 3,335 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
;;; ----------------------------------------------------------------------
;;;    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 (search basic)
             (unit-test)
             (oop goops))

(define-class <test-dfs> (<test-case>))

(define-method (test-number-search (self <test-dfs>))
  (let ((finish?  (lambda (numlst) (= (car numlst) 25)))
        (expander (lambda (numlst) (if (> (car numlst) 50)
                                    '()
                                    (list (cons (* (car numlst) 2) 
                                                numlst)
                                          (cons (- (car numlst) 1)
                                                numlst))))))
  (assert-equal '(25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 20 10)
                (depth-first-search (list 10) finish? expander))
  (assert-equal '(25 26 13 14 7 8 9 10)
                (breadth-first-search (list 10) finish? expander))))

(define-class <test-binary-search> (<test-case>)
  (lst-1-to-100 #:accessor lst-1-to-100))

(define-method (set-up-test (self <test-binary-search>))
  (set! (lst-1-to-100 self)
        (let loop ((ans '())
                   (num 100))
          (if (< num 0)
              ans
              (loop (cons num ans) (- num 1))))))
 
(define-method (test-binary-edges  (self <test-binary-search>))
  ;; no entries...
  (assert-equal #f
                (binary-search-sorted-vector #() 3))

  ;; single entry...
  (assert-equal 0
                (binary-search-sorted-vector #(33) 33))
  (assert-equal #f
                (binary-search-sorted-vector #(33) 35))
  (assert-equal #f
                (binary-search-sorted-vector #(33) 32))

  ;; last entry
  (assert-equal 2
                (binary-search-sorted-vector #(33 35 45) 45))

  ;; middle entry
  (assert-equal 1
                (binary-search-sorted-vector #(33 35 45) 35))

  ;; first entry
  (assert-equal 0
                (binary-search-sorted-vector #(33 35 45) 33)))

(define-method (test-binary-results (self <test-binary-search>))
  ;; test single case
  (assert-equal 5
                (binary-search-sorted-vector (list->vector (lst-1-to-100 self)) 5))

  ;; test across an entire vector
  (assert-equal (lst-1-to-100 self)
                (map (lambda (n) (binary-search-sorted-vector (list->vector (lst-1-to-100 self)) n))
                     (lst-1-to-100 self))))

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

;;; arch-tag: 45377a62-02e9-4708-a8c5-34d6c6f8b606