|
20 | 20 |
|
21 | 21 | -module(test_list_to_binary).
|
22 | 22 |
|
23 |
| --export([start/0, concat/2, concat2/2, compare_bin/3, id/1]). |
| 23 | +-export([start/0, concat_space/2, concat/2, is_binary_equal/3, id/1]). |
| 24 | +-define(ID(X), ?MODULE:id(X)). |
| 25 | +-define(ID(X1, X2), ?MODULE:id(X1), ?MODULE:id(X2)). |
24 | 26 |
|
25 | 27 | start() ->
|
26 |
| - ok = test_concat(), |
27 |
| - ok = test_iolist(), |
| 28 | + ok = test_list_to_binary(), |
28 | 29 | ok = test_iolist_to_binary(),
|
29 |
| - ok = test_empty_list_to_binary(), |
30 | 30 | 0.
|
| 31 | +test_list_to_binary() -> |
| 32 | + <<"Hello world">> = erlang:list_to_binary(?ID([<<"Hello ">>, [<<"wor">>, [$l, $d]]])), |
| 33 | + <<"">> = erlang:list_to_binary(?ID([])), |
31 | 34 |
|
32 |
| -test_concat() -> |
33 |
| - Bin = concat("Hello", "world"), |
34 |
| - Bin2 = concat2("", ""), |
35 |
| - CompRes1 = compare_bin(Bin, <<"Hello world">>) - compare_bin(Bin, <<"HelloXworld">>), |
36 |
| - 1 = CompRes1 + byte_size(Bin2) + invalid(42), |
37 |
| - ok. |
| 35 | + % Concatenation |
| 36 | + Hello = concat_space(?ID("Hello", "world")), |
| 37 | + Empty = concat(?ID("", "")), |
| 38 | + 0 = byte_size(Empty), |
| 39 | + true = is_binary_equal(Hello, <<"Hello world">>), |
| 40 | + false = is_binary_equal(Hello, <<"HelloXworld">>), |
38 | 41 |
|
39 |
| -test_iolist() -> |
40 |
| - <<"Hello world">> = list_to_binary(id([<<"Hello ">>, [<<"wor">>, [$l, $d]]])), |
| 42 | + % Errors |
| 43 | + ok = raises_badarg(fun() -> erlang:list_to_binary(?ID(<<>>)) end), |
| 44 | + ok = raises_badarg(fun() -> erlang:list_to_binary(?ID(<<"foo">>)) end), |
| 45 | + ok = raises_badarg(fun() -> erlang:list_to_binary(?ID(42)) end), |
41 | 46 | ok.
|
42 | 47 |
|
43 | 48 | test_iolist_to_binary() ->
|
44 |
| - <<"Hello world">> = iolist_to_binary(id([<<"Hello ">>, [<<"wor">>, [$l, $d]]])), |
45 |
| - ok = |
46 |
| - try |
47 |
| - _ = list_to_binary(id(<<"foo">>)), |
48 |
| - fail |
49 |
| - catch |
50 |
| - error:badarg -> |
51 |
| - ok |
52 |
| - end, |
53 |
| - ok = |
54 |
| - try |
55 |
| - <<"foo">> = iolist_to_binary(id(<<"foo">>)), |
56 |
| - ok |
57 |
| - catch |
58 |
| - error:badarg -> |
59 |
| - fail |
60 |
| - end, |
61 |
| - ok. |
62 |
| - |
63 |
| -test_empty_list_to_binary() -> |
64 |
| - <<"">> = erlang:list_to_binary([]), |
| 49 | + <<"foo">> = erlang:iolist_to_binary(?ID(<<"foo">>)), |
| 50 | + <<"">> = erlang:iolist_to_binary(?ID(<<>>)), |
| 51 | + <<"">> = erlang:iolist_to_binary(?ID([])), |
| 52 | + % The binary and charlist is "atom" in katakana as english transliteration ("atomu") |
| 53 | + % (in direct binary form since formatter butchers the utf-8) |
| 54 | + <<227, 130, 162, 227, 131, 136, 227, 131, 160>> = iolist_to_binary( |
| 55 | + ?ID(<<227, 130, 162, 227, 131, 136, 227, 131, 160>>) |
| 56 | + ), |
| 57 | + ok = raises_badarg(fun() -> iolist_to_binary(?ID([12450, 12488, 12512])) end), |
| 58 | + ok = raises_badarg(fun() -> iolist_size(?ID([12450, 12488, 12512])) end), |
65 | 59 | ok.
|
66 | 60 |
|
67 |
| -concat(A, B) -> |
| 61 | +concat_space(A, B) -> |
68 | 62 | list_to_binary(A ++ " " ++ B).
|
69 | 63 |
|
70 |
| -concat2(A, B) -> |
| 64 | +concat(A, B) -> |
71 | 65 | list_to_binary(A ++ B).
|
72 | 66 |
|
73 |
| -invalid(A) -> |
74 |
| - try list_to_binary(A) of |
75 |
| - Any -> byte_size(Any) |
76 |
| - catch |
77 |
| - error:badarg -> 0; |
78 |
| - _:_ -> 1000 |
79 |
| - end. |
80 |
| - |
81 |
| -compare_bin(Bin1, Bin2) -> |
82 |
| - compare_bin(Bin1, Bin2, byte_size(Bin1) - 1). |
| 67 | +is_binary_equal(Bin1, Bin2) -> |
| 68 | + is_binary_equal(Bin1, Bin2, byte_size(Bin1) - 1). |
83 | 69 |
|
84 |
| -compare_bin(_Bin1, _Bin2, -1) -> |
85 |
| - 1; |
86 |
| -compare_bin(Bin1, Bin2, Index) -> |
| 70 | +is_binary_equal(_Bin1, _Bin2, -1) -> |
| 71 | + true; |
| 72 | +is_binary_equal(Bin1, Bin2, Index) -> |
87 | 73 | B1 = binary:at(Bin1, Index),
|
88 | 74 | case binary:at(Bin2, Index) of
|
89 | 75 | B1 ->
|
90 |
| - compare_bin(Bin1, Bin2, Index - 1); |
| 76 | + is_binary_equal(Bin1, Bin2, Index - 1); |
91 | 77 | _Any ->
|
92 |
| - 0 |
| 78 | + false |
93 | 79 | end.
|
94 | 80 |
|
95 | 81 | id(X) ->
|
96 | 82 | X.
|
| 83 | + |
| 84 | +raises_badarg(Fun) -> |
| 85 | + try Fun() of |
| 86 | + Ret -> {unexpected, Ret} |
| 87 | + catch |
| 88 | + error:badarg -> ok; |
| 89 | + C:E -> {unexpected, C, E} |
| 90 | + end. |
0 commit comments