Debian Codegraph @

t/leak.t

raw
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 #
5 # $Id: leak.t,v 1.3 2002/03/07 23:04:54 eserte Exp $
6 # Author: Slaven Rezic
7 #
8
9 # Some leak tests. You need Devel::Leak installed.
10
11 use strict;
12 use Config;
13 use Devel::Peek;
14
15 BEGIN {
16 if (!eval q{
17 use Test::More;
18 use Devel::Leak;
19 1;
20 }) {
21 print "# tests only work with installed Test and Devel::Leak modules\n";
22 print "1..0 # skip need Devel::Leak\n";
23 CORE::exit;
24 }
25 }
26
27 use Tk;
28 use Tk::Button;
29 use Tk::Canvas;
30
31
32 plan tests => 15;
33
34 my $mw = new MainWindow;
35 $mw->optionAdd("*Button.Background",'#dcdcdc');
36 my $handle;
37 my($c1,$c2);
38
39 # Tests for leaking subroutine set
40
41 # first binding always creates some SVs
42 my $N = 100;
43
44 $mw->bind("<Motion>" => [sub { warn }]);
45
46 $c1 = Devel::Leak::NoteSV($handle);
47 for(1..100) {
48 $mw->bind("<Motion>" => [sub { warn }]);
49 }
50 $c2 = Devel::Leak::NoteSV($handle);
51 cmp_ok($c2-$c1, "<=", 1, "Array reference with subroutine to bind");
52
53
54 $c1 = Devel::Leak::NoteSV($handle);
55 for(1..100) {
56 $mw->bind("<Motion>" => sub { warn });
57 }
58 $c2 = Devel::Leak::NoteSV($handle);
59 is($c2, $c1, "Anonymous subroutine to bind");
60
61
62 $c1 = Devel::Leak::NoteSV($handle);
63 for(1..100) {
64 $mw->bind("<Motion>" => \&test);
65 }
66 $c2 = Devel::Leak::NoteSV($handle);
67 is($c2, $c1, "Subroutine reference to bind");
68
69 my $btn = $mw->Button(-command => sub { warn });
70 $c1 = Devel::Leak::NoteSV($handle);
71 for(1..100) {
72 $btn->configure(-command => sub { warn });
73 }
74 $c2 = Devel::Leak::NoteSV($handle);
75 cmp_ok($c2-$c1, "<", 10, "configure -command multiple times (got " . ($c2-$c1) . " leaked scalars)");
76
77 # Tests for leaking Tk_GetUid (e.g. canvas items)
78
79 my $c = $mw->Canvas->pack;
80 $c->delete($c->createLine(10,10,100,100, -tags => "a"));
81
82 $c1 = Devel::Leak::NoteSV($handle);
83 for(1..1000) {
84 $c->createLine(10,10,100,100,-tags => "a");
85 $c->delete("a");
86 }
87 $c2 = Devel::Leak::NoteSV($handle);
88 cmp_ok(($c2-$c1), "<", 10, "Leaking Tk_GetUid, e.g. canvas items");
89
90 $c1 = Devel::Leak::NoteSV($handle);
91 for(1..100) {
92 my $id = $c->createLine(10,10,100,100);
93 $c->delete($id);
94 }
95 $c2 = Devel::Leak::NoteSV($handle);
96 cmp_ok(($c2-$c1), "<", 10, "Canvas createLine");
97
98 # Tests for leaking widget destroys
99 my $btn2 = $mw->Button;
100 $btn2->destroy;
101 undef $btn2;
102
103 $c1 = Devel::Leak::NoteSV($handle);
104 for(1..100) {
105 $btn2 = $mw->Button;
106 $btn2->destroy;
107 }
108 $c2 = Devel::Leak::CheckSV($handle);
109 print "# was $c1 now $c2 ",($c2-$c1)/100," per iter\n";
110 cmp_ok(($c2-$c1), "<", 10, "Creating and destroying buttons");
111
112 # Tests for leaking fileevent callbacks
113 $mw->fileevent(\*STDIN, 'readable', sub { });
114 $mw->fileevent(\*STDIN, 'readable','');
115
116 $c1 = Devel::Leak::NoteSV($handle);
117 for (1..100)
118 {
119 $mw->fileevent(\*STDIN, 'readable', sub { });
120 $mw->fileevent(\*STDIN, 'readable','');
121 }
122 $c2 = Devel::Leak::CheckSV($handle);
123 cmp_ok(($c2-$c1), "<", 10, "Fileevent callbacks");
124
125 require Tk::After;
126 $mw->withdraw;
127 $mw->update;
128 my $count = 0;
129 Tk->after(cancel => Tk->after(10,sub { $count-- }));
130 $c1 = Devel::Leak::NoteSV($handle);
131 for (1..$N)
132 {
133 Tk->after(cancel => Tk->after($_*10,sub { $count-- }));
134 }
135 $c2 = Devel::Leak::CheckSV($handle);
136 diag "was $c1 now $c2 ",($c2-$c1)/$N," per iter\n";
137 cmp_ok(($c2-$c1), "<", $N, "after");
138
139 $mw->repeat(30,sub {})->cancel;
140 my $id;
141 $c1 = Devel::Leak::NoteSV($handle);
142 $id = $mw->repeat(2, sub { $id->cancel if ($count++ == $N)});
143 Tk::DoOneEvent(0) while $id->[1];
144 undef $id;
145 $c2 = Devel::Leak::CheckSV($handle);
146 diag "was $c1 now $c2 ",($c2-$c1)/$N," per iter\n";
147 cmp_ok(($c2-$c1), "<", $N, "repeat");
148
149 sub test { warn }
150
151 {
152 # Tk::Listbox::insert leak
153 my $lb = $mw->Listbox;
154 $lb->insert("end", 1);
155 $c1 = Devel::Leak::NoteSV($handle);
156 $lb->insert("end", 2);
157 $c2 = Devel::Leak::CheckSV($handle);
158 cmp_ok($c2-$c1, "<=", 1, "Insert one listbox element");
159
160 for (1..100) { $lb->insert("end", $_+2) }
161 $c1 = Devel::Leak::NoteSV($handle);
162 $lb->insert("end", 9999);
163 $c2 = Devel::Leak::CheckSV($handle);
164 cmp_ok($c2-$c1, "<=", 1, "Insert one listbox element at end");
165
166 $lb->delete(0, "end");
167 $c1 = Devel::Leak::NoteSV($handle);
168 $lb->insert("end", 1..10);
169 $lb->delete(0, "end");
170 $c2 = Devel::Leak::CheckSV($handle);
171 is($c2-$c1, 0, "Inserting and deleting listbox elements");
172
173 $lb->delete(0, 'end');
174 $c1 = Devel::Leak::NoteSV($handle);
175 for(1..10) {
176 $lb->insert('end', $_);
177 }
178 $lb->delete(0, 'end');
179 $c2 = Devel::Leak::CheckSV($handle);
180 is($c2-$c1, 0, "Inserting and deleting individual listbox elements");
181 }
182
183 {
184 $mw->afterIdle(sub {});
185 $mw->idletasks;
186 $c1 = Devel::Leak::NoteSV($handle);
187 $mw->afterIdle(sub {});
188 $mw->idletasks;
189 $c2 = Devel::Leak::CheckSV($handle);
190
191 is($c2-$c1, 0, "afterIdle callback");
192 }
193
194 __END__