test-procedures.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ;;; Procedure tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-procedures")
  22. (test-call "()" (lambda args args))
  23. (test-call "(1)" (lambda args args) 1)
  24. (test-call "(1 2)" (lambda args args) 1 2)
  25. (test-call "(1 2 3)" (lambda args args) 1 2 3)
  26. (test-call "(1 2 3 4)" (lambda args args) 1 2 3 4)
  27. (test-call "(1 2 3 4 5)" (lambda args args) 1 2 3 4 5)
  28. (test-call "(1 2 3 4 5 6)" (lambda args args) 1 2 3 4 5 6)
  29. (test-call "(1 2 3 4 5 6 7)" (lambda args args) 1 2 3 4 5 6 7)
  30. (test-call "(1 2 3 4 5 6 7 8)" (lambda args args) 1 2 3 4 5 6 7 8)
  31. (test-call "(1 2 3 4 5 6 7 8 9)" (lambda args args) 1 2 3 4 5 6 7 8 9)
  32. (test-call "(1 2 3 4 5 6 7 8 9 10)" (lambda args args) 1 2 3 4 5 6 7 8 9 10)
  33. (test-call "(1 2)" (lambda (a . args) (cons* a args)) 1 2)
  34. (test-call "(1 2 3)" (lambda (a b . args) (cons* a b args)) 1 2 3)
  35. (test-call "(1 2 3 4)" (lambda (a b c . args) (cons* a b c args)) 1 2 3 4)
  36. (test-call "(1 2 3 4 5)" (lambda (a b c d . args) (cons* a b c d args)) 1 2 3 4 5)
  37. (test-call "(1 2 3 4 5 6 7 8 9 10 11 12)"
  38. (lambda (a b c d e f g h i j . args)
  39. (cons* a b c d e f g h i j args))
  40. 1 2 3 4 5 6 7 8 9 10 11 12)
  41. ;; inner call that grows argv
  42. (test-call "36"
  43. (lambda (f) (f 1 2 3 4 5 6 7 8))
  44. (lambda (a b c d e f g h) (+ a b c d e f g h)))
  45. ;; inner apply that grows argv
  46. (test-call "(1 2 3 4 5 6 7 8 9 10 11 12 13 14)"
  47. (lambda (args) (apply cons* args))
  48. (list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 '()))
  49. (test-call "#f" (lambda* (#:optional a) a))
  50. (test-call "(42 69)" (lambda* (#:optional (a 42) (b 69)) (list a b)))
  51. (test-call "(10 20)" (lambda* (#:optional (a 42) (b 69)) (list a b)) 10 20)
  52. (test-call "(1 #f ())" (lambda* (a #:optional b . rest) (list a b rest)) 1)
  53. (test-call "(1 2 3 4 5 6 7 8 9 10 11 12)"
  54. (lambda* (a b c d #:optional e f g h i j . args)
  55. (cons* a b c d e f g h i j args))
  56. 1 2 3 4 5 6 7 8 9 10 11 12)
  57. (test-call "(1 2 3 4 5 6 #f #f #f #f)"
  58. (lambda* (a b c d #:optional e f g h i j . args)
  59. (cons* a b c d e f g h i j args))
  60. 1 2 3 4 5 6)
  61. (test-call "20" (lambda (f . args) (apply f args)) (lambda (x y) (+ x y)) 12 8)
  62. (test-call "12\n8" (lambda (f . args) (apply f args)) values 12 8)
  63. (test-call "(1 2 3)"
  64. (lambda (thunk) (call-with-values thunk list))
  65. (lambda () (values 1 2 3)))
  66. (test-call "52" (lambda (f) ((f 42))) (lambda (n) (lambda () (+ n 10))))
  67. (test-call "120" (lambda (n)
  68. (let fac ((n n))
  69. (if (eq? n 0)
  70. 1
  71. (* n (fac (1- n))))))
  72. 5)
  73. (test-call "42" (case-lambda ((a) a) ((a b) (+ a b))) 42)
  74. (test-call "52" (case-lambda ((a) a) ((a b) (+ a b))) 42 10)
  75. (test-call "42" (case-lambda* ((a) a) ((a b) (+ a b))) 42)
  76. (test-call "52" (case-lambda* ((a) a) ((a b) (+ a b))) 42 10)
  77. (test-call "69" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))))
  78. (test-call "42" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))) 42)
  79. (test-call "52" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))) 42 10)
  80. (test-call "69" (lambda* (#:key (a 69)) a))
  81. (test-call "42" (lambda* (#:key (a 69)) a) #:a 42)
  82. (test-call "10" (lambda* (#:key (a 69)) a) #:a 42 #:a 10)
  83. (test-call "(69 69)" (lambda* (#:key (a 69) (b a)) (list a b)))
  84. (test-call "(42 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42)
  85. (test-call "(10 10)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42 #:a 10)
  86. (test-call "(69 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:b 42)
  87. (test-call "(42 69)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42 #:b 69)
  88. (test-call "(10 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:b 42 #:a 10)
  89. (test-call "(1 2 3)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1)
  90. (test-call "(1 42 3)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 42)
  91. (test-call "(1 2 42)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 #:c 42)
  92. (test-call "(1 42 69)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 42 #:c 69)
  93. (test-call "(1 2 3 ())"
  94. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1)
  95. (test-call "(1 42 3 ())"
  96. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42)
  97. (test-call "(1 2 42 (#:c 42))"
  98. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 #:c 42)
  99. (test-call "(1 42 69 (#:c 69))"
  100. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42 #:c 69)
  101. (test-call "(1 42 100 (#:c 69 #:c 100))"
  102. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42 #:c 69 #:c 100)
  103. (test-call "(second 3)"
  104. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  105. ((#:key (c 3)) (list 'second c))))
  106. ;; FIXME: this test passes with v8 but fails on the hoot VM.
  107. ;;(test-call "(second 42)"
  108. ;; (case-lambda* ((a #:key (b 2)) (list 'first a b))
  109. ;; ((#:key (c 3)) (list 'second c)))
  110. ;; #:c 42)
  111. (test-call "(first 10 2)"
  112. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  113. ((#:key (c 3)) (list 'second c)))
  114. 10)
  115. (test-call "(first 10 20)"
  116. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  117. ((#:key (c 3)) (list 'second c)))
  118. 10 #:b 20)
  119. (with-additional-imports ((hoot exceptions))
  120. (test-call "not-a-procedure"
  121. (lambda (proc)
  122. (with-exception-handler (lambda (exn)
  123. (and (failed-type-check? exn)
  124. 'not-a-procedure))
  125. (lambda ()
  126. (proc 'arg))
  127. #:unwind? #t))
  128. 'nope)
  129. (test-call "not-a-procedure"
  130. (lambda (proc . args)
  131. (with-exception-handler (lambda (exn)
  132. (and (failed-type-check? exn)
  133. 'not-a-procedure))
  134. (lambda ()
  135. (apply proc args))
  136. #:unwind? #t))
  137. 'nope 1 2 3))
  138. ;; (test-call "9227465" (lambda (n)
  139. ;; (let fib ((n n))
  140. ;; (if (<= n 1)
  141. ;; 1
  142. ;; (+ (fib (- n 1)) (fib (- n 2))))))
  143. ;; 34)
  144. ;; (test-call "1000000" (lambda ()
  145. ;; (let lp ((n 0))
  146. ;; (if (< n #e1e6)
  147. ;; (lp (1+ n))
  148. ;; n))))
  149. (with-additional-imports ((only (hoot procedures) procedure-name))
  150. (test-call "\"list\"" procedure-name list))
  151. (test-end* "test-procedures")