test-procedures.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "52" (lambda (f) ((f 42))) (lambda (n) (lambda () (+ n 10))))
  64. (test-call "120" (lambda (n)
  65. (let fac ((n n))
  66. (if (eq? n 0)
  67. 1
  68. (* n (fac (1- n))))))
  69. 5)
  70. (test-call "42" (case-lambda ((a) a) ((a b) (+ a b))) 42)
  71. (test-call "52" (case-lambda ((a) a) ((a b) (+ a b))) 42 10)
  72. (test-call "42" (case-lambda* ((a) a) ((a b) (+ a b))) 42)
  73. (test-call "52" (case-lambda* ((a) a) ((a b) (+ a b))) 42 10)
  74. (test-call "69" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))))
  75. (test-call "42" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))) 42)
  76. (test-call "52" (case-lambda* ((#:optional (a 69)) a) ((a b) (+ a b))) 42 10)
  77. (test-call "69" (lambda* (#:key (a 69)) a))
  78. (test-call "42" (lambda* (#:key (a 69)) a) #:a 42)
  79. (test-call "10" (lambda* (#:key (a 69)) a) #:a 42 #:a 10)
  80. (test-call "(69 69)" (lambda* (#:key (a 69) (b a)) (list a b)))
  81. (test-call "(42 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42)
  82. (test-call "(10 10)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42 #:a 10)
  83. (test-call "(69 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:b 42)
  84. (test-call "(42 69)" (lambda* (#:key (a 69) (b a)) (list a b)) #:a 42 #:b 69)
  85. (test-call "(10 42)" (lambda* (#:key (a 69) (b a)) (list a b)) #:b 42 #:a 10)
  86. (test-call "(1 2 3)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1)
  87. (test-call "(1 42 3)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 42)
  88. (test-call "(1 2 42)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 #:c 42)
  89. (test-call "(1 42 69)" (lambda* (a #:optional (b 2) #:key (c 3)) (list a b c)) 1 42 #:c 69)
  90. (test-call "(1 2 3 ())"
  91. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1)
  92. (test-call "(1 42 3 ())"
  93. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42)
  94. (test-call "(1 2 42 (#:c 42))"
  95. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 #:c 42)
  96. (test-call "(1 42 69 (#:c 69))"
  97. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42 #:c 69)
  98. (test-call "(1 42 100 (#:c 69 #:c 100))"
  99. (lambda* (a #:optional (b 2) #:key (c 3) #:rest d) (list a b c d)) 1 42 #:c 69 #:c 100)
  100. (test-call "(second 3)"
  101. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  102. ((#:key (c 3)) (list 'second c))))
  103. ;; FIXME: this test passes with d8 but fails on the hoot VM.
  104. ;;(test-call "(second 42)"
  105. ;; (case-lambda* ((a #:key (b 2)) (list 'first a b))
  106. ;; ((#:key (c 3)) (list 'second c)))
  107. ;; #:c 42)
  108. (test-call "(first 10 2)"
  109. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  110. ((#:key (c 3)) (list 'second c)))
  111. 10)
  112. (test-call "(first 10 20)"
  113. (case-lambda* ((a #:key (b 2)) (list 'first a b))
  114. ((#:key (c 3)) (list 'second c)))
  115. 10 #:b 20)
  116. ;; (test-call "9227465" (lambda (n)
  117. ;; (let fib ((n n))
  118. ;; (if (<= n 1)
  119. ;; 1
  120. ;; (+ (fib (- n 1)) (fib (- n 2))))))
  121. ;; 34)
  122. ;; (test-call "1000000" (lambda ()
  123. ;; (let lp ((n 0))
  124. ;; (if (< n #e1e6)
  125. ;; (lp (1+ n))
  126. ;; n))))
  127. (test-end* "test-procedures")