File: regression.t

package info (click to toggle)
libdata-hexdumper-perl 3.0001-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 136 kB
  • ctags: 5
  • sloc: perl: 387; makefile: 2
file content (176 lines) | stat: -rwxr-xr-x 6,761 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!perl -w
# $Id: regression.t,v 1.1 2009/03/02 22:00:39 drhyde Exp $

use strict;

use Test::More tests => 16;

use Data::Hexdumper qw(hexdump);

eval { hexdump('foo', {number_format => 'R', suppress_warnings => 1}) };
ok($@, "invalid format is fatal: $@");

is("\n".hexdump(
    data => join('', map { pack('C', $_) } (0x20 .. 0x3F)),
    number_format => 'N',
    start_position => 0,
    suppress_warnings => 1,
    end_position => 0x1F
), q{
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C3D3E3F             : 0123456789:;<=>?
}, "big-endian 32-bit words, no padding");

ok("\n".hexdump(
    data => join('', map { pack('C', $_) } (0x20 .. 0x3F)),
    number_format => 'N',
    start_position => 0,
    end_position => 0x1F,
    suppress_warnings => 1,
    space_as_space => 1
) eq q{
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             :  !"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C3D3E3F             : 0123456789:;<=>?
}, "space_as_space");

ok("\n".hexdump(
    data => join('', map { pack('C', $_) } (0x20 .. 0x3F)),
    suppress_warnings => 1,
    number_format => 'N'
) eq q{
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C3D3E3F             : 0123456789:;<=>?
}, "start_position and end_position defaults");

my $results = '';
foreach my $format(qw (C n v V)) { # same trivial test for other formats
    $results .= hexdump(
        data => join('', map { pack('C', $_) } (0x20 .. 0x3F)),
        suppress_warnings => 1,
        number_format => $format 
    );
}
ok("\n".$results eq q{
  0x0000 : 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F : .!"#$%&'()*+,-./
  0x0010 : 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F : 0123456789:;<=>?
  0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F         : .!"#$%&'()*+,-./
  0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 3E3F         : 0123456789:;<=>?
  0x0000 : 2120 2322 2524 2726 2928 2B2A 2D2C 2F2E         : .!"#$%&'()*+,-./
  0x0010 : 3130 3332 3534 3736 3938 3B3A 3D3C 3F3E         : 0123456789:;<=>?
  0x0000 : 23222120 27262524 2B2A2928 2F2E2D2C             : .!"#$%&'()*+,-./
  0x0010 : 33323130 37363534 3B3A3938 3F3E3D3C             : 0123456789:;<=>?
}, "other data formats");

ok("\n".hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'N'
) eq q{
  0x0000 : 10111213 14151617 18191A1B 1C1D1E1F             : ................
  0x0010 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
}, "unprintable characters");

ok(hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'S'
) eq hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'n'
) || hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'S'
) eq hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'v'
), "S eq n or v");

ok(hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'L'
) eq hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'N'
) || hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'L'
) eq hexdump(
    data => join('', map { pack('C', $_) } (0x10 .. 0x2F)),
    suppress_warnings => 1,
    number_format => 'V'
), "L eq N or V");

$results = '';
foreach my $format (qw(N n)) {
    foreach my $max (0x3C, 0x3D, 0x3E) {
        $results .= hexdump(
            data => join('', map { pack('C', $_) } (0x20 .. $max)),
            suppress_warnings => 1,
            number_format => $format,
            suppress_warnings => 1
        );
    }
}
is("\n".$results, q{
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C000000             : 0123456789:;<...
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C3D0000             : 0123456789:;<=..
  0x0000 : 20212223 24252627 28292A2B 2C2D2E2F             : .!"#$%&'()*+,-./
  0x0010 : 30313233 34353637 38393A3B 3C3D3E00             : 0123456789:;<=>.
  0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F         : .!"#$%&'()*+,-./
  0x0010 : 3031 3233 3435 3637 3839 3A3B 3C00 0000         : 0123456789:;<...
  0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F         : .!"#$%&'()*+,-./
  0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 0000         : 0123456789:;<=..
  0x0000 : 2021 2223 2425 2627 2829 2A2B 2C2D 2E2F         : .!"#$%&'()*+,-./
  0x0010 : 3031 3233 3435 3637 3839 3A3B 3C3D 3E00         : 0123456789:;<=>.
}, "NULL-padding");

is("\n".hexdump(data => '!', suppress_warnings => 1), q{
  0x0000 : 21 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : !...............
}, "Single byte data");

is("\n".hexdump(data => '0', suppress_warnings => 1), q{
  0x0000 : 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : 0...............
}, "Can dump a zero (0, not NULL!) byte");

ok(hexdump(data => 'abcdefghijklmnop') eq hexdump('abcdefghijklmnop'),
    'hexdump($string) works');
ok(hexdump(
    data => join('', map { pack('C', $_) } (0x00 .. 0x3F)),
    number_format => 'N',
    start_position => 0,
    suppress_warnings => 1,
    end_position => 0x1F,
    space_as_space => 1
) eq hexdump(
    join('', map { pack('C', $_) } (0x00 .. 0x3F)),
    {
        number_format => 'N',
        start_position => 0,
        suppress_warnings => 1,
        end_position => 0x1F,
        space_as_space => 1
    }
), 'hexdump($string, {... opts ...}) works');

ok((Data::Hexdumper::LITTLEENDIAN &&
    hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q' }) eq 
    hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q<' })
) || (
    Data::Hexdumper::BIGENDIAN &&
    hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q' }) eq 
    hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q>' })
), "64 bit native byte order works");
is_deeply("\n".hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q<' }) , q{
  0x0000 : 6867666564636261 706F6E6D6C6B6A69               : abcdefghijklmnop
}, "64 bit little-endian works");
ok("\n".hexdump('abcdefghijklmnop', { suppress_warnings => 1, number_format => 'Q>' }) eq q{
  0x0000 : 6162636465666768 696A6B6C6D6E6F70               : abcdefghijklmnop
}, "64 bit big-endian works");