OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / bitvector / t / 01________new.t
1 #!perl -w
2
3 use strict;
4 no strict "vars";
5 use integer;
6
7 use Bit::Vector;
8
9 @ISA = qw(Bit::Vector);
10
11 # ======================================================================
12 #   $set = Bit::Vector::new('Bit::Vector',$elements);
13 # ======================================================================
14
15 print "1..131\n";
16
17 $n = 1;
18
19 # test if the constructor works at all:
20
21 $set = Bit::Vector::new('Bit::Vector',1);
22 if (defined $set)
23 {print "ok $n\n";} else {print "not ok $n\n";}
24 $n++;
25 if (ref($set) eq 'Bit::Vector')
26 {print "ok $n\n";} else {print "not ok $n\n";}
27 $n++;
28 if (${$set} != 0)
29 {print "ok $n\n";} else {print "not ok $n\n";}
30 $n++;
31
32 # test if the constructor handles NULL pointers as expected:
33
34 eval { $ref = Bit::Vector::new('Bit::Vector',0); };
35 unless ($@)
36 {print "ok $n\n";} else {print "not ok $n\n";}
37 $n++;
38
39 # test if the copy of an object reference works as expected:
40
41 $ref = $set;
42 if (defined $ref)
43 {print "ok $n\n";} else {print "not ok $n\n";}
44 $n++;
45 if (ref($ref) eq 'Bit::Vector')
46 {print "ok $n\n";} else {print "not ok $n\n";}
47 $n++;
48 if (${$ref} != 0)
49 {print "ok $n\n";} else {print "not ok $n\n";}
50 $n++;
51
52 if (${$ref} == ${$set})
53 {print "ok $n\n";} else {print "not ok $n\n";}
54 $n++;
55
56 # test the constructor with a large set (13,983,816 elements):
57
58 $set = Bit::Vector::new('Bit::Vector',&binomial(49,6));
59 if (defined $set)
60 {print "ok $n\n";} else {print "not ok $n\n";}
61 $n++;
62 if (ref($set) eq 'Bit::Vector')
63 {print "ok $n\n";} else {print "not ok $n\n";}
64 $n++;
65 if (${$set} != 0)
66 {print "ok $n\n";} else {print "not ok $n\n";}
67 $n++;
68
69 # are the two sets really distinct and set objects behaving as expected?
70
71 if (${$ref} != ${$set})
72 {print "ok $n\n";} else {print "not ok $n\n";}
73 $n++;
74
75 # are set objects behaving as expected, i.e. are they write-protected?
76
77 eval { ${$set} = 0x00088850; };
78 if ($@ =~ /Modification of a read-only value attempted/)
79 {print "ok $n\n";} else {print "not ok $n\n";}
80 $n++;
81 $set = 0x00088850;
82 if ($set == 559184)
83 {print "ok $n\n";} else {print "not ok $n\n";}
84 $n++;
85
86 eval { ${$ref} = 0x000E9CE0; };
87 if ($@ =~ /Modification of a read-only value attempted/)
88 {print "ok $n\n";} else {print "not ok $n\n";}
89 $n++;
90 $ref = 0x000E9CE0;
91 if ($ref == 957664)
92 {print "ok $n\n";} else {print "not ok $n\n";}
93 $n++;
94
95 # test various ways of calling the constructor:
96
97 # 1: $set = Bit::Vector::new('Bit::Vector',1);
98 # 2: $class = 'Bit::Vector'; $set = Bit::Vector::new($class,2);
99 # 3: $set = new Bit::Vector(3);
100 # 4: $set = Bit::Vector->new(4);
101 # 5: $ref = $set->new(5);
102 # 6: $set = $set->new(6);
103
104 # (test case #1 has been handled above)
105
106 # test case #2:
107
108 $class = 'Bit::Vector';
109 $set = Bit::Vector::new($class,2);
110 if (defined $set)
111 {print "ok $n\n";} else {print "not ok $n\n";}
112 $n++;
113 if (ref($set) eq 'Bit::Vector')
114 {print "ok $n\n";} else {print "not ok $n\n";}
115 $n++;
116 if (${$set} != 0)
117 {print "ok $n\n";} else {print "not ok $n\n";}
118 $n++;
119
120 # test case #3:
121
122 $ref = new Bit::Vector(3);
123 if (defined $ref)
124 {print "ok $n\n";} else {print "not ok $n\n";}
125 $n++;
126 if (ref($ref) eq 'Bit::Vector')
127 {print "ok $n\n";} else {print "not ok $n\n";}
128 $n++;
129 if (${$ref} != 0)
130 {print "ok $n\n";} else {print "not ok $n\n";}
131 $n++;
132
133 # coherence test:
134
135 if (${$ref} != ${$set})
136 {print "ok $n\n";} else {print "not ok $n\n";}
137 $n++;
138
139 # test case #4:
140
141 $set = Bit::Vector->new(4);
142 if (defined $set)
143 {print "ok $n\n";} else {print "not ok $n\n";}
144 $n++;
145 if (ref($set) eq 'Bit::Vector')
146 {print "ok $n\n";} else {print "not ok $n\n";}
147 $n++;
148 if (${$set} != 0)
149 {print "ok $n\n";} else {print "not ok $n\n";}
150 $n++;
151
152 # coherence test:
153
154 if (${$ref} != ${$set})
155 {print "ok $n\n";} else {print "not ok $n\n";}
156 $n++;
157
158 # prepare possibility for id check:
159
160 $old = ${$set};
161 if (${$set} == $old)
162 {print "ok $n\n";} else {print "not ok $n\n";}
163 $n++;
164
165 # test case #5:
166
167 $ref = $set->new(5);
168 if (defined $ref)
169 {print "ok $n\n";} else {print "not ok $n\n";}
170 $n++;
171 if (ref($ref) eq 'Bit::Vector')
172 {print "ok $n\n";} else {print "not ok $n\n";}
173 $n++;
174 if (${$ref} != 0)
175 {print "ok $n\n";} else {print "not ok $n\n";}
176 $n++;
177
178 # coherence tests:
179
180 if (defined $set)
181 {print "ok $n\n";} else {print "not ok $n\n";}
182 $n++;
183 if (ref($set) eq 'Bit::Vector')
184 {print "ok $n\n";} else {print "not ok $n\n";}
185 $n++;
186 if (${$set} != 0)
187 {print "ok $n\n";} else {print "not ok $n\n";}
188 $n++;
189
190 if (${$set} == $old)
191 {print "ok $n\n";} else {print "not ok $n\n";}
192 $n++;
193
194 if (${$ref} != ${$set})
195 {print "ok $n\n";} else {print "not ok $n\n";}
196 $n++;
197
198 # prepare exact copy of object reference:
199
200 $ref = $set;
201 if (defined $ref)
202 {print "ok $n\n";} else {print "not ok $n\n";}
203 $n++;
204 if (ref($ref) eq 'Bit::Vector')
205 {print "ok $n\n";} else {print "not ok $n\n";}
206 $n++;
207 if (${$ref} != 0)
208 {print "ok $n\n";} else {print "not ok $n\n";}
209 $n++;
210
211 if (${$ref} == ${$set})
212 {print "ok $n\n";} else {print "not ok $n\n";}
213 $n++;
214
215 if (${$ref} == $old)
216 {print "ok $n\n";} else {print "not ok $n\n";}
217 $n++;
218
219 # test case #6 (pseudo auto-destruction test):
220
221 $set = $set->new(6);
222 if (defined $set)
223 {print "ok $n\n";} else {print "not ok $n\n";}
224 $n++;
225 if (ref($set) eq 'Bit::Vector')
226 {print "ok $n\n";} else {print "not ok $n\n";}
227 $n++;
228 if (${$set} != 0)
229 {print "ok $n\n";} else {print "not ok $n\n";}
230 $n++;
231
232 # coherence tests:
233
234 if (defined $ref)
235 {print "ok $n\n";} else {print "not ok $n\n";}
236 $n++;
237 if (ref($ref) eq 'Bit::Vector')
238 {print "ok $n\n";} else {print "not ok $n\n";}
239 $n++;
240 if (${$ref} != 0)
241 {print "ok $n\n";} else {print "not ok $n\n";}
242 $n++;
243
244 if (${$ref} == $old)
245 {print "ok $n\n";} else {print "not ok $n\n";}
246 $n++;
247
248 if (${$ref} != ${$set})
249 {print "ok $n\n";} else {print "not ok $n\n";}
250 $n++;
251
252 # auto-destruction test:
253
254 $set = $set->new(7);
255 if (defined $set)
256 {print "ok $n\n";} else {print "not ok $n\n";}
257 $n++;
258 if (ref($set) eq 'Bit::Vector')
259 {print "ok $n\n";} else {print "not ok $n\n";}
260 $n++;
261 if (${$set} != 0)
262 {print "ok $n\n";} else {print "not ok $n\n";}
263 $n++;
264
265 # coherence test:
266
267 if (${$ref} != ${$set})
268 {print "ok $n\n";} else {print "not ok $n\n";}
269 $n++;
270
271 # test weird ways of calling the constructor:
272
273 eval { $set = Bit::Vector::new("",8); };
274 if (ref($set) eq 'Bit::Vector')
275 {print "ok $n\n";} else {print "not ok $n\n";}
276 $n++;
277
278 eval { $set = Bit::Vector::new('',9); };
279 if (ref($set) eq 'Bit::Vector')
280 {print "ok $n\n";} else {print "not ok $n\n";}
281 $n++;
282
283 eval { $set = Bit::Vector::new(undef,10); };
284 if (ref($set) eq 'Bit::Vector')
285 {print "ok $n\n";} else {print "not ok $n\n";}
286 $n++;
287
288 eval { $set = Bit::Vector::new(6502,11); };
289 if (ref($set) eq 'Bit::Vector')
290 {print "ok $n\n";} else {print "not ok $n\n";}
291 $n++;
292
293 eval { $set = Bit::Vector::new('main',12); };
294 unless ($@)
295 {print "ok $n\n";} else {print "not ok $n\n";}
296 $n++;
297 if (defined $set)
298 {print "ok $n\n";} else {print "not ok $n\n";}
299 $n++;
300 if ( (ref($set) eq 'main') || (ref($set) eq 'Bit::Vector') )
301 {print "ok $n\n";} else {print "not ok $n\n";}
302 $n++;
303 if (${$set} != 0)
304 {print "ok $n\n";} else {print "not ok $n\n";}
305 $n++;
306
307 eval { $set = 0; };
308 unless ($@)
309 {print "ok $n\n";} else {print "not ok $n\n";}
310 $n++;
311
312 eval { $set = Bit::Vector::new('nonsense',13); };
313 unless ($@)
314 {print "ok $n\n";} else {print "not ok $n\n";}
315 $n++;
316 if (defined $set)
317 {print "ok $n\n";} else {print "not ok $n\n";}
318 $n++;
319 if ( (ref($set) eq 'nonsense') || (ref($set) eq 'Bit::Vector') )
320 {print "ok $n\n";} else {print "not ok $n\n";}
321 $n++;
322 if (${$set} != 0)
323 {print "ok $n\n";} else {print "not ok $n\n";}
324 $n++;
325
326 eval { $set = 0; };
327 unless ($@)
328 {print "ok $n\n";} else {print "not ok $n\n";}
329 $n++;
330
331 eval { $set = new main(14); };
332 unless ($@)
333 {print "ok $n\n";} else {print "not ok $n\n";}
334 $n++;
335 if (defined $set)
336 {print "ok $n\n";} else {print "not ok $n\n";}
337 $n++;
338 if ( (ref($set) eq 'main') || (ref($set) eq 'Bit::Vector') )
339 {print "ok $n\n";} else {print "not ok $n\n";}
340 $n++;
341 if (${$set} != 0)
342 {print "ok $n\n";} else {print "not ok $n\n";}
343 $n++;
344
345 eval { $set = 0; };
346 unless ($@)
347 {print "ok $n\n";} else {print "not ok $n\n";}
348 $n++;
349
350 @parameters = ( 'main', 15 );
351 eval { $set = Bit::Vector::new(@parameters); };
352 unless ($@)
353 {print "ok $n\n";} else {print "not ok $n\n";}
354 $n++;
355 if (defined $set)
356 {print "ok $n\n";} else {print "not ok $n\n";}
357 $n++;
358 if ( (ref($set) eq 'main') || (ref($set) eq 'Bit::Vector') )
359 {print "ok $n\n";} else {print "not ok $n\n";}
360 $n++;
361 if (${$set} != 0)
362 {print "ok $n\n";} else {print "not ok $n\n";}
363 $n++;
364
365 eval { $set = 0; };
366 unless ($@)
367 {print "ok $n\n";} else {print "not ok $n\n";}
368 $n++;
369
370 # test syntactically incorrect constructor calls:
371
372 eval { $set = Bit::Vector::new(16); };
373 if ($@ =~ /Usage: new\(class,bits\[,count\]\)/)
374 {print "ok $n\n";} else {print "not ok $n\n";}
375 $n++;
376
377 eval { $set = Bit::Vector::new('main'); };
378 if ($@ =~ /Usage: new\(class,bits\[,count\]\)/)
379 {print "ok $n\n";} else {print "not ok $n\n";}
380 $n++;
381
382 eval { $set = Bit::Vector::new($set); };
383 if ($@ =~ /Usage: new\(class,bits\[,count\]\)/)
384 {print "ok $n\n";} else {print "not ok $n\n";}
385 $n++;
386
387 eval { $set = Bit::Vector::new('main',17,1,0); };
388 if ($@ =~ /Usage: new\(class,bits\[,count\]\)/)
389 {print "ok $n\n";} else {print "not ok $n\n";}
390 $n++;
391
392 eval { $set = Bit::Vector::Create($set,'main',18,0); };
393 if ($@ =~ /Usage: Create\(class,bits\[,count\]\)/)
394 {print "ok $n\n";} else {print "not ok $n\n";}
395 $n++;
396
397 eval { $set = Bit::Vector::new($set,19,'main',0); };
398 if ($@ =~ /Usage: new\(class,bits\[,count\]\)/)
399 {print "ok $n\n";} else {print "not ok $n\n";}
400 $n++;
401
402 # test if size is correct:
403
404 for ( $i = 1; $i <= 16; $i++ )
405 {
406     $k = int(2 ** $i + 0.5);
407     for ( $j = $k-1; $j <= $k+1; $j++ )
408     {
409         $set = Bit::Vector->new($j);
410         if ($set->Size() == $j)
411         {print "ok $n\n";} else {print "not ok $n\n";}
412         $n++;
413     }
414 }
415
416 exit;
417
418 sub binomial
419 {
420     my($n,$k) = @_;
421     my($prod) = 1;
422     my($j) = 0;
423
424     if (($n <= 0) || ($k <= 0) || ($n <= $k)) { return(1); }
425     if ($k > $n - $k) { $k = $n - $k; }
426     while ($j < $k)
427     {
428         $prod *= $n--;
429         $prod /= ++$j;
430     }
431     return(int($prod + 0.5));
432 }
433
434 __END__
435