|
20 | 20 |
|
21 | 21 | -module(test_binary_split).
|
22 | 22 |
|
23 |
| --export([start/0, split_compare/3, split_compare/2, compare_bin/2, fail_split/1]). |
| 23 | +-export([start/0, split_compare/3, split_compare/2, compare_bin/2, fail_split/1, id/1]). |
24 | 24 |
|
25 | 25 | start() ->
|
26 |
| - split_compare(<<"Hello:World">>, <<"Hello">>, <<"World">>) + |
27 |
| - split_compare(<<"Hello:::World:">>, <<"Hello">>, <<"::World:">>) + |
28 |
| - split_compare(<<"Test:">>, <<"Test">>, <<>>) + |
29 |
| - split_compare(<<":">>, <<>>, <<>>) + |
30 |
| - split_compare(<<>>, <<>>) + |
31 |
| - split_compare(<<"Test">>, <<>>) + |
32 |
| - split_compare2(<<"Test">>, <<>>) + |
33 |
| - split_compare2(<<"helloSEPARATORworld">>, <<"hello">>, <<"world">>) + |
34 |
| - fail_split(<<>>) + |
35 |
| - fail_split({1, 2}) + |
36 |
| - fail_split2({1, 2}). |
| 26 | + ok = split_compare(<<"Hello:World">>, <<"Hello">>, <<"World">>), |
| 27 | + ok = split_compare(<<"Hello:::World:">>, <<"Hello">>, <<"::World:">>), |
| 28 | + ok = split_compare(<<"Test:">>, <<"Test">>, <<>>), |
| 29 | + ok = split_compare(<<":">>, <<>>, <<>>), |
| 30 | + ok = split_compare(<<>>, <<>>), |
| 31 | + ok = split_compare(<<"Test">>, <<>>), |
| 32 | + ok = split_compare2(<<"Test">>, <<>>), |
| 33 | + ok = split_compare2(<<"helloSEPARATORworld">>, <<"hello">>, <<"world">>), |
| 34 | + ok = fail_split(<<>>), |
| 35 | + ok = fail_split({1, 2}), |
| 36 | + ok = fail_split2({1, 2}), |
| 37 | + case erlang:system_info(machine) of |
| 38 | + "BEAM" -> ok; |
| 39 | + "ATOM" -> ok = memory_allocation_split() |
| 40 | + end, |
| 41 | + 0. |
37 | 42 |
|
38 | 43 | split_compare(Bin, Part1) ->
|
39 | 44 | [A] = binary:split(Bin, <<":">>),
|
40 | 45 | compare_bin(Part1, A).
|
41 | 46 |
|
42 | 47 | split_compare(Bin, Part1, Part2) ->
|
43 | 48 | [A, B] = binary:split(Bin, <<":">>),
|
44 |
| - compare_bin(Part1, A) + compare_bin(B, Part2). |
| 49 | + ok = compare_bin(Part1, A), |
| 50 | + ok = compare_bin(B, Part2). |
45 | 51 |
|
46 | 52 | split_compare2(Bin, Part1) ->
|
47 | 53 | [A] = binary:split(Bin, <<"SEPARATOR">>),
|
48 | 54 | compare_bin(Part1, A).
|
49 | 55 |
|
50 | 56 | split_compare2(Bin, Part1, Part2) ->
|
51 | 57 | [A, B] = binary:split(Bin, <<"SEPARATOR">>),
|
52 |
| - compare_bin(Part1, A) + compare_bin(B, Part2). |
| 58 | + ok = compare_bin(Part1, A), |
| 59 | + ok = compare_bin(B, Part2). |
53 | 60 |
|
54 | 61 | compare_bin(Bin1, Bin2) ->
|
55 | 62 | compare_bin(Bin1, Bin2, byte_size(Bin1) - 1).
|
56 | 63 |
|
57 | 64 | compare_bin(_Bin1, _Bin2, -1) ->
|
58 |
| - 1; |
| 65 | + ok; |
59 | 66 | compare_bin(Bin1, Bin2, Index) ->
|
60 | 67 | B1 = binary:at(Bin1, Index),
|
61 |
| - case binary:at(Bin2, Index) of |
62 |
| - B1 -> |
63 |
| - compare_bin(Bin1, Bin2, Index - 1); |
64 |
| - _Any -> |
65 |
| - 0 |
66 |
| - end. |
| 68 | + B1 = binary:at(Bin2, Index), |
| 69 | + compare_bin(Bin1, Bin2, Index - 1). |
67 | 70 |
|
68 | 71 | fail_split(Separator) ->
|
69 | 72 | try binary:split(<<"TESTBIN">>, Separator) of
|
70 |
| - _Any -> 2000 |
| 73 | + _Any -> {unexpected, _Any} |
71 | 74 | catch
|
72 |
| - error:badarg -> 1; |
73 |
| - _:_ -> 4000 |
| 75 | + error:badarg -> ok; |
| 76 | + T:V -> {unexpected, {T, V}} |
74 | 77 | end.
|
75 | 78 |
|
76 | 79 | fail_split2(Bin) ->
|
77 | 80 | try binary:split(Bin, <<"TESTSEPARATOR">>) of
|
78 |
| - _Any -> 2000 |
| 81 | + _Any -> {unxpected, _Any} |
79 | 82 | catch
|
80 |
| - error:badarg -> 1; |
81 |
| - _:_ -> 4000 |
| 83 | + error:badarg -> ok; |
| 84 | + T:V -> {unxpected, {T, V}} |
82 | 85 | end.
|
| 86 | + |
| 87 | +memory_allocation_split() -> |
| 88 | + Parent = self(), |
| 89 | + Hostname = <<"atomvm">>, |
| 90 | + {Pid, MonitorRef} = spawn_opt( |
| 91 | + fun() -> |
| 92 | + % Carefully designed lists to generate a crash on unix 64 bits |
| 93 | + % This binary is 63 bytes long, so it's stored on heap on 64 bits |
| 94 | + % binary:split should allocate sufficient bytes as subbinaries |
| 95 | + % have to be on heap as well |
| 96 | + HeapBin = list_to_binary([ |
| 97 | + id(Hostname), <<"@atomvms3.object.stream.atomvms3.object.stream.atomvms3.o">> |
| 98 | + ]), |
| 99 | + List1 = binary:split(HeapBin, <<"@">>, [global]), |
| 100 | + Parent ! {self(), List1} |
| 101 | + end, |
| 102 | + [link, monitor, {atomvm_heap_growth, minimum}] |
| 103 | + ), |
| 104 | + ok = |
| 105 | + receive |
| 106 | + {Pid, List1} -> |
| 107 | + 2 = length(List1), |
| 108 | + ok |
| 109 | + after 5000 -> timeout |
| 110 | + end, |
| 111 | + normal = |
| 112 | + receive |
| 113 | + {'DOWN', MonitorRef, process, Pid, Reason} -> Reason |
| 114 | + after 5000 -> timeout |
| 115 | + end, |
| 116 | + ok. |
| 117 | + |
| 118 | +id(X) -> X. |
0 commit comments