File: spans.t

package info (click to toggle)
libset-intspan-perl 1.19-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 1,236; makefile: 2
file content (136 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (3)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- perl -*-

use strict;
use Set::IntSpan 1.17 qw(grep_spans map_spans);

my $N = 1;
sub Not { print "not " }
sub OK  { print "ok ", $N++, "\n" }

my @Sets  = split(' ', q{ - (-) (-0 0-) 1 5 1-3 3-7 1-3,8,10-23 1-3,8,10-23,30-) });

sub long_span
{
    my($l, $u) = @$_; 
    not defined $l or 
    not defined $u or
    $u-$l > 3
}

sub short_span
{
    my($l, $u) = @$_; 
    defined $l and
    defined $u and
    $u-$l < 3
}

my @Greps = ('0', '1', 'long_span', 'short_span');

sub mirror
{
    my($l, $u) = @$_;

       if (    defined $l and 	  defined $u) { return [ -$u  , -$l   ] }
    elsif (not defined $l and 	  defined $u) { return [ -$u  , undef ] }
    elsif (    defined $l and not defined $u) { return [ undef, -$l   ] }
    else                                      { return [ undef, undef ] }
}

sub mirror_mirror
{
    my($l, $u) = @$_;

       if (    defined $l and 	  defined $u) { return [ -$u  , -$l   ], [ $l    , $u   ] }
    elsif (not defined $l and 	  defined $u) { return [ -$u  , undef ], [ undef , $u   ] }
    elsif (    defined $l and not defined $u) { return [ undef, -$l   ], [ $l    , undef] }
    else                                      { return [ undef, undef ], [ undef, undef ] }
}

sub double_up
{
    my($l, $u) = @$_;

       if (    defined $l and 	  defined $u) { return [ 2*$l , 2*$u  ] }
    elsif (not defined $l and 	  defined $u) { return [ undef, 2*$u  ] }
    elsif (    defined $l and not defined $u) { return [ 2*$l,  undef ] }
    else                                      { return [ undef, undef ] }
}

sub stretch_up
{
    my($l, $u) = @$_;

       if (    defined $l and 	  defined $u) { return [ $l   , $u+5  ] }
    elsif (not defined $l and 	  defined $u) { return [ undef, $u+5  ] }
    elsif (    defined $l and not defined $u) { return [ $l   , undef ] }
    else                                      { return [ undef, undef ] }
}


my @Maps  = ('()', '$_', 'mirror', 'mirror_mirror', 'double_up', 'stretch_up');

print "1..", @Sets * (@Greps + @Maps), "\n";

Grep();
Map ();

sub Grep
{
    print "#grep_span\n";

    my @expected = 
    (['-', ' - ', ' - ', ' - ', '-', '-', ' - ', ' - ', ' -         ', ' -              '],
     ['-', '(-)', '(-0', '0-)', '1', '5', '1-3', '3-7', '1-3,8,10-23', '1-3,8,10-23,30-)'],
     ['-', '(-)', '(-0', '0-)', '-', '-', ' - ', '3-7', '      10-23', '      10-23,30-)'],
     ['-', ' - ', ' - ', ' - ', '1', '5', '1-3', ' - ', '1-3,8      ', '1-3,8,          '],
     );

    for (my $g=0; $g<@Greps; $g++)
    {
	for (my $s=0; $s<@Sets; $s++)
        {
	    my $set      = new Set::IntSpan $Sets[$s];
	    my $result   = grep_spans { eval $Greps[$g] } $set;
	    my $expected = new Set::IntSpan $expected[$g][$s];

	    printf "#%3d: grep_span { %-8s } %-20s -> %s\n",
	    $N, $Greps[$g], $Sets[$s], $result->run_list;

	    equal $result $expected or Not; OK;
        }
    }
}

sub Map
{
    print "#map_span\n";

    my @expected = 
    (['-', ' - ', ' - ', ' - ', ' -', ' -', '  - ' , '  -  ', '  -             ', '  -              	 '],
     ['-', '(-)', '(-0', '0-)', ' 1', ' 5', ' 1-3' , ' 3-7 ', ' 1-3,8,10-23    ', ' 1-3,8,10-23,30-)	 '],  
     ['-', '(-)', '0-)', '(-0', '-1', '-5', '-3--1', '-7--3', '-23--10,-8,-3--1', '(--30,-23--10,-8,-3--1'],

     ['-', '(-)', '(-)', '(-)', '-1,1', '-5,5', '-3--1,1-3', '-7--3,3-7 ', 
      '-23--10,-8,-3--1,1-3,8,10-23', '(--30,-23--10,-8,-3--1, 1-3,8,10-23,30-)'],  

     ['-', '(-)', '(-0', '0-)', ' 2', ' 10', '2-6', '6-14', '2-6,16,20-46', '2-6,16,20-46,60-)' ],
     ['-', '(-)', '(-5', '0-)', ' 1-6', '5-10', ' 1-8', '3-12', '1-28'        , '1-28,30-)'     ],

     );

    for (my $g=0; $g<@Maps; $g++)
    {
	for (my $s=0; $s<@Sets; $s++)
        {
	    my $set      = new Set::IntSpan $Sets[$s];
	    my $result   = map_spans { eval $Maps[$g] } $set;
	    my $expected = new Set::IntSpan $expected[$g][$s];

	    printf "#%3d: map_span { %-8s } %-20s -> %s\n",
	    $N, $Maps[$g], $Sets[$s], $result->run_list;

	    equal $result $expected or Not; OK;
        }
    }
}