Skip to content
Browse files

Adding framework for interface w/ R via command line so QC tests can …

…validate results
  • Loading branch information...
1 parent 8a6d673 commit 7a87ffb4bab98a4cd0c8c88cbf8b3902c14d2bbf @dizzyd dizzyd committed Dec 7, 2009
Showing with 95 additions and 2 deletions.
  1. +2 −1 ebin/stats.app
  2. +2 −0 include/stats.hrl
  3. +2 −1 src/stats_sample.erl
  4. +89 −0 src/stats_utils.erl
View
3 ebin/stats.app
@@ -2,7 +2,8 @@
[{description, "Erlang Statistics Library"},
{vsn, "1"},
{modules, [ stats_sample,
- stats_histogram ]},
+ stats_histogram,
+ stats_utils]},
{registered, []},
{applications, [kernel,
stdlib,
View
2 include/stats.hrl
@@ -0,0 +1,2 @@
+
+-define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))).
View
3 src/stats_sample.erl
@@ -92,6 +92,7 @@ sdev(State) ->
-ifdef(EUNIT).
simple_test() ->
- ok.
+ {ok, [1,2,3,3,4,5]} = stats_utils:r_run([1,2,3,4,5], "summary(x)"),
+ {ok, [0,25,50,50,75,100]} = stats_utils:r_run(lists:seq(0,100), "summary(x)").
-endif.
View
89 src/stats_utils.erl
@@ -0,0 +1,89 @@
+%% -------------------------------------------------------------------
+%%
+%% stats: Statistics Suite for Erlang
+%%
+%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
+%%
+%% This file is provided to you under the Apache License,
+%% Version 2.0 (the "License"); you may not use this file
+%% except in compliance with the License. You may obtain
+%% a copy of the License at
+%%
+%% http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+%% -------------------------------------------------------------------
+-module(stats_utils).
+
+-include("stats.hrl").
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+-compile(export_all).
+-endif.
+
+%% ===================================================================
+%% Unit Test Helpers
+%% ===================================================================
+
+-ifdef(EUNIT).
+
+r_run(Input, Command) ->
+ case r_port() of
+ {ok, Port} ->
+ InputStr = [integer_to_list(I) || I <- Input],
+ port_command(Port, ?FMT("x <- c(~s)\n", [string:join(InputStr, ",")])),
+ port_command(Port, ?FMT("write(~s, ncolumns=1, file=stdout())\n", [Command])),
+ port_command(Port, "write('', file=stdout())\n"),
+ {ok, r_simple_read_loop(Port, [])};
+ {error, Reason} ->
+ {error, Reason}
+ end.
+
+r_port() ->
+ case erlang:get(r_port) of
+ undefined ->
+ Port = open_port({spawn, "R --vanilla --slave"},
+ [use_stdio, exit_status, {line, 16384},
+ stderr_to_stdout]),
+ erlang:put(r_port, Port);
+ Port ->
+ ok
+ end,
+
+ %% Check the status of the port
+ port_command(Port, "write('', file=stdout())\n"),
+ receive
+ {Port, {data, {eol, []}}} ->
+ {ok, Port};
+ {Port, {data, {eol, Other}}} ->
+ erlang:erase(r_port),
+ {error, Other}
+ end.
+
+r_simple_read_loop(Port, Acc) ->
+ receive
+ {Port, {data, {eol, []}}} ->
+ lists:reverse(Acc);
+ {Port, {data, {eol, Line}}} ->
+ r_simple_read_loop(Port, [to_number(Line) | Acc]);
+ {Port, {exit_status, _}} ->
+ lists:reverse(Acc)
+ end.
+
+to_number(Str) ->
+ case catch(list_to_integer(Str)) of
+ {'EXIT', _} ->
+ list_to_float(Str);
+ Value ->
+ Value
+ end.
+
+-endif.
+

0 comments on commit 7a87ffb

Please sign in to comment.
Something went wrong with that request. Please try again.