Ver a proveniência

添加测试

master
SisMaker há 4 anos
ascendente
cometimento
d19cb43c1e
2 ficheiros alterados com 217 adições e 1 eliminações
  1. +137
    -0
      src/testCase/tc.erl
  2. +80
    -1
      src/testCase/utTryCatchCase.erl

+ 137
- 0
src/testCase/tc.erl Ver ficheiro

@ -0,0 +1,137 @@
-module(tc).
-compile([export_all, nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]).
%% LoopTimes是循环次数
%% tc:t(Module, Function, ArgsList, LoopTimes).
%% SpawnProcessesCount是并发的进程数
%% tc:ct(Module, Function, ArgsList, SpawnProcessesCount).
t1() ->
[ X || X <- lists:seq(1, 5000) ].
t2() ->
t2(lists:seq(1, 5000)).
t2([H|T]) ->
H,
t2(T);
t2([]) ->
ok.
tc(M, F, A) ->
{Microsecond, _} = timer:tc (M, F, A),
Microsecond.
distribution(List, Aver) ->
distribution(List, Aver, 0, 0).
distribution([H|T], Aver, Greater, Less) ->
case H > Aver of
true ->
distribution(T, Aver, Greater + 1, Less);
false ->
distribution(T, Aver, Greater, Less + 1)
end;
distribution([], _Aver, Greater, Less) ->
{Greater, Less}.
%% ===================================================================
%% test: one process test N times
%% ===================================================================
t(M, F, A, N) ->
{Max, Min, Sum, Aver, Greater, Less} = loop ({M, F, A}, N),
io:format ("=====================~n"),
io:format ("execute [~p] times of {~p, ~p, ~p}:~n", [N, M, F, A]),
io:format ("Maximum: ~p(μs)\t~p(s)~n", [Max, Max / 1000000]),
io:format ("Minimum: ~p(μs)\t~p(s)~n", [Min, Min / 1000000]),
io:format ("Sum: ~p(μs)\t~p(s)~n", [Sum, Sum / 1000000]),
io:format ("Average: ~p(μs)\t~p(s)~n", [Aver, Aver / 1000000]),
io:format ("Greater: ~p~nLess: ~p~n", [Greater, Less]),
io:format ("=====================~n").
loop({M, F, A}, N) ->
loop ({M, F, A}, N, 1, 0, 0, 0, []).
loop({M, F, A}, N, I, Max, Min, Sum, List) when N >= I ->
Microsecond = tc (M, F, A),
NewSum = Sum + Microsecond,
if
Max == 0 ->
NewMax = NewMin = Microsecond;
Max < Microsecond ->
NewMax = Microsecond,
NewMin = Min;
Min > Microsecond ->
NewMax = Max,
NewMin = Microsecond;
true ->
NewMax = Max,
NewMin = Min
end,
loop ({M, F, A}, N, I + 1, NewMax, NewMin, NewSum, [Microsecond|List]);
loop({_M, _F, _A}, N, _I, Max, Min, Sum, List) ->
Aver = Sum / N,
{Greater, Less} = distribution(List, Aver),
{Max, Min, Sum, Aver, Greater, Less}.
%% ===================================================================
%% Concurrency test: N processes each test one time
%% ===================================================================
ct(M, F, A, N) ->
{Max, Min, Sum, Aver, Greater, Less} = cloop ({M, F, A}, N),
io:format ("=====================~n"),
io:format ("spawn [~p] processes of {~p, ~p, ~p}:~n", [N, M, F, A]),
io:format ("Maximum: ~p(μs)\t~p(s)~n", [Max, Max / 1000000]),
io:format ("Minimum: ~p(μs)\t~p(s)~n", [Min, Min / 1000000]),
io:format ("Sum: ~p(μs)\t~p(s)~n", [Sum, Sum / 1000000]),
io:format ("Average: ~p(μs)\t~p(s)~n", [Aver, Aver / 1000000]),
io:format ("Greater: ~p~nLess: ~p~n", [Greater, Less]),
io:format ("=====================~n").
cloop({M, F, A}, N) ->
CollectorPid = self(),
ok = loop_spawn({M, F, A}, CollectorPid, N),
collector(0, 0, 0, N, 1, []).
loop_spawn({M, F, A}, CollectorPid, N) when N > 0 ->
spawn_link(fun() -> worker({M, F, A}, CollectorPid) end),
loop_spawn({M, F, A}, CollectorPid, N - 1);
loop_spawn(_, _, 0) ->
ok.
collector(Max, Min, Sum, N, I, List) when N >= I ->
receive
{result, Microsecond} ->
NewSum = Sum + Microsecond,
if
Max == 0 ->
NewMax = NewMin = Microsecond;
Max < Microsecond ->
NewMax = Microsecond,
NewMin = Min;
Min > Microsecond ->
NewMax = Max,
NewMin = Microsecond;
true ->
NewMax = Max,
NewMin = Min
end,
collector(NewMax, NewMin, NewSum, N, I + 1, [Microsecond|List])
after
10000 ->
ok
end;
collector(Max, Min, Sum, N, _, List) ->
Aver = Sum / N,
{Greater, Less} = distribution(List, Aver),
{Max, Min, Sum, Aver, Greater, Less}.
worker({M, F, A}, CollectorPid) ->
Microsecond = tc(M, F, A),
CollectorPid ! {result, Microsecond}.

+ 80
- 1
src/testCase/utTryCatchCase.erl Ver ficheiro

@ -1,7 +1,7 @@
-module(utTryCatchCase).
-compile([export_all, nowarn_unused_function, nowarn_unused_vars, nowarn_export_all]).
-export([loopTest/4, testTryCatch/1, testTryCatch/2, testCatch/1, testCatch/2, testTryCatch2/1, testTryCatch2/2]).
t1() ->
Pid = spawn(fun() -> do_t1(1) end),
@ -211,5 +211,84 @@ do_exit1() ->
end.
loopTest(0, _, _, _) -> ok;
loopTest(Times, Fun, Type, IsLoop) ->
?MODULE:Fun(Type, IsLoop),
loopTest(Times - 1, Fun, Type, IsLoop).
testTryCatch(Type) -> testTryCatch(Type, false).
testTryCatch(Type, TestLoop) ->
try
makeException(Type)
catch
Type:Reason ->
case TestLoop of
true -> ok;
false -> io:format("try .. catch block caught exception of ~p: ~p~n", [Type, Reason])
end
end.
testTryCatch2(Type) -> testTryCatch(Type, false).
testTryCatch2(Type, TestLoop) ->
try
makeException(Type)
catch
Type:Reason:Strace ->
case TestLoop of
true -> ok;
false -> io:format("try .. catch block caught exception of ~p: ~p: ~p:~n", [Type, Reason, Strace])
end
end.
testCatch(Type) -> testCatch(Type, false).
testCatch(Type, TestLoop) ->
case catch makeException(Type) of
ignore -> ignore;
Exception ->
case TestLoop of
true -> ok;
false -> io:format("catch block caught exception of ~p~n", [Exception])
end
end.
makeException(Type) ->
case Type of
throw -> erlang:throw(test);
error -> erlang:error(test);
exit -> erlang:exit(test);
_ -> ignore
end.
comRet() ->
ok.
throwRet() ->
throw(ok).
comRet1() ->
A = {tuple, 11},
F = A,
case F of
{tt, 44} ->
ok;
_ ->
A
end,
ok.
throwRet1() ->
A = {tuple, 11},
F = A,
case F of
{tt, 44} ->
ok;
_ ->
A
end,
throw(ok).

Carregando…
Cancelar
Guardar