|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2014 Andreas Pohl <apohl79 at gmail.com> |
| 3 | + * |
| 4 | + * This file is part of libtasks. |
| 5 | + * |
| 6 | + * libtasks is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * libtasks 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 |
| 17 | + * along with libtasks. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <tasks/tools/bitset.h> |
| 21 | + |
| 22 | +#include "test_bitset.h" |
| 23 | + |
| 24 | +using namespace tasks::tools; |
| 25 | + |
| 26 | +void full_iteration(bitset& bs) { |
| 27 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 28 | + bs.toggle(i); |
| 29 | + } |
| 30 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 31 | + CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") toggle test 1: i=" |
| 32 | + + std::to_string(i), bs.test(i)); |
| 33 | + } |
| 34 | + |
| 35 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 36 | + bs.toggle(i); |
| 37 | + } |
| 38 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 39 | + CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") toggle test 2: i=" |
| 40 | + + std::to_string(i), !bs.test(i)); |
| 41 | + } |
| 42 | + |
| 43 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 44 | + bs.set(i); |
| 45 | + } |
| 46 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 47 | + CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") set test: i=" |
| 48 | + + std::to_string(i), bs.test(i)); |
| 49 | + } |
| 50 | + |
| 51 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 52 | + bs.unset(i); |
| 53 | + } |
| 54 | + for (bitset::int_type i = 0; i < bs.bits(); i++) { |
| 55 | + CPPUNIT_ASSERT_MESSAGE("bitset(" + std::to_string(bs.bits()) + ") unset test: i=" |
| 56 | + + std::to_string(i), !bs.test(i)); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void test_bitset::test() { |
| 61 | + bitset::int_type bit; |
| 62 | + |
| 63 | + bitset bs1(10); |
| 64 | + CPPUNIT_ASSERT(1 == bs1.buckets()); |
| 65 | + full_iteration(bs1); |
| 66 | + bs1.set(0); |
| 67 | + bs1.set(3); |
| 68 | + bs1.set(6); |
| 69 | + bs1.set(9); |
| 70 | + CPPUNIT_ASSERT(bs1.first(bit)); |
| 71 | + CPPUNIT_ASSERT(0 == bit); |
| 72 | + bs1.unset(0); |
| 73 | + CPPUNIT_ASSERT(bs1.first(bit)); |
| 74 | + CPPUNIT_ASSERT(3 == bit); |
| 75 | + CPPUNIT_ASSERT(bs1.next(bit)); |
| 76 | + CPPUNIT_ASSERT(3 == bit); |
| 77 | + CPPUNIT_ASSERT(bs1.next(bit, 3)); |
| 78 | + CPPUNIT_ASSERT(3 == bit); |
| 79 | + CPPUNIT_ASSERT(bs1.next(bit, 4)); |
| 80 | + CPPUNIT_ASSERT(6 == bit); |
| 81 | + CPPUNIT_ASSERT(bs1.next(bit, 7)); |
| 82 | + CPPUNIT_ASSERT(9 == bit); |
| 83 | + bs1.unset(9); |
| 84 | + CPPUNIT_ASSERT(bs1.next(bit, 9)); |
| 85 | + CPPUNIT_ASSERT(3 == bit); |
| 86 | + |
| 87 | + bitset bs2(64); |
| 88 | + CPPUNIT_ASSERT(1 == bs2.buckets()); |
| 89 | + full_iteration(bs2); |
| 90 | + bs2.set(63); |
| 91 | + CPPUNIT_ASSERT(bs2.next(bit)); |
| 92 | + CPPUNIT_ASSERT(63 == bit); |
| 93 | + CPPUNIT_ASSERT(bs2.next(bit, 62)); |
| 94 | + CPPUNIT_ASSERT(63 == bit); |
| 95 | + CPPUNIT_ASSERT(bs2.next(bit, 63)); |
| 96 | + CPPUNIT_ASSERT(63 == bit); |
| 97 | + |
| 98 | + bitset bs3(120); |
| 99 | + CPPUNIT_ASSERT(2 == bs3.buckets()); |
| 100 | + full_iteration(bs3); |
| 101 | + bs3.set(3); |
| 102 | + bs3.set(63); |
| 103 | + bs3.set(64); |
| 104 | + bs3.set(118); |
| 105 | + CPPUNIT_ASSERT(bs3.next(bit)); |
| 106 | + CPPUNIT_ASSERT(3 == bit); |
| 107 | + CPPUNIT_ASSERT(bs3.next(bit, 4)); |
| 108 | + CPPUNIT_ASSERT(63 == bit); |
| 109 | + CPPUNIT_ASSERT(bs3.next(bit, 64)); |
| 110 | + CPPUNIT_ASSERT(64 == bit); |
| 111 | + CPPUNIT_ASSERT(bs3.next(bit, 65)); |
| 112 | + CPPUNIT_ASSERT(118 == bit); |
| 113 | + CPPUNIT_ASSERT(bs3.next(bit, 119)); |
| 114 | + bs3.unset(3); |
| 115 | + bs3.unset(63); |
| 116 | + bs3.unset(64); |
| 117 | + CPPUNIT_ASSERT(bs3.next(bit)); |
| 118 | + CPPUNIT_ASSERT(118 == bit); |
| 119 | + CPPUNIT_ASSERT(bs3.first(bit)); |
| 120 | + CPPUNIT_ASSERT(118 == bit); |
| 121 | + |
| 122 | + bitset bs4(129); |
| 123 | + CPPUNIT_ASSERT(3 == bs4.buckets()); |
| 124 | + full_iteration(bs4); |
| 125 | + bs4.set(128); |
| 126 | + CPPUNIT_ASSERT(bs4.next(bit)); |
| 127 | + CPPUNIT_ASSERT(128 == bit); |
| 128 | + CPPUNIT_ASSERT(bs4.first(bit)); |
| 129 | + CPPUNIT_ASSERT(128 == bit); |
| 130 | + bs4.unset(128); |
| 131 | + bs4.set(60); |
| 132 | + CPPUNIT_ASSERT(bs4.next(bit, 61)); |
| 133 | + CPPUNIT_ASSERT(60 == bit); |
| 134 | +} |
0 commit comments