1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1998-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20-module(c_SUITE).
21-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
22	 init_per_group/2,end_per_group/2]).
23-export([c_1/1, c_2/1, c_3/1, c_4/1, nc_1/1, nc_2/1, nc_3/1, nc_4/1,
24	 c_default_outdir_1/1, c_default_outdir_2/1,
25         nc_default_outdir_1/1, nc_default_outdir_2/1,
26         ls/1, memory/1]).
27
28-include_lib("common_test/include/ct.hrl").
29
30-import(c, [c/2, nc/2]).
31
32suite() -> [{ct_hooks,[ts_install_cth]}].
33
34all() ->
35    [c_1, c_2, c_3, c_4, nc_1, nc_2, nc_3, nc_4,
36     c_default_outdir_1, c_default_outdir_2,
37     nc_default_outdir_1, nc_default_outdir_2,
38     ls, memory].
39
40groups() ->
41    [].
42
43init_per_suite(Config) ->
44    Config.
45
46end_per_suite(_Config) ->
47    ok.
48
49init_per_group(_GroupName, Config) ->
50    Config.
51
52end_per_group(_GroupName, Config) ->
53    Config.
54
55
56%%% Write output to a directory other than current directory:
57
58%% OTP-1209: Check that c:c/2 works also with option 'outdir'.
59c_1(Config) when is_list(Config) ->
60    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
61    W = proplists:get_value(priv_dir, Config),
62    Result = c(R,[{outdir,W}]),
63    {ok, m} = Result.
64
65%% OTP-1209: Check that c:c/2 works also with option 'outdir'.
66c_2(Config) when is_list(Config) ->
67    R = filename:join(proplists:get_value(data_dir, Config), "m"),
68    W = proplists:get_value(priv_dir, Config),
69    Result = c(R,[{outdir,W}]),
70    {ok, m} = Result.
71
72
73%%% Put results in current directory (or rather, change current dir
74%%% to the output dir):
75
76%% OTP-1209: Check that c:c/2 works also with option 'outdir'
77%% (same as current directory).
78c_3(Config) when is_list(Config) ->
79    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
80    W = proplists:get_value(priv_dir, Config),
81    file:set_cwd(W),
82    Result = c(R,[{outdir,W}]),
83    {ok, m} = Result.
84
85%% OTP-1209: Check that c:c/2 works also with option 'outdir'
86%% (same as current directory).
87c_4(Config) when is_list(Config) ->
88    R = filename:join(proplists:get_value(data_dir, Config), "m"),
89    W = proplists:get_value(priv_dir, Config),
90    file:set_cwd(W),
91    Result = c(R,[{outdir,W}]),
92    {ok, m} = Result.
93
94%%% Write output to a directory other than current directory:
95
96%% Check that c:nc/2 works also with option 'outdir'.
97nc_1(Config) when is_list(Config) ->
98    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
99    W = proplists:get_value(priv_dir, Config),
100    Result = nc(R,[{outdir,W}]),
101    {ok, m} = Result.
102
103%% Check that c:nc/2 works also with option 'outdir'.
104nc_2(Config) when is_list(Config) ->
105    R = filename:join(proplists:get_value(data_dir, Config), "m"),
106    W = proplists:get_value(priv_dir, Config),
107    Result = nc(R,[{outdir,W}]),
108    {ok, m} = Result.
109
110
111%%% Put results in current directory (or rather, change current dir
112%%% to the output dir):
113
114%% Check that c:nc/2 works also with option 'outdir'
115%% (same as current directory).
116nc_3(Config) when is_list(Config) ->
117    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
118    W = proplists:get_value(priv_dir, Config),
119    file:set_cwd(W),
120    Result = nc(R,[{outdir,W}]),
121    {ok, m} = Result.
122
123%% Check that c:nc/2 works also with option 'outdir'
124%% (same as current directory).
125nc_4(Config) when is_list(Config) ->
126    R = filename:join(proplists:get_value(data_dir, Config), "m"),
127    W = proplists:get_value(priv_dir, Config),
128    file:set_cwd(W),
129    Result = nc(R,[{outdir,W}]),
130    {ok, m} = Result.
131
132c_default_outdir_1(Config) ->
133    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
134    W = proplists:get_value(priv_dir, Config),
135    file:set_cwd(W),
136    Obj = "m" ++ code:objfile_extension(),
137    _ = file:delete(Obj),
138    false = filelib:is_file(Obj),
139    Result = c:c(R),
140    {ok, m} = Result,
141    true = filelib:is_file(Obj).
142
143c_default_outdir_2(Config) ->
144    R = filename:join(proplists:get_value(data_dir, Config), "m"),
145    W = proplists:get_value(priv_dir, Config),
146    file:set_cwd(W),
147    Obj = "m" ++ code:objfile_extension(),
148    _ = file:delete(Obj),
149    false = filelib:is_file(Obj),
150    Result = c:c(R),
151    {ok, m} = Result,
152    true = filelib:is_file(Obj).
153
154nc_default_outdir_1(Config) ->
155    R = filename:join(proplists:get_value(data_dir, Config), "m.erl"),
156    W = proplists:get_value(priv_dir, Config),
157    file:set_cwd(W),
158    Obj = "m" ++ code:objfile_extension(),
159    _ = file:delete(Obj),
160    false = filelib:is_file(Obj),
161    Result = c:nc(R),
162    {ok, m} = Result,
163    true = filelib:is_file(Obj).
164
165nc_default_outdir_2(Config) ->
166    R = filename:join(proplists:get_value(data_dir, Config), "m"),
167    W = proplists:get_value(priv_dir, Config),
168    file:set_cwd(W),
169    Obj = "m" ++ code:objfile_extension(),
170    _ = file:delete(Obj),
171    false = filelib:is_file(Obj),
172    Result = c:nc(R),
173    {ok, m} = Result,
174    true = filelib:is_file(Obj).
175
176ls(Config) when is_list(Config) ->
177    Directory = proplists:get_value(data_dir, Config),
178    ok = c:ls(Directory),
179    File = filename:join(Directory, "m.erl"),
180    ok = c:ls(File),
181    ok = c:ls([[[[File]]]]),
182    ok = c:ls("no_such_file"),
183    ok = c:ls(list_to_atom(code:which(c))),
184    ok.
185
186%% Check that c:memory/[0,1] returns consistent results.
187memory(Config) when is_list(Config) ->
188    try
189	ML = c:memory(),
190	T =  mget(total, ML),
191	P =  mget(processes, ML),
192	S =  mget(system, ML),
193	A =  mget(atom, ML),
194	AU = mget(atom_used, ML),
195	B =  mget(binary, ML),
196	C =  mget(code, ML),
197	E =  mget(ets, ML),
198	T = P + S,
199	if S >= A + B + C + E -> ok end,
200	if A >= AU -> ok end,
201	ok
202    catch
203	error:notsup ->
204	    {skipped,
205	     "erlang:memory/[0,1] and c:memory/[0,1] not supported"}
206    end.
207
208%% Help function for c_SUITE:memory/1
209mget(K, L) ->
210    {value,{K,V}} = lists:keysearch(K, 1, L),
211    test_v(c:memory(K)), % Check that c:memory/1 also accept this
212						% argument and returns an integer (usally
213						% *not* the same as V).
214    test_v(V).
215
216%% Help function for c_SUITE:memory/1
217test_v(V) when is_integer(V) ->
218    V.
219