Commits

arkdro committed 2fffd60

add test for list operations

  • Participants
  • Parent commits 9f9d2f2

Comments (0)

Files changed (2)

erlang/test/num_SUITE.erl

 -define(d_precision_short, ?d_precision - 2).
 -define(d_context, [{precision, ?d_precision}]).
 -define(d_context_short, [{precision, ?d_precision_short}]).
+-define(d_one, {0, 1, 0}).
 
 % -----------------------------------------------------------------------------
 % Function: suite() -> Info
 % -----------------------------------------------------------------------------
 suite() ->
     [
+     %% {require, simple_operations},
+     %% {require, list_operations},
      {require, length},
      {require, odeci_prog},
      {timetrap,{seconds,10}}
 all() ->
     [
      %% start
+     test2,
      test1
     ].
 
      multiply
      ].
 
+vec_ops() ->
+    [
+     multiply
+    ].
+
 random_choice(List) ->
     Len = length(List),
     N = crypto:rand_uniform(1, Len + 1),
 
 %% this is necessary for divide only
 do_compare2({_S1, _M1, E1} = R1, {_S2, _M2, E2} = R2) ->
+    Allowed_delta = ct:get_config(allowed_delta_in_result, 1),
     {_Ds, Dm, De} = decimal:subtract(R1, R2),
     Min_exp = min(E1, E2),
-    if (Dm =< 1) and (De =< Min_exp) ->
+    if (Dm =< Allowed_delta) and (De =< Min_exp) ->
             Verbose = ct:get_config(verbose, 0),
             if Verbose > 3 ->
                     ct:pal("do_compare2 almost equal~nr1=~p~nr2=~p~n", [R1, R2]);
     Res = c_server:get_timing(),
     ct:pal("timing:~n~p~n", [Res]).
 
+do_vec_test_loop(Len) ->
+    Ops = vec_ops(),
+    Ignore = ct:get_config(ignore_errors),
+    do_vec_test_loop2(Len, Ops, Ignore).
+
+do_vec_test_loop2(0, _Ops, _Ignore) ->
+    ok;
+do_vec_test_loop2(Len, Ops, Ignore) ->
+    case do_one_vec_operation(Ops) of
+        {true, _Op, _I1, _O1, _O2} ->
+            do_vec_test_loop2(Len-1, Ops, Ignore);
+        {false, _Op, _I1, _O1, _O2} when Ignore == true ->
+            do_vec_test_loop2(Len-1, Ops, Ignore);
+        {false, Op, I1, O1, O2} ->
+            ct:pal("one vec op error, op=~p~n",
+                   "i1=~p~n",
+                   "o1=~p~no2=~p~n",
+                   [Op, I1, O1, O2])
+    end.
+
+do_one_vec_operation(Ops) ->
+    Cur = random_choice(Ops),
+    D1 = gen_decimal_vec(),
+    {R1, R2} = do_vec_op(Cur, D1),
+    Verbose = ct:get_config(verbose, 0),
+    if Verbose > 2 ->
+            ct:pal("do_one_operation,~n"
+                   "cur op: ~p,~n"
+                   "d1=~p~n"
+                   "r1=~p~nr2=~p~n", [Cur, D1, R1, R2]);
+       true ->
+            ok
+    end,
+    Res = compare(R1, R2),
+    {Res, Cur, D1, R1, R2}.
+
+gen_decimal_vec() ->
+    Len = ct:get_config(vector_length, 10),
+    [gen_one_decimal() || _ <- lists:duplicate(Len, true)].
+
+do_vec_op(multiply, D1) ->
+    Res1 = call_ocaml_vector(multiply, D1),
+    Res2 = call_decimal_vector(multiply, D1),
+    {Res1, Res2}.
+
+call_ocaml_vector(Op, D1) ->
+    Flag = ct:get_config(do_ocaml_calc, false),
+    call_ocaml_vector(Flag, Op, D1).
+
+call_ocaml_vector(false, _, _D1) ->
+    no_ocaml_calc_configured;
+call_ocaml_vector(true, multiply, D1) ->
+    c_server:multiply_list(D1).
+
+call_decimal_vector(Op, D1) ->
+    Flag = ct:get_config(do_decimal_calc, false),
+    call_decimal_vector(Flag, Op, D1).
+
+call_decimal_vector(false, _, _List) ->
+    true;
+call_decimal_vector(true, multiply, List) ->
+    F = fun(X, Acc) ->
+                decimal:multiply(X, Acc, ?d_context)
+        end,
+    lists:foldl(F, ?d_one, List).
+
 % -----------------------------------------------------------------------------
 % Function: TestCase(Config0) ->
 %               ok | exit() | {skip,Reason} | {comment,Comment} |
     start_prog(Config).
 
 test1(Config) ->
+    case ct:require(simple_operations) of
+        ok ->
+            test1_ok(Config);
+        _ ->
+            ct:pal("simple operations test skipped")
+    end.
+
+test1_ok(Config) ->
     start_prog(Config),
     Warmup = ct:get_config(warm_up_time, 1000),
     timer:sleep(Warmup),
     ct:timetrap(round(Len) * 10 + 10000),
     do_test_loop(Len),
     log_timing().
+
+%% list operations
+test2(Config) ->
+    case ct:require(list_operations) of
+        ok ->
+            test2_ok(Config);
+        _ ->
+            ct:pal("list operations test skipped")
+    end.
+
+test2_ok(Config) ->
+    ok = ct:require(list_operations),
+    start_prog(Config),
+    Warmup = ct:get_config(warm_up_time, 1000),
+    timer:sleep(Warmup),
+    Len = ct:get_config(length, 1),
+    ct:timetrap(round(Len) * 10 + 10000),
+    do_vec_test_loop(Len),
+    log_timing().

erlang/test/test.config

 %%-*- mode: erlang -*-
 %% vi: filetype=erlang ts=4 expandtab
 
+%% which test to run. They should be run separately because ct starts
+%% nodes with the very same names
+%{simple_operations, true}.
+{list_operations, true}.
+
 {verbose, 0}.
 %% {odeci_node, 't2@localhost.localdomain'}.
 %{odeci_prog, 't1.sh'}. %% relative to priv/
 {odeci_prog, 'odec_port.native'}. %% relative to priv/
 
 {warm_up_time, 1000}. %% time between odeci_prog start and tests
-{length, 100000}. %% length of test sequence
+{length, 1000}. %% length of test sequence
+{vector_length, 10}. %% length of one list to operate on
 
 %% random sign for decimals
 {random_sign, true}.
 
 %% random exponent for decimals, min, max.
 {random_exponent, true}.
-{exponent_min, -10}.
-{exponent_max, 10}.
+{exponent_min, -1}.
+{exponent_max, 1}.
 
 %% random mantissa for decimals, min, max.
 {random_mantissa, true}.
 {mantissa_min, 0}.
-{mantissa_max, 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999}.
+{mantissa_max, 99999999999999999999999}.
+
+{allowed_delta_in_result, 9}.
 
-{do_compare, true}.
+%{do_compare, true}.
 {do_ocaml_calc, true}.
-{do_decimal_calc, true}.
+%{do_decimal_calc, true}.