|
| 1 | +%% EXPECT: binary_split_options_ok |
| 2 | +%% Regression: native binary:split ignored trim, scope and unknown options, |
| 3 | +%% and rounded a partial-byte subject up to a binary. The CSV and streamed |
| 4 | +%% line inputs below exercise the reported defects and BEAM's option rules. |
| 5 | +%% Run this same fixture on native Nera and BEAM; reaching the marker is required. |
| 6 | +-module(binary_split_options). |
| 7 | +-export([test/0]). |
| 8 | + |
| 9 | +test() -> |
| 10 | + %% trim removes only trailing empty fields; trim_all removes every empty |
| 11 | + %% field, even when trim is also present, independent of option order. |
| 12 | + lists:foreach(fun check/1, |
| 13 | + [{<<"a,b,,">>, <<",">>, [global, trim], [<<"a">>, <<"b">>]}, |
| 14 | + {<<",a,,b,,">>, <<",">>, [trim, global], [<<>>, <<"a">>, <<>>, <<"b">>]}, |
| 15 | + {<<"a,,">>, <<",">>, [trim], [<<"a">>, <<",">>]}, |
| 16 | + {<<"a,">>, <<",">>, [trim], [<<"a">>]}, |
| 17 | + {<<",">>, <<",">>, [trim], []}, |
| 18 | + {<<>>, <<",">>, [], [<<>>]}, |
| 19 | + {<<>>, <<",">>, [trim], []}, |
| 20 | + {<<",,">>, <<",">>, [global, trim], []}, |
| 21 | + {<<"a,b">>, <<";">>, [trim], [<<"a,b">>]}, |
| 22 | + {<<",a,,b,,">>, <<",">>, [trim_all, trim, global], [<<"a">>, <<"b">>]}, |
| 23 | + {<<",a,,b,,">>, <<",">>, [trim, global, trim_all], [<<"a">>, <<"b">>]}, |
| 24 | + {<<"a,b,,">>, <<",">>, [global, global, trim, trim], [<<"a">>, <<"b">>]}, |
| 25 | + %% BEAM accepts an improper tail after the final option cell. |
| 26 | + {<<"a,b,,">>, <<",">>, [global, trim | tail], [<<"a">>, <<"b">>]}, |
| 27 | + %% A scope restricts matching, not the returned subject's prefix/suffix. |
| 28 | + {<<"a,b,c">>, <<",">>, [global, {scope, {2, 3}}], [<<"a,b">>, <<"c">>]}, |
| 29 | + {<<"a,b,c">>, <<",">>, [{scope, {2, 3}}], [<<"a,b">>, <<"c">>]}, |
| 30 | + {<<"a,b,c">>, <<",">>, [global, {scope, {5, -3}}], [<<"a,b">>, <<"c">>]}, |
| 31 | + {<<"a,b,c">>, <<",">>, [global, {scope, {5, -5}}], [<<"a">>, <<"b">>, <<"c">>]}, |
| 32 | + {<<"a,b,c">>, <<",">>, [{scope, {0, 0}}], [<<"a,b,c">>]}, |
| 33 | + {<<"a,b,c">>, <<",">>, [{scope, {5, 0}}, trim], [<<"a,b,c">>]}, |
| 34 | + {<<"a,b,c">>, <<",">>, [{scope, {2, 1}}], [<<"a,b,c">>]}, |
| 35 | + {<<>>, <<",">>, [{scope, {0, 0}}, trim], []}, |
| 36 | + {<<"a,b,c">>, <<",">>, [{scope, {0, 2}}, {scope, {2, 3}}, global], |
| 37 | + [<<"a,b">>, <<"c">>]}, |
| 38 | + {<<"a,b,c">>, <<",">>, [{scope, {2, 3}}, {scope, {0, 2}}, global], |
| 39 | + [<<"a">>, <<"b,c">>]}, |
| 40 | + {<<"a,b,,">>, <<",">>, [global, trim, {scope, {0, 4}}], [<<"a">>, <<"b">>, <<",">>]}, |
| 41 | + %% Only whole matches inside the scope count; the longest eligible |
| 42 | + %% pattern wins when a longer pattern crosses the scope's end. |
| 43 | + {<<"one\r\ntwo\r\n">>, [<<"\r\n">>], [global, {scope, {0, 4}}], |
| 44 | + [<<"one\r\ntwo\r\n">>]}, |
| 45 | + {<<"one\r\ntwo\r\n">>, [<<"\r\n">>, <<"\r">>], [global, {scope, {0, 4}}], |
| 46 | + [<<"one">>, <<"\ntwo\r\n">>]}, |
| 47 | + {<<"one\r\ntwo\r\n">>, [<<"\r\n">>, <<"\r">>], [global, trim, {scope, {3, 7}}], |
| 48 | + [<<"one">>, <<"two">>]}]), |
| 49 | + |
| 50 | + %% A scope is relative to a sub-binary, not to its retained backing bytes. |
| 51 | + Line = <<"data: hi\r\n">>, |
| 52 | + Width = byte_size(Line), |
| 53 | + Stream = iolist_to_binary(lists:duplicate(100, Line)), |
| 54 | + Sub = binary:part(Stream, Width, 10 * Width), |
| 55 | + Prefix = iolist_to_binary([lists:duplicate(9, Line), <<"data: hi">>]), |
| 56 | + check({Sub, [<<"\r\n">>, <<"\n">>], [global, trim, {scope, {9 * Width, Width}}], |
| 57 | + [Prefix]}), |
| 58 | + <<_:3, Shifted:5/binary, _:5>> = id(<<0:3, "a,b,c", 0:5>>), |
| 59 | + check({Shifted, <<",">>, [global, {scope, {2, 3}}], [<<"a,b">>, <<"c">>]}), |
| 60 | + |
| 61 | + %% Unknown cells and malformed or out-of-range scopes raise badarg, even |
| 62 | + %% when no match is possible. Every scope is checked before the last wins. |
| 63 | + lists:foreach(fun(Options) -> |
| 64 | + lists:foreach(fun(S) -> check_badarg(S, <<",">>, Options) end, |
| 65 | + [<<"a,b,c">>, <<>>]) |
| 66 | + end, [[foo], [global, foo], [trim, 42], [{foo, {0, 1}}], [global, {trim}], |
| 67 | + foo, 42, {}, <<>>, [{scope}], [{scope, 1}], [{scope, {0}}], |
| 68 | + [{scope, {0, 1, 2}}], [{scope, {a, 1}}], [{scope, {0, 1.0}}], |
| 69 | + [{scope, {-1, 1}}], [{scope, {6, 0}}], [{scope, {0, 6}}], |
| 70 | + [{scope, {2, -3}}], [{scope, {0, 1 bsl 100}}], |
| 71 | + [{scope, {1 bsl 100, 0}}], [{scope, {0, -(1 bsl 100)}}], |
| 72 | + [{scope, {16#7fffffffffffffff, 1}}], |
| 73 | + [{scope, {99, 0}}, {scope, {0, 0}}], |
| 74 | + [{scope, {0, 0}}, {scope, {99, 0}}]]), |
| 75 | + lists:foreach(fun(S) -> |
| 76 | + check_badarg(S, <<"a">>, []), |
| 77 | + check_badarg(S, <<"a">>, [global, trim, {scope, {0, 0}}]), |
| 78 | + badarg = failure(fun() -> binary:split(id(S), <<"a">>) end), |
| 79 | + badarg = failure(fun() -> apply(binary, split, [S, <<"a">>]) end), |
| 80 | + badarg = failure(fun() -> (fun binary:split/2)(S, <<"a">>) end) |
| 81 | + end, [<<1:3>>, <<"data", 1:1>>, atom, 42, []]), |
| 82 | + io:format("binary_split_options_ok~n"), |
| 83 | + ok. |
| 84 | + |
| 85 | +check({Subject, Pattern, Options, Expected}) -> |
| 86 | + Expected = binary:split(id(Subject), Pattern, Options), |
| 87 | + Expected = apply(binary, split, [Subject, Pattern, Options]), |
| 88 | + Expected = (fun binary:split/3)(Subject, Pattern, Options). |
| 89 | + |
| 90 | +check_badarg(Subject, Pattern, Options) -> |
| 91 | + badarg = failure(fun() -> binary:split(id(Subject), Pattern, Options) end), |
| 92 | + badarg = failure(fun() -> apply(binary, split, [Subject, Pattern, Options]) end), |
| 93 | + badarg = failure(fun() -> (fun binary:split/3)(Subject, Pattern, Options) end). |
| 94 | + |
| 95 | +failure(F) -> |
| 96 | + try F() of |
| 97 | + Value -> {unexpected_return, Value} |
| 98 | + catch |
| 99 | + error:Reason -> Reason |
| 100 | + end. |
| 101 | + |
| 102 | +id(X) -> X. |
0 commit comments