dawn-cmake/src/tint/utils/bitset_test.cc
Ben Clayton e43034bef2 tint/utils: Add Bitset.
A compact, dynamically sized, vector of bits.

Bug: tint:1613
Change-Id: Ida10d1bdedad48dd4c2ab7908732f328b4b96055
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/96544
Reviewed-by: dan sinclair <dsinclair@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
2022-07-21 23:32:24 +00:00

120 lines
2.8 KiB
C++

// Copyright 2022 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/bitset.h"
#include "gtest/gtest.h"
namespace tint::utils {
namespace {
TEST(Bitset, Length) {
Bitset<8> bits;
EXPECT_EQ(bits.Length(), 0u);
bits.Resize(100u);
EXPECT_EQ(bits.Length(), 100u);
}
TEST(Bitset, InitCleared_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_FALSE(bits[i]);
}
}
TEST(Bitset, InitCleared_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_FALSE(bits[i]);
}
}
TEST(Bitset, ReadWrite_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = (i & 0x2) == 0;
}
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], (i & 0x2) == 0);
}
}
TEST(Bitset, ReadWrite_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = (i & 0x2) == 0;
}
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], (i & 0x2) == 0);
}
}
TEST(Bitset, ShinkGrowAlignedClears_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(64);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 64u);
}
}
TEST(Bitset, ShinkGrowAlignedClears_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(64);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 64u);
}
}
TEST(Bitset, ShinkGrowMisalignedClears_NoSpill) {
Bitset<256> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(42);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 42u);
}
}
TEST(Bitset, ShinkGrowMisalignedClears_Spill) {
Bitset<64> bits;
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
bits[i] = true;
}
bits.Resize(42);
bits.Resize(256);
for (size_t i = 0; i < 256; i++) {
EXPECT_EQ(bits[i], i < 42u);
}
}
} // namespace
} // namespace tint::utils