• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
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 
15 #include "gtest/gtest.h"
16 
17 #include "dawn/EnumClassBitmasks.h"
18 
19 namespace dawn {
20 
21     enum class Color : uint32_t {
22         R = 1,
23         G = 2,
24         B = 4,
25         A = 8,
26     };
27 
28     template<>
29     struct IsDawnBitmask<Color> {
30         static constexpr bool enable = true;
31     };
32 
TEST(BitmaskTests,BasicOperations)33     TEST(BitmaskTests, BasicOperations) {
34         Color test1 = Color::R | Color::G;
35         ASSERT_EQ(1u | 2u, static_cast<uint32_t>(test1));
36 
37         Color test2 = test1 ^ (Color::R | Color::A);
38         ASSERT_EQ(2u | 8u, static_cast<uint32_t>(test2));
39 
40         Color test3 = test2 & Color::A;
41         ASSERT_EQ(8u, static_cast<uint32_t>(test3));
42 
43         Color test4 = ~test3;
44         ASSERT_EQ(~uint32_t(8), static_cast<uint32_t>(test4));
45     }
46 
TEST(BitmaskTests,AssignOperations)47     TEST(BitmaskTests, AssignOperations) {
48         Color test1 = Color::R;
49         test1 |= Color::G;
50         ASSERT_EQ(1u | 2u, static_cast<uint32_t>(test1));
51 
52         Color test2 = test1;
53         test2 ^= (Color::R | Color::A);
54         ASSERT_EQ(2u | 8u, static_cast<uint32_t>(test2));
55 
56         Color test3 = test2;
57         test3 &= Color::A;
58         ASSERT_EQ(8u, static_cast<uint32_t>(test3));
59     }
60 
TEST(BitmaskTests,BoolConversion)61     TEST(BitmaskTests, BoolConversion) {
62         bool test1 = Color::R | Color::G;
63         ASSERT_TRUE(test1);
64 
65         bool test2 = Color::R & Color::G;
66         ASSERT_FALSE(test2);
67 
68         bool test3 = Color::R ^ Color::G;
69         ASSERT_TRUE(test3);
70 
71         if (Color::R & ~Color::R) {
72             ASSERT_TRUE(false);
73         }
74     }
75 
TEST(BitmaskTests,ThreeOrs)76     TEST(BitmaskTests, ThreeOrs) {
77         Color c = Color::R | Color::G | Color::B;
78         ASSERT_EQ(7u, static_cast<uint32_t>(c));
79     }
80 
TEST(BitmaskTests,ZeroOrOneBits)81     TEST(BitmaskTests, ZeroOrOneBits) {
82         Color zero = static_cast<Color>(0);
83         ASSERT_TRUE(HasZeroOrOneBits(zero));
84         ASSERT_TRUE(HasZeroOrOneBits(Color::R));
85         ASSERT_TRUE(HasZeroOrOneBits(Color::G));
86         ASSERT_TRUE(HasZeroOrOneBits(Color::B));
87         ASSERT_TRUE(HasZeroOrOneBits(Color::A));
88         ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::R | Color::G)));
89         ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::G | Color::B)));
90         ASSERT_FALSE(HasZeroOrOneBits(static_cast<Color>(Color::B | Color::A)));
91     }
92 
93 }  // namespace dawn
94