Summary: Add support for tuples, including expressions, pattern matching and pulse models. Tuples are modeled similarly to `Cons` (lists): they have a dynamic type (including the size of the tuple) and elements as fields. Reviewed By: mmarescotti Differential Revision: D29875650 fbshipit-source-id: f0262a42cmaster
parent
489c55487b
commit
7107de7c8e
@ -0,0 +1,72 @@
|
|||||||
|
% Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
%
|
||||||
|
% This source code is licensed under the MIT license found in the
|
||||||
|
% LICENSE file in the root directory of this source tree.
|
||||||
|
|
||||||
|
-module(tuples).
|
||||||
|
|
||||||
|
-export([
|
||||||
|
test_first_Ok/0,
|
||||||
|
test_first_Bad/0,
|
||||||
|
test_second_Ok/0,
|
||||||
|
test_second_Bad/0,
|
||||||
|
test_third_Ok/0,
|
||||||
|
test_third_Bad/0,
|
||||||
|
test_nested_Ok/0,
|
||||||
|
test_nested_Bad/0
|
||||||
|
]).
|
||||||
|
|
||||||
|
% Call this method with warn(1) to trigger a warning to expect
|
||||||
|
warn(0) -> ok.
|
||||||
|
|
||||||
|
first({X, _, _}) -> X.
|
||||||
|
second({_, Y, _}) -> Y.
|
||||||
|
third({_, _, Z}) -> Z.
|
||||||
|
|
||||||
|
test_first_Ok() ->
|
||||||
|
N = first({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
1 -> ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_first_Bad() ->
|
||||||
|
N = first({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
1 -> warn(1)
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_second_Ok() ->
|
||||||
|
N = second({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
2 -> ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_second_Bad() ->
|
||||||
|
N = second({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
2 -> warn(1)
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_third_Ok() ->
|
||||||
|
N = third({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
3 -> ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_third_Bad() ->
|
||||||
|
N = third({1, 2, 3}),
|
||||||
|
case N of
|
||||||
|
3 -> warn(1)
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_nested_Ok() ->
|
||||||
|
N = first(second({1, {2, 3, 4}, 5})),
|
||||||
|
case N of
|
||||||
|
2 -> ok
|
||||||
|
end.
|
||||||
|
|
||||||
|
test_nested_Bad() ->
|
||||||
|
N = first(second({1, {2, 3, 4}, 5})),
|
||||||
|
case N of
|
||||||
|
2 -> warn(1)
|
||||||
|
end.
|
@ -0,0 +1,120 @@
|
|||||||
|
% Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
%
|
||||||
|
% This source code is licensed under the MIT license found in the
|
||||||
|
% LICENSE file in the root directory of this source tree.
|
||||||
|
|
||||||
|
-module(tuples).
|
||||||
|
|
||||||
|
-export([
|
||||||
|
test_size1_Ok/0,
|
||||||
|
test_size2_Bad/0,
|
||||||
|
test_size3_Bad/0,
|
||||||
|
test_elements1_Ok/0,
|
||||||
|
test_elements2_Bad/0,
|
||||||
|
test_elements3_Bad/0,
|
||||||
|
test_elements_partial1_Ok/0,
|
||||||
|
test_elements_partial2_Ok/0,
|
||||||
|
test_elements_partial3_Bad/0,
|
||||||
|
test_elements_partial4_Bad/0,
|
||||||
|
test_nested1_Ok/0,
|
||||||
|
test_nested2_Ok/0,
|
||||||
|
test_nested3_Bad/0,
|
||||||
|
test_nested4_Bad/0,
|
||||||
|
test_first1_Ok/0,
|
||||||
|
test_first2_Ok/0,
|
||||||
|
test_first3_Ok/0,
|
||||||
|
test_first4_Bad/0,
|
||||||
|
test_first5_Bad/0
|
||||||
|
]).
|
||||||
|
|
||||||
|
accepts_tuple_of_two({_, _}) -> ok.
|
||||||
|
|
||||||
|
test_size1_Ok() ->
|
||||||
|
accepts_tuple_of_two({1, 2}).
|
||||||
|
|
||||||
|
test_size2_Bad() ->
|
||||||
|
accepts_tuple_of_two({1, 2, 3}).
|
||||||
|
|
||||||
|
test_size3_Bad() ->
|
||||||
|
accepts_tuple_of_two({1}).
|
||||||
|
|
||||||
|
test_elements1_Ok() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{1, 2} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements2_Bad() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{1, 3} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements3_Bad() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{1, 2, 3} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements_partial1_Ok() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{1, _} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements_partial2_Ok() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{_, 2} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements_partial3_Bad() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{2, _} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_elements_partial4_Bad() ->
|
||||||
|
T = {1, 2},
|
||||||
|
{_, 1} = T,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
test_nested1_Ok() ->
|
||||||
|
T = {1, {1, 2}, 3},
|
||||||
|
{_, {_, 2}, 3} = T.
|
||||||
|
|
||||||
|
test_nested2_Ok() ->
|
||||||
|
T = {{1, 2, 3}, {1, 2}, 3},
|
||||||
|
{_, {_, 2}, 3} = T.
|
||||||
|
|
||||||
|
test_nested3_Bad() ->
|
||||||
|
T = {1, {1, 2, 3}, 3},
|
||||||
|
{_, {_, 2}, 3} = T.
|
||||||
|
|
||||||
|
test_nested4_Bad() ->
|
||||||
|
T = {1, {1, 2}, 5},
|
||||||
|
{_, {_, 2}, 3} = T.
|
||||||
|
|
||||||
|
first_from_at_most_three({X}) -> X;
|
||||||
|
first_from_at_most_three({X, _}) -> X;
|
||||||
|
first_from_at_most_three({X, _, _}) -> X.
|
||||||
|
|
||||||
|
test_first1_Ok() ->
|
||||||
|
first_from_at_most_three({1}).
|
||||||
|
|
||||||
|
test_first2_Ok() ->
|
||||||
|
first_from_at_most_three({1, 2}).
|
||||||
|
|
||||||
|
test_first3_Ok() ->
|
||||||
|
first_from_at_most_three({1, 2, 3}).
|
||||||
|
|
||||||
|
test_first4_Bad() ->
|
||||||
|
first_from_at_most_three({}).
|
||||||
|
|
||||||
|
test_first5_Bad() ->
|
||||||
|
first_from_at_most_three({1, 2, 3, 4}).
|
||||||
|
|
||||||
|
accepts_empty({}) -> ok.
|
||||||
|
|
||||||
|
test_empty1_Ok() ->
|
||||||
|
accepts_empty({}).
|
||||||
|
|
||||||
|
test_empty2_Bad() ->
|
||||||
|
accepts_empty({1}).
|
||||||
|
|
||||||
|
test_empty3_Bad() ->
|
||||||
|
accepts_empty({1, 2}).
|
Loading…
Reference in new issue