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