1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2000-2011. All Rights Reserved.
    5: %% 
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %% 
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %% 
   17: %% %CopyrightEnd%
   18: 
   19: -module(random_SUITE).
   20: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   21: 	 init_per_group/2,end_per_group/2]).
   22: 
   23: -export([interval_1/1, seed0/1, seed/1]).
   24: 
   25: -export([init_per_testcase/2, end_per_testcase/2]).
   26: 
   27: -include_lib("test_server/include/test_server.hrl").
   28: 
   29: % Default timetrap timeout (set in init_per_testcase).
   30: -define(default_timeout, ?t:minutes(1)).
   31: 
   32: init_per_testcase(_Case, Config) ->
   33:     ?line Dog = ?t:timetrap(?default_timeout),
   34:     [{watchdog, Dog} | Config].
   35: end_per_testcase(_Case, Config) ->
   36:     Dog = ?config(watchdog, Config),
   37:     test_server:timetrap_cancel(Dog),
   38:     ok.
   39: 
   40: suite() -> [{ct_hooks,[ts_install_cth]}].
   41: 
   42: all() -> 
   43:     [interval_1, seed0, seed].
   44: 
   45: groups() -> 
   46:     [].
   47: 
   48: init_per_suite(Config) ->
   49:     Config.
   50: 
   51: end_per_suite(_Config) ->
   52:     ok.
   53: 
   54: init_per_group(_GroupName, Config) ->
   55:     Config.
   56: 
   57: end_per_group(_GroupName, Config) ->
   58:     Config.
   59: 
   60: 
   61: seed0(doc) ->
   62:     ["Test that seed is set implicitly, and always the same."];
   63: seed0(suite) ->
   64:     [];
   65: seed0(Config) when is_list(Config) ->
   66:     ?line Self = self(),
   67:     ?line _ = spawn(fun() -> Self ! random:uniform() end),
   68:     ?line F1 = receive
   69: 		   Fa -> Fa
   70: 	  end,
   71:     ?line _ = spawn(fun() -> random:seed(),
   72: 			     Self ! random:uniform() end),
   73:     ?line F2 = receive
   74: 		   Fb -> Fb
   75: 	       end,
   76:     ?line F1 = F2,
   77:     ok.
   78: 
   79: seed(doc) ->
   80:     ["Test that seed/1 and seed/3 is equivalent."];
   81: seed(suite) ->
   82:     [];
   83: seed(Config) when is_list(Config) ->
   84:     ?line Self = self(),
   85:     ?line Seed = {S1, S2, S3} = now(),
   86:     ?line _ = spawn(fun() ->
   87:     	random:seed(S1,S2,S3),
   88:     	Rands = lists:foldl(fun
   89: 	    (_, Out) -> [random:uniform(10000)|Out]
   90: 	end, [], lists:seq(1,100)),
   91:     	Self ! {seed_test, Rands}
   92:     end),
   93:     ?line Rands1 = receive {seed_test, R1s} -> R1s end,
   94:     ?line _ = spawn(fun() ->
   95:     	random:seed(Seed),
   96:     	Rands = lists:foldl(fun
   97: 	    (_, Out) -> [random:uniform(10000)|Out]
   98: 	end, [], lists:seq(1,100)),
   99:     	Self ! {seed_test, Rands}
  100:     end),
  101:     ?line Rands2 = receive {seed_test, R2s} -> R2s end,
  102:     ?line Rands1 = Rands2,
  103:     ok.
  104: 
  105: 
  106: interval_1(doc) ->
  107:     ["Check that uniform/1 returns values within the proper interval."];
  108: interval_1(suite) ->
  109:     [];
  110: interval_1(Config) when is_list(Config) ->
  111:     ?line Top = 7,
  112:     ?line N = 10,
  113:     ?line check_interval(N, Top),
  114:     ok.
  115: 
  116: check_interval(0, _) -> ok;
  117: check_interval(N, Top) ->
  118:     X = random:uniform(Top),
  119:     if
  120: 	X < 1 ->
  121: 	    test_server:fail(too_small);
  122: 	X > Top ->
  123: 	    test_server:fail(too_large);
  124: 	true ->
  125: 	    ok
  126:     end,
  127:     check_interval(N-1, Top).