test.el 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ;;; Test GNU Emacs modules.
  2. ;; Copyright 2015-2016 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is free software: you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; GNU Emacs is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  14. (require 'ert)
  15. (add-to-list 'load-path
  16. (file-name-directory (or #$ (expand-file-name (buffer-file-name)))))
  17. (require 'mod-test)
  18. ;;
  19. ;; Basic tests.
  20. ;;
  21. (ert-deftest mod-test-sum-test ()
  22. (should (= (mod-test-sum 1 2) 3))
  23. (let ((descr (should-error (mod-test-sum 1 2 3))))
  24. (should (eq (car descr) 'wrong-number-of-arguments))
  25. (should (stringp (nth 1 descr)))
  26. (should (eq 0
  27. (string-match
  28. (concat "#<module function "
  29. "\\(at \\(0x\\)?[0-9a-fA-F]+\\( from .*\\)?"
  30. "\\|Fmod_test_sum from .*\\)>")
  31. (nth 1 descr))))
  32. (should (= (nth 2 descr) 3)))
  33. (should-error (mod-test-sum "1" 2) :type 'wrong-type-argument)
  34. (should-error (mod-test-sum 1 "2") :type 'wrong-type-argument)
  35. ;; The following tests are for 32-bit build --with-wide-int.
  36. (should (= (mod-test-sum -1 most-positive-fixnum)
  37. (1- most-positive-fixnum)))
  38. (should (= (mod-test-sum 1 most-negative-fixnum)
  39. (1+ most-negative-fixnum)))
  40. (when (< #x1fffffff most-positive-fixnum)
  41. (should (= (mod-test-sum 1 #x1fffffff)
  42. (1+ #x1fffffff)))
  43. (should (= (mod-test-sum -1 #x20000000)
  44. #x1fffffff)))
  45. (should-error (mod-test-sum 1 most-positive-fixnum)
  46. :type 'overflow-error)
  47. (should-error (mod-test-sum -1 most-negative-fixnum)
  48. :type 'overflow-error))
  49. (ert-deftest mod-test-sum-docstring ()
  50. (should (string= (documentation 'mod-test-sum) "Return A + B")))
  51. ;;
  52. ;; Non-local exists (throw, signal).
  53. ;;
  54. (ert-deftest mod-test-non-local-exit-signal-test ()
  55. (should-error (mod-test-signal)))
  56. (ert-deftest mod-test-non-local-exit-throw-test ()
  57. (should (equal
  58. (catch 'tag
  59. (mod-test-throw)
  60. (ert-fail "expected throw"))
  61. 65)))
  62. (ert-deftest mod-test-non-local-exit-funcall-normal ()
  63. (should (equal (mod-test-non-local-exit-funcall (lambda () 23))
  64. 23)))
  65. (ert-deftest mod-test-non-local-exit-funcall-signal ()
  66. (should (equal (mod-test-non-local-exit-funcall
  67. (lambda () (signal 'error '(32))))
  68. '(signal error (32)))))
  69. (ert-deftest mod-test-non-local-exit-funcall-throw ()
  70. (should (equal (mod-test-non-local-exit-funcall (lambda () (throw 'tag 32)))
  71. '(throw tag 32))))
  72. ;;
  73. ;; String tests.
  74. ;;
  75. (defun multiply-string (s n)
  76. (let ((res ""))
  77. (dotimes (i n res)
  78. (setq res (concat res s)))))
  79. (ert-deftest mod-test-globref-make-test ()
  80. (let ((mod-str (mod-test-globref-make))
  81. (ref-str (multiply-string "abcdefghijklmnopqrstuvwxyz" 100)))
  82. (garbage-collect) ;; XXX: not enough to really test but it's something..
  83. (should (string= ref-str mod-str))))
  84. (ert-deftest mod-test-string-a-to-b-test ()
  85. (should (string= (mod-test-string-a-to-b "aaa") "bbb")))
  86. ;;
  87. ;; User-pointer tests.
  88. ;;
  89. (ert-deftest mod-test-userptr-fun-test ()
  90. (let* ((n 42)
  91. (v (mod-test-userptr-make n))
  92. (r (mod-test-userptr-get v)))
  93. (should (eq (type-of v) 'user-ptr))
  94. (should (integerp r))
  95. (should (= r n))))
  96. ;; TODO: try to test finalizer
  97. ;;
  98. ;; Vector tests.
  99. ;;
  100. (ert-deftest mod-test-vector-test ()
  101. (dolist (s '(2 10 100 1000))
  102. (dolist (e '(42 foo "foo"))
  103. (let* ((v-ref (make-vector 2 e))
  104. (eq-ref (eq (aref v-ref 0) (aref v-ref 1)))
  105. (v-test (make-vector s nil)))
  106. (should (eq (mod-test-vector-fill v-test e) t))
  107. (should (eq (mod-test-vector-eq v-test e) eq-ref))))))