1
- % %%
2
- % %% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
- % %% Version 2, December 2004
4
- % %%
5
- % %% Copyright (C) 2011 Adam Rutkowski <[email protected] >
6
- % %%
7
- % %% Everyone is permitted to copy and distribute verbatim or modified
8
- % %% copies of this license document, and changing it is allowed as long
9
- % %% as the name is changed.
10
- % %%
11
- % %% DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
12
- % %% TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
13
- % %%
14
-
15
1
% % @doc Pretty printer for Erlang binaries.
16
- % %
17
2
-module (binpp ).
18
3
-
author (
'Adam Rutkowski [email protected] ' ).
19
4
@@ -217,8 +202,24 @@ buckets(X, N, M, [H|T], [A|Acc]) ->
217
202
-define (MAX_BIN_SIZE , 2048 ).
218
203
-define (RUNS , 100 ).
219
204
205
+ -ifdef (rand_only ).
206
+ -define (random , rand ).
207
+ -else .
208
+ -define (random , random ).
209
+ -endif .
210
+
211
+ -ifdef (rand_only ).
212
+ random_seed () ->
213
+ % % the rand module self-seeds
214
+ ok .
215
+ -else .
216
+ random_seed () ->
217
+ <<A :32 , B :32 , C :32 >> = crypto :rand_bytes (12 ),
218
+ random :seed ({A ,B ,C }).
219
+ -endif .
220
+
220
221
setup_random () ->
221
- _ = random : seed ( erlang : now () ),
222
+ _ = random_seed ( ),
222
223
ok .
223
224
224
225
binpp_random_test_ () ->
@@ -345,20 +346,22 @@ convert_bin_test_() ->
345
346
346
347
rand_pprint () ->
347
348
F = fun pprint /1 ,
348
- Tests = [ { crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )), ok } || _ <- lists :seq (1 , ? RUNS ) ],
349
+ Tests = [ { crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )), ok } || _ <- lists :seq (1 , ? RUNS ) ],
349
350
[ { <<" Random pprint" >>, fun () -> ? assertEqual (R , F (I )) end }
350
351
|| { I , R } <- Tests ].
351
352
352
353
rand_pprint_bitstring () ->
353
354
F = fun pprint /1 ,
354
- Tests = [ { << (crypto :rand_bytes (random :uniform (? MAX_BIN_SIZE )))/binary , 0 :(random :uniform (7 ))>>, ok }
355
+ Tests = [ { <<
356
+ (crypto :strong_rand_bytes (? random :uniform (? MAX_BIN_SIZE )))/binary ,
357
+ 0 :(? random :uniform (7 ))>>, ok }
355
358
|| _ <- lists :seq (1 , ? RUNS ) ],
356
359
[ { <<" Random pprint (bitstring)" >>, fun () -> ? assertEqual (R , F (I )) end }
357
360
|| { I , R } <- Tests ].
358
361
359
362
rand_compare () ->
360
363
F = fun compare /2 ,
361
- Rand = fun () -> crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )) end ,
364
+ Rand = fun () -> crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )) end ,
362
365
Tests = [ { { Rand (), Rand () }, ok } || _ <- lists :seq (1 , ? RUNS ) ],
363
366
[ { <<" Random compare" >>, fun () -> ? assertEqual (R , F (I1 , I2 )) end }
364
367
|| { {I1 , I2 }, R } <- Tests ].
@@ -375,8 +378,8 @@ rand_pprint_opts() ->
375
378
],
376
379
Range = length (OptsMap ),
377
380
Rand = fun () ->
378
- Input = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
379
- {Opt , Predicate } = lists :nth (random :uniform (Range ), OptsMap ),
381
+ Input = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
382
+ {Opt , Predicate } = lists :nth (? random :uniform (Range ), OptsMap ),
380
383
{Input , Opt , Predicate }
381
384
end ,
382
385
Tests = [ Rand () || _ <- lists :seq (1 , ? RUNS ) ],
@@ -388,9 +391,9 @@ rand_pprint_opts() ->
388
391
rand_pprint_slice () ->
389
392
F = fun pprint /3 ,
390
393
Rand = fun () ->
391
- Bytes = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
392
- Pos = random :uniform (byte_size (Bytes )),
393
- Len = random :uniform (byte_size (Bytes )),
394
+ Bytes = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
395
+ Pos = ? random :uniform (byte_size (Bytes )),
396
+ Len = ? random :uniform (byte_size (Bytes )),
394
397
{Bytes , Pos , Len }
395
398
end ,
396
399
Tests = [ Rand () || _ <- lists :seq (1 , ? RUNS ) ],
@@ -403,9 +406,9 @@ rand_pprint_slice() ->
403
406
rand_from_str () ->
404
407
F = fun from_str /1 ,
405
408
Rand = fun () ->
406
- Bytes = crypto :rand_bytes ( random :uniform (? MAX_BIN_SIZE )),
409
+ Bytes = crypto :strong_rand_bytes ( ? random :uniform (? MAX_BIN_SIZE )),
407
410
{ok , Converted } = convert (Bytes ),
408
- case random :uniform (2 ) of
411
+ case ? random :uniform (2 ) of
409
412
1 -> {lists :flatten (Converted ), Bytes };
410
413
2 -> {string :join (Converted , " " ), Bytes }
411
414
end
0 commit comments