aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/coroutine-tests.el
blob: ef2917c0c991e93a62ac580d9c428c5f4c4b871f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; coroutine-tests.el --- coroutine tests           -*- lexical-binding: t; -*-

;; Copyright (C) 2016  Free Software Foundation, Inc.

;; Author: Philipp Stephani <p.stephani2@gmail.com>

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Unit tests for src/coroutine.c.

;;; Code:

(defun coroutine-tests--prime-task (channel)
  (let ((prime (receive-from-channel channel)))
    (message "%d" prime)
    (let ((new-channel (make-channel)))
      (start-coroutine (lambda () (coroutine-tests--prime-task new-channel)))
      (while t
        (let ((i (receive-from-channel channel)))
          (unless (zerop (mod i prime))
            (send-to-channel new-channel i)))))))

(ert-deftest coroutines-tests--prime ()
  (let ((channel (make-channel)))
    (start-coroutine (lambda () (coroutine-tests--prime-task channel)))
    (cl-loop for i from 2 to 100
             do (send-to-channel channel i))))

(ert-deftest coroutines-test--go-tour-1 ()
  (cl-flet ((say (s)
                 (dotimes (_ 5)
                   (sleep-for 0.1)
                   (message "%s" s))))
    (start-coroutine (lambda () (say "world")))
    (say "hello")))

(ert-deftest coroutines-test--go-tour-2 ()
  (cl-flet ((sum (s c)
                 (let ((sum 0))
                   (dolist (v s) (cl-incf sum v))
                   (send-to-channel c sum))))
    (let ((s '(7 2 8 -9 4 0))
          (c (make-channel)))
      (start-coroutine (lambda () (sum (butlast s (/ (length s) 2)) c)))
      (start-coroutine (lambda () (sum (nthcdr (/ (length s) 2) s) c)))
      (let ((x (receive-from-channel c))
            (y (receive-from-channel c)))
        (message "%d" (+ x y))
        (should (equal (+ x y) (apply #'+ s)))))))

(ert-deftest coroutines-test--go-tour-3 ()
  (let ((ch (make-channel 2)))
    (send-to-channel ch 1)
    (send-to-channel ch 2)
    (should (equal (receive-from-channel ch) 1))
    (should (equal (receive-from-channel ch) 2))))

(ert-deftest coroutines-test--go-tour-4 ()
  (cl-flet ((fibonacci (n c close)
                       (let ((x 0) (y 1))
                         (dotimes (_ n)
                           (send-to-channel c x)
                           (cl-psetq x y
                                     y (+ x y))))
                       (send-to-channel close nil)))
    (let ((c (make-channel 10))
          (close (make-channel 1)))
      (start-coroutine (lambda () (fibonacci 10 c close)))
      (cl-block nil
        (while t
          (select
           ((receive c i)
            (message "%d" i))
           ((receive close)
            (cl-return))))))))

(ert-deftest coroutines-test--go-tour-5 ()
  (cl-flet ((fibonacci (c quit)
                       (cl-block nil
                         (let ((x 0) (y 1))
                           (while t
                             (select
                              ((send c x)
                               (cl-psetq x y
                                         y (+ x y)))
                              ((receive quit)
                               (message "quit")
                               (cl-return))))))))
    (let ((c (make-channel))
          (quit (make-channel)))
      (start-coroutine
       (lambda ()
         (dotimes (_ 10)
           (message "%S" (receive-from-channel c)))
         (send-to-channel quit nil)))
      (fibonacci c quit))))

(defun coroutines-test--time-tick (d)
  (cl-check-type d (and number cl-plus))
  (let ((c (make-channel 1)))
    (run-at-time nil d (lambda ()
                         (try-send-to-channel c (current-time))))
    c))

(defun coroutines-test--time-after (d)
  (cl-check-type d (and number cl-plus))
  (let ((c (make-channel 1)))
    (run-at-time d nil (lambda ()
                         (try-send-to-channel c (current-time))))
    c))

(ert-deftest coroutines-test--go-tour-6 ()
  (cl-block nil
    (let ((tick (coroutines-test--time-tick 0.1))
          (boom (coroutines-test--time-after 0.5)))
      (while t
        (select
         ((receive tick)
          (message "tick."))
         ((receive boom)
           (message "BOOM!")
           (cl-return))
         (default
           (message "    .")
           (sleep-for 0.05)))))))

(defvar coroutines-test--var nil)

(ert-deftest coroutines-test--specbind ()
  (let ((coroutines-test--var 'main)
        (channel (make-channel)))
    (start-coroutine
     (lambda ()
       (let ((coroutines-test--var 'child))
         (setq coroutines-test--var 'child-setq)
         (receive-from-channel channel)
         (should (equal coroutines-test--var 'child-setq)))))
    (should (equal coroutines-test--var 'main))
    (setq coroutines-test--var 'main-setq)
    (send-to-channel channel nil)
    (should (equal coroutines-test--var 'main-setq))))

;; (ert-deftest coroutines-test--deadlock ()
;;   (let ((ch-1 (make-channel))
;;         (ch-2 (make-channel)))
;;     (start-coroutine
;;      (lambda ()
;;        (receive-from-channel ch-1)
;;        (send-to-channel ch-2 nil)))
;;     (receive-from-channel ch-2)
;;     (send-to-channel ch-1 nil)))

(ert-deftest coroutines-test--signal ()
  (start-coroutine (lambda () (signal 'beginning-of-buffer '(foo))))
  (let ((err (should-error (sleep-for 0.1))))
    (should (equal err '(beginning-of-buffer foo)))))

(ert-deftest channelp ()
  (should-not (channelp nil))
  (should-not (channelp 5))
  (should (channelp (make-channel))))

;;; coroutine-tests.el ends here