test-procedures.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ;;; 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 "9227465" (lambda (n)
  78. ;; (let fib ((n n))
  79. ;; (if (<= n 1)
  80. ;; 1
  81. ;; (+ (fib (- n 1)) (fib (- n 2))))))
  82. ;; 34)
  83. ;; (test-call "1000000" (lambda ()
  84. ;; (let lp ((n 0))
  85. ;; (if (< n #e1e6)
  86. ;; (lp (1+ n))
  87. ;; n))))
  88. (test-end* "test-procedures")