test-records.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Record type tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-records")
  22. (test-call "42" (lambda ()
  23. (define-record-type q (make-q a) q? (a q-a))
  24. (q-a (make-q 42))))
  25. (test-call "42" (lambda ()
  26. (define-record-type q (make-q a) q? (a q-a set-q-a!))
  27. (define x (make-q 10))
  28. (set-q-a! x 42)
  29. (q-a x)))
  30. (test-call "42" (lambda (args)
  31. (match args
  32. ((make-q q-a set-q-a!)
  33. (define x (make-q 10))
  34. (set-q-a! x 42)
  35. (q-a x))))
  36. (let ()
  37. (define-record-type q (make-q a) q? (a q-a set-q-a!))
  38. (list make-q q-a set-q-a!)))
  39. (test-call "42" (lambda (args)
  40. (match args
  41. ((make-q q-a set-q-a!)
  42. (define x (make-q 10))
  43. (set-q-a! x 42)
  44. (q-a x))))
  45. (let ()
  46. (define-record-type q (make-q b a) q? (b q-b set-q-b!) (a q-a set-q-a!))
  47. (list (lambda (a) (make-q #f a)) q-a set-q-a!)))
  48. (test-call "\"#<q>\"" (lambda ()
  49. (define-record-type q (make-q) q?)
  50. (define (call-with-output-string f)
  51. (let ((p (open-output-string)))
  52. (f p)
  53. (get-output-string p)))
  54. (call-with-output-string
  55. (lambda (port)
  56. (write (make-q) port)))))
  57. (test-call "\"#<q a: 42>\"" (lambda ()
  58. (define-record-type q (make-q a) q? (a q-a))
  59. (define (call-with-output-string f)
  60. (let ((p (open-output-string)))
  61. (f p)
  62. (get-output-string p)))
  63. (call-with-output-string
  64. (lambda (port)
  65. (write (make-q 42) port)))))
  66. (with-additional-imports ((prefix (hoot records) hoot:))
  67. (test-call "\"#<q>\"" (lambda ()
  68. (hoot:define-record-type q
  69. #:opaque? #t
  70. (make-q a) q? (a q-a))
  71. (define (call-with-output-string f)
  72. (let ((p (open-output-string)))
  73. (f p)
  74. (get-output-string p)))
  75. (call-with-output-string
  76. (lambda (port)
  77. (write (make-q 42) port))))))
  78. (test-call "\"#<big a: 1 b: 2 c: 3 d: 4 e: 5 f: 6 g: 7 h: 8 i: 9 j: 10>\""
  79. (lambda ()
  80. (define-record-type big
  81. (make-big a b c d e f g h i j)
  82. big?
  83. (a big-a)
  84. (b big-b)
  85. (c big-c)
  86. (d big-d)
  87. (e big-e)
  88. (f big-f)
  89. (g big-g)
  90. (h big-h)
  91. (i big-i)
  92. (j big-j))
  93. (define (call-with-output-string f)
  94. (let ((p (open-output-string)))
  95. (f p)
  96. (get-output-string p)))
  97. (call-with-output-string
  98. (lambda (port)
  99. (write (make-big 1 2 3 4 5 6 7 8 9 10) port)))))
  100. (with-additional-imports ((prefix (hoot records) hoot:))
  101. (test-call "\"#<marmot 42>\""
  102. (lambda ()
  103. (hoot:define-record-type q
  104. #:printer (lambda (x port)
  105. (write-string "#<marmot " port)
  106. (write (q-a x) port)
  107. (write-string ">" port))
  108. (make-q a)
  109. q?
  110. (a q-a))
  111. (define (call-with-output-string f)
  112. (let ((p (open-output-string)))
  113. (f p)
  114. (get-output-string p)))
  115. (call-with-output-string
  116. (lambda (port)
  117. (write (make-q 42) port))))))
  118. (test-call "#t"
  119. (lambda ()
  120. (define-record-type q (make-q a) q? (a q-a))
  121. (let ((a (make-q 42))
  122. (b (make-q 42))
  123. (c (make-q 69)))
  124. (and (eq? a a) (eq? b b) (eq? c c)
  125. (eqv? a a) (eqv? b b) (eqv? c c)
  126. (equal? a a) (equal? b b) (equal? c c)
  127. (not (eqv? a b))
  128. (not (eqv? b c))
  129. (not (eqv? a c))
  130. (equal? a b)
  131. (not (equal? b c))
  132. (not (equal? a c))))))
  133. (with-additional-imports ((prefix (hoot records) hoot:))
  134. (test-call "#t"
  135. (lambda ()
  136. (hoot:define-record-type q #:opaque? #t (make-q a) q? (a q-a))
  137. (let ((a (make-q 42))
  138. (b (make-q 42))
  139. (c (make-q 69)))
  140. (and (eq? a a) (eq? b b) (eq? c c)
  141. (eqv? a a) (eqv? b b) (eqv? c c)
  142. (equal? a a) (equal? b b) (equal? c c)
  143. (not (eqv? a b))
  144. (not (eqv? b c))
  145. (not (eqv? a c))
  146. (not (equal? a b))
  147. (not (equal? b c))
  148. (not (equal? a c))))))
  149. (test-call "#t"
  150. (lambda ()
  151. (hoot:define-record-type x #:extensible? #t (make-x a) x? (a x-a))
  152. (hoot:define-record-type y #:extensible? #t #:parent x (make-y a b) y? (b y-b))
  153. (hoot:define-record-type z #:parent y (make-z a b c) z? (c z-c))
  154. (let ((q (make-y 42 69))
  155. (r (make-z 42 69 420)))
  156. (and (x? q)
  157. (y? q)
  158. (not (z? q))
  159. (eq? (x-a q) 42)
  160. (eq? (y-b q) 69)
  161. (x? r)
  162. (y? r)
  163. (z? r)
  164. (eq? (x-a r) 42)
  165. (eq? (y-b r) 69)
  166. (eq? (z-c r) 420))))))
  167. (test-call "(#f 42)" (lambda ()
  168. (define-record-type <foo>
  169. (make-foo y)
  170. foo?
  171. (x foo-x)
  172. (y foo-y))
  173. (define foo (make-foo 42))
  174. (list (foo-x foo) (foo-y foo))))
  175. (with-additional-imports ((hoot applicable-structs)
  176. (prefix (hoot records) hoot:))
  177. (test-call "#t"
  178. (lambda ()
  179. (hoot:define-record-type <cool-procedure>
  180. #:parent <applicable-struct>
  181. (make-cool-procedure procedure)
  182. cool-procedure?)
  183. (procedure? (make-cool-procedure list))))
  184. (test-call "10"
  185. (lambda ()
  186. (hoot:define-record-type <counter>
  187. #:parent <applicable-struct>
  188. (%make-counter procedure count)
  189. counter?
  190. (count counter-count set-counter-count!))
  191. (define (make-counter)
  192. (define (next!)
  193. (let ((x (1+ (counter-count counter))))
  194. (set-counter-count! counter x)
  195. x))
  196. (define counter (%make-counter next! 0))
  197. counter)
  198. (define c (make-counter))
  199. (+ (c) (c) (c) (c)))))
  200. (test-end* "test-records")