OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / bitvector / t / 11______shift.t
1 #!perl -w
2
3 use strict;
4 no strict "vars";
5
6 use Bit::Vector::Overload;
7
8 # ======================================================================
9 #   $carry_out = $vector->rotate_left();
10 #   $carry_out = $vector->rotate_right();
11 #   $carry_out = $vector->shift_left($carry_in);
12 #   $carry_out = $vector->shift_right($carry_in);
13 #   $vector->Move_Left($bits);
14 #   $vector->Move_Right($bits);
15 # ======================================================================
16 #   $vec1 = $vec2->Shadow();
17 #   $vec1 = $vec2->Clone();
18 # ======================================================================
19
20 print "1..36416\n";
21
22 $n = 1;
23
24 foreach $limit (15,16,31,32,63,64,127,128,255,256,511,512,1023,1024)
25 {
26     $ref = Bit::Vector->new($limit);
27
28     $ref->Fill();
29     $ref->Bit_Off(0);
30     $ref->Bit_Off(1);
31     for ( $j = 4; $j < $limit; $j += 2 ) { $ref->Bit_Off($j); }
32     for ( $i = 3; ($j = $i * $i) < $limit; $i += 2 )
33     {
34         for ( ; $j < $limit; $j += $i ) { $ref->Bit_Off($j); }
35     }
36
37     $rol = $ref->Clone();
38     $ror = $ref->Clone();
39     $shl = $ref->Clone();
40     $shr = $ref->Clone();
41
42     $crl = $rol->Shadow();
43     $crr = $ror->Shadow();
44     $csl = $shl->Shadow();
45     $csr = $shr->Shadow();
46
47     &test_rotat_reg_same(0);
48     &test_shift_reg_same(0);
49     &test_rotat_carry_same(1);
50     &test_shift_carry_same(1);
51
52     for ( $i = 0; $i < $limit; $i++ )
53     {
54         $crl->shift_left ( $rol->rotate_left () );
55         $crr->shift_right( $ror->rotate_right() );
56         $csl->shift_left ( $shl->shift_left  ( $shl->bit_test($limit-1) ) );
57         $csr->shift_right( $shr->shift_right ( $shr->bit_test(0)        ) );
58
59         if (($i == 0) || ($i == ($limit-2)))
60         {
61             &test_rotat_reg_same(1);
62             &test_shift_reg_same(1);
63             &test_rotat_carry_same(1);
64             &test_shift_carry_same(1);
65             &test_rotat_reg_diff;
66             &test_rotat_carry_diff if ($i);
67             &test_shift_reg_diff;
68             &test_shift_carry_diff if ($i);
69         }
70     }
71
72     &test_rotat_reg_same(0);
73     &test_shift_reg_same(0);
74     &test_rotat_carry_same(0);
75     &test_shift_carry_same(0);
76 }
77
78 $ref = Bit::Vector->new(1);
79 $Minimum = $ref->Min();
80 $Maximum = $ref->Max();
81
82 if ($Minimum >= 32767)
83 {print "ok $n\n";} else {print "not ok $n\n";}
84 $n++;
85
86 if ($Maximum <= -32767)
87 {print "ok $n\n";} else {print "not ok $n\n";}
88 $n++;
89
90 foreach $limit (15,16,31,32,63,64,127,128,1023,1024)
91 {
92     $ref = Bit::Vector->new($limit);
93
94     for ( $bits = -2; $bits <= $limit + 1; $bits++ )
95     {
96         $ref->Fill();
97         $vec = ($ref << $bits);
98         $ref->Move_Left($bits);
99         $norm_ = $limit - $bits;
100         $min_ = $bits;
101         $max_ = $limit - 1;
102         if (($norm_ <= 0) || ($bits < 0))
103         {
104             $norm_ = 0;
105             $min_ = $Minimum;
106             $max_ = $Maximum;
107         }
108         &verify;
109
110         if ($vec->equal($ref))
111         {print "ok $n\n";} else {print "not ok $n\n";}
112         $n++;
113         $vec->Fill();
114         $vec <<= $bits;
115         if ($vec->equal($ref))
116         {print "ok $n\n";} else {print "not ok $n\n";}
117         $n++;
118
119         $ref->Fill();
120         $vec = ($ref >> $bits);
121         $ref->Move_Right($bits);
122         $norm_ = $limit - $bits;
123         $min_ = 0;
124         $max_ = $limit - $bits - 1;
125         if (($norm_ <= 0) || ($bits < 0))
126         {
127             $norm_ = 0;
128             $min_ = $Minimum;
129             $max_ = $Maximum;
130         }
131         &verify;
132
133         if ($vec->equal($ref))
134         {print "ok $n\n";} else {print "not ok $n\n";}
135         $n++;
136         $vec->Fill();
137         $vec >>= $bits;
138         if ($vec->equal($ref))
139         {print "ok $n\n";} else {print "not ok $n\n";}
140         $n++;
141     }
142 }
143
144 exit;
145
146 sub test_rotat_reg_same
147 {
148     my($flag) = @_;
149
150     if (($ref->equal($rol)) ^ $flag)
151     {print "ok $n\n";} else {print "not ok $n\n";}
152     $n++;
153
154     if (($ref->equal($ror)) ^ $flag)
155     {print "ok $n\n";} else {print "not ok $n\n";}
156     $n++;
157 }
158
159 sub test_shift_reg_same
160 {
161     my($flag) = @_;
162
163     if (($ref->equal($shl)) ^ $flag)
164     {print "ok $n\n";} else {print "not ok $n\n";}
165     $n++;
166
167     if (($ref->equal($shr)) ^ $flag)
168     {print "ok $n\n";} else {print "not ok $n\n";}
169     $n++;
170 }
171
172 sub test_rotat_carry_same
173 {
174     my($flag) = @_;
175
176     if (($ref->equal($crl)) ^ $flag)
177     {print "ok $n\n";} else {print "not ok $n\n";}
178     $n++;
179
180     if (($ref->equal($crr)) ^ $flag)
181     {print "ok $n\n";} else {print "not ok $n\n";}
182     $n++;
183 }
184
185 sub test_shift_carry_same
186 {
187     my($flag) = @_;
188
189     if (($ref->equal($csl)) ^ $flag)
190     {print "ok $n\n";} else {print "not ok $n\n";}
191     $n++;
192
193     if (($ref->equal($csr)) ^ $flag)
194     {print "ok $n\n";} else {print "not ok $n\n";}
195     $n++;
196 }
197
198 sub test_rotat_reg_diff
199 {
200     unless ($rol->equal($ror))
201     {print "ok $n\n";} else {print "not ok $n\n";}
202     $n++;
203 }
204
205 sub test_rotat_carry_diff
206 {
207     unless ($crl->equal($crr))
208     {print "ok $n\n";} else {print "not ok $n\n";}
209     $n++;
210 }
211
212 sub test_shift_reg_diff
213 {
214     unless ($shl->equal($shr))
215     {print "ok $n\n";} else {print "not ok $n\n";}
216     $n++;
217 }
218
219 sub test_shift_carry_diff
220 {
221     unless ($csl->equal($csr))
222     {print "ok $n\n";} else {print "not ok $n\n";}
223     $n++;
224 }
225
226 sub verify
227 {
228     $norm = $ref->Norm();
229     if ($norm == $norm_)
230     {print "ok $n\n";} else {print "not ok $n\n";}
231     $n++;
232     unless (($min,$max) = $ref->Interval_Scan_inc(0))
233     {
234         $min = $ref->Min();
235         $max = $ref->Max();
236     }
237     if ($min == $min_)
238     {print "ok $n\n";} else {print "not ok $n\n";}
239     $n++;
240     if ($max == $max_)
241     {print "ok $n\n";} else {print "not ok $n\n";}
242     $n++;
243     unless (($min,$max) = $ref->Interval_Scan_dec($limit-1))
244     {
245         $min = $ref->Min();
246         $max = $ref->Max();
247     }
248     if ($min == $min_)
249     {print "ok $n\n";} else {print "not ok $n\n";}
250     $n++;
251     if ($max == $max_)
252     {print "ok $n\n";} else {print "not ok $n\n";}
253     $n++;
254 }
255
256 __END__
257