Skip to content

Commit

Permalink
Merge pull request #146 from inaka/euen.145.update_elvis
Browse files Browse the repository at this point in the history
[#145] update Elvis dependency and ktn_random module
  • Loading branch information
Brujo Benavides authored Mar 17, 2017
2 parents f73083b + 060f626 commit 5238008
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 112 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

%% == Dependencies ==

{deps, [{elvis, "0.3.2", {pkg, elvis_core}}]}.
{deps, [{elvis, "0.3.5", {pkg, elvis_core}}]}.

%% == Dialyzer ==

Expand Down
18 changes: 14 additions & 4 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{"1.1.0",
[{<<"aleppo">>,{pkg,<<"inaka_aleppo">>,<<"1.0.0">>},2},
{<<"elvis">>,{pkg,<<"elvis_core">>,<<"0.3.2">>},0},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.7">>},2},
{<<"elvis">>,{pkg,<<"elvis_core">>,<<"0.3.5">>},0},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},2},
{<<"katana_code">>,{pkg,<<"katana_code">>,<<"0.1.0">>},1},
{<<"lager">>,{pkg,<<"lager">>,<<"3.0.2">>},1},
{<<"zipper">>,{pkg,<<"zipper">>,<<"1.0.0">>},1}].
{<<"lager">>,{pkg,<<"lager">>,<<"3.2.4">>},1},
{<<"zipper">>,{pkg,<<"zipper">>,<<"1.0.1">>},1}]}.
[
{pkg_hash,[
{<<"aleppo">>, <<"8DB14CF16BB8C263C14FF4C3F69F64D7C849D40888944F4204D2CA74F1114CEB">>},
{<<"elvis">>, <<"9C6DE2DA5317081D12512CA34EC9CAC858D3A169E6882E86BFAC97FA47962C4D">>},
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
{<<"katana_code">>, <<"C34F3926A258D6BEACD8D21F140F3D47D175501936431C460B144339D5271A0B">>},
{<<"lager">>, <<"A6DEB74DAE7927F46BD13255268308EF03EB206EC784A94EAF7C1C0F3B811615">>},
{<<"zipper">>, <<"3CCB4F14B97C06B2749B93D8B6C204A1ECB6FAFC6050CACC3B93B9870C05952A">>}]}
].
111 changes: 20 additions & 91 deletions src/ktn_random.erl
Original file line number Diff line number Diff line change
@@ -1,120 +1,49 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% ktn_random: a gen_server for generating random alfanumeric strings
%%% ktn_random: a wrapper for generating random alfanumeric strings
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(ktn_random).
-behaviour(gen_server).

-export([
start_link/0,
generate/0,
generate/1,
string/0,
string/1,
uniform/1,
uniform/2,
pick/1
]).

-export([
init/1,
terminate/2,
code_change/3,
handle_call/3,
handle_cast/2,
handle_info/2
]).

-type state() :: {}.

-spec start_link() -> {ok, pid()}.
start_link() ->
Seed = os:timestamp(),
gen_server:start_link({local, ?MODULE}, ?MODULE, Seed, []).

-spec generate() -> nonempty_string().
generate() ->
gen_server:call(?MODULE, random_string).
-spec string() -> nonempty_string().
string() ->
Length = get_random_length(),
string(Length).

-spec generate(pos_integer()) -> nonempty_string().
generate(Length) ->
gen_server:call(?MODULE, {random_string, Length}).
-spec string(pos_integer()) -> nonempty_string().
string(Length) ->
RandomAllowedChars = get_random_allowed_chars(),
[ random_alphanumeric(RandomAllowedChars)
|| _N <- lists:seq(1, Length)
].

-spec uniform(term()) -> non_neg_integer() | {error, {invalid_value, term()}}.
uniform(Max) when Max > 0->
rand:uniform(Max);
uniform(Max) ->
gen_server:call(?MODULE, {random_uniform, Max}).
{error, {invalid_value, Max}}.

-spec uniform(term(), term()) ->
non_neg_integer() | {error, {invalid_range, term(), term()}}.
uniform(Min, Max) when Max > Min ->
Min + rand:uniform(Max - Min + 1) - 1;
uniform(Min, Max) ->
gen_server:call(?MODULE, {random_uniform, Min, Max}).
{error, {invalid_range, Min, Max}}.

%% @doc Randomly chooses one element from the list
-spec pick([X, ...]) -> X.
pick(List) -> lists:nth(uniform(length(List)), List).

