-
Notifications
You must be signed in to change notification settings - Fork 14
/
tests.erl2
205 lines (124 loc) · 3.54 KB
/
tests.erl2
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
%% we start with defMods
%% Why? we need to know if
%% X:Y means a call to the Erlang module X or the module we are defining
%% here.
%%
defMods shell mod1 mod2 mod3 mod4 end.
addMod shell.
%% hello world
def hello() -> io:format("Hello world~n") end.
hello().
def test(N) -> io:format("Passing test ~p~n",[N]) end.
def print(X) -> io:format("~p~n", [X]) end.
test(1).
print("factorial defined as a fun").
def fac = fun(0) -> 1; (N) -> N*fac(N-1) end.
print("Unit test of factorial").
120 = fac(5).
print("Print a large factorial").
print(fac(50)).
test(3).
print("define factorial using a fun").
def fac1 = fun(0) -> 1;(N) -> N*fac1(N-1) end.
print(fac1(50)).
test(4).
beginFunc f1/1 end.
%% f1 is a local fucntion - that is in the export list
%% so it will be exported
shell:test(5).
def f1(X) -> {f1,X} end.
{f1,123} = f1(123).
shell:test(5). %% test(N) would mean in the local scope
%% so we have to call this shell:test()
%% define a local function
def local(X) -> {local, X} end.
{local,222} = local(222).
shell:test(7).
endFunc.
%% Test that I can call the exported function
{f1, a} = f1(a).
test(8).
%% test that calling the local function will fail
{'EXIT', _} = (catch local(222)).
test(9).
%% Now make two functions both using the
%% same auxilliary functions to test name hiding is correct
%% BOTH of these use the same auxilliary function
beginFunc f2/1 end.
def f2(X) -> foo(X) end.
def foo(X) -> {f2foo, X} end. %% foo is not exported
endFunc.
beginFunc f3/1 end.
def foo(X) -> {f3foo, X} end. %% foo is not exported
def f3(X) -> foo(X) end.
endFunc.
{f2foo,a} = f2(a).
{f3foo,a} = f3(a).
test(10).
%% add unit tests inside a function definition
beginFunc f4/1 end.
def foo(X,Y) -> {f3foo, X+Y} end. %% foo is not exported
{f3foo,5} = foo(2,3).
def f3(X) -> foo(1,X) end.
{f3foo,6} = f3(5).
endFunc.
test(11).
print("Modules").
addMod mod1.
defExports a/1 b/2 c/3 end. %% ignored for now
def test(N) -> io:format("Passing local test in mod1:~p~n",[N]) end.
def a() -> {mod1,a} end.
{mod1, a} = a().
shell:test(12).
def a(X) -> {mod1,a,X} end.
{mod1, a, 12} = a(12).
shell:test(13).
test(14).
addMod shell.
print("Testing that we can call functions in a module from outside").
{mod1, a, 12} = mod1:a(12).
test(15).
{'EXIT', _} = (catch mod1:b(12)).
test(16).
%% Mod with private functions
addMod mod2.
defExports a/1 b/2 c/3 end. %% ignored for now
def test(N) -> io:format("Passing local test ~p in mod2~n",[N]) end.
test(17).
beginFunc a/1 end.
def a(X) -> b(X) end.
def b(X) -> {mod2,b,X} end.
endFunc.
test(18).
{mod2,b,123} = a(123).
test(19).
addMod shell.
{mod2,b,123} = mod2:a(123).
test(20).
print("Meta programming ...").
%% We can bind variable *OUTSIDE* a module and
%% use them *inside* the module
F25 = fac(25).
addMod mod3.
defExports a/1 end. %% ignored for now
def a(N) -> F25 + N end.
addMod shell.
print(mod3:a(10)).
print("Just imagine what you could do with this ...").
print("More fancy stuff").
%% We can do unit tests *inside the module*
%% If the unit tests fail - we crash and the module will not be generated
addMod mod4.
defExports fac/1 end.
def fac(0) -> 1; fac(N) -> N*fac(N-1) end.
%% unit tests
120 = fac(5).
shell:print("unit test worked").
def twice(X) -> 3*X end.
9 = twice(3).
shell:print("** I know this is bad but don't worry").
addMod shell.
test(21).
print("Horray - everything worked dump the results into a file
which we can compile later").
erl2:make_mods().