1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2000-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,groups/0,init_per_group/2,end_per_group/2,
25	 init_per_testcase/2,end_per_testcase/2,
26	 init_per_suite/1,end_per_suite/1,
27	 aligned/1,unaligned/1,zero_tail/1]).
28
29-include_lib("common_test/include/ct.hrl").
30
31suite() ->
32    [{ct_hooks,[ts_install_cth]},
33     {timetrap,{minutes,1}}].
34
35all() ->
36    cases().
37
38groups() ->
39    [].
40
41init_per_group(_GroupName, Config) ->
42    Config.
43
44end_per_group(_GroupName, Config) ->
45    Config.
46
47
48cases() ->
49    [aligned, unaligned, zero_tail].
50
51init_per_testcase(_Case, Config) ->
52    test_lib:interpret(?MODULE),
53    Config.
54
55end_per_testcase(_Case, _Config) ->
56    ok.
57
58init_per_suite(Config) when is_list(Config) ->
59    test_lib:interpret(?MODULE),
60    true = lists:member(?MODULE, int:interpreted()),
61    Config.
62
63end_per_suite(Config) when is_list(Config) ->
64    ok.
65
66%% Test aligned tails.
67aligned(Config) when is_list(Config) ->
68    Tail1 = mkbin([]),
69    {258,Tail1} = al_get_tail_used(mkbin([1,2])),
70    Tail2 = mkbin(lists:seq(1, 127)),
71    {35091,Tail2} = al_get_tail_used(mkbin([137,19|Tail2])),
72
73    64896 = al_get_tail_unused(mkbin([253,128])),
74    64895 = al_get_tail_unused(mkbin([253,127|lists:seq(42, 255)])),
75
76    Tail3 = mkbin(lists:seq(0, 19)),
77    {0,Tail1} = get_dyn_tail_used(Tail1, 0),
78    {0,Tail3} = get_dyn_tail_used(mkbin([Tail3]), 0),
79    {73,Tail3} = get_dyn_tail_used(mkbin([73|Tail3]), 8),
80
81    0 = get_dyn_tail_unused(mkbin([]), 0),
82    233 = get_dyn_tail_unused(mkbin([233]), 8),
83    23 = get_dyn_tail_unused(mkbin([23,22,2]), 8),
84    ok.
85
86al_get_tail_used(<<A:16,T/binary>>) -> {A,T}.
87al_get_tail_unused(<<A:16,_/binary>>) -> A.
88
89%% Test that an non-aligned tail cannot be matched out.
90unaligned(Config) when is_list(Config) ->
91    {'EXIT',{function_clause,_}} = (catch get_tail_used(mkbin([42]))),
92    {'EXIT',{{badmatch,_},_}} = (catch get_dyn_tail_used(mkbin([137]), 3)),
93    {'EXIT',{function_clause,_}} = (catch get_tail_unused(mkbin([42,33]))),
94    {'EXIT',{{badmatch,_},_}} = (catch get_dyn_tail_unused(mkbin([44]), 7)),
95    ok.
96
97get_tail_used(<<A:1,T/binary>>) -> {A,T}.
98
99get_tail_unused(<<A:15,_/binary>>) -> A.
100
101get_dyn_tail_used(Bin, Sz) ->
102    <<A:Sz,T/binary>> = Bin,
103    {A,T}.
104
105get_dyn_tail_unused(Bin, Sz) ->
106    <<A:Sz,_/binary>> = Bin,
107    A.
108
109%% Test that zero tails are tested correctly.
110zero_tail(Config) when is_list(Config) ->
111    7 = (catch test_zero_tail(mkbin([7]))),
112    {'EXIT',{function_clause,_}} = (catch test_zero_tail(mkbin([1,2]))),
113    {'EXIT',{function_clause,_}} = (catch test_zero_tail2(mkbin([1,2,3]))),
114    ok.
115
116test_zero_tail(<<A:8>>) -> A.
117
118test_zero_tail2(<<_A:4,_B:4>>) -> ok.
119
120mkbin(L) when is_list(L) -> list_to_binary(L).
121