1: %% 2: %% %CopyrightBegin% 3: %% 4: %% Copyright Ericsson AB 2008-2013. 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: 20: %% This module tests the ordsets, sets, and gb_sets modules. 21: %% 22: 23: -module(dict_SUITE). 24: 25: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 26: init_per_group/2,end_per_group/2, 27: init_per_testcase/2,end_per_testcase/2, 28: create/1,store/1]). 29: 30: -include_lib("test_server/include/test_server.hrl"). 31: 32: -import(lists, [foldl/3,reverse/1]). 33: 34: suite() -> [{ct_hooks,[ts_install_cth]}]. 35: 36: all() -> 37: [create, store]. 38: 39: groups() -> 40: []. 41: 42: init_per_suite(Config) -> 43: Config. 44: 45: end_per_suite(_Config) -> 46: ok. 47: 48: init_per_group(_GroupName, Config) -> 49: Config. 50: 51: end_per_group(_GroupName, Config) -> 52: Config. 53: 54: 55: init_per_testcase(_Case, Config) -> 56: Dog = ?t:timetrap(?t:minutes(5)), 57: [{watchdog,Dog}|Config]. 58: 59: end_per_testcase(_Case, Config) -> 60: Dog = ?config(watchdog, Config), 61: test_server:timetrap_cancel(Dog), 62: ok. 63: 64: create(Config) when is_list(Config) -> 65: test_all(fun create_1/1). 66: 67: create_1(M) -> 68: D0 = M(empty, []), 69: [] = M(to_list, D0), 70: 0 = M(size, D0), 71: D0. 72: 73: store(Config) when is_list(Config) -> 74: test_all([{0,132},{253,258},{510,514}], fun store_1/2). 75: 76: store_1(List, M) -> 77: D0 = M(from_list, List), 78: 79: %% Make sure that we get the same result by inserting 80: %% elements one at the time. 81: D1 = foldl(fun({K,V}, Dict) -> M(enter, {K,V,Dict}) end, 82: M(empty, []), List), 83: true = M(equal, {D0,D1}), 84: D0. 85: 86: %%% 87: %%% Helper functions. 88: %%% 89: 90: dict_mods() -> 91: Orddict = dict_test_lib:new(orddict, fun(X, Y) -> X == Y end), 92: Dict = dict_test_lib:new(dict, fun(X, Y) -> 93: lists:sort(dict:to_list(X)) == 94: lists:sort(dict:to_list(Y)) end), 95: Gb = dict_test_lib:new(gb_trees, fun(X, Y) -> 96: gb_trees:to_list(X) == 97: gb_trees:to_list(Y) end), 98: [Orddict,Dict,Gb]. 99: 100: test_all(Tester) -> 101: Pids = [spawn_tester(M, Tester) || M <- dict_mods()], 102: collect_all(Pids, []). 103: 104: spawn_tester(M, Tester) -> 105: Parent = self(), 106: spawn_link(fun() -> 107: random:seed(1, 2, 42), 108: S = Tester(M), 109: Res = {M(size, S),lists:sort(M(to_list, S))}, 110: Parent ! {result,self(),Res} 111: end). 112: 113: collect_all([Pid|Pids], Acc) -> 114: receive 115: {result,Pid,Result} -> 116: collect_all(Pids, [Result|Acc]) 117: end; 118: collect_all([], Acc) -> 119: all_same(Acc). 120: 121: test_all(ListTemplate, Tester) -> 122: List = random_list(ListTemplate), 123: test_all(fun(M) -> Tester(List, M) end). 124: 125: all_same([H|T]) -> 126: all_same_1(T, H). 127: 128: all_same_1([H|T], H) -> 129: all_same_1(T, H); 130: all_same_1([], _) -> ok. 131: 132: rnd_list(Sz) -> 133: rnd_list_1(Sz, []). 134: 135: random_list([{Low,High}|T]) -> 136: random_list(lists:seq(Low, High)++T); 137: random_list([Sz|T]) when is_integer(Sz) -> 138: rnd_list(Sz)++random_list(T); 139: random_list([]) -> []. 140: 141: rnd_list_1(0, Acc) -> 142: Acc; 143: rnd_list_1(N, Acc) -> 144: Key = atomic_rnd_term(), 145: Value = random:uniform(100), 146: rnd_list_1(N-1, [{Key,Value}|Acc]). 147: 148: atomic_rnd_term() -> 149: case random:uniform(3) of 150: 1 -> list_to_atom(integer_to_list($\s+random:uniform(94))++"rnd"); 151: 2 -> random:uniform(); 152: 3 -> random:uniform(50)-37 153: end.