%% Callback implementation
-spec init(erlang:timestamp()) -> {ok, state()}.
init(Seed) ->
_ = random:seed(Seed),
{ok, {}}. % no state necessary, random uses process dictionary

-spec handle_call
(random_string, _, state()) ->
{reply, nonempty_string(), state()};
({random_string, pos_integer()}, _, state()) ->
{reply, nonempty_string(), state()};
({random_uniform, non_neg_integer()}, _, state()) ->
{reply, non_neg_integer(), state()};
({random_uniform, non_neg_integer(), non_neg_integer()}, _, state()) ->
{reply, non_neg_integer(), state()}.
handle_call(random_string, _From, State) ->
{reply, random_string(), State};
handle_call({random_string, Length}, _From, State) ->
{reply, random_string(Length), State};
handle_call({random_uniform, Max}, _From, State) ->
{reply, random_uniform(Max), State};
handle_call({random_uniform, Min, Max}, _From, State) ->
{reply, random_uniform(Min, Max), State}.

%% Unused callbacks
-spec handle_cast(_, state()) -> {noreply, state()}.
handle_cast(_Msg, State) -> {noreply, State}.

-spec handle_info(_, state()) -> {noreply, state()}.
handle_info(_Msg, State) -> {noreply, State}.

-spec terminate(_, state()) -> ok.
terminate(_Reason, _State) -> ok.

-spec code_change(_, state(), _) -> {ok, state()}.
code_change(_OldVersion, State, _Extra) -> {ok, State}.

%% internal
random_string() ->
Length = get_random_length(),
random_string_cont(Length).

random_string(Length) ->
random_string_cont(Length).

random_string_cont(Length) ->
RandomAllowedChars = get_random_allowed_chars(),
[ random_alphanumeric(RandomAllowedChars)
|| _N <- lists:seq(1, Length)
].

random_uniform(Max) when Max > 0->
random:uniform(Max);
random_uniform(Max) ->
{error, {invalid_value, Max}}.

random_uniform(Min, Max) when Max > Min ->
Min + random:uniform(Max - Min + 1) - 1;
random_uniform(Min, Max) ->
{error, {invalid_range, Min, Max}}.

%% internal
random_alphanumeric(AllowedChars) ->
Length = erlang:length(AllowedChars),
lists:nth(random:uniform(Length), AllowedChars).
lists:nth(rand:uniform(Length), AllowedChars).

get_random_length() ->
case application:get_env(katana, random_length) of
Expand Down
5 changes: 4 additions & 1 deletion test/ktn_meta_SUITE.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-module(ktn_meta_SUITE).

-export([all/0]).
-export([init_per_suite/1]).
-export([init_per_suite/1, end_per_suite/1]).
-export([xref/1, dialyzer/1, elvis/1]).

-type config() :: [{atom(), term()}].
Expand All @@ -12,6 +12,9 @@ all() -> [dialyzer, elvis, xref].
-spec init_per_suite(config()) -> config().
init_per_suite(Config) -> [{application, katana} | Config].

-spec end_per_suite(config()) -> config().
end_per_suite(Config) -> Config.

%% @doc xref's your code using xref_runner.
%% Available Options:
%% - xref_config: Configuration for xref_runner
Expand Down
23 changes: 8 additions & 15 deletions test/ktn_random_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
-export([
all/0,
init_per_suite/1,
end_per_suite/1,
init_per_testcase/2
end_per_suite/1
]).

-export([
generate/1,
string/1,
uniform/1,
pick/1
]).
Expand All @@ -19,8 +18,7 @@
all,
test,
init_per_suite,
end_per_suite,
init_per_testcase
end_per_suite
]).

-type config() :: [{atom(), term()}].
Expand All @@ -42,20 +40,15 @@ init_per_suite(Config) ->
end_per_suite(Config) ->
Config.

-spec init_per_testcase(atom(), config()) -> config().
init_per_testcase(_, Config) ->
{ok, _} = ktn_random:start_link(),
Config.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Test Cases
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec generate(config()) -> ok.
generate(_Config) ->
true = is_list(ktn_random:generate()),
16 = length(ktn_random:generate()),
25 = length(ktn_random:generate(25)),
-spec string(config()) -> ok.
string(_Config) ->
true = is_list(ktn_random:string()),
16 = length(ktn_random:string()),
25 = length(ktn_random:string(25)),
ok.

-spec uniform(config()) -> ok.
Expand Down

0 comments on commit 5238008

Please sign in to comment.