OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / bitvector / t / 13__increment.t
1 #!perl -w
2
3 use strict;
4 no strict "vars";
5
6 use Bit::Vector;
7
8 # ======================================================================
9 #   $vector->increment();
10 #   $vector->decrement();
11 # ======================================================================
12
13 print "1..5296\n";
14
15 $n = 1;
16
17 $bits = 10;
18
19 $limit = (1 << $bits) - 1;
20
21 $k = 0;
22
23 $test_vector = bitvector($bits,$k);
24
25 for ( $i = 0; $i <= $limit; $i++ )
26 {
27     if ($k++ == $limit) { $k = 0; }
28
29     $ref_carry = ($test_vector->Norm() == $bits);
30
31     $test_carry = $test_vector->increment();
32
33     if ($test_carry == $ref_carry)
34     {print "ok $n\n";} else {print "not ok $n\n";}
35     $n++;
36
37     $ref_vector = bitvector($bits,$k);
38
39     if ($test_vector->equal($ref_vector))
40     {print "ok $n\n";} else {print "not ok $n\n";}
41     $n++;
42 }
43
44 $k = $limit;
45
46 $test_vector = bitvector($bits,$k);
47
48 for ( $i = $limit; $i >= 0; $i-- )
49 {
50     if ($k-- == 0) { $k = $limit; }
51
52     $ref_carry = ($test_vector->Norm() == 0);
53
54     $test_carry = $test_vector->decrement();
55
56     if ($test_carry == $ref_carry)
57     {print "ok $n\n";} else {print "not ok $n\n";}
58     $n++;
59
60     $ref_vector = bitvector($bits,$k);
61
62     if ($test_vector->equal($ref_vector))
63     {print "ok $n\n";} else {print "not ok $n\n";}
64     $n++;
65 }
66
67 $bits = 2000;
68
69 $upper =  150;
70 $lower = -150;
71
72 $k = $lower;
73
74 $test_vector = bitvector($bits,$k);
75
76 while (++$k <= $upper)
77 {
78     $ref_carry = ($test_vector->Norm() == $bits);
79
80     $test_carry = $test_vector->increment();
81
82     if ($test_carry == $ref_carry)
83     {print "ok $n\n";} else {print "not ok $n\n";}
84     $n++;
85
86     $ref_vector = bitvector($bits,$k);
87
88     if ($test_vector->equal($ref_vector))
89     {print "ok $n\n";} else {print "not ok $n\n";}
90     $n++;
91 }
92
93 $k = $upper;
94
95 $test_vector = bitvector($bits,$k);
96
97 while (--$k >= $lower)
98 {
99     $ref_carry = ($test_vector->Norm() == 0);
100
101     $test_carry = $test_vector->decrement();
102
103     if ($test_carry == $ref_carry)
104     {print "ok $n\n";} else {print "not ok $n\n";}
105     $n++;
106
107     $ref_vector = bitvector($bits,$k);
108
109     if ($test_vector->equal($ref_vector))
110     {print "ok $n\n";} else {print "not ok $n\n";}
111     $n++;
112 }
113
114 exit;
115
116 sub bitvector
117 {
118     my($bits,$value) = @_;
119     my($vector,$bit);
120
121     $vector = Bit::Vector->new($bits);
122
123     if ($value < 0)
124     {
125         $value = -1 - $value;
126         $vector->Fill();
127     }
128
129     $bit = 0;
130     while ($value)
131     {
132         if ($value & 1) { $vector->bit_flip($bit); }
133         $value >>= 1;
134         $bit++;
135     }
136
137     return($vector);
138 }
139
140 __END__
141