Commits

Peer Stritzinger committed 37dade9

Add tests and fix error cases that didn't pass

  • Participants
  • Parent commits 3786e1a

Comments (0)

Files changed (2)

+
+all:
+	erl -make
+
+clean:
+	-rm ebin/*.beam
+
+test: all
+	erl -noshell -pa ebin -eval 'eunit:test("ebin", [verbose])' -s init stop
 
 -module(ndarray). 
 
+-include_lib("eunit/include/eunit.hrl").
+
 -export([new/3, new/4, to_list/1]). 
 
 -record(ndarray, {dimensions, strides, type, elem_size, data}).
 new(Dims, Type, Elem_size) ->
     new(Dims, Type, Elem_size, 0).
 
-new(Dims, Type, Elem_size, Value) ->
+new([_|_]=Dims, Type, Elem_size, Value) ->
+    case [ X || X <- Dims,  not(is_integer(X) and (X > 0))] of
+	[] -> ok;
+	_ -> erlang:error(badarg)
+    end,
     Count = lists:foldl(fun(X, Prod) -> X*Prod end, 1, Dims),
     {Strides, _Bc} = lists:mapfoldl(fun(X, Prod) -> {Prod, X*Prod} end,  
 				    Elem_size, lists:reverse(Dims)),
 %% are those of the authors and should not be interpreted as representing
 %% official policies, either expressed or implied, of Peer Stritzinger GmbH.
 
+new_test_() ->
+    [?_assertEqual([[0,0],[0,0],[0,0]], to_list(new([3,2], integer, 3))),
+     ?_assertEqual([[1,1,1],[1,1,1]], to_list(new([2,3], integer, 8, 1))),
+     ?_assertError(badarg, to_list(new([-1,3], integer, 8, 1))),
+     ?_assertError(badarg, to_list(new([2,0], integer, 8, 1))),
+     ?_assertError(function_clause, to_list(new([], integer, 8, 1)))].