summaryrefslogtreecommitdiff
path: root/test-suite/tests/poe.test
blob: 6c76256023aeca5e374a488a94cce61ba783ac98 (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
;;;; poe.test --- exercise ice-9/poe.scm      -*- scheme -*-
;;;;
;;;; Copyright 2003, 2006 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 2.1 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-suite test-ice-9-poe)
  #:use-module (test-suite lib)
  #:use-module (ice-9 poe))


;;
;; pure-funcq
;;

(with-test-prefix "pure-funcq"

  (with-test-prefix "no args"
    (define obj (vector 123))  ;; not gc'ed
    (define called #f)
    (define (foo)
      (set! called #t)
      obj)

    (let ((func (pure-funcq foo)))

      (pass-if "called first"
	(set! called #f)
	(and (eq? obj (func))
	     called))

      (pass-if "not called second"
	(set! called #f)
	(and (eq? obj (func))
	     (not called)))))

  (with-test-prefix "1 arg"
    (define obj1 (vector 123))  ;; not gc'ed
    (define obj2 (vector 456))  ;; not gc'ed
    (define called #f)
    (define (foo sym)
      (set! called #t)
      (case sym
	((x) obj1)
	((y) obj2)
	(else (error "oops"))))

    (let ((func (pure-funcq foo)))

      (pass-if "called first x"
	(set! called #f)
	(and (eq? obj1 (func 'x))
	     called))

      (pass-if "not called second x"
	(set! called #f)
	(and (eq? obj1 (func 'x))
	     (not called)))

      (pass-if "called first y"
	(set! called #f)
	(and (eq? obj2 (func 'y))
	     called))

      (pass-if "not called second y"
	(set! called #f)
	(and (eq? obj2 (func 'y))
	     (not called)))

      (pass-if "not called third x"
	(set! called #f)
	(and (eq? obj1 (func 'x))
	     (not called))))))

;;
;; perfect-funcq
;;

(with-test-prefix "perfect-funcq"
  
  (with-test-prefix "no args"
    (define called #f)
    (define (foo)
      (set! called #t)
      'foo)
    
    (let ((func (perfect-funcq 31 foo)))
      
      (pass-if "called first"
	(set! called #f)
	(and (eq? 'foo (func))
	     called))
      
      (pass-if "not called second"
	(set! called #f)
	(and (eq? 'foo (func))
	     (not called)))))
  
  (with-test-prefix "1 arg"
    (define called #f)
    (define (foo str)
      (set! called #t)
      (string->number str))
    
    (let ((func (perfect-funcq 31 foo)))
      (define s1 "123")
      (define s2 "123")
      
      (pass-if "called first s1"
	(set! called #f)
	(and (= 123 (func s1))
	     called))
      
      (pass-if "not called second s1"
	(set! called #f)
	(and (= 123 (func s1))
	     (not called)))
      
      (pass-if "called first s2"
	(set! called #f)
	(and (= 123 (func s2))
	     called))
      
      (pass-if "not called second s2"
	(set! called #f)
	(and (= 123 (func s2))
	     (not called))))))