<commit_before>#! perl
# Copyright (C) 2006-2007, The Perl Foundation.
# $Id$

use strict;
use warnings;

use lib qw( . lib ../lib ../../lib );
use Test::More tests => 1;
use Parrot::Distribution;

=head1 NAME

t/codingstd/check_isxxx.t - checks that the isxxx() functions are passed
unsigned char

=head1 SYNOPSIS

    # test all files
    % prove t/codingstd/check_isxxx.t

    # test specific files
    % perl t/codingstd/check_isxxx.t src/foo.c include/parrot/bar.h

=head1 DESCRIPTION

Checks all C language files to make sure that arguments to the isxxx()
functions are explicitly cast to unsigned char.

=head1 SEE ALSO

L<docs/pdds/pdd07_codingstd.pod>

=cut

my $DIST = Parrot::Distribution->new;
my @files = @ARGV ? @ARGV : $DIST->get_c_language_files();
my @no_explicit_cast;
my @isxxx_functions_list = qw(
    isalnum
    isalpha
    isblank
    iscntrl
    isdigit
    isgraph
    islower
    isprint
    ispunct
    isspace
    isupper
);
my $isxxx_functions = join '|', @isxxx_functions_list;

foreach my $file (@files) {
    # if we have command line arguments, the file is the full path
    # otherwise, use the relevant Parrot:: path method
    my $path = @ARGV ? $file : $file->path;

    my $buf = $DIST->slurp( $path );

    my @buffer_lines = split( /\n/, $buf );

    # find out if isxxx() functions appear in the file
    my $num_isxxx = grep m/[^_]($isxxx_functions)\(/, @buffer_lines;

    # if so, check if the args are cast to unsigned char
    if ($num_isxxx) {

        # get the lines just matching isxxx
        my @isxxx_lines = grep m/[^_]($isxxx_functions)\(/, @buffer_lines;

        # find the instances without the explicit cast
        my $num_no_cast = grep !m/[^_]($isxxx_functions)\(\(unsigned char\)/, @isxxx_lines;

        $path .= "\n";
        push @no_explicit_cast, $path if $num_no_cast;
    }
    else {
        next;
    }
}

ok( !scalar(@no_explicit_cast), 'isxxx() functions cast correctly' )
    or diag( "isxxx() function not cast to unsigned char "
        . scalar @no_explicit_cast
        . " files:\n@no_explicit_cast" );

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:<commit_msg>[t] Make the isxxx() test slightly less algorithmically intensive and slightly more useful in its diagnostic output.<commit_after>#! perl
# Copyright (C) 2006-2007, The Perl Foundation.
# $Id$

use strict;
use warnings;

use lib qw( . lib ../lib ../../lib );
use Test::More tests => 1;
use Parrot::Distribution;

=head1 NAME

t/codingstd/check_isxxx.t - checks that the isxxx() functions are passed
unsigned char

=head1 SYNOPSIS

    # test all files
    % prove t/codingstd/check_isxxx.t

    # test specific files
    % perl t/codingstd/check_isxxx.t src/foo.c include/parrot/bar.h

=head1 DESCRIPTION

Checks all C language files to make sure that arguments to the isxxx()
functions are explicitly cast to unsigned char.

=head1 SEE ALSO

L<docs/pdds/pdd07_codingstd.pod>

=cut

my $DIST  = Parrot::Distribution->new;
my @files = @ARGV ? @ARGV : $DIST->get_c_language_files();

my @no_explicit_cast;

my @isxxx_functions_list = qw(
    isalnum
    isalpha
    isblank
    iscntrl
    isdigit
    isgraph
    islower
    isprint
    ispunct
    isspace
    isupper
);

my $isxxx_functions = join '|', @isxxx_functions_list;

foreach my $file (@files) {
    # if we have command line arguments, the file is the full path
    # otherwise, use the relevant Parrot:: path method
    my $path = @ARGV ? $file : $file->path;

    my $buf = $DIST->slurp( $path );

    my @buffer_lines = split( /\n/, $buf );

    my $i = 1;

    # get the lines just matching isxxx
    my @isxxx_lines = grep { $_->[0] =~ /[^_]($isxxx_functions)\(/ }
        map { [ $_, $i++ ] } @buffer_lines;

    next unless @isxxx_lines;

    # find the instances without the explicit cast
    my @no_cast =
        grep { $_->[0] !~ /[^_]($isxxx_functions)\(\(unsigned char\)/ }
        @isxxx_lines;

    push @no_explicit_cast, $path . ' ('
        . join(", ", map { $_->[1] } @isxxx_lines) . ")\n" if @no_cast;
}

ok( ! @no_explicit_cast, 'isxxx() functions cast correctly' )
    or diag( "isxxx() function not cast to unsigned char "
        . scalar @no_explicit_cast
        . " files:\n@no_explicit_cast" );

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
{'fraction_non_alphanumeric': 0.1251131221719457, 'fraction_numerical': 0.009954751131221719, 'mean_word_length': 2.993676603432701, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4367635', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1525, 'n_words': 465}