File: 020_sort_keys.t

package info (click to toggle)
libsereal-encoder-perl 5.004%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,384 kB
  • sloc: ansic: 12,087; perl: 6,049; sh: 25; makefile: 9
file content (102 lines) | stat: -rw-r--r-- 2,541 bytes parent folder | download | duplicates (4)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!perl
use strict;
use warnings;
use File::Spec;
use lib File::Spec->catdir(qw(t lib));

BEGIN {
    lib->import('lib')
        if !-d 't';
}
use Sereal::TestSet;
use Sereal::Encoder qw(encode_sereal);
use List::Util qw(shuffle);
use Test::More;

BEGIN {
    if (!eval "use Hash::Util 'num_buckets'; 1") {
        my %hash= ( test => 1);
        my $scalar= scalar(%hash);
        if ($scalar=~m!/!) {
            eval "sub num_buckets(\\%) { (split( m!/!, scalar %{\$_[0]}))[-1] } 1"
        } else {
            plan skip_all => "Hash::Util not installed, this is perl $], scalar(%h)='$scalar'";
        }
    }
}

my %test= ( foo => 1, bar => 2);
my $num_buckets= num_buckets(%test);
if ($num_buckets < 8) {
    plan skip_all => "num_buckets() not working as expected.";
}

# This logic needs to be revisted...
#
# Try and find 15 hash collisions in "A".."Z"
# we will use the colliding keys to produce hashes
# with the same contents, but with different key orders,
# which we will use to test the "sort_keys" logic.

my $max= 15;
my %hash;
my ( %i, %j );
keys %i= $max;
keys %j= $max;

LOOP:
for my $x ( "A" .. "Z" ) {
    for my $y ( chr( ord($x) + 1 ) .. "Z" ) {
        %i= ();
        %j= ();
        $i{$x}= 1; $i{$y}= 1;
        $j{$y}= 1; $j{$x}= 1;
        if ( "@{[keys %i]}" ne "@{[keys %j]}" ) {    # collission?
            $hash{$x}= 1;
            last LOOP if keys %hash == $max;
            $hash{$y}= 1;
            last LOOP if keys %hash == $max;
        }
    }
}

my %copy= %hash;
my $copy_keys= join "", keys %copy;

my %bigger= %hash;
keys(%bigger)= 1024;

my %shuffled;
$shuffled{$_}= $hash{$_} for shuffle keys %hash;

my %encoded;
my %encoded_unsorted;
for ( \%hash, \%copy, \%bigger, \%shuffled ) {
    my $keys= join "", keys %$_;
    $encoded{$keys}          ||= encode_sereal( $_, { sort_keys => 1 } );
    $encoded_unsorted{$keys} ||= encode_sereal($_);
}
my @keys= keys %encoded;

if ( @keys > 1 ) {
    plan tests => 2 * ( ( @keys * ( @keys - 1 ) ) / 2 );
}
else {
    plan skip_all => "Could not generate test hashes";
}

foreach my $x ( 0 .. $#keys ) {
    foreach my $y ( $x + 1 .. $#keys ) {
        is(
            $encoded{ $keys[$x] }, $encoded{ $keys[$y] },
            "$keys[$x] vs $keys[$y] (same: sort_keys)"
        );
        SKIP: {
            skip "test causes random false failures", 1;
            isnt(
                $encoded_unsorted{ $keys[$x] }, $encoded_unsorted{ $keys[$y] },
                "$keys[$x] vs $keys[$y] (different: no sort_keys)"
            );
        }
    }
}