Update copyright in libstdc++-v3.
[official-gcc.git] / libstdc++-v3 / testsuite / 23_containers / bitset / operations / 1.cc
blob6dbb16b8d9f3fecb5a2ba7666d4cc7b2120f6128
1 // 2000-01-15 Anders Widell <awl@hem.passagen.se>
3 // Copyright (C) 2000-2013 Free Software Foundation, Inc.
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 3, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 #include <string>
21 #include <set>
22 #include <bitset>
23 #include <testsuite_hooks.h>
25 static char original_bits[1024];
26 static char left_shifted[1024];
27 static char right_shifted[1024];
29 char
30 random_bit() {
31 static long x = 1;
32 return ((x = (3432L*x + 6789L) % 9973L) & 1) + '0';
35 void
36 initialise(size_t size) {
37 for (size_t i=0; i<size; i++)
38 original_bits[i] = random_bit();
40 original_bits[size] = '\0';
41 left_shifted[size] = '\0';
42 right_shifted[size] = '\0';
45 void
46 shift_arrays(size_t shift_step, size_t size) {
47 for (size_t i=shift_step; i<size; i++) {
48 right_shifted[i] = original_bits[i-shift_step];
49 left_shifted[size-i-1] = original_bits[size+shift_step-i-1];
51 for (size_t i=0; i<shift_step && i<size; i++) {
52 right_shifted[i] = '0';
53 left_shifted[size-i-1] = '0';
57 template <size_t size>
58 bool
59 do_test() {
60 bool test __attribute__((unused)) = true;
62 std::bitset<size> shifted;
63 std::bitset<size> correct;
65 initialise(size);
67 //std::bitset<size> original = std::string(original_bits);
68 std::bitset<size> original = std::bitset<size> (std::string(original_bits));
70 for (size_t shift_step=0; shift_step==0 || shift_step<size; shift_step++) {
71 shift_arrays(shift_step, size);
73 shifted = original;
74 shifted <<= shift_step;
75 //correct = std::string(left_shifted);
76 correct = std::bitset<size> (std::string(left_shifted));
77 VERIFY( shifted == correct );
79 shifted = original;
80 shifted >>= shift_step;
81 //correct = std::string(right_shifted);
82 correct = std::bitset<size> (std::string(right_shifted));
83 VERIFY( shifted == correct );
86 return test;
89 bool
90 test01() {
91 bool test __attribute__((unused)) = true;
93 VERIFY( do_test<32>() );
94 VERIFY( do_test<48>() );
95 VERIFY( do_test<64>() );
97 VERIFY( do_test<511>() );
98 VERIFY( do_test<513>() );
99 VERIFY( do_test<997>() );
100 return test;
104 main()
106 test01();
107 return 0;