]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/bitset/operations/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / bitset / operations / 1.cc
CommitLineData
b2dad0e3
BK
1// 2000-01-15 Anders Widell <awl@hem.passagen.se>
2
a945c346 3// Copyright (C) 2000-2024 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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
b2dad0e3
BK
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
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
b2dad0e3
BK
19
20#include <string>
21#include <set>
22#include <bitset>
fe413112 23#include <testsuite_hooks.h>
b2dad0e3
BK
24
25static char original_bits[1024];
26static char left_shifted[1024];
27static char right_shifted[1024];
28
29char
30random_bit() {
31 static long x = 1;
32 return ((x = (3432L*x + 6789L) % 9973L) & 1) + '0';
33}
34
35void
36initialise(size_t size) {
37 for (size_t i=0; i<size; i++)
38 original_bits[i] = random_bit();
39
40 original_bits[size] = '\0';
41 left_shifted[size] = '\0';
42 right_shifted[size] = '\0';
43}
44
45void
46shift_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];
50 }
51 for (size_t i=0; i<shift_step && i<size; i++) {
52 right_shifted[i] = '0';
53 left_shifted[size-i-1] = '0';
54 }
55}
56
57template <size_t size>
58 bool
a9260b7e
PC
59 do_test()
60 {
61 bool test = true;
b2dad0e3
BK
62
63 std::bitset<size> shifted;
64 std::bitset<size> correct;
65
66 initialise(size);
67
68 //std::bitset<size> original = std::string(original_bits);
69 std::bitset<size> original = std::bitset<size> (std::string(original_bits));
70
71 for (size_t shift_step=0; shift_step==0 || shift_step<size; shift_step++) {
72 shift_arrays(shift_step, size);
73
74 shifted = original;
75 shifted <<= shift_step;
76 //correct = std::string(left_shifted);
77 correct = std::bitset<size> (std::string(left_shifted));
aa1b2f7d 78 VERIFY( shifted == correct );
b2dad0e3
BK
79
80 shifted = original;
81 shifted >>= shift_step;
82 //correct = std::string(right_shifted);
83 correct = std::bitset<size> (std::string(right_shifted));
aa1b2f7d 84 VERIFY( shifted == correct );
b2dad0e3
BK
85 }
86
87 return test;
88 }
89
a9260b7e 90void
b2dad0e3 91test01() {
aa1b2f7d
BV
92 VERIFY( do_test<32>() );
93 VERIFY( do_test<48>() );
94 VERIFY( do_test<64>() );
b2dad0e3 95
aa1b2f7d
BV
96 VERIFY( do_test<511>() );
97 VERIFY( do_test<513>() );
98 VERIFY( do_test<997>() );
b2dad0e3
BK
99}
100
101int
17472bb6
BK
102main()
103{
b2dad0e3 104 test01();
b2dad0e3
BK
105 return 0;
106}