1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1999-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
21-module(bs_match_tail_SUITE).
22
23-author('bjorn@erix.ericsson.se').
24-export([all/0, suite/0,
25	 aligned/1,unaligned/1,zero_tail/1]).
26
27-include_lib("common_test/include/ct.hrl").
28
29suite() -> [{ct_hooks,[ts_install_cth]}].
30
31all() ->
32    [aligned, unaligned, zero_tail].
33
34
35%% Test aligned tails.
36aligned(Config) when is_list(Config) ->
37    Tail1 = mkbin([]),
38    {258,Tail1} = al_get_tail_used(mkbin([1,2])),
39    Tail2 = mkbin(lists:seq(1, 127)),
40    {35091,Tail2} = al_get_tail_used(mkbin([137,19|Tail2])),
41
42    64896 = al_get_tail_unused(mkbin([253,128])),
43    64895 = al_get_tail_unused(mkbin([253,127|lists:seq(42, 255)])),
44
45    Tail3 = mkbin(lists:seq(0, 19)),
46    {0,Tail1} = get_dyn_tail_used(Tail1, 0),
47    {0,Tail3} = get_dyn_tail_used(mkbin([Tail3]), 0),
48    {73,Tail3} = get_dyn_tail_used(mkbin([73|Tail3]), 8),
49
50    0 = get_dyn_tail_unused(mkbin([]), 0),
51    233 = get_dyn_tail_unused(mkbin([233]), 8),
52    23 = get_dyn_tail_unused(mkbin([23,22,2]), 8),
53    ok.
54
55al_get_tail_used(<<A:16,T/binary>>) -> {A,T}.
56al_get_tail_unused(<<A:16,_/binary>>) -> A.
57
58%% Test that an non-aligned tail cannot be matched out.
59unaligned(Config) when is_list(Config) ->
60    {'EXIT',{function_clause,_}} = (catch get_tail_used(mkbin([42]))),
61    {'EXIT',{{badmatch,_},_}} = (catch get_dyn_tail_used(mkbin([137]), 3)),
62    {'EXIT',{function_clause,_}} = (catch get_tail_unused(mkbin([42,33]))),
63    {'EXIT',{{badmatch,_},_}} = (catch get_dyn_tail_unused(mkbin([44]), 7)),
64    ok.
65
66get_tail_used(<<A:1,T/binary>>) -> {A,T}.
67
68get_tail_unused(<<A:15,_/binary>>) -> A.
69
70get_dyn_tail_used(Bin, Sz) ->
71    <<A:Sz,T/binary>> = Bin,
72    {A,T}.
73
74get_dyn_tail_unused(Bin, Sz) ->
75    <<A:Sz,_/binary>> = Bin,
76    A.
77
78%% Test that zero tails are tested correctly.
79zero_tail(Config) when is_list(Config) ->
80    7 = (catch test_zero_tail(mkbin([7]))),
81    {'EXIT',{function_clause,_}} = (catch test_zero_tail(mkbin([1,2]))),
82    {'EXIT',{function_clause,_}} = (catch test_zero_tail2(mkbin([1,2,3]))),
83    ok.
84
85test_zero_tail(<<A:8>>) -> A.
86
87test_zero_tail2(<<_A:4,_B:4>>) -> ok.
88
89mkbin(L) when is_list(L) -> list_to_binary(L).
90