summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Watson <tim@rabbitmq.com>2014-03-21 15:10:19 +0000
committerTim Watson <tim@rabbitmq.com>2014-03-21 15:10:19 +0000
commita1f1bd614404a593f4432ade1f5b5449124592d2 (patch)
tree91ef6c37f0a72ce336ab6037bb378baff2e921c7
parent861de1cbda2fb007369eaf81e3eb6412e2a8dd10 (diff)
downloadrabbitmq-server-a1f1bd614404a593f4432ade1f5b5449124592d2.tar.gz
Tweak list handling
-rw-r--r--src/rabbit_trunc_term.erl24
-rw-r--r--src/rabbit_trunc_term_tests.erl20
2 files changed, 23 insertions, 21 deletions
diff --git a/src/rabbit_trunc_term.erl b/src/rabbit_trunc_term.erl
index 55eb30fa..0340ae91 100644
--- a/src/rabbit_trunc_term.erl
+++ b/src/rabbit_trunc_term.erl
@@ -55,17 +55,15 @@ shrink_term([A|B], N) when not is_list(B) ->
shrink_term([A,B], N);
shrink_term(T, N) when is_list(T) ->
IsPrintable = io_lib:printable_list(T),
- case length(T) > N of
- true when IsPrintable ->
- lists:append(lists:sublist(T, suffix_len(N-3)), "...");
- true ->
- lists:append([shrink_term(E, N-1) ||
- E <- lists:sublist(T, suffix_len(N-1))],
- ['...']);
- false when IsPrintable ->
- T;
- false ->
- [shrink_term(E, N-1) || E <- T]
+ Len = length(T),
+ case {Len > N, IsPrintable} of
+ {true, true}
+ when N > 3 -> lists:append(lists:sublist(T, resize(N-3)), "...");
+ {true, false} -> lists:append([shrink_term(E, resize(N-1, Len)) ||
+ E <- lists:sublist(T, resize(N-1))],
+ ['...']);
+ {false, false} -> [shrink_term(E, resize(N-1, Len)) || E <- T];
+ _ -> T
end;
shrink_term(T, N) when is_tuple(T) ->
case tuple_size(T) > N of
@@ -78,5 +76,7 @@ shrink_term(T, N) when is_tuple(T) ->
end;
shrink_term(T, _) -> T.
-suffix_len(N) -> erlang:max(N, 1).
+resize(N) -> resize(N, 1).
+
+resize(N, M) -> erlang:max(N, M).
diff --git a/src/rabbit_trunc_term_tests.erl b/src/rabbit_trunc_term_tests.erl
index e298da81..697acc5d 100644
--- a/src/rabbit_trunc_term_tests.erl
+++ b/src/rabbit_trunc_term_tests.erl
@@ -30,11 +30,13 @@ prop_trunc_any_term() ->
Shrunk = rabbit_trunc_term:shrink_term(GenAny, MaxSz),
SzShrunk = erts_debug:size(Shrunk),
?WHENFAIL(begin
- io:format("MaxLen: ~p\n", [MaxSz]),
- io:format("Input-Size: ~p\n", [SzInitial]),
- io:format("Shrunk-Size: ~p\n", [SzShrunk]),
- io:format("Input: ~p\n", [GenAny]),
- io:format("Output: ~p\n", [Shrunk])
+ io:format("MaxLen: ~p~n", [MaxSz]),
+ io:format("SizeOfThing: ~p~n",
+ [size_of_thing(GenAny)]),
+ io:format("Input-Size: ~p~n", [SzInitial]),
+ io:format("Shrunk-Size: ~p~n", [SzShrunk]),
+ io:format("Input: ~p~n", [GenAny]),
+ io:format("Output: ~p~n", [Shrunk])
end,
case size_of_thing(GenAny) > MaxSz of
true -> true = SzShrunk < SzInitial;
@@ -42,16 +44,16 @@ prop_trunc_any_term() ->
end)
catch
_:Err ->
- io:format("\nException: ~p\n",
+ io:format("\nException: ~p~n",
[{Err, erlang:get_stacktrace()}]),
- io:format("Input: ~p\n", [GenAny]),
- io:format("Max-Size: ~p\n", [MaxSz]),
+ io:format("Input: ~p~n", [GenAny]),
+ io:format("Max-Size: ~p~n", [MaxSz]),
false
end
end).
-size_of_thing(Thing) when is_binary(Thing) -> size(Thing);
size_of_thing(Thing) when is_bitstring(Thing) -> byte_size(Thing);
+size_of_thing(Thing) when is_binary(Thing) -> size(Thing);
size_of_thing(Thing) when is_list(Thing) -> length(Thing);
size_of_thing(Thing) when is_tuple(Thing) -> size(Thing);
size_of_thing(Thing) -> error({cannot_size, Thing}).