File: 03_all_formats.t

package info (click to toggle)
libtest-hexdifferences-perl 1.001-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 862; makefile: 7
file content (65 lines) | stat: -rw-r--r-- 1,419 bytes parent folder | download
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
#!perl -T

use strict;
use warnings;

use Test::More tests => 2 + 1;
use Test::NoWarnings;
use Test::Differences;

BEGIN {
    use_ok('Test::HexDifferences::HexDump');
}

my $bytes = <<"EOT";
\x11
\x21\x22\x21\x22\x21\x22
\x21\x22\x21\x22
\x41\x42\x43\x44\x41\x42\x43\x44\x41\x42\x43\x44
\x41\x42\x43\x44\x41\x42\x43\x44
\x81\x82\x83\x84\x85\x86\x87\x88
\x81\x82\x83\x84\x85\x86\x87\x88
\x81\x82\x83\x84\x85\x86\x87\x88
EOT
$bytes =~ s{\n}{}xmsg;

my $format = <<"EOT";
1 byte: %a %C\n%1x%
2 byte: %a %S %S< %S>\n%1x%
2 byte: %a %v %n\n%1x%
4 byte: %a %L %L< %L>\n%1x%
4 byte: %a %V %N\n%1x%
8 byte: %a %Q\n%1x%
8 byte: %a %Q<\n%1x%
8 byte: %a %Q>\n%1x%
EOT

# big-endian (network order) or little-endian
my $result = ( pack 'S', 1 ) eq ( pack 'n', 1 ) ? <<'EOT' : <<'EOT';
1 byte: 0000 11
2 byte: 0001 2122 2221 2122
2 byte: 0007 2221 2122
4 byte: 000B 41424344 44434241 41424344
4 byte: 0017 44434241 41424344
8 byte: 001F 8182838485868788
8 byte: 0027 8887868584838281
8 byte: 002F 8182838485868788
EOT
1 byte: 0000 11
2 byte: 0001 2221 2221 2122
2 byte: 0007 2221 2122
4 byte: 000B 44434241 44434241 41424344
4 byte: 0017 44434241 41424344
8 byte: 001F 8887868584838281
8 byte: 0027 8887868584838281
8 byte: 002F 8182838485868788
EOT

eq_or_diff(
    hex_dump(
        $bytes,
        { format => $format },
    ),
    $result,
    'all formats',
);