]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 2000-01-15 Anders Widell <awl@hem.passagen.se> |
2 | ||
cb584bcf | 3 | // Copyright (C) 2000, 2003 Free Software Foundation, Inc. |
b2dad0e3 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 2, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // You should have received a copy of the GNU General Public License along | |
17 | // with this library; see the file COPYING. If not, write to the Free | |
18 | // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
19 | // USA. | |
20 | ||
21 | #include <string> | |
22 | #include <set> | |
23 | #include <bitset> | |
24 | ||
fe413112 | 25 | #include <testsuite_hooks.h> |
b2dad0e3 BK |
26 | |
27 | static char original_bits[1024]; | |
28 | static char left_shifted[1024]; | |
29 | static char right_shifted[1024]; | |
30 | ||
31 | char | |
32 | random_bit() { | |
33 | static long x = 1; | |
34 | return ((x = (3432L*x + 6789L) % 9973L) & 1) + '0'; | |
35 | } | |
36 | ||
37 | void | |
38 | initialise(size_t size) { | |
39 | for (size_t i=0; i<size; i++) | |
40 | original_bits[i] = random_bit(); | |
41 | ||
42 | original_bits[size] = '\0'; | |
43 | left_shifted[size] = '\0'; | |
44 | right_shifted[size] = '\0'; | |
45 | } | |
46 | ||
47 | void | |
48 | shift_arrays(size_t shift_step, size_t size) { | |
49 | for (size_t i=shift_step; i<size; i++) { | |
50 | right_shifted[i] = original_bits[i-shift_step]; | |
51 | left_shifted[size-i-1] = original_bits[size+shift_step-i-1]; | |
52 | } | |
53 | for (size_t i=0; i<shift_step && i<size; i++) { | |
54 | right_shifted[i] = '0'; | |
55 | left_shifted[size-i-1] = '0'; | |
56 | } | |
57 | } | |
58 | ||
59 | template <size_t size> | |
60 | bool | |
61 | do_test() { | |
62 | bool test = true; | |
63 | ||
64 | std::bitset<size> shifted; | |
65 | std::bitset<size> correct; | |
66 | ||
67 | initialise(size); | |
68 | ||
69 | //std::bitset<size> original = std::string(original_bits); | |
70 | std::bitset<size> original = std::bitset<size> (std::string(original_bits)); | |
71 | ||
72 | for (size_t shift_step=0; shift_step==0 || shift_step<size; shift_step++) { | |
73 | shift_arrays(shift_step, size); | |
74 | ||
75 | shifted = original; | |
76 | shifted <<= shift_step; | |
77 | //correct = std::string(left_shifted); | |
78 | correct = std::bitset<size> (std::string(left_shifted)); | |
aa1b2f7d | 79 | VERIFY( shifted == correct ); |
b2dad0e3 BK |
80 | |
81 | shifted = original; | |
82 | shifted >>= shift_step; | |
83 | //correct = std::string(right_shifted); | |
84 | correct = std::bitset<size> (std::string(right_shifted)); | |
aa1b2f7d | 85 | VERIFY( shifted == correct ); |
b2dad0e3 BK |
86 | } |
87 | ||
88 | return test; | |
89 | } | |
90 | ||
91 | bool | |
92 | test01() { | |
93 | bool test = true; | |
94 | ||
aa1b2f7d BV |
95 | VERIFY( do_test<32>() ); |
96 | VERIFY( do_test<48>() ); | |
97 | VERIFY( do_test<64>() ); | |
b2dad0e3 | 98 | |
aa1b2f7d BV |
99 | VERIFY( do_test<511>() ); |
100 | VERIFY( do_test<513>() ); | |
101 | VERIFY( do_test<997>() ); | |
b2dad0e3 BK |
102 | return test; |
103 | } | |
104 | ||
3bbfb3d9 PE |
105 | bool |
106 | test02() | |
107 | { | |
108 | bool test = true; | |
109 | ||
110 | std::bitset<66> b; | |
111 | b <<= 400; | |
112 | VERIFY( b.count() == 0 ); | |
3bbfb3d9 PE |
113 | return test; |
114 | } | |
115 | ||
b2dad0e3 BK |
116 | int |
117 | main() { | |
118 | test01(); | |
3bbfb3d9 | 119 | test02(); |
b2dad0e3 BK |
120 | |
121 | return 0; | |
122 | } |