aboutsummaryrefslogtreecommitdiffstats
path: root/lib/hipe/test/basic_SUITE_data/basic_lists.erl
blob: 264a7f86f6c7e739a79a7112fbae791cbd3b1af9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
%%% -*- erlang-indent-level: 2 -*-
%%%-------------------------------------------------------------------
%%% Author: Kostis Sagonas
%%%
%%% Contains tests that manipulate and pattern match against lists
%%% (perhaps by calling functions from the 'lists' module).
%%%-------------------------------------------------------------------
-module(basic_lists).

-export([test/0]).

test() ->
  ok = test_length(),
  ok = test_lists_key(),
  ok = test_lists_and_strings(),
  ok.

%%--------------------------------------------------------------------

test_length() ->
  Len = 42,
  Lst = mklist(Len, []),
  Len = iterate(100, Lst),
  ok.

mklist(0, L) -> L;
mklist(X, L) -> mklist(X-1, [X|L]).

iterate(0, L) -> len(L, 0);
iterate(X, L) -> len(L, 0), iterate(X-1, L).

len([_|X], L) -> len(X, L+1);
len([], L) -> L.

%%--------------------------------------------------------------------

test_lists_key() ->
  First = {x, 42.0},
  Second = {y, -77},
  Third = {z, [a, b, c], {5.0}},
  List = [First, Second, Third],
  {value, First} = key_search_find(42, 2, List),
  ok.

key_search_find(Key, Pos, List) ->
  case lists:keyfind(Key, Pos, List) of
    false ->
      false = lists:keysearch(Key, Pos, List);
    Tuple when is_tuple(Tuple) ->
      {value, Tuple} = lists:keysearch(Key, Pos, List)
  end.

%%--------------------------------------------------------------------

test_lists_and_strings() ->
  LL = ["H'A", " H'B", " H'C"],
  LL2 = lists:map(fun string:strip/1, LL),
  HexFormat = fun(X, Acc) -> {string:substr(X, 3), Acc} end,
  {LL3,_Ret} = lists:mapfoldl(HexFormat, 0, LL2),
  ["A", "B", "C"] = lists:sublist(LL3, 42),
  ok.