1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2000-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19 20-module(random_SUITE). 21-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 22 init_per_testcase/2, end_per_testcase/2, 23 init_per_group/2,end_per_group/2]). 24 25-export([interval_1/1, seed0/1, seed/1]). 26 27 28-include_lib("common_test/include/ct.hrl"). 29 30init_per_testcase(_Case, Config) -> 31 Config. 32 33end_per_testcase(_Case, _Config) -> 34 ok. 35 36suite() -> 37 [{ct_hooks,[ts_install_cth]}, 38 {timetrap,{minutes,1}}]. 39 40all() -> 41 [interval_1, seed0, seed]. 42 43groups() -> 44 []. 45 46init_per_suite(Config) -> 47 Config. 48 49end_per_suite(_Config) -> 50 ok. 51 52init_per_group(_GroupName, Config) -> 53 Config. 54 55end_per_group(_GroupName, Config) -> 56 Config. 57 58 59%% Test that seed is set implicitly, and always the same. 60seed0(Config) when is_list(Config) -> 61 Self = self(), 62 _ = spawn(fun() -> Self ! random:uniform() end), 63 F1 = receive 64 Fa -> Fa 65 end, 66 _ = spawn(fun() -> random:seed(), 67 Self ! random:uniform() end), 68 F2 = receive 69 Fb -> Fb 70 end, 71 F1 = F2, 72 ok. 73 74%% Test that seed/1 and seed/3 are equivalent. 75seed(Config) when is_list(Config) -> 76 Self = self(), 77 Seed = {S1, S2, S3} = erlang:timestamp(), 78 _ = spawn(fun() -> 79 random:seed(S1,S2,S3), 80 Rands = lists:foldl(fun 81 (_, Out) -> [random:uniform(10000)|Out] 82 end, [], lists:seq(1,100)), 83 Self ! {seed_test, Rands} 84 end), 85 Rands1 = receive {seed_test, R1s} -> R1s end, 86 _ = spawn(fun() -> 87 random:seed(Seed), 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 Rands2 = receive {seed_test, R2s} -> R2s end, 94 Rands1 = Rands2, 95 ok. 96 97 98%% Check that uniform/1 returns values within the proper interval. 99interval_1(Config) when is_list(Config) -> 100 Top = 7, 101 N = 10, 102 check_interval(N, Top), 103 ok. 104 105check_interval(0, _) -> ok; 106check_interval(N, Top) -> 107 X = random:uniform(Top), 108 if 109 X < 1 -> 110 ct:fail(too_small); 111 X > Top -> 112 ct:fail(too_large); 113 true -> 114 ok 115 end, 116 check_interval(N-1, Top). 117