lager_trunc_io.erl 19.4 KB
Newer Older
Andrew Thompson's avatar
Andrew Thompson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with your Erlang distribution. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Corelatus AB.
%% Portions created by Corelatus are Copyright 2003, Corelatus
%% AB. All Rights Reserved.''
%%
%% @doc Module to print out terms for logging. Limits by length rather than depth.
%%
%% The resulting string may be slightly larger than the limit; the intention
%% is to provide predictable CPU and memory consumption for formatting
%% terms, not produce precise string lengths.
%%
%% Typical use:
%%
%%   trunc_io:print(Term, 500).
%%
%% Source license: Erlang Public License.
%% Original author: Matthias Lang, <tt>matthias@corelatus.se</tt>
28
29
%%
%% Various changes to this module, most notably the format/3 implementation
Andrew Thompson's avatar
Andrew Thompson committed
30
%% were added by Andrew Thompson `<andrew@basho.com>'. The module has been renamed
31
%% to avoid conflicts with the vanilla module.
Andrew Thompson's avatar
Andrew Thompson committed
32

33
-module(lager_trunc_io).
Andrew Thompson's avatar
Andrew Thompson committed
34
35
-author('matthias@corelatus.se').
%% And thanks to Chris Newcombe for a bug fix 
36
-export([format/3, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions
Andrew Thompson's avatar
Andrew Thompson committed
37
38
-version("$Id: trunc_io.erl,v 1.11 2009-02-23 12:01:06 matthias Exp $").

39
40
41
42
43
-ifdef(TEST).
-export([perf/0, perf/3, perf1/0, test/0, test/2]). % testing functions
-include_lib("eunit/include/eunit.hrl").
-endif.

44
45
46
47
48
49
50
51
52
53
-record(print_options, {
        %% negative depth means no depth limiting
        depth = -1 :: integer(),
        %% whether to print lists as strings, if possible
        lists_as_strings = false :: boolean(),
        %% force strings, or binaries to be printed as a string,
        %% even if they're not printable
        force_strings = false :: boolean()
    }).

54
format(Fmt, Args, Max) ->
55
56
57
58
59
60
    try lager_format:format(Fmt, Args, Max) of
        Result -> Result
    catch
        _:_ ->
            erlang:error(badarg, [Fmt, Args])
    end.
61

Andrew Thompson's avatar
Andrew Thompson committed
62
63
64
%% @doc Returns an flattened list containing the ASCII representation of the given
%% term.
-spec fprint(term(), pos_integer()) -> string().
65
66
67
68
69
70
71
72
73
fprint(Term, Max) ->
    fprint(Term, Max, []).


%% @doc Returns an flattened list containing the ASCII representation of the given
%% term.
-spec fprint(term(), pos_integer(), #print_options{}) -> string().
fprint(T, Max, Options) -> 
    {L, _} = print(T, Max, prepare_options(Options, #print_options{})),
Andrew Thompson's avatar
Andrew Thompson committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    lists:flatten(L).

%% @doc Same as print, but never crashes. 
%%
%% This is a tradeoff. Print might conceivably crash if it's asked to
%% print something it doesn't understand, for example some new data
%% type in a future version of Erlang. If print crashes, we fall back
%% to io_lib to format the term, but then the formatting is
%% depth-limited instead of length limited, so you might run out
%% memory printing it. Out of the frying pan and into the fire.
%% 
-spec safe(term(), pos_integer()) -> {string(), pos_integer()} | {string()}.
safe(What, Len) ->
    case catch print(What, Len) of
	{L, Used} when is_list(L) -> {L, Used};
	_ -> {"unable to print" ++ io_lib:write(What, 99)}
    end.	     

%% @doc Returns {List, Length}
-spec print(term(), pos_integer()) -> {iolist(), pos_integer()}.
94
95
96
97
98
99
100
101
print(Term, Max) ->
    print(Term, Max, []).

%% @doc Returns {List, Length}
-spec print(term(), pos_integer(), #print_options{}) -> {iolist(), pos_integer()}.
print(Term, Max, Options) when is_list(Options) ->
    %% need to convert the proplist to a record
    print(Term, Max, prepare_options(Options, #print_options{}));
102
103
104
105

print(Term, _Max, #print_options{force_strings=true}) when not is_list(Term), not is_binary(Term), not is_atom(Term) ->
    erlang:error(badarg);

106
print(_, Max, _Options) when Max < 0 -> {"...", 3};
107
print(_, _, #print_options{depth=0}) -> {"...", 3};
108

109
110
print(Tuple, Max, Options) when is_tuple(Tuple) -> 
    {TC, Len} = tuple_contents(Tuple, Max-2, Options),
Andrew Thompson's avatar
Andrew Thompson committed
111
112
113
114
115
116
117
    {[${, TC, $}], Len + 2};

%% @doc We assume atoms, floats, funs, integers, PIDs, ports and refs never need 
%% to be truncated. This isn't strictly true, someone could make an 
%% arbitrarily long bignum. Let's assume that won't happen unless someone
%% is being malicious.
%%
118
print(Atom, _Max, #print_options{force_strings=NoQuote}) when is_atom(Atom) ->
Andrew Thompson's avatar
Andrew Thompson committed
119
    L = atom_to_list(Atom),
120
    R = case atom_needs_quoting_start(L) andalso not NoQuote of
121
122
123
124
        true -> lists:flatten([$', L, $']);
        false -> L
    end,
    {R, length(R)};
Andrew Thompson's avatar
Andrew Thompson committed
125

126
print(<<>>, _Max, _Options) ->
Andrew Thompson's avatar
Andrew Thompson committed
127
128
    {"<<>>", 4};

129
print(Binary, 0, _Options) when is_binary(Binary) ->
130
131
    {"<<..>>", 6};

132
print(Binary, Max, Options) when is_binary(Binary) ->
Andrew Thompson's avatar
Andrew Thompson committed
133
    B = binary_to_list(Binary, 1, lists:min([Max, size(Binary)])),
134
135
136
137
138
    {L, Len} = case Options#print_options.lists_as_strings orelse
        Options#print_options.force_strings of
        true ->
            alist_start(B, Max-4, Options);
        _ ->
139
            list_body(B, Max-4, Options, false)
140
    end,
141
142
143
144
145
146
    {Res, Length} = case L of
        [91, X, 93] ->
            {X, Len - 2};
        X ->
            {X, Len}
    end,
147
148
149
150
151
152
    case Options#print_options.force_strings of
        true ->
            {Res, Length};
        _ ->
            {["<<", Res, ">>"], Length+4}
    end;
Andrew Thompson's avatar
Andrew Thompson committed
153

154
print(Float, _Max, _Options) when is_float(Float) ->
155
156
157
    %% use the same function io_lib:format uses to print floats
    %% float_to_list is way too verbose.
    L = io_lib_format:fwrite_g(Float),
Andrew Thompson's avatar
Andrew Thompson committed
158
159
    {L, length(L)};

160
print(Fun, Max, _Options) when is_function(Fun) ->
Andrew Thompson's avatar
Andrew Thompson committed
161
    L = erlang:fun_to_list(Fun),
162
163
164
165
166
167
168
169
    case length(L) > Max of
        true ->
            S = erlang:max(5, Max),
            Res = string:substr(L, 1, S) ++ "..>",
            {Res, length(Res)};
        _ ->
            {L, length(L)}
    end;
Andrew Thompson's avatar
Andrew Thompson committed
170

171
print(Integer, _Max, _Options) when is_integer(Integer) ->
Andrew Thompson's avatar
Andrew Thompson committed
172
173
174
    L = integer_to_list(Integer),
    {L, length(L)};

175
print(Pid, _Max, _Options) when is_pid(Pid) ->
Andrew Thompson's avatar
Andrew Thompson committed
176
177
178
    L = pid_to_list(Pid),
    {L, length(L)};

179
print(Ref, _Max, _Options) when is_reference(Ref) ->
Andrew Thompson's avatar
Andrew Thompson committed
180
181
182
    L = erlang:ref_to_list(Ref),
    {L, length(L)};

183
print(Port, _Max, _Options) when is_port(Port) ->
Andrew Thompson's avatar
Andrew Thompson committed
184
185
186
    L = erlang:port_to_list(Port),
    {L, length(L)};

187
print(List, Max, Options) when is_list(List) ->
188
    alist_start(List, Max, dec_depth(Options)).
Andrew Thompson's avatar
Andrew Thompson committed
189
190

%% Returns {List, Length}
191
tuple_contents(Tuple, Max, Options) ->
Andrew Thompson's avatar
Andrew Thompson committed
192
    L = tuple_to_list(Tuple),
193
    list_body(L, Max, dec_depth(Options), true).
Andrew Thompson's avatar
Andrew Thompson committed
194
195
196

%% Format the inside of a list, i.e. do not add a leading [ or trailing ].
%% Returns {List, Length}
197
198
199
200
list_body([], _Max, _Options, _Tuple) -> {[], 0};
list_body(_, Max, _Options, _Tuple) when Max < 4 -> {"...", 3};
list_body(_, _Max, #print_options{depth=0}, _Tuple) -> {"...", 3};
list_body([H|T], Max, Options, Tuple) -> 
201
    {List, Len} = print(H, Max, Options),
202
    {Final, FLen} = list_bodyc(T, Max - Len, Options, Tuple),
Andrew Thompson's avatar
Andrew Thompson committed
203
    {[List|Final], FLen + Len};
204
list_body(X, Max, Options, _Tuple) ->  %% improper list
205
    {List, Len} = print(X, Max - 1, Options),
Andrew Thompson's avatar
Andrew Thompson committed
206
207
    {[$|,List], Len + 1}.

208
list_bodyc([], _Max, _Options, _Tuple) -> {[], 0};
209
list_bodyc(_, Max, _Options, _Tuple) when Max < 4 -> {",...", 3};
210
211
212
213
214
215
216
217
218
list_bodyc([H|T], Max, #print_options{depth=Depth} = Options, Tuple) -> 
    {List, Len} = print(H, Max, dec_depth(Options)),
    {Final, FLen} = list_bodyc(T, Max - Len - 1, Options, Tuple),
    Sep = case Depth == 1 andalso not Tuple of
        true -> $|;
        _ -> $,
    end,
    {[Sep, List|Final], FLen + Len + 1};
list_bodyc(X, Max, Options, _Tuple) ->  %% improper list
219
    {List, Len} = print(X, Max - 1, Options),
Andrew Thompson's avatar
Andrew Thompson committed
220
221
222
223
224
225
226
227
228
    {[$|,List], Len + 1}.

%% The head of a list we hope is ascii. Examples:
%%
%% [65,66,67] -> "ABC"
%% [65,0,67] -> "A"[0,67]
%% [0,65,66] -> [0,65,66]
%% [65,b,66] -> "A"[b,66]
%%
229
alist_start([], _Max, #print_options{force_strings=true}) -> {"", 0};
230
231
alist_start([], _Max, _Options) -> {"[]", 2};
alist_start(_, Max, _Options) when Max < 4 -> {"...", 3};
232
alist_start(_, _Max, #print_options{depth=0}) -> {"[...]", 3};
233
234
235
236
alist_start(L, Max, #print_options{force_strings=true} = Options) ->
    alist(L, Max, Options);
alist_start([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e ->  % definitely printable
    try alist([H|T], Max -1, Options) of
237
238
239
240
        {L, Len} ->
            {[$"|L], Len + 1}
    catch
        throw:unprintable ->
241
            {R, Len} = list_body([H|T], Max-2, Options, false),
242
243
            {[$[, R, $]], Len + 2}
    end;
244
245
alist_start([H|T], Max, Options) when H =:= 9; H =:= 10; H =:= 13 ->
    try alist([H|T], Max -1, Options) of
246
247
248
249
        {L, Len} ->
            {[$"|L], Len + 1}
    catch
        throw:unprintable ->
250
            {R, Len} = list_body([H|T], Max-2, Options, false),
251
252
            {[$[, R, $]], Len + 2}
    end;
253
alist_start(L, Max, Options) ->
254
    {R, Len} = list_body(L, Max-2, Options, false),
Andrew Thompson's avatar
Andrew Thompson committed
255
256
    {[$[, R, $]], Len + 2}.

257
258
259
260
261
262
263
264
265
alist([], _Max, #print_options{force_strings=true}) -> {"", 0};
alist([], _Max, _Options) -> {"\"", 1};
alist(_, Max, #print_options{force_strings=true}) when Max < 4 -> {"...", 3};
alist(_, Max, #print_options{force_strings=false}) when Max < 5 -> {"...\"", 4};
alist([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e ->     % definitely printable
    {L, Len} = alist(T, Max-1, Options),
    {[H|L], Len + 1};
alist([H|T], Max, Options) when H =:= 9; H =:= 10; H =:= 13 ->
    {L, Len} = alist(T, Max-1, Options),
Andrew Thompson's avatar
Andrew Thompson committed
266
    {[H|L], Len + 1};
267
268
alist([H|T], Max, #print_options{force_strings=true} = Options) when is_integer(H) ->
    {L, Len} = alist(T, Max-1, Options),
269
    {[H|L], Len + 1};
270
271
272
alist(_, _, #print_options{force_strings=true}) ->
    error(badarg);
alist(_L, _Max, _Options) ->
273
    throw(unprintable).
Andrew Thompson's avatar
Andrew Thompson committed
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
%% is the first character in the atom alphabetic & lowercase?
atom_needs_quoting_start([H|T]) when H >= $a, H =< $z ->
    atom_needs_quoting(T);
atom_needs_quoting_start(_) ->
    true.

atom_needs_quoting([]) ->
    false;
atom_needs_quoting([H|T]) when (H >= $a andalso H =< $z);
                        (H >= $A andalso H =< $Z);
                         H == $@; H == $_ ->
    atom_needs_quoting(T);
atom_needs_quoting(_) ->
    true.
Andrew Thompson's avatar
Andrew Thompson committed
289

290
291
292
293
294
295
296
297
298
prepare_options([], Options) ->
    Options;
prepare_options([{depth, Depth}|T], Options) when is_integer(Depth) ->
    prepare_options(T, Options#print_options{depth=Depth});
prepare_options([{lists_as_strings, Bool}|T], Options) when is_boolean(Bool) ->
    prepare_options(T, Options#print_options{lists_as_strings = Bool});
prepare_options([{force_strings, Bool}|T], Options) when is_boolean(Bool) ->
    prepare_options(T, Options#print_options{force_strings = Bool}).

299
300
301
302
dec_depth(#print_options{depth=Depth} = Options) when Depth > 0 ->
    Options#print_options{depth=Depth-1};
dec_depth(Options) ->
    Options.
303

304
-ifdef(TEST).
Andrew Thompson's avatar
Andrew Thompson committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
%%--------------------
%% The start of a test suite. So far, it only checks for not crashing.
-spec test() -> ok.
test() ->
    test(trunc_io, print).

-spec test(atom(), atom()) -> ok.
test(Mod, Func) ->
    Simple_items = [atom, 1234, 1234.0, {tuple}, [], [list], "string", self(),
		    <<1,2,3>>, make_ref(), fun() -> ok end],
    F = fun(A) ->
		Mod:Func(A, 100),
		Mod:Func(A, 2),
		Mod:Func(A, 20)
	end,

    G = fun(A) ->
		case catch F(A) of
		    {'EXIT', _} -> exit({failed, A});
		    _ -> ok
		end
	end,
    
    lists:foreach(G, Simple_items),
    
    Tuples = [ {1,2,3,a,b,c}, {"abc", def, 1234},
	       {{{{a},b,c,{d},e}},f}],
    
    Lists = [ [1,2,3,4,5,6,7], lists:seq(1,1000),
	      [{a}, {a,b}, {a, [b,c]}, "def"], [a|b], [$a|$b] ],
    
    
    lists:foreach(G, Tuples),
    lists:foreach(G, Lists).

-spec perf() -> ok.
perf() ->
    {New, _} = timer:tc(trunc_io, perf, [trunc_io, print, 1000]),
    {Old, _} = timer:tc(trunc_io, perf, [io_lib, write, 1000]),
    io:fwrite("New code took ~p us, old code ~p\n", [New, Old]).

-spec perf(atom(), atom(), integer()) -> done.
perf(M, F, Reps) when Reps > 0 ->
    test(M,F),
    perf(M,F,Reps-1);
perf(_,_,_) ->
    done.    

%% Performance test. Needs a particularly large term I saved as a binary...
-spec perf1() -> {non_neg_integer(), non_neg_integer()}.
perf1() ->
    {ok, Bin} = file:read_file("bin"),
    A = binary_to_term(Bin),
    {N, _} = timer:tc(trunc_io, print, [A, 1500]),
    {M, _} = timer:tc(io_lib, write, [A]),
    {N, M}.
361
362
363
364

format_test() ->
    %% simple format strings
    ?assertEqual("foobar", lists:flatten(format("~s", [["foo", $b, $a, $r]], 50))),
365
366
    ?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~p", [["foo", $b, $a, $r]], 50))),
    ?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~P", [["foo", $b, $a, $r], 10], 50))),
367
    ?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~w", [["foo", $b, $a, $r]], 50))),
368
369
    
    %% complex ones
370
    ?assertEqual("    foobar", lists:flatten(format("~10s", [["foo", $b, $a, $r]], 50))),
371
372
    ?assertEqual("     [\"foo\",98,97,114]", lists:flatten(format("~22p", [["foo", $b, $a, $r]], 50))),
    ?assertEqual("     [\"foo\",98,97,114]", lists:flatten(format("~22P", [["foo", $b, $a, $r], 10], 50))),
373
374
    ?assertEqual("**********", lists:flatten(format("~10W", [["foo", $b, $a, $r], 10], 50))),
    ?assertEqual("   [\"foo\",98,97,114]", lists:flatten(format("~20W", [["foo", $b, $a, $r], 10], 50))),
375
376
    ok.

377
378
379
atom_quoting_test() ->
    ?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
    ?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
380
    ?assertEqual("'Hello world'", lists:flatten(format("~p", ['Hello world'], 50))),
381
382
383
384
385
386
387
388
389
390
391
392
393
    ?assertEqual("hello_world", lists:flatten(format("~p", ['hello_world'], 50))),
    ?assertEqual("'node@127.0.0.1'", lists:flatten(format("~p", ['node@127.0.0.1'], 50))),
    ?assertEqual("node@nohost", lists:flatten(format("~p", [node@nohost], 50))),
    ok.

sane_float_printing_test() ->
    ?assertEqual("1.0", lists:flatten(format("~p", [1.0], 50))),
    ?assertEqual("1.23456789", lists:flatten(format("~p", [1.23456789], 50))),
    ?assertEqual("1.23456789", lists:flatten(format("~p", [1.234567890], 50))),
    ?assertEqual("0.3333333333333333", lists:flatten(format("~p", [1/3], 50))),
    ?assertEqual("0.1234567", lists:flatten(format("~p", [0.1234567], 50))),
    ok.

394
float_inside_list_test() ->
395
    ?assertEqual("[97,38.233913133184835,99]", lists:flatten(format("~p", [[$a, 38.233913133184835, $c]], 50))),
396
    ?assertError(badarg, lists:flatten(format("~s", [[$a, 38.233913133184835, $c]], 50))),
397
398
    ok.

399
400
401
402
403
quote_strip_test() ->
    ?assertEqual("\"hello\"", lists:flatten(format("~p", ["hello"], 50))),
    ?assertEqual("hello", lists:flatten(format("~s", ["hello"], 50))),
    ?assertEqual("hello", lists:flatten(format("~s", [hello], 50))),
    ?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
404
405
406
407
408
    ?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
    ?assertEqual("hello world", lists:flatten(format("~s", ['hello world'], 50))),
    ok.

binary_printing_test() ->
409
410
    ?assertEqual("<<>>", lists:flatten(format("~p", [<<>>], 50))),
    ?assertEqual("<<..>>", lists:flatten(format("~p", [<<"hi">>], 0))),
411
412
    ?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<$h, $e, $l, $l, $o>>], 50))),
    ?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<"hello">>], 50))),
413
    ?assertEqual("<<104,101,108,108,111>>", lists:flatten(format("~w", [<<"hello">>], 50))),
414
    ?assertEqual("<<1,2,3,4>>", lists:flatten(format("~p", [<<1, 2, 3, 4>>], 50))),
415
    ?assertEqual([1,2,3,4], lists:flatten(format("~s", [<<1, 2, 3, 4>>], 50))),
416
    ?assertEqual("hello", lists:flatten(format("~s", [<<"hello">>], 50))),
417
    ?assertEqual("     hello", lists:flatten(format("~10s", [<<"hello">>], 50))),
418
    ok.
419
420

list_printing_test() ->
421
422
423
424
425
    ?assertEqual("[]", lists:flatten(format("~p", [[]], 50))),
    ?assertEqual("[]", lists:flatten(format("~w", [[]], 50))),
    ?assertEqual("", lists:flatten(format("~s", [[]], 50))),
    ?assertEqual("...", lists:flatten(format("~s", [[]], -1))),
    ?assertEqual("[[]]", lists:flatten(format("~p", [[[]]], 50))),
426
    ?assertEqual("[13,11,10,8,5,4]", lists:flatten(format("~p", [[13,11,10,8,5,4]], 50))),
427
    ?assertEqual("\"\rabc\"", lists:flatten(format("~p", [[13,$a, $b, $c]], 50))),
428
    ?assertEqual("[1,2,3|4]", lists:flatten(format("~p", [[1, 2, 3|4]], 50))),
429
430
    ?assertEqual("[...]", lists:flatten(format("~p", [[1, 2, 3,4]], 4))),
    ?assertEqual("[1,...]", lists:flatten(format("~p", [[1, 2, 3,4]], 6))),
431
    ?assertEqual("[1|4]", lists:flatten(format("~p", [[1|4]], 50))),
432
    ?assertEqual("[1]", lists:flatten(format("~p", [[1]], 50))),
433
    ?assertError(badarg, lists:flatten(format("~s", [[1|4]], 50))),
434
    ?assertEqual("\"hello...\"", lists:flatten(format("~p", ["hello world"], 10))),
435
    ?assertEqual("hello w...", lists:flatten(format("~s", ["hello world"], 10))),
436
437
438
    ?assertEqual("hello world\r\n", lists:flatten(format("~s", ["hello world\r\n"], 50))),
    ?assertEqual("\rhello world\r\n", lists:flatten(format("~s", ["\rhello world\r\n"], 50))),
    ?assertEqual("...", lists:flatten(format("~s", ["\rhello world\r\n"], 3))),
439
440
441
442
443
444
445
446
447
    ok.

tuple_printing_test() ->
    ?assertEqual("{}", lists:flatten(format("~p", [{}], 50))),
    ?assertEqual("{}", lists:flatten(format("~w", [{}], 50))),
    ?assertError(badarg, lists:flatten(format("~s", [{}], 50))),
    ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 3))),
    ?assertEqual("{foo,...}", lists:flatten(format("~p", [{foo,bar}], 6))),
    ?assertEqual("{foo,bar}", lists:flatten(format("~p", [{foo,bar}], 9))),
448
    ok.
449
450
451
452
453
454

unicode_test() ->
    ?assertEqual([231,167,129], lists:flatten(format("~s", [<<231,167,129>>], 50))),
    ?assertEqual([31169], lists:flatten(format("~ts", [<<231,167,129>>], 50))),
    ok.

455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
depth_limit_test() ->
    ?assertEqual("{...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 1], 50))),
    ?assertEqual("{a,...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 2], 50))),
    ?assertEqual("{a,[...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 3], 50))),
    ?assertEqual("{a,[b|...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 4], 50))),
    ?assertEqual("{a,[b,[...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 5], 50))),
    ?assertEqual("{a,[b,[c|...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 6], 50))),
    ?assertEqual("{a,[b,[c,[...]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 7], 50))),
    ?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 8], 50))),
    ?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 9], 50))),

    ?assertEqual("{a,{...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 3], 50))),
    ?assertEqual("{a,{b,...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 4], 50))),
    ?assertEqual("{a,{b,{...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 5], 50))),
    ?assertEqual("{a,{b,{c,...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 6], 50))),
    ?assertEqual("{a,{b,{c,{...}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 7], 50))),
    ?assertEqual("{a,{b,{c,{d}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 8], 50))),

    ?assertEqual("{\"a\",[...]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 3], 50))),
    ?assertEqual("{\"a\",[\"b\",[[...]|...]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 6], 50))),
    ?assertEqual("{\"a\",[\"b\",[\"c\",[\"d\"]]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 9], 50))),
    ok.

478
-endif.