Why am I getting error while running a eunit test?

242 views Asked by At

I am new to erlang and while learning I was trying to test the code written for Reverse Polish Notation Calculator. I have kept the test function written in a test file and then using the command "eunit:test(calc1,[verbose)], I get the error.

however, if i kept the source code and test function in same module... everything runs fine.... Please help me understand the error and how to resolve it.

May be correct me if I am doing anything wrong.

Source code module for RPN calc:

-module(calc1).

-export([calc/1]).

calc(L) when is_list(L) -> 

    [Result] = lists:foldl(fun calc/2, [], string:tokens(L," ")),

    Result.

calc("+", [N1,N2|Stack]) -> [N2+N1|Stack];

calc("-", [N1,N2|Stack]) -> [N2-N1|Stack];

calc("*", [N1,N2|Stack]) -> [N2*N1|Stack];

calc("/", [N1,N2|Stack]) -> [N2/N1|Stack];

calc("^", [N1,N2|Stack]) -> [math:pow(N2,N1)|Stack];

calc("ln", [N|Stack])    -> [math:log(N)|Stack];

calc("log10", [N|Stack]) -> [math:log10(N)|Stack];

calc("sum", Stack)   -> [lists:sum(Stack)];

calc("prod", Stack)  -> [lists:foldl(fun erlang:'*'/2, 1, Stack)];

calc(X, [Stack]) -> [read(X)|Stack].

%% read(String()) -> Int() | Float()

read(X) ->

    case string:to_float(X) of

        {error, no_float} -> list_to_integer(X);

        {F,_} -> F

    end.

test file code:

-module(calc1_tests).

-include_lib("eunit/include/eunit.hrl").

calc_test() ->
    87 = calc1:calc("90 3 -").

Test case was supposed to be passed but I am getting error.

10> c(calc1).
{ok,calc1}

11> c(calc1_tests). 

{ok,calc1_tests}

12> eunit:test(calc1).

calc1_tests: calc1_test (module 'calc1_tests')...*failed*

in function calc1:calc/2 (calc1.erl, line 22)

  called as calc("90",[])

in call from lists:foldl/3 (lists.erl, line 1263)

in call from calc1:calc/1 (calc1.erl, line 19)

in call from calc1_tests:calc_test/0 (calc1_tests.erl, line 6)

in call from eunit_test:'-mf_wrapper/2-fun-0-'/2 (eunit_test.erl, line 273)

in call from eunit_test:run_testfun/1 (eunit_test.erl, line 71)

in call from eunit_proc:run_test/1 (eunit_proc.erl, line 510)

in call from eunit_proc:with_timeout/3 (eunit_proc.erl, line 335)

**error:function_clause

  output:<<"">>
1

There are 1 answers

0
Roger Lipscombe On

This function clause is wrong:

calc(X, [Stack]) -> [read(X)|Stack].

You're attempting to match a list containing a single item, but by the time you get to the end of the RPN, you've actually got an empty list (stack). It should probably be calc(X, Stack) -> ...