bitset_test.cpp revision 8a1362ad
18a1362adSmaya/*
28a1362adSmaya * Copyright © 2019 Red Hat
38a1362adSmaya *
48a1362adSmaya * Permission is hereby granted, free of charge, to any person obtaining a
58a1362adSmaya * copy of this software and associated documentation files (the "Software"),
68a1362adSmaya * to deal in the Software without restriction, including without limitation
78a1362adSmaya * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88a1362adSmaya * and/or sell copies of the Software, and to permit persons to whom the
98a1362adSmaya * Software is furnished to do so, subject to the following conditions:
108a1362adSmaya *
118a1362adSmaya * The above copyright notice and this permission notice (including the next
128a1362adSmaya * paragraph) shall be included in all copies or substantial portions of the
138a1362adSmaya * Software.
148a1362adSmaya *
158a1362adSmaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168a1362adSmaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178a1362adSmaya * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188a1362adSmaya * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198a1362adSmaya * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208a1362adSmaya * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218a1362adSmaya * IN THE SOFTWARE.
228a1362adSmaya */
238a1362adSmaya
248a1362adSmaya#include <gtest/gtest.h>
258a1362adSmaya#include "util/bitset.h"
268a1362adSmaya
278a1362adSmayaTEST(bitset, sizes)
288a1362adSmaya{
298a1362adSmaya   EXPECT_EQ(sizeof(BITSET_WORD), 4);
308a1362adSmaya
318a1362adSmaya   BITSET_DECLARE(mask32, 32);
328a1362adSmaya   BITSET_DECLARE(mask64, 64);
338a1362adSmaya   BITSET_DECLARE(mask128, 128);
348a1362adSmaya
358a1362adSmaya   EXPECT_EQ(sizeof(mask32), 4);
368a1362adSmaya   EXPECT_EQ(sizeof(mask64), 8);
378a1362adSmaya   EXPECT_EQ(sizeof(mask128), 16);
388a1362adSmaya}
398a1362adSmaya
408a1362adSmayaTEST(bitset, testsetclear)
418a1362adSmaya{
428a1362adSmaya   BITSET_DECLARE(mask128, 128);
438a1362adSmaya   BITSET_ZERO(mask128);
448a1362adSmaya
458a1362adSmaya   for (int i = 0; i < 128; i++) {
468a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), false);
478a1362adSmaya      BITSET_SET(mask128, i);
488a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), true);
498a1362adSmaya      BITSET_CLEAR(mask128, i);
508a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), false);
518a1362adSmaya   }
528a1362adSmaya}
538a1362adSmaya
548a1362adSmayaTEST(bitset, testsetones)
558a1362adSmaya{
568a1362adSmaya   BITSET_DECLARE(mask128, 128);
578a1362adSmaya   BITSET_ONES(mask128);
588a1362adSmaya
598a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 1);
608a1362adSmaya
618a1362adSmaya   for (int i = 0; i < 128; i++) {
628a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), true);
638a1362adSmaya      BITSET_CLEAR(mask128, i);
648a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), false);
658a1362adSmaya      BITSET_SET(mask128, i);
668a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), true);
678a1362adSmaya   }
688a1362adSmaya}
698a1362adSmaya
708a1362adSmayaTEST(bitset, testbasicrange)
718a1362adSmaya{
728a1362adSmaya   BITSET_DECLARE(mask128, 128);
738a1362adSmaya   BITSET_ZERO(mask128);
748a1362adSmaya
758a1362adSmaya   const int max_set = 15;
768a1362adSmaya   BITSET_SET_RANGE(mask128, 0, max_set);
778a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, max_set), true);
788a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, max_set + 1, max_set + 15), false);
798a1362adSmaya   for (int i = 0; i < 128; i++) {
808a1362adSmaya      if (i <= max_set)
818a1362adSmaya         EXPECT_EQ(BITSET_TEST(mask128, i), true);
828a1362adSmaya      else
838a1362adSmaya         EXPECT_EQ(BITSET_TEST(mask128, i), false);
848a1362adSmaya   }
858a1362adSmaya   BITSET_CLEAR_RANGE(mask128, 0, max_set);
868a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, max_set), false);
878a1362adSmaya   for (int i = 0; i < 128; i++) {
888a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), false);
898a1362adSmaya   }
908a1362adSmaya}
918a1362adSmaya
928a1362adSmayaTEST(bitset, testbitsetffs)
938a1362adSmaya{
948a1362adSmaya   BITSET_DECLARE(mask128, 128);
958a1362adSmaya   BITSET_ZERO(mask128);
968a1362adSmaya
978a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 0);
988a1362adSmaya
998a1362adSmaya   BITSET_SET(mask128, 14);
1008a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 15);
1018a1362adSmaya
1028a1362adSmaya   BITSET_SET(mask128, 28);
1038a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 15);
1048a1362adSmaya
1058a1362adSmaya   BITSET_CLEAR(mask128, 14);
1068a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 29);
1078a1362adSmaya
1088a1362adSmaya   BITSET_SET_RANGE(mask128, 14, 18);
1098a1362adSmaya   EXPECT_EQ(BITSET_FFS(mask128), 15);
1108a1362adSmaya}
1118a1362adSmaya
1128a1362adSmayaTEST(bitset, testrangebits)
1138a1362adSmaya{
1148a1362adSmaya   BITSET_DECLARE(mask128, 128);
1158a1362adSmaya   BITSET_ZERO(mask128);
1168a1362adSmaya
1178a1362adSmaya   BITSET_SET_RANGE(mask128, 0, 31);
1188a1362adSmaya   BITSET_SET_RANGE(mask128, 32, 63);
1198a1362adSmaya   BITSET_SET_RANGE(mask128, 64, 95);
1208a1362adSmaya   BITSET_SET_RANGE(mask128, 96, 127);
1218a1362adSmaya
1228a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 0, 31), true);
1238a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 32, 63), true);
1248a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 64, 95), true);
1258a1362adSmaya   EXPECT_EQ(BITSET_TEST_RANGE(mask128, 96, 127), true);
1268a1362adSmaya   for (int i = 0; i < 128; i++) {
1278a1362adSmaya      EXPECT_EQ(BITSET_TEST(mask128, i), true);
1288a1362adSmaya   }
1298a1362adSmaya}
130