diff options
| author | Tim Watson <tim@rabbitmq.com> | 2014-03-21 15:10:19 +0000 |
|---|---|---|
| committer | Tim Watson <tim@rabbitmq.com> | 2014-03-21 15:10:19 +0000 |
| commit | 681b97c8bc4eb1f28c14ff533df26acc242852f2 (patch) | |
| tree | 91ef6c37f0a72ce336ab6037bb378baff2e921c7 | |
| parent | 0084ada496b343cd2fce7f8332acb787666d6c30 (diff) | |
| download | rabbitmq-server-git-681b97c8bc4eb1f28c14ff533df26acc242852f2.tar.gz | |
Tweak list handling
| -rw-r--r-- | src/rabbit_trunc_term.erl | 24 | ||||
| -rw-r--r-- | src/rabbit_trunc_term_tests.erl | 20 |
2 files changed, 23 insertions, 21 deletions
diff --git a/src/rabbit_trunc_term.erl b/src/rabbit_trunc_term.erl index 55eb30fa82..0340ae9121 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 e298da8171..697acc5d40 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}). |
