vars-test.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. (test-begin "variables-and-patterns" 22)
  2. (let ((foo (lambda ([x::integer ...]) (+ x ...))))
  3. (test-equal 9 (foo [2 3 4]))
  4. (test-equal 0 (foo []))
  5. (test-equal 6 (foo (list 4 2)))
  6. (test-error #t
  7. (display (foo [3 4.5 8]))))
  8. (let ((foo (lambda ([x::long ...]) (+ x ...))))
  9. (test-equal 9 (foo [2 3 4]))
  10. (test-equal 0 (foo []))
  11. (test-equal 6 (foo (list 4 2)))
  12. (test-error #t
  13. (display (foo [3 "4" 8]))))
  14. (let ((fun (lambda ([[x ...] ...] [y ...])
  15. [[(+ x y) ...] ...])))
  16. (test-equal
  17. [[101 102 103] [210 211 212]]
  18. (fun [[1 2 3] [10 11 12]] [100 200])))
  19. (let ((A [3 4 5]))
  20. (test-equal [103 104 105]
  21. [(+ 100 (scan A)) ...]))
  22. ;; simple guards
  23. (let ((fun (lambda (x #!if (> x 0) y) (list x y))))
  24. (test-equal '(3 4) (fun 3 4)))
  25. (let ((fun (lambda (x y #!if (> x y)) (list x y))))
  26. (test-equal '(4 2) (fun 4 2))
  27. (test-equal 'caught
  28. (try-catch
  29. (fun 3 4)
  30. (ex java.lang.IllegalArgumentException
  31. 'caught))))
  32. ;; #!if guard after #!rest
  33. (let ((fun (lambda (x y #!rest r #!if (> x y)) (list x y r))))
  34. (test-equal '(4 2 (a b c)) (fun 4 2 'a 'b 'c))
  35. (test-equal 'caught
  36. (try-catch
  37. (fun 3 4)
  38. (ex java.lang.IllegalArgumentException
  39. 'caught))))
  40. (let* ((fun (lambda ([[x y] ...]) [[y x] ...]))
  41. (A [[11 12] [21 22] [31 32]])
  42. (B [[11 12] [21 22 23] [31 32]]))
  43. (test-equal [[12 11] [22 21] [32 31]]
  44. (fun A))
  45. (test-error (fun B)))
  46. (let* ((fun (lambda ([[x @r] ...])
  47. [[@r (+ 100 x)] ...]))
  48. (A [[11] [21 22 23 24] [31 32]])
  49. (B [[] [21 22 23 24] [31 32]]))
  50. (test-equal [[111] [22 23 24 121] [32 131]]
  51. (fun A))
  52. (test-error (fun B)))
  53. (let* ((fun (lambda ([[x ... y] ...])
  54. [[(+ 100 y) (+ 200 x) ...] ...]))
  55. (A [[11] [21 22 23 24] [31 32]]))
  56. (test-equal [[111] [124 221 222 223] [132 231]]
  57. (fun A)))
  58. (let* ((fun (lambda ([[@r x] ...])
  59. [[@r (+ 100 x)] ...]))
  60. (A [[11] [21 22 23 24] [31 32]]))
  61. (test-equal [[111] [21 22 23 124] [31 132]]
  62. (fun A)))
  63. (let* ((A [[11 12] [21 22] [31 32]])
  64. ([[x::integer y] ...] A))
  65. (test-equal [23 43 63] [(+ x y) ...]))
  66. (test-end)