test/src/test_cut.erl
author Simon MacMullen <simon@rabbitmq.com>
Thu Jan 24 13:12:27 2013 +0000 (15 months ago)
changeset 330 49b441575ff0
parent 92 a030c880ef75
permissions -rw-r--r--
Merge bug25343
     1 %% The contents of this file are subject to the Mozilla Public License
     2 %% Version 1.1 (the "License"); you may not use this file except in
     3 %% compliance with the License. You may obtain a copy of the License
     4 %% at http://www.mozilla.org/MPL/
     5 %%
     6 %% Software distributed under the License is distributed on an "AS IS"
     7 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
     8 %% the License for the specific language governing rights and
     9 %% limitations under the License.
    10 %%
    11 %% The Original Code is Erlando.
    12 %%
    13 %% The Initial Developer of the Original Code is VMware, Inc.
    14 %% Copyright (c) 2011-2013 VMware, Inc.  All rights reserved.
    15 %%
    16 
    17 -module(test_cut).
    18 -compile({parse_transform, cut}).
    19 -compile({parse_transform, do}).
    20 
    21 -compile(export_all).
    22 
    23 -record(r, { f1 = false,
    24              f2 = wibble,
    25              f3 = juice }).
    26 
    27 test_cut() ->
    28     F0 = foo(a, b, _, 5+6, _),
    29     F1 = F0(_, e),
    30     ok = F1(c),
    31     F2 = _(1,2),
    32     3 = F2(fun erlang:'+'/2),
    33     -1 = F2(fun erlang:'-'/2),
    34     F3 = _:foo(a, b, c, _, e),
    35     ok = F3(?MODULE, 11),
    36     F4 = _:_(_), %% this isn't getting silly at all...
    37     true = 3 == F4(math, sqrt, 9).
    38 
    39 foo(a, b, c, 11, e) -> ok.
    40 
    41 test_cut_nested() ->
    42     F = f1(1, f2(1 + _), _),
    43     %% should be:
    44     %% F = \X -> f1(1, f2(\Y -> 1 + Y), X)
    45     ok = F(3).
    46 
    47 f1(N, M, L) when N + M =:= L -> ok.
    48 f2(Nf) -> Nf(1).
    49 
    50 test_cut_op() ->
    51     F = 1 + _,
    52     3 = F(2).
    53 
    54 test_cut_unary_op() ->
    55     F = -_,
    56     0 = 1 + F(1).
    57 
    58 test_cut_tuple() ->
    59     {foo, _} = {foo, F} = {foo, {bar, _}},
    60     {bar, qux} = F(qux).
    61 
    62 test_cut_record() ->
    63     true = #r{} =/= #r{f3 = _},
    64     orange = ((#r{f3 = _})(orange))#r.f3,
    65     {r, foo, bar, baz} = (#r{f1 = _, f3 = _, f2 = _})(foo, baz, bar),
    66     R = #r{},
    67     F = R#r{f3 = _, f2 = _},
    68     wobble = (F(orange, wobble))#r.f2,
    69     Getter = _#r.f2,
    70     wibble = Getter(R),
    71     Setter = _#r{f2 = gerbil},
    72     gerbil = Getter(Setter(R)),
    73     Setter2 = _#r{f2 = _},
    74     hamster = Getter(Setter2(R, hamster)).
    75 
    76 test_cut_record_nested() ->
    77     F = #r{f1 = #r{f1 = _, f3 = _}, f2 = _},
    78     R = F(apple),
    79     F1 = R#r.f1,
    80     #r{f1 = orange, f3 = banana} = F1(orange, banana).
    81 
    82 test_cut_binary() ->
    83     <<"AbA", _/binary>> = (<<65, _, 65>>)($b),
    84     F = <<_:_>>,
    85     G = F(15, _),
    86     <<>> = G(0),
    87     <<1:1/unsigned, 1:1/unsigned, 1:1/unsigned, 1:1/unsigned>> = G(4).
    88 
    89 test_cut_list() ->
    90     F = [_|_],
    91     [a,b] = F(a,[b]),
    92     G = [_, _ | [33]],
    93     [a,b,33] = G(a,b),
    94 
    95     H = [1, _, _, [_], 5 | [6, [_] | [_]]],
    96     %% This is the same as:
    97     %%  [1, _, _, [_], 5, 6, [_], _]
    98     %% And thus becomes
    99     %% \A, B, C -> [1, A, B, \D -> [D], 5, 6, \E -> [E], C]
   100     [1, 2, 3, H1, 5, 6, H2, 8] = H(2, 3, 8),
   101     [4] = H1(4),
   102     [7] = H2(7),
   103 
   104     I = [_, [_]],
   105     [a, I1] = I(a),
   106     [b] = I1(b).
   107 
   108 test_cut_case() ->
   109     F = case _ of
   110             N when is_integer(N) andalso 0 =:= (N rem 2) ->
   111                 even;
   112             N when is_integer(N) ->
   113                 odd;
   114             _ ->
   115                 not_a_number
   116         end,
   117     even = F(1234),
   118     odd = F(6789),
   119     not_a_number = F(my_atom).
   120 
   121 test_cut_comprehensions() ->
   122     F = << <<(1 + (X*2))>> || _ <- _, X <- _ >>, %% Note, this'll only be a /2 !
   123     <<"AAA">> = F([a,b,c], [32]),
   124     F1 = [ {X, Y, Z} || X <- _, Y <- _, Z <- _,
   125                         math:pow(X,2) + math:pow(Y,2) == math:pow(Z,2) ],
   126     [{3,4,5}, {4,3,5}, {6,8,10}, {8,6,10}] =
   127         lists:usort(F1(lists:seq(1,10), lists:seq(1,10), lists:seq(1,10))).
   128 
   129 test() ->
   130     test:test([{?MODULE, [test_cut,
   131                           test_cut_nested,
   132                           test_cut_op,
   133                           test_cut_unary_op,
   134                           test_cut_tuple,
   135                           test_cut_record,
   136                           test_cut_record_nested,
   137                           test_cut_binary,
   138                           test_cut_list,
   139                           test_cut_case,
   140                           test_cut_comprehensions]}],
   141               [report, {name, ?MODULE}]).