Skip to content

Instantly share code, notes, and snippets.

@gausby
Last active May 14, 2019 12:26
Show Gist options
  • Save gausby/29a8dff29c14a43bc404dd4cae8a0721 to your computer and use it in GitHub Desktop.
Save gausby/29a8dff29c14a43bc404dd4cae8a0721 to your computer and use it in GitHub Desktop.
test "take and free" do
mutex_pid = Mutex.start()
parent = self()
assert :ok = Mutex.wait(mutex_pid)
spawn_link(fn() ->
send parent, :ready
:ok = Mutex.wait(mutex_pid)
send parent, :done
end)
assert_receive :ready
assert :ok = Mutex.signal(mutex_pid)
assert_receive :done
end
defmodule Ring do
def start(number_of_children, msgs) do
spawn(Ring, :initialize_parent, [number_of_children, msgs])
end
def initialize_parent(n, m) do
pid = initialize_childen(n, self())
parent_loop(pid, m)
end
defp parent_loop(pid, 0) do
send pid, :stop
receive do
:stop ->
IO.inspect :terminating
:ok
end
end
defp parent_loop(pid, m) do
send pid, {:msg, "hello"}
receive do
{:msg, msg} ->
IO.inspect {:parent, m, msg}
parent_loop(pid, m - 1)
end
end
defp initialize_childen(1, parent) do
spawn(Ring, :initialize_child, [parent])
end
defp initialize_childen(n, parent) do
pid = spawn(Ring, :initialize_child, [parent])
initialize_childen(n - 1, pid)
end
def initialize_child(neighbor) do
child_loop(neighbor)
end
defp child_loop(neighbor) do
receive do
{:msg, msg} ->
send neighbor, {:msg, msg}
IO.inspect {self(), msg}
child_loop(neighbor)
:stop ->
IO.inspect {self(), :terminating}
send neighbor, :stop
:ok
end
end
end
defmodule Db do
def new() do
[]
end
def destroy(_) do
:ok
end
def write(db, key, element) do
# delete the previous entry under key, if it exist
[{key, element} | delete(db, key)]
end
def delete([], _key) do
[]
end
def delete([{match, _}|tail], match) do
delete(tail, match)
end
def delete([head|tail], key) do
[head | delete(tail, key)]
end
def read([], _) do
{:error, :instance}
end
def read([{match, value} | _], match) do
{:ok, value}
end
def read([_ | tail], match) do
read(tail, match)
end
def match([], _needle) do
[]
end
def match([{key, match} | tail ], match) do
[key | match(tail, match)]
end
def match([_ | tail], needle) do
match(tail, needle)
end
end
defmodule DbTest do
use ExUnit.Case
doctest Db
test "create a new database" do
assert [] == Db.new()
end
test "destroy a new database" do
assert :ok == Db.destroy([])
end
test "insert an element to the database" do
db = Db.new()
assert [] == db
assert [{:martin, :london}] == Db.write(db, :martin, :london)
assert [{:martin, :london}] ==
Db.write(db, :martin, :copenhagen)
|> Db.write(:martin, :london)
end
test "delete an element from a database" do
db = [{:martin, :london}]
assert [] = Db.delete(db, :martin)
db = [{:martin, :london}, {:johan, :stockholm}]
assert [{:martin, :london}] = Db.delete(db, :johan)
end
test "read an element from a database" do
db = [{:martin, :london}]
assert {:ok, :london} = Db.read(db, :martin)
db = [{:martin, :london}]
assert {:error, :instance} = Db.read(db, :lars)
end
test "match on elements and return matching keys" do
db = [{:martin, :london}, {:fritz, :berlin}, {:john, :london}]
assert [:martin, :john] == Db.match(db, :london)
assert [:fritz] == Db.match(db, :berlin)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment