1#/bin/perl -w
2# Copyright (c) 2001 Flavio Soibelmann Glock. All rights reserved.
3# This program is free software; you can redistribute it and/or
4# modify it under the same terms as Perl itself.
5#
6# Tests for last()
7#
8
9use strict;
10# use warnings;
11
12use Set::Infinite qw($inf);
13$| = 1;
14
15my $neg_inf = -$inf;
16my $test = 0;
17my ($result, $errors);
18my @a;
19my $c;
20my $span;
21
22sub test {
23	my ($header, $sub, $expected) = @_;
24	$test++;
25	#print "\t# $header \n";
26    $result = eval $sub;
27    $result = '' unless defined $result;
28	if ("$expected" eq "$result") {
29		print "ok $test";
30	}
31	else {
32		print "not ok $test\n    # $header\n"; # \n\t# expected \"$expected\" got \"$result\"";
33		print "    # $sub expected \"$expected\" got \"$result\"  $@";
34		$errors++;
35	}
36	print " \n";
37}
38
39# print "1..40\n";
40$| = 1;
41
42# $Set::Infinite::TRACE = 1;
43# $Set::Infinite::PRETTY_PRINT = 1;
44
45$a = Set::Infinite->new([1,2],[4,5],[7,8]);
46@a = $a->last;
47test ("last, tail", '"@a"', '[7..8] [1..2],[4..5]');
48@a = defined $a[1] ? $a[1]->last : ();
49test ("last, tail", '"@a"', '[4..5] [1..2]');
50@a = defined $a[1] ? $a[1]->last : ();
51test ("last, tail", '"@a"', '[1..2]');
52@a = defined $a[1] ? $a[1]->last : ();
53test ("last, tail", '"@a"', '');
54
55# complement
56
57# $Set::Infinite::TRACE = 1;
58# $Set::Infinite::PRETTY_PRINT = 1;
59$a = Set::Infinite->new($neg_inf, 25)->quantize;
60test ("quantize/complement", '$a->complement->last', "[26..$inf)");
61# $Set::Infinite::TRACE = 0;
62
63# $Set::Infinite::TRACE = 1;
64# $Set::Infinite::PRETTY_PRINT = 1;
65@a = $a->complement->last;
66test ("last, tail 1", '"$a[0]"', "[26..$inf)");
67@a = defined $a[1] ? $a[1]->last : ();
68test ("last, tail 2", '"$a[0]"', '');   # complement is empty between quantize() elements
69# $Set::Infinite::TRACE = 0;
70
71$a = Set::Infinite->new($neg_inf,5)->quantize->complement->complement(10,11);
72
73# $a = Set::Infinite->new(5,$inf)->quantize; warn $a;
74# $a = $a->complement; warn $a;
75# $a = $a->complement(10,11); warn $a;
76@a = $a->last;
77# warn "1-last,tail= @a";
78@a = $a[0]->last;
79# warn "2-first,tail= @a";
80
81test ("quantize/complement", '$a->last', "(11..$inf)");
82@a = $a->last;
83test ("last, tail", '"$a[0]"', "(11..$inf)");
84# $Set::Infinite::TRACE = 1;
85@a = defined $a[1] ? $a[1]->last : ();
86# $Set::Infinite::TRACE = 0;
87test ("last, tail", '"$a[0]"', '[6..10)');
88@a = defined $a[1] ? $a[1]->last : ("");
89test ("last, tail", '"$a[0]"', '');   # complement is empty between quantize() elements
90
91# select
92
93# $Set::Infinite::TRACE = 1;
94# $Set::Infinite::PRETTY_PRINT = 1;
95$a = Set::Infinite->new([25,$inf])->quantize;
96# warn $a;
97$b = $a->select(by => [2,3]);
98# warn $b;
99@a = $b->last;
100test ("last, tail", '"@a"', '[28..29) [27..28)');
101@a = defined $a[1] ? $a[1]->last : ();
102test ("last, tail", '"@a"', '[27..28)');
103@a = defined $a[1] ? $a[1]->last : ();
104test ("last, tail", '"@a"', '');
105$Set::Infinite::TRACE = 0;
106$Set::Infinite::PRETTY_PRINT = 0;
107
108
109# find '2nd' using complement/last
110test ("2nd", ' $b->complement( $b->last )->last ', '[27..28)');
111
112# @a = $a->last;
113# warn "last, tail is @a";
114# warn "last of $b is ".$b->last;
115
116#$c = $a->select(by => [2,3], count => 2, freq => 5 );
117#@a = $c->last;
118#test ("last, tail", '"@a"', '[33..34) [27..28),[28..29),[32..33)');
119#@a = defined $a[1] ? $a[1]->last : ();
120#test ("last, tail", '"@a"', '[32..33) [27..28),[28..29)');
121#@a = defined $a[1] ? $a[1]->last : ();
122#test ("last, tail", '"@a"', '[28..29) [27..28)');
123#@a = defined $a[1] ? $a[1]->last : ();
124#test ("last, tail", '"@a"', '[27..28)');
125## $Set::Infinite::TRACE = 1;
126## $Set::Infinite::PRETTY_PRINT = 1;
127#@a = defined $a[1] ? $a[1]->last : ();
128#test ("last, tail", '"@a"', '');
129
130# TODO: test with negative values
131# $Set::Infinite::TRACE = 1;
132# $Set::Infinite::PRETTY_PRINT = 1;
133# warn "select by=[2,3] freq=5 $a";
134#$c = $a->select(by => [2,3], freq => 5 );
135#@a = $c->last;
136
137#if ( !defined $a[0] ) {
138#    print "1..20\n";
139#    print "  # TODO: fail last() of select()\n";
140#    exit (0);
141#}
142
143#test ("last, tail", '"@a"', '[27..28) Too complex');
144#@a = defined $a[1] ? $a[1]->last : ();
145#test ("last, tail", '"@a"', '[28..29) Too complex');
146#@a = defined $a[1] ? $a[1]->last : ();
147#test ("last, tail", '"@a"', '[32..33) Too complex');
148#@a = defined $a[1] ? $a[1]->last : ();
149#test ("last, tail", '"@a"', '[33..34) Too complex');
150#@a = defined $a[1] ? $a[1]->last : ();
151#test ("last, tail", '"@a"', '[37..38) Too complex');
152
153# $Set::Infinite::TRACE = 1;
154# $Set::Infinite::PRETTY_PRINT = 1;
155# count
156$a = Set::Infinite->new([25,$inf])->quantize;
157# warn $a;
158$b = $a->select(count => 2);
159# warn $b;
160@a = $b->last;
161test ("last, tail", '"@a"', '[26..27) [25..26)');
162@a = defined $a[1] ? $a[1]->last : ();
163test ("last, tail", '"@a"', '[25..26)');
164@a = defined $a[1] ? $a[1]->last : ();
165test ("last, tail", '"@a"', '');
166$Set::Infinite::TRACE = 0;
167
168
169# $Set::Infinite::TRACE = 1;
170# $Set::Infinite::PRETTY_PRINT = 1;
171$a = Set::Infinite->new($neg_inf,15)->quantize->complement(15);
172@a = $a->last;
173test ("last, tail", '"@a"', "(15..16) ($neg_inf..15)");
174@a = defined $a[1] ? $a[1]->last : ();
175test ("last, tail", '"@a"', "($neg_inf..15)");
176
177print "1..20\n";
178
1791;
180