Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

eunit

  • Loading branch information...
commit 3cf013f00853495d5e6abd0274d83bc3d63349f9 1 parent 8550849
@irr authored
Showing with 31 additions and 1 deletion.
  1. +11 −1 et/src/etrader.erl
  2. +20 −0 et/src/etrader_stats.erl
View
12 et/src/etrader.erl
@@ -8,7 +8,7 @@
-export([start_link/0, stop/0, init/1,
handle_call/3, handle_cast/2, handle_info/2,
- terminate/2, code_change/3,
+ terminate/2, code_change/3, unconsult/1,
source/1, load/1, sma/1, ema/1]).
-define(SERVER, ?MODULE).
@@ -53,6 +53,9 @@ ema(N) ->
%% =======================
+unconsult(File) ->
+ gen_server:call(?SERVER, {unconsult, File}, timeout()).
+
state(S) ->
Size = case S#etrs.data of
undefined ->
@@ -78,6 +81,13 @@ handle_call({source, File}, _From, State) ->
NewState = State#etrs{csv = File},
{reply, ok, state(NewState)};
+handle_call({unconsult, File}, _From, State) ->
+ Data = State#etrs.data,
+ {ok, S} = file:open(File, write),
+ io:format(S, "~p.~n" ,[Data]),
+ file:close(S),
+ {reply, {ok, Data}, State};
+
handle_call(_Request, _From, State) ->
{noreply, State}.
View
20 et/src/etrader_stats.erl
@@ -8,6 +8,9 @@
-type ma_t() :: {queue(), array()}.
-include("etrader.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+-define(DATA, "../priv/test.dat").
%% R:
%% library(quantmod)
@@ -71,3 +74,20 @@ sma(A, N) ->
ema(A, N) ->
ma(array:size(A), A, N, 0, {queue:new(), array:new(array:size(A))}, fun em/4).
+%% erl -make && erl -pa ../ebin +K true +A 42 +B -run etrader_app start -etrader limit 10
+%% ps.: limit MUST BE 10 to pass tests!
+ma_test() ->
+ {ok, [A]} = file:consult(?DATA),
+ EMA = ema(A, 3),
+ ?assert(array:to_list(EMA) =:= [undefined, undefined,
+ 20.216089999999998, 20.06671, 20.265885,
+ 20.1911925, 20.17376625, 20.145133125, 19.7075765625,
+ 19.66307328125]),
+ SMA = sma(A, 3),
+ ?assert(array:to_list(SMA) =:= [undefined, undefined,
+ 20.216089999999998, 19.88413333333333, 20.166296666666664,
+ 20.166296666666668, 20.245966666666664, 20.12978, 19.847620000000003,
+ 19.668363333333332]).
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.