-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathex01.exs
87 lines (68 loc) · 2.11 KB
/
ex01.exs
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
#####################################################################
#
# Read the notes in the tests, then implement the code in the module
# at the end of the file.
#
#####################################################################
# In this exercise you'll implement a simple process that acts as a
# counter. Each time you send it `{:next, from}`, it will send to
# `from` the tuple `{:next_is, value}`, and that value will increase
# by one on each call. Your code will allow you to set the initial
# value returned.
ExUnit.start()
defmodule Test do
use ExUnit.Case
# STEP 1:
#
# This test assumes you have a function `Ex01.counter` that
# can be spawned and which handles the `{:next, from}` message.
# Your job is to implement the body of `counter` in the definition
# of the Ex01 module tht follows these tests.
#
# Implement just the `counter` function, then run the tests using
#
# $ elixir ex01.exs
#
# 5 points
test "basic message interface" do
count = spawn Ex01, :counter, []
send count, { :next, self() }
assert_receive({ :next_is, value }, 10)
assert value == 0
send count, { :next, self() }
assert_receive({ :next_is, value }, 10)
assert value == 1
end
# STEP 2:
#
# Now write code in the `Ex01.new_counter` and
# `Ex01.next_value` that wrap the counter function, giving it
# a higher level API. The test below shows how this API will be used.
#
# To test your functions, you need to delete the following line
@tag :skip
# then rerun `elixir ex01.exs`
#
# 5 points
test "higher level API interface" do
count = Ex01.new_counter(5)
assert Ex01.next_value(count) == 5
assert Ex01.next_value(count) == 6
end
end
########################################
# #
# This is the code you'll be changing #
# #
########################################
defmodule Ex01 do
def counter(value \\ 0) do
# ...your code
end
def new_counter(initial_value \\ 0) do
# ... your code
end
def next_value(counter_pid) do
# ... your code
end
end