blob: 0a27aa21b05c833b6d751b98e291fea96bd2364d [file] [log] [blame] [edit]
// Copyright 2017 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
#include "dawn/EnumClassBitmasks.h"
namespace dawn {
enum class Color : uint32_t {
R = 1,
G = 2,
B = 4,
A = 8,
};
template <>
struct IsDawnBitmask<Color> {
static constexpr bool enable = true;
};
TEST(BitmaskTests, BasicOperations) {
Color test1 = Color::R | Color::G;
ASSERT_EQ(1u | 2u, static_cast<uint32_t>(test1));
Color test2 = test1 ^ (Color::R | Color::A);
ASSERT_EQ(2u | 8u, static_cast<uint32_t>(test2));
Color test3 = test2 & Color::A;
ASSERT_EQ(8u, static_cast<uint32_t>(test3));
Color test4 = ~test3;
ASSERT_EQ(~uint32_t(8), static_cast<uint32_t>(test4));
}
TEST(BitmaskTests, AssignOperations) {
Color test1 = Color::R;
test1 |= Color::G;
ASSERT_EQ(1u | 2u, static_cast<uint32_t>(test1));
Color test2 = test1;
test2 ^= (Color::R | Color::A);
ASSERT_EQ(2u | 8u, static_cast<uint32_t>(test2));
Color test3 = test2;
test3 &= Color::A;
ASSERT_EQ(8u, static_cast<uint32_t>(test3));
}
TEST(BitmaskTests, BoolConversion) {
bool test1 = Color::R | Color::G;
ASSERT_TRUE(test1);
bool test2 = Color::R & Color::G;
ASSERT_FALSE(test2);
bool test3 = Color::R ^ Color::G;
ASSERT_TRUE(test3);
if (Color::R & ~Color::R) {
ASSERT_TRUE(false);
}
}
TEST(BitmaskTests, ThreeOrs) {
Color c = Color::R | Color::G | Color::B;
ASSERT_EQ(7u, static_cast<uint32_t>(c));
}
TEST(BitmaskTests, ZeroOrOneBits) {
Color zero = static_cast<Color>(0);
ASSERT_TRUE(HasZeroOrOneBits(zero));
ASSERT_TRUE(HasZeroOrOneBits(Color::R));
ASSERT_TRUE(HasZeroOrOneBits(Color::G));
ASSERT_TRUE(HasZeroOrOneBits(Color::B));
ASSERT_TRUE(HasZeroOrOneBits(Color::A));
ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::R | Color::G)));
ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::G | Color::B)));
ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::B | Color::A)));
}
} // namespace dawn