Blame tests/gen-casemap-txt.pl

Packit ae235b
#! /usr/bin/perl -w
Packit ae235b
Packit ae235b
#    Copyright (C) 1998, 1999 Tom Tromey
Packit ae235b
#    Copyright (C) 2001 Red Hat Software
Packit ae235b
Packit ae235b
#    This program is free software; you can redistribute it and/or modify
Packit ae235b
#    it under the terms of the GNU General Public License as published by
Packit ae235b
#    the Free Software Foundation; either version 2, or (at your option)
Packit ae235b
#    any later version.
Packit ae235b
Packit ae235b
#    This program is distributed in the hope that it will be useful,
Packit ae235b
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit ae235b
#    GNU General Public License for more details.
Packit ae235b
Packit ae235b
#    You should have received a copy of the GNU General Public License
Packit ae235b
#    along with this program; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
Packit ae235b
# gen-casemap-test.pl - Generate test cases for case mapping from Unicode data.
Packit ae235b
# See http://www.unicode.org/Public/UNIDATA/UnicodeCharacterDatabase.html
Packit ae235b
# I consider the output of this program to be unrestricted.  Use it as
Packit ae235b
# you will.
Packit ae235b
Packit ae235b
require 5.006;
Packit ae235b
use utf8;
Packit ae235b
Packit ae235b
if (@ARGV != 3) {
Packit ae235b
    $0 =~ s@.*/@@;
Packit ae235b
    die "Usage: $0 UNICODE-VERSION UnicodeData.txt SpecialCasing.txt\n";
Packit ae235b
}
Packit ae235b
 
