test-records.scm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; Copyright (C) 2023 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. ;;; Numeric operation 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. (test-call "\"#<q>\"" (lambda ()
  67. (define-record-type q
  68. #:opaque? #t
  69. (make-q a) q? (a q-a))
  70. (define (call-with-output-string f)
  71. (let ((p (open-output-string)))
  72. (f p)
  73. (get-output-string p)))
  74. (call-with-output-string
  75. (lambda (port)
  76. (write (make-q 42) port)))))
  77. (test-call "\"#<big a: 1 b: 2 c: 3 d: 4 e: 5 f: 6 g: 7 h: 8 i: 9 j: 10>\""
  78. (lambda ()
  79. (define-record-type big
  80. (make-big a b c d e f g h i j)
  81. big?
  82. (a big-a)
  83. (b big-b)
  84. (c big-c)
  85. (d big-d)
  86. (e big-e)
  87. (f big-f)
  88. (g big-g)
  89. (h big-h)
  90. (i big-i)
  91. (j big-j))
  92. (define (call-with-output-string f)
  93. (let ((p (open-output-string)))
  94. (f p)
  95. (get-output-string p)))
  96. (call-with-output-string
  97. (lambda (port)
  98. (write (make-big 1 2 3 4 5 6 7 8 9 10) port)))))
  99. (test-call "\"#<marmot 42>\""
  100. (lambda ()
  101. (define-record-type q
  102. #:printer (lambda (x port)
  103. (write-string "#<marmot " port)
  104. (write (q-a x) port)
  105. (write-string ">" port))
  106. (make-q a)
  107. q?
  108. (a q-a))
  109. (define (call-with-output-string f)
  110. (let ((p (open-output-string)))
  111. (f p)
  112. (get-output-string p)))
  113. (call-with-output-string
  114. (lambda (port)
  115. (write (make-q 42) port)))))
  116. (test-call "#t"
  117. (lambda ()
  118. (define-record-type q (make-q a) q? (a q-a))
  119. (let ((a (make-q 42))
  120. (b (make-q 42))
  121. (c (make-q 69)))
  122. (and (eq? a a) (eq? b b) (eq? c c)
  123. (eqv? a a) (eqv? b b) (eqv? c c)
  124. (equal? a a) (equal? b b) (equal? c c)
  125. (not (eqv? a b))
  126. (not (eqv? b c))
  127. (not (eqv? a c))
  128. (equal? a b)
  129. (not (equal? b c))
  130. (not (equal? a c))))))
  131. (test-call "#t"
  132. (lambda ()
  133. (define-record-type q #:opaque? #t (make-q a) q? (a q-a))
  134. (let ((a (make-q 42))
  135. (b (make-q 42))
  136. (c (make-q 69)))
  137. (and (eq? a a) (eq? b b) (eq? c c)
  138. (eqv? a a) (eqv? b b) (eqv? c c)
  139. (equal? a a) (equal? b b) (equal? c c)
  140. (not (eqv? a b))
  141. (not (eqv? b c))
  142. (not (eqv? a c))
  143. (not (equal? a b))
  144. (not (equal? b c))
  145. (not (equal? a c))))))
  146. (test-call "#t"
  147. (lambda ()
  148. (define-record-type x #:extensible? #t (make-x a) x? (a x-a))
  149. (define-record-type y #:extensible? #t #:parent x (make-y a b) y? (b y-b))
  150. (define-record-type z #:parent y (make-z a b c) z? (c z-c))
  151. (let ((q (make-y 42 69))
  152. (r (make-z 42 69 420)))
  153. (and (x? q)
  154. (y? q)
  155. (not (z? q))
  156. (eq? (x-a q) 42)
  157. (eq? (y-b q) 69)
  158. (x? r)
  159. (y? r)
  160. (z? r)
  161. (eq? (x-a r) 42)
  162. (eq? (y-b r) 69)
  163. (eq? (z-c r) 420)))))
  164. (test-end* "test-records")