Debian Codegraph @

tests/datamash-md5.pl

raw
1 #!/usr/bin/env perl
2 =pod
3 Unit Tests for GNU Datamash - tests md5 operations
4
5 Copyright (C) 2013-2021 Assaf Gordon <[email protected]>
6 Copyright (C) 2022-2025 Timothy Rice <[email protected]>
7
8 This file is part of GNU Datamash.
9
10 GNU Datamash is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 GNU Datamash is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GNU Datamash. If not, see <https://www.gnu.org/licenses/>.
22
23 Written by Assaf Gordon.
24 =cut
25 ## NOTE: Digest::MD5 is supposed to be a core module,
26 ## but some OSes don't distributed it (e.g. CentOS 7 requires
27 ## a separate package 'perl-Digest-MD5').
28 ## If not available, skip this tests (instead of failing).
29 use strict;
30 use warnings;
31
32 # Until a better way comes along to auto-use Coreutils Perl modules
33 # as in the coreutils' autotools system.
34 use Coreutils;
35 use CuSkip;
36 use CuTmpdir qw(datamash);
37
38 ## Perl 5.8 and earlier do not have Digest::SHA as core module.
39 ## Skip the test if it is not found.
40 my $have_sha =
41 eval qq{use Digest::MD5 qw(md5_hex);1;};
42
43 CuSkip::skip "requires Perl with Digest::MD5 module\nload error:\n$@"
44 unless $have_sha;
45
46 (my $program_name = $0) =~ s|.*/||;
47 my $prog_bin = 'datamash';
48
49 ## Cross-Compiling portability hack:
50 ## under qemu/binfmt, argv[0] (which is used to report errors) will contain
51 ## the full path of the binary, if the binary is on the $PATH.
52 ## So we try to detect what is the actual returned value of the program
53 ## in case of an error.
54 my $prog = `$prog_bin ---print-progname`;
55 $prog = $prog_bin unless $prog;
56
57 # Turn off localization of executable's output.
58 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
59
60 my $in_g1=<<'EOF';
61 A 100
62 A 10
63 A 50
64 A 35
65 EOF
66
67 # Header line, with custom field separator
68 my $in_hdr2=<<'EOF';
69 x:y:z
70 A:3:W
71 A:5:W
72 A:7:W
73 A:11:X
74 A:13:X
75 B:17:Y
76 B:19:Z
77 C:23:Z
78 EOF
79
80 =pod
81 Example:
82 my $data = "a 1\nb 2\n";
83 my $out = transform_column($data, 2, \&md5_hex);
84 # out => md5_hex("1") . "\n" . md5_hex("2") . "\n" ;
85 =cut
86 sub transform_column($$$)
87 {
88 my $input_text = shift;
89 my $input_column = shift;
90 my $function = shift;
91
92 return join "",
93 map { "$_\n" }
94 map { &$function($_->[ $input_column - 1 ]) }
95 map { [ split / / ] }
96 split("\n", $input_text);
97 }
98
99 # md5 of the second column of '$in_g1'
100 my $out_g1_md5 = transform_column ($in_g1, 2, \&md5_hex);
101
102 my @Tests =
103 (
104 ['md5-1', '-W md5 2', {IN_PIPE=>$in_g1}, {OUT=>$out_g1_md5}],
105 );
106
107 my $save_temps = $ENV{SAVE_TEMPS};
108 my $verbose = $ENV{VERBOSE};
109
110 my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
111 exit $fail;