Packit ae235b
use vars qw($CODE $NAME $CATEGORY $COMBINING_CLASSES $BIDI_CATEGORY $DECOMPOSITION $DECIMAL_VALUE $DIGIT_VALUE $NUMERIC_VALUE $MIRRORED $OLD_NAME $COMMENT $UPPER $LOWER $TITLE $BREAK_CODE $BREAK_CATEGORY $BREAK_NAME $CASE_CODE $CASE_LOWER $CASE_TITLE $CASE_UPPER $CASE_CONDITION);
Packit ae235b
Packit ae235b
# Names of fields in Unicode data table.
Packit ae235b
$CODE = 0;
Packit ae235b
$NAME = 1;
Packit ae235b
$CATEGORY = 2;
Packit ae235b
$COMBINING_CLASSES = 3;
Packit ae235b
$BIDI_CATEGORY = 4;
Packit ae235b
$DECOMPOSITION = 5;
Packit ae235b
$DECIMAL_VALUE = 6;
Packit ae235b
$DIGIT_VALUE = 7;
Packit ae235b
$NUMERIC_VALUE = 8;
Packit ae235b
$MIRRORED = 9;
Packit ae235b
$OLD_NAME = 10;
Packit ae235b
$COMMENT = 11;
Packit ae235b
$UPPER = 12;
Packit ae235b
$LOWER = 13;
Packit ae235b
$TITLE = 14;
Packit ae235b
Packit ae235b
# Names of fields in the SpecialCasing table
Packit ae235b
$CASE_CODE = 0;
Packit ae235b
$CASE_LOWER = 1;
Packit ae235b
$CASE_TITLE = 2;
Packit ae235b
$CASE_UPPER = 3;
Packit ae235b
$CASE_CONDITION = 4;
Packit ae235b
Packit ae235b
my @upper;
Packit ae235b
my @title;
Packit ae235b
my @lower;
Packit ae235b
Packit ae235b
binmode STDOUT, ":utf8";
Packit ae235b
open (INPUT, "< $ARGV[1]") || exit 1;
Packit ae235b
Packit ae235b
$last_code = -1;
Packit ae235b
while (<INPUT>)
Packit ae235b
{
Packit ae235b
    chop;
Packit ae235b
    @fields = split (';', $_, 30);
Packit ae235b
    if ($#fields != 14)
Packit ae235b
    {
Packit ae235b
	printf STDERR ("Entry for $fields[$CODE] has wrong number of fields (%d)\n", $#fields);
Packit ae235b
    }
Packit ae235b
Packit ae235b
    $code = hex ($fields[$CODE]);
Packit ae235b
Packit ae235b
    if ($code > $last_code + 1)
Packit ae235b
    {
Packit ae235b
	# Found a gap.
Packit ae235b
	if ($fields[$NAME] =~ /Last>/)
Packit ae235b
	{
Packit ae235b
	    # Fill the gap with the last character read,
Packit ae235b
            # since this was a range specified in the char database
Packit ae235b
	    @gfields = @fields;
Packit ae235b
	}
Packit ae235b
	else
Packit ae235b
	{
Packit ae235b
	    # The gap represents undefined characters.  Only the type
Packit ae235b
	    # matters.
Packit ae235b
	    @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
Packit ae235b
			'', '', '', '');
Packit ae235b
	}
Packit ae235b
	for (++$last_code; $last_code < $code; ++$last_code)
Packit ae235b
	{
Packit ae235b
	    $gfields{$CODE} = sprintf ("%04x", $last_code);
Packit ae235b
	    &process_one ($last_code, @gfields);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
    &process_one ($code, @fields);
Packit ae235b
    $last_code = $code;
Packit ae235b
}
Packit ae235b
Packit ae235b
close INPUT;
Packit ae235b
Packit ae235b
open (INPUT, "< $ARGV[2]") || exit 1;
Packit ae235b
Packit ae235b
while (<INPUT>)
Packit ae235b
{
Packit ae235b
    my $code;
Packit ae235b
    
Packit ae235b
    chop;
Packit ae235b
Packit ae235b
    next if /^#/;
Packit ae235b
    next if /^\s*$/;
Packit ae235b
Packit ae235b
    s/\s*#.*//;
Packit ae235b
Packit ae235b
    @fields = split ('\s*;\s*', $_, 30);
Packit ae235b
Packit ae235b
    $raw_code = $fields[$CASE_CODE];
Packit ae235b
    $code = hex ($raw_code);
Packit ae235b
Packit ae235b
    if ($#fields != 4 && $#fields != 5)
Packit ae235b
    {
Packit ae235b
	printf STDERR ("Entry for $raw_code has wrong number of fields (%d)\n", $#fields);
Packit ae235b
	next;
Packit ae235b
    }
Packit ae235b
Packit ae235b
    if (defined $fields[5]) {
Packit ae235b
	# Ignore conditional special cases - we'll handle them manually
Packit ae235b
	next;
Packit ae235b
    }
Packit ae235b
Packit ae235b
    $upper[$code] = &make_hex ($fields[$CASE_UPPER]);
Packit ae235b
    $lower[$code] = &make_hex ($fields[$CASE_LOWER]);
Packit ae235b
    $title[$code] = &make_hex ($fields[$CASE_TITLE]);
Packit ae235b
}
Packit ae235b
Packit ae235b
close INPUT;
Packit ae235b
Packit ae235b
print <
Packit ae235b
# Test cases generated from Unicode $ARGV[0] data
Packit ae235b
# by gen-case-tests.pl. Do not edit.
Packit ae235b
#
Packit ae235b
# Some special hand crafted tests
Packit ae235b
#
Packit ae235b
tr_TR\ti\ti\t\x{0130}\t\x{0130}\t# i => LATIN CAPITAL LETTER I WITH DOT ABOVE
Packit ae235b
tr_TR\tI\t\x{0131}\tI\tI\t# I => LATIN SMALL LETTER DOTLESS I
Packit ae235b
tr_TR\tI\x{0307}\ti\tI\x{0307}\tI\x{0307}\t# I => LATIN SMALL LETTER DOTLESS I
Packit ae235b
tr_TR.UTF-8\ti\ti\t\x{0130}\t\x{0130}\t# i => LATIN CAPITAL LETTER I WITH DOT ABOVE
Packit ae235b
tr_TR.UTF-8\tI\t\x{0131}\tI\tI\t# I => LATIN SMALL LETTER DOTLESS I
Packit ae235b
tr_TR.UTF-8\tI\x{0307}\ti\tI\x{0307}\tI\x{0307}\t# I => LATIN SMALL LETTER DOTLESS I
Packit ae235b
# Test reordering of YPOGEGRAMMENI across other accents
Packit ae235b
\t\x{03b1}\x{0345}\x{0314}\t\x{03b1}\x{0345}\x{314}\t\x{0391}\x{0345}\x{0314}\t\x{0391}\x{0314}\x{0399}\t
Packit ae235b
\t\x{03b1}\x{0314}\x{0345}\t\x{03b1}\x{314}\x{0345}\t\x{0391}\x{0314}\x{0345}\t\x{0391}\x{0314}\x{0399}\t
Packit ae235b
# Handling of final and nonfinal sigma
Packit ae235b
	ΜΆΙΟΣ 	μάιος 	Μάιος 	ΜΆΙΟΣ 	
Packit ae235b
	ΜΆΙΟΣ	μάιος	Μάιος	ΜΆΙΟΣ	
Packit ae235b
	ΣΙΓΜΑ	σιγμα	Σιγμα	ΣΙΓΜΑ	
Packit ae235b
# Lithuanian rule of i followed by letter with dot. Not at all sure
Packit ae235b
# about the titlecase part here
Packit ae235b
lt_LT\ti\x{117}\ti\x{117}\tIe\tIE\t
Packit ae235b
lt_LT\tie\x{307}\tie\x{307}\tIe\tIE\t
Packit ae235b
lt_LT\t\x{00cc}\ti\x{0307}\x{0300}\t\x{00cc}\t\x{00cc}\t # LATIN CAPITAL LETTER I WITH GRAVE
Packit ae235b
lt_LT\t\x{00CD}\ti\x{0307}\x{0301}\t\x{00CD}\t\x{00CD}\t # LATIN CAPITAL LETTER I WITH ACUTE
Packit ae235b
lt_LT\t\x{0128}\ti\x{0307}\x{0303}\t\x{0128}\t\x{0128}\t # LATIN CAPITAL LETTER I WITH TILDE
Packit ae235b
lt_LT\tI\x{0301}\ti\x{0307}\x{0301}\tI\x{0301}\tI\x{0301}\t # LATIN CAPITAL LETTER I (with acute accent)
Packit ae235b
lt_LT\tI\x{0300}\ti\x{0307}\x{0300}\tI\x{0300}\tI\x{0300}\t # LATIN CAPITAL LETTER I (with grave accent)
Packit ae235b
lt_LT\tI\x{0303}\ti\x{0307}\x{0303}\tI\x{0303}\tI\x{0303}\t # LATIN CAPITAL LETTER I (with tilde above)
Packit ae235b
lt_LT\tI\x{0328}\x{0301}\ti\x{0307}\x{0328}\x{0301}\tI\x{0328}\x{0301}\tI\x{0328}\x{0301}\t # LATIN CAPITAL LETTER I (with ogonek and acute accent)
Packit ae235b
lt_LT\tJ\x{0301}\tj\x{0307}\x{0301}\tJ\x{0301}\tJ\x{0301}\t # LATIN CAPITAL LETTER J (with acute accent)
Packit ae235b
lt_LT\t\x{012e}\x{0301}\t\x{012f}\x{0307}\x{0301}\t\x{012e}\x{0301}\t\x{012e}\x{0301}\t # LATIN CAPITAL LETTER I WITH OGONEK (with acute accent)
Packit ae235b
lt_LT.UTF-8\ti\x{117}\ti\x{117}\tIe\tIE\t
Packit ae235b
lt_LT.UTF-8\tie\x{307}\tie\x{307}\tIe\tIE\t
Packit ae235b
lt_LT.UTF-8\t\x{00cc}\ti\x{0307}\x{0300}\t\x{00cc}\t\x{00cc}\t # LATIN CAPITAL LETTER I WITH GRAVE
Packit ae235b
lt_LT.UTF-8\t\x{00CD}\ti\x{0307}\x{0301}\t\x{00CD}\t\x{00CD}\t # LATIN CAPITAL LETTER I WITH ACUTE
Packit ae235b
lt_LT.UTF-8\t\x{0128}\ti\x{0307}\x{0303}\t\x{0128}\t\x{0128}\t # LATIN CAPITAL LETTER I WITH TILDE
Packit ae235b
lt_LT.UTF-8\tI\x{0301}\ti\x{0307}\x{0301}\tI\x{0301}\tI\x{0301}\t # LATIN CAPITAL LETTER I (with acute accent)
Packit ae235b
lt_LT.UTF-8\tI\x{0300}\ti\x{0307}\x{0300}\tI\x{0300}\tI\x{0300}\t # LATIN CAPITAL LETTER I (with grave accent)
Packit ae235b
lt_LT.UTF-8\tI\x{0303}\ti\x{0307}\x{0303}\tI\x{0303}\tI\x{0303}\t # LATIN CAPITAL LETTER I (with tilde above)
Packit ae235b
lt_LT.UTF-8\tI\x{0328}\x{0301}\ti\x{0307}\x{0328}\x{0301}\tI\x{0328}\x{0301}\tI\x{0328}\x{0301}\t # LATIN CAPITAL LETTER I (with ogonek and acute accent)
Packit ae235b
lt_LT.UTF-8\tJ\x{0301}\tj\x{0307}\x{0301}\tJ\x{0301}\tJ\x{0301}\t # LATIN CAPITAL LETTER J (with acute accent)
Packit ae235b
lt_LT.UTF-8\t\x{012e}\x{0301}\t\x{012f}\x{0307}\x{0301}\t\x{012e}\x{0301}\t\x{012e}\x{0301}\t # LATIN CAPITAL LETTER I WITH OGONEK (with acute accent)
Packit ae235b
# Special case not at initial position
Packit ae235b
\ta\x{fb04}\ta\x{fb04}\tAffl\tAFFL\t# FB04
Packit ae235b
#
Packit ae235b
# Now the automatic tests
Packit ae235b
#
Packit ae235b
EOT
Packit ae235b
&print_tests;
Packit ae235b
Packit ae235b
exit 0;
Packit ae235b
Packit ae235b
# Process a single character.
Packit ae235b
sub process_one
Packit ae235b
{
Packit ae235b
    my ($code, @fields) = @_;
Packit ae235b
Packit ae235b
    my $type =  $fields[$CATEGORY];
Packit ae235b
    if ($type eq 'Ll')
Packit ae235b
    {
Packit ae235b
	$upper[$code] = make_hex ($fields[$UPPER]);
Packit ae235b
	$lower[$code] = pack ("U", $code);
Packit ae235b
	$title[$code] = make_hex ($fields[$TITLE]);
Packit ae235b
    }
Packit ae235b
    elsif ($type eq 'Lu')
Packit ae235b
    {
Packit ae235b
	$lower[$code] = make_hex ($fields[$LOWER]);
Packit ae235b
	$upper[$code] = pack ("U", $code);
Packit ae235b
	$title[$code] = make_hex ($fields[$TITLE]);
Packit ae235b
    }
Packit ae235b
Packit ae235b
    if ($type eq 'Lt')
Packit ae235b
    {
Packit ae235b
	$upper[$code] = make_hex ($fields[$UPPER]);
Packit ae235b
	$lower[$code] = pack ("U", hex ($fields[$LOWER]));
Packit ae235b
	$title[$code] = make_hex ($fields[$LOWER]);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
sub print_tests
Packit ae235b
{
Packit ae235b
    for ($i = 0; $i < 0x10ffff; $i++) {
Packit ae235b
	if ($i == 0x3A3) {
Packit ae235b
	    # Greek sigma needs special tests
Packit ae235b
	    next;
Packit ae235b
	}
Packit ae235b
	
Packit ae235b
	my $lower = $lower[$i];
Packit ae235b
	my $title = $title[$i];
Packit ae235b
	my $upper = $upper[$i];
Packit ae235b
Packit ae235b
	if (defined $upper || defined $lower || defined $title) {
Packit ae235b
	    printf "\t%s\t%s\t%s\t%s\t# %4X\n",
Packit ae235b
		    pack ("U", $i),
Packit ae235b
		    (defined $lower ? $lower : ""),
Packit ae235b
		    (defined $title ? $title : ""),
Packit ae235b
		    (defined $upper ? $upper : ""),
Packit ae235b
                    $i;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
sub make_hex
Packit ae235b
{
Packit ae235b
    my $codes = shift;
Packit ae235b
Packit ae235b
    $codes =~ s/^\s+//;
Packit ae235b
    $codes =~ s/\s+$//;
Packit ae235b
Packit ae235b
    if ($codes eq "0" || $codes eq "") {
Packit ae235b
	return "";
Packit ae235b
    } else {
Packit ae235b
	return pack ("U*", map { hex ($_) } split /\s+/, $codes);
Packit ae235b
    }
Packit ae235b
}