File: 04russian_plural_function.t

package info (click to toggle)
libintl-perl 1.26-2
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 5,696 kB
  • ctags: 495
  • sloc: perl: 156,143; makefile: 137
file content (54 lines) | stat: -rw-r--r-- 1,336 bytes parent folder | download | duplicates (9)
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
#! /usr/local/bin/perl -w

# vim: syntax=perl
# vim: tabstop=4

use strict;

use Test;

use Locale::gettext_pp;

BEGIN {
	plan tests => 2006;
}

sub russian_plural {
    my $n = shift;

    my ($plural, $nplurals);

    $nplurals = 3;
    $plural = ($n % 10 == 1 && $n % 100 != 11 ? 0 : $n % 10 >= 2 && $n % 10 <= 4 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);

    return ($nplurals, $plural ? $plural : 0);
}

# This test uses private functions of Locale::gettext_pp.  Do NOT use this as
# an example for your own code.

my $code = 'nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2';

my $untainted = Locale::gettext_pp::__untaint_plural_header $code;

ok length $untainted;

my $plural_function = Locale::gettext_pp::__compile_plural_function $code;

ok $plural_function;
ok ref $plural_function;
ok 'CODE' eq ref $plural_function;

foreach my $n (0 .. 1000) {
    my ($got_nplurals, $got_plural) = $plural_function->($n);
    my ($wanted_nplurals, $wanted_plural) = russian_plural $n;

    ok $got_nplurals, $wanted_nplurals,
       "wanted $wanted_nplurals, got $got_nplurals nplurals for n = $n";
    ok $got_plural, $wanted_plural,
       "wanted plural form #$wanted_nplurals, got #$got_nplurals for n = $n";


    print "$n:$got_plural:$wanted_plural\n";
}