bit_vector_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <vector>
  15. #include "gmock/gmock.h"
  16. #include "source/util/bit_vector.h"
  17. namespace spvtools {
  18. namespace utils {
  19. namespace {
  20. using BitVectorTest = ::testing::Test;
  21. TEST(BitVectorTest, Initialize) {
  22. BitVector bvec;
  23. // Checks that all values are 0. Also tests checking a bit past the end of
  24. // the vector containing the bits.
  25. for (int i = 1; i < 10000; i *= 2) {
  26. EXPECT_FALSE(bvec.Get(i));
  27. }
  28. }
  29. TEST(BitVectorTest, Set) {
  30. BitVector bvec;
  31. // Since 10,000 is larger than the initial size, this tests the resizing
  32. // code.
  33. for (int i = 3; i < 10000; i *= 2) {
  34. bvec.Set(i);
  35. }
  36. // Check that bits that were not set are 0.
  37. for (int i = 1; i < 10000; i *= 2) {
  38. EXPECT_FALSE(bvec.Get(i));
  39. }
  40. // Check that bits that were set are 1.
  41. for (int i = 3; i < 10000; i *= 2) {
  42. EXPECT_TRUE(bvec.Get(i));
  43. }
  44. }
  45. TEST(BitVectorTest, SetReturnValue) {
  46. BitVector bvec;
  47. // Make sure |Set| returns false when the bit was not set.
  48. for (int i = 3; i < 10000; i *= 2) {
  49. EXPECT_FALSE(bvec.Set(i));
  50. }
  51. // Make sure |Set| returns true when the bit was already set.
  52. for (int i = 3; i < 10000; i *= 2) {
  53. EXPECT_TRUE(bvec.Set(i));
  54. }
  55. }
  56. TEST(BitVectorTest, Clear) {
  57. BitVector bvec;
  58. for (int i = 3; i < 10000; i *= 2) {
  59. bvec.Set(i);
  60. }
  61. // Check that the bits were properly set.
  62. for (int i = 3; i < 10000; i *= 2) {
  63. EXPECT_TRUE(bvec.Get(i));
  64. }
  65. // Clear all of the bits except for bit 3.
  66. for (int i = 6; i < 10000; i *= 2) {
  67. bvec.Clear(i);
  68. }
  69. // Make sure bit 3 was not cleared.
  70. EXPECT_TRUE(bvec.Get(3));
  71. // Make sure all of the other bits that were set have been cleared.
  72. for (int i = 6; i < 10000; i *= 2) {
  73. EXPECT_FALSE(bvec.Get(i));
  74. }
  75. }
  76. TEST(BitVectorTest, ClearReturnValue) {
  77. BitVector bvec;
  78. for (int i = 3; i < 10000; i *= 2) {
  79. bvec.Set(i);
  80. }
  81. // Make sure |Clear| returns true if the bit was set.
  82. for (int i = 3; i < 10000; i *= 2) {
  83. EXPECT_TRUE(bvec.Clear(i));
  84. }
  85. // Make sure |Clear| returns false if the bit was not set.
  86. for (int i = 3; i < 10000; i *= 2) {
  87. EXPECT_FALSE(bvec.Clear(i));
  88. }
  89. }
  90. TEST(BitVectorTest, SimpleOrTest) {
  91. BitVector bvec1;
  92. bvec1.Set(3);
  93. bvec1.Set(4);
  94. BitVector bvec2;
  95. bvec2.Set(2);
  96. bvec2.Set(4);
  97. // Check that |bvec1| changed when doing the |Or| operation.
  98. EXPECT_TRUE(bvec1.Or(bvec2));
  99. // Check that the values are all correct.
  100. EXPECT_FALSE(bvec1.Get(0));
  101. EXPECT_FALSE(bvec1.Get(1));
  102. EXPECT_TRUE(bvec1.Get(2));
  103. EXPECT_TRUE(bvec1.Get(3));
  104. EXPECT_TRUE(bvec1.Get(4));
  105. }
  106. TEST(BitVectorTest, ResizingOrTest) {
  107. BitVector bvec1;
  108. bvec1.Set(3);
  109. bvec1.Set(4);
  110. BitVector bvec2;
  111. bvec2.Set(10000);
  112. // Similar to above except with a large value to test resizing.
  113. EXPECT_TRUE(bvec1.Or(bvec2));
  114. EXPECT_FALSE(bvec1.Get(0));
  115. EXPECT_FALSE(bvec1.Get(1));
  116. EXPECT_FALSE(bvec1.Get(2));
  117. EXPECT_TRUE(bvec1.Get(3));
  118. EXPECT_TRUE(bvec1.Get(10000));
  119. }
  120. TEST(BitVectorTest, SubsetOrTest) {
  121. BitVector bvec1;
  122. bvec1.Set(3);
  123. bvec1.Set(4);
  124. BitVector bvec2;
  125. bvec2.Set(3);
  126. // |Or| returns false if |bvec1| does not change.
  127. EXPECT_FALSE(bvec1.Or(bvec2));
  128. }
  129. } // namespace
  130. } // namespace utils
  131. } // namespace spvtools