erlang: send + selective receive via shift/reset (+13 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -327,6 +327,48 @@
|
||||
(er-io-buffer-content))
|
||||
"true;true")
|
||||
|
||||
;; ── ! (send) + receive ──────────────────────────────────────────
|
||||
(er-eval-test "self-send + receive"
|
||||
(nm (ev "Me = self(), Me ! hello, receive Msg -> Msg end")) "hello")
|
||||
(er-eval-test "send returns msg"
|
||||
(nm (ev "Me = self(), Msg = Me ! ok, Me ! x, receive _ -> Msg end")) "ok")
|
||||
(er-eval-test "receive int"
|
||||
(ev "Me = self(), Me ! 42, receive N -> N + 1 end") 43)
|
||||
(er-eval-test "receive with pattern"
|
||||
(ev "Me = self(), Me ! {ok, 7}, receive {ok, V} -> V * 2 end") 14)
|
||||
(er-eval-test "receive with guard"
|
||||
(ev "Me = self(), Me ! 5, receive N when N > 0 -> positive end")
|
||||
(er-mk-atom "positive"))
|
||||
(er-eval-test "receive skips non-match"
|
||||
(nm (ev "Me = self(), Me ! wrong, Me ! right, receive right -> ok end"))
|
||||
"ok")
|
||||
(er-eval-test "receive selective leaves others"
|
||||
(nm (ev "Me = self(), Me ! a, Me ! b, receive b -> got_b end"))
|
||||
"got_b")
|
||||
(er-eval-test "two receives consume both"
|
||||
(ev "Me = self(), Me ! 1, Me ! 2, X = receive A -> A end, Y = receive B -> B end, X + Y") 3)
|
||||
|
||||
;; ── spawn + send + receive (real process communication) ─────────
|
||||
(er-eval-test "spawn sends back"
|
||||
(nm
|
||||
(ev "Me = self(), spawn(fun () -> Me ! pong end), receive pong -> got_pong end"))
|
||||
"got_pong")
|
||||
(er-eval-test "ping-pong"
|
||||
(do
|
||||
(er-io-flush!)
|
||||
(ev "Me = self(), Child = spawn(fun () -> receive {ping, From} -> From ! pong end end), Child ! {ping, Me}, receive pong -> io:format(\"pong~n\") end")
|
||||
(er-io-buffer-content))
|
||||
"pong\n")
|
||||
(er-eval-test "echo server"
|
||||
(ev "Me = self(), Echo = spawn(fun () -> receive {From, Msg} -> From ! Msg end end), Echo ! {Me, 99}, receive R -> R end") 99)
|
||||
|
||||
;; ── receive with multiple clauses ────────────────────────────────
|
||||
(er-eval-test "receive multi-clause"
|
||||
(nm (ev "Me = self(), Me ! foo, receive ok -> a; foo -> b; bar -> c end"))
|
||||
"b")
|
||||
(er-eval-test "receive nested tuple"
|
||||
(ev "Me = self(), Me ! {result, {ok, 42}}, receive {result, {ok, V}} -> V end") 42)
|
||||
|
||||
(define
|
||||
er-eval-test-summary
|
||||
(str "eval " er-eval-test-pass "/" er-eval-test-count))
|
||||
|
||||
Reference in New Issue
Block a user