File: 17_misc_meth.t

package info (click to toggle)
perl 5.14.2-21%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,728 kB
  • sloc: perl: 421,086; ansic: 195,186; sh: 37,852; pascal: 8,746; cpp: 3,893; makefile: 2,346; xml: 1,972; yacc: 1,602
file content (158 lines) | stat: -rw-r--r-- 3,489 bytes parent folder | download | duplicates (10)
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
#!/usr/bin/perl
#
# Check miscellaneous tied-array interface methods
# EXTEND, CLEAR, DELETE, EXISTS
#

my $file = "tf$$.txt";
$: = Tie::File::_default_recsep();
1 while unlink $file;

print "1..35\n";

my $N = 1;
use Tie::File;
print "ok $N\n"; $N++;

my $o = tie @a, 'Tie::File', $file, autodefer => 0;
print $o ? "ok $N\n" : "not ok $N\n";
$N++;

# (3-8) EXTEND
$o->EXTEND(3);
check_contents("$:$:$:");
$o->EXTEND(4);
check_contents("$:$:$:$:");
$o->EXTEND(3);
check_contents("$:$:$:$:");

# (9-10) CLEAR
@a = ();
check_contents("");

# (11-20) EXISTS
if ($] >= 5.006) {
  eval << 'TESTS';
print !exists $a[0] ? "ok $N\n" : "not ok $N\n";
$N++;
$a[0] = "I like pie.";
print exists $a[0] ? "ok $N\n" : "not ok $N\n";
$N++;
print !exists $a[1] ? "ok $N\n" : "not ok $N\n";
$N++;
$a[2] = "GIVE ME PIE";
print exists $a[0] ? "ok $N\n" : "not ok $N\n";
$N++;
# exists $a[1] is not defined by this module under these circumstances
print exists $a[1] ? "ok $N\n" : "ok $N\n";
$N++;
print exists $a[2] ? "ok $N\n" : "not ok $N\n";
$N++;
print exists $a[-1] ? "ok $N\n" : "not ok $N\n";
$N++;
print exists $a[-2] ? "ok $N\n" : "not ok $N\n";
$N++;
print exists $a[-3] ? "ok $N\n" : "not ok $N\n";
$N++;
print !exists $a[-4] ? "ok $N\n" : "not ok $N\n";
$N++;
TESTS
  } else {                      # perl 5.005 doesn't have exists $array[1]
    for (11..20) {
      print "ok $_ \# skipped (no exists for arrays)\n";
          $N++;
    }
  }

my $del;

# (21-35) DELETE
if ($] >= 5.006) {
  eval << 'TESTS';
$del = delete $a[0];
check_contents("$:$:GIVE ME PIE$:");
# 20020317 Through 0.20, the 'delete' function returned the wrong values.
expect($del, "I like pie.");
$del = delete $a[2];
check_contents("$:$:");
expect($del, "GIVE ME PIE");
$del = delete $a[0];
check_contents("$:$:");
expect($del, "");
$del = delete $a[1];
check_contents("$:");
expect($del, "");

# 20020317 Through 0.20, we had a bug where deleting an element past the 
# end of the array would actually extend the array to that length.
$del = delete $a[4];
check_contents("$:");
expect($del, undef);



TESTS
  } else {                      # perl 5.005 doesn't have delete $array[1]
    for (21..35) {
      print "ok $_ \# skipped (no delete for arrays)\n";
          $N++;
    }
  }

use POSIX 'SEEK_SET';
sub check_contents {
  my $x = shift;
  local *FH = $o->{fh};
  seek FH, 0, SEEK_SET;
  my $a;
  { local $/; $a = <FH> }
  $a = "" unless defined $a;
  if ($a eq $x) {
    print "ok $N\n";
  } else {
    ctrlfix(my $msg = "# expected <$x>, got <$a>");
    print "not ok $N # $msg\n";
  }
  $N++;
  print $o->_check_integrity($file, $ENV{INTEGRITY}) ? "ok $N\n" : "not ok $N\n";
  $N++;
}

sub expect {
  if (@_ == 1) {
    print $_[0] ? "ok $N\n" : "not ok $N\n";
  } elsif (@_ == 2) {
    my ($a, $x) = @_;
    if    (! defined($a) && ! defined($x)) { print "ok $N\n" }
    elsif (  defined($a) && ! defined($x)) { 
      ctrlfix(my $msg = "expected UNDEF, got <$a>");
      print "not ok $N \# $msg\n";
    }
    elsif (! defined($a) &&   defined($x)) { 
      ctrlfix(my $msg = "expected <$x>, got UNDEF");
      print "not ok $N \# $msg\n";
    } elsif ($a eq $x) { print "ok $N\n" }
    else {
      ctrlfix(my $msg = "expected <$x>, got <$a>");
      print "not ok $N \# $msg\n";
    }
  } else {
    die "expect() got ", scalar(@_), " args, should have been 1 or 2";
  }
  $N++;
}

sub ctrlfix {
  for (@_) {
    s/\n/\\n/g;
    s/\r/\\r/g;
  }
}

END {
  undef $o;
  untie @a;
  1 while unlink $file;
}