File: last.t

package info (click to toggle)
libset-infinite-perl 0.65-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 2,401; makefile: 2
file content (179 lines) | stat: -rw-r--r-- 5,071 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#/bin/perl -w
# Copyright (c) 2001 Flavio Soibelmann Glock. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Tests for last()
#

use strict;
# use warnings;

use Set::Infinite qw($inf);
$| = 1;

my $neg_inf = -$inf;
my $test = 0;
my ($result, $errors);
my @a;
my $c;
my $span;

sub test {
	my ($header, $sub, $expected) = @_;
	$test++;
	#print "\t# $header \n";
    $result = eval $sub;
    $result = '' unless defined $result;
	if ("$expected" eq "$result") {
		print "ok $test";
	}
	else {
		print "not ok $test\n    # $header\n"; # \n\t# expected \"$expected\" got \"$result\"";
		print "    # $sub expected \"$expected\" got \"$result\"  $@";
		$errors++;
	}
	print " \n";
}

# print "1..40\n";
$| = 1;

# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;

$a = Set::Infinite->new([1,2],[4,5],[7,8]);
@a = $a->last;
test ("last, tail", '"@a"', '[7..8] [1..2],[4..5]');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '[4..5] [1..2]');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '[1..2]');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '');

# complement

# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
$a = Set::Infinite->new($neg_inf, 25)->quantize;
test ("quantize/complement", '$a->complement->last', "[26..$inf)");
# $Set::Infinite::TRACE = 0;

# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
@a = $a->complement->last;
test ("last, tail 1", '"$a[0]"', "[26..$inf)");
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail 2", '"$a[0]"', '');   # complement is empty between quantize() elements
# $Set::Infinite::TRACE = 0;

$a = Set::Infinite->new($neg_inf,5)->quantize->complement->complement(10,11);

# $a = Set::Infinite->new(5,$inf)->quantize; warn $a;
# $a = $a->complement; warn $a;
# $a = $a->complement(10,11); warn $a;
@a = $a->last;
# warn "1-last,tail= @a";
@a = $a[0]->last;
# warn "2-first,tail= @a";

test ("quantize/complement", '$a->last', "(11..$inf)");
@a = $a->last;
test ("last, tail", '"$a[0]"', "(11..$inf)");
# $Set::Infinite::TRACE = 1;
@a = defined $a[1] ? $a[1]->last : ();
# $Set::Infinite::TRACE = 0;
test ("last, tail", '"$a[0]"', '[6..10)');   
@a = defined $a[1] ? $a[1]->last : ("");
test ("last, tail", '"$a[0]"', '');   # complement is empty between quantize() elements

# select

# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
$a = Set::Infinite->new([25,$inf])->quantize;
# warn $a;
$b = $a->select(by => [2,3]);
# warn $b;
@a = $b->last;
test ("last, tail", '"@a"', '[28..29) [27..28)');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '[27..28)');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '');
$Set::Infinite::TRACE = 0;
$Set::Infinite::PRETTY_PRINT = 0;


# find '2nd' using complement/last 
test ("2nd", ' $b->complement( $b->last )->last ', '[27..28)');

# @a = $a->last;
# warn "last, tail is @a";
# warn "last of $b is ".$b->last;

#$c = $a->select(by => [2,3], count => 2, freq => 5 );
#@a = $c->last;
#test ("last, tail", '"@a"', '[33..34) [27..28),[28..29),[32..33)');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[32..33) [27..28),[28..29)');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[28..29) [27..28)');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[27..28)');
## $Set::Infinite::TRACE = 1;
## $Set::Infinite::PRETTY_PRINT = 1;
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '');

# TODO: test with negative values
# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
# warn "select by=[2,3] freq=5 $a";
#$c = $a->select(by => [2,3], freq => 5 );
#@a = $c->last;

#if ( !defined $a[0] ) {
#    print "1..20\n";
#    print "  # TODO: fail last() of select()\n";
#    exit (0);
#}

#test ("last, tail", '"@a"', '[27..28) Too complex');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[28..29) Too complex');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[32..33) Too complex');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[33..34) Too complex');
#@a = defined $a[1] ? $a[1]->last : ();
#test ("last, tail", '"@a"', '[37..38) Too complex');

# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
# count
$a = Set::Infinite->new([25,$inf])->quantize;
# warn $a;
$b = $a->select(count => 2);
# warn $b;
@a = $b->last;
test ("last, tail", '"@a"', '[26..27) [25..26)');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '[25..26)');
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', '');
$Set::Infinite::TRACE = 0;


# $Set::Infinite::TRACE = 1;
# $Set::Infinite::PRETTY_PRINT = 1;
$a = Set::Infinite->new($neg_inf,15)->quantize->complement(15);
@a = $a->last;
test ("last, tail", '"@a"', "(15..16) ($neg_inf..15)");
@a = defined $a[1] ? $a[1]->last : ();
test ("last, tail", '"@a"', "($neg_inf..15)");

print "1..20\n";

1;