Fix GCC sign-compare warnings
[platform/upstream/glslang.git] / gtests / Common.cpp
1 // Copyright (c) 2021 Google LLC
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 <cstdint>
16
17 #include <gmock/gmock.h>
18 #include "glslang/Include/Common.h"
19
20 namespace {
21
22 TEST(IsPow2, Int_Negative) {
23   EXPECT_EQ(false, glslang::IsPow2(-5));
24   EXPECT_EQ(false, glslang::IsPow2(-1));
25   EXPECT_EQ(false, glslang::IsPow2(INT_MIN));
26   EXPECT_EQ(false, glslang::IsPow2(int64_t(-10)));
27 }
28
29 TEST(IsPow2, Zero) {
30   EXPECT_EQ(false, glslang::IsPow2(0));
31   EXPECT_EQ(false, glslang::IsPow2(0u));
32   EXPECT_EQ(false, glslang::IsPow2(0));
33   EXPECT_EQ(false, glslang::IsPow2(uint64_t(0)));
34   EXPECT_EQ(false, glslang::IsPow2(int64_t(0)));
35 }
36
37 TEST(IsPow2, Int_Positive_PowersOf2) {
38   EXPECT_EQ(true, glslang::IsPow2(1));
39   EXPECT_EQ(true, glslang::IsPow2(2));
40   EXPECT_EQ(true, glslang::IsPow2(4));
41   EXPECT_EQ(true, glslang::IsPow2(8));
42   EXPECT_EQ(true, glslang::IsPow2(16));
43   EXPECT_EQ(true, glslang::IsPow2(32768));
44   EXPECT_EQ(true, glslang::IsPow2(65536));
45   EXPECT_EQ(true, glslang::IsPow2(2147483648));
46 }
47
48 TEST(IsPow2, Int_Positive_NonPowersOf2) {
49   EXPECT_EQ(false, glslang::IsPow2(3));
50   EXPECT_EQ(false, glslang::IsPow2(5));
51   EXPECT_EQ(false, glslang::IsPow2(2147483647));
52 }
53
54 TEST(IsPow2, Uint_Positive_PowersOf2) {
55   EXPECT_EQ(true, glslang::IsPow2(1u));
56   EXPECT_EQ(true, glslang::IsPow2(2u));
57   EXPECT_EQ(true, glslang::IsPow2(4u));
58   EXPECT_EQ(true, glslang::IsPow2(8u));
59   EXPECT_EQ(true, glslang::IsPow2(16u));
60   EXPECT_EQ(true, glslang::IsPow2(32768u));
61   EXPECT_EQ(true, glslang::IsPow2(65536u));
62   EXPECT_EQ(true, glslang::IsPow2(2147483648u));
63 }
64
65 TEST(IsPow2, Uint_Positive_NonPowersOf2) {
66   EXPECT_EQ(false, glslang::IsPow2(3u));
67   EXPECT_EQ(false, glslang::IsPow2(5u));
68   EXPECT_EQ(false, glslang::IsPow2(2147483647u));
69 }
70
71 TEST(IntLog2, Int) {
72   EXPECT_EQ(0, glslang::IntLog2(1));
73   EXPECT_EQ(1, glslang::IntLog2(2));
74   EXPECT_EQ(2, glslang::IntLog2(4));
75   EXPECT_EQ(3, glslang::IntLog2(8));
76   EXPECT_EQ(4, glslang::IntLog2(16));
77   EXPECT_EQ(5, glslang::IntLog2(32));
78   EXPECT_EQ(6, glslang::IntLog2(64));
79   EXPECT_EQ(7, glslang::IntLog2(128));
80   EXPECT_EQ(8, glslang::IntLog2(256));
81   EXPECT_EQ(9, glslang::IntLog2(512));
82   EXPECT_EQ(10, glslang::IntLog2(1024));
83   EXPECT_EQ(11, glslang::IntLog2(2048));
84   EXPECT_EQ(12, glslang::IntLog2(0x1000));
85   EXPECT_EQ(13, glslang::IntLog2(0x2000));
86   EXPECT_EQ(14, glslang::IntLog2(0x4000));
87   EXPECT_EQ(15, glslang::IntLog2(0x8000));
88   EXPECT_EQ(16, glslang::IntLog2(0x10000));
89   EXPECT_EQ(17, glslang::IntLog2(0x20000));
90   EXPECT_EQ(18, glslang::IntLog2(0x40000));
91   EXPECT_EQ(19, glslang::IntLog2(0x80000));
92   EXPECT_EQ(20, glslang::IntLog2(0x100000));
93   EXPECT_EQ(21, glslang::IntLog2(0x200000));
94   EXPECT_EQ(22, glslang::IntLog2(0x400000));
95   EXPECT_EQ(23, glslang::IntLog2(0x800000));
96   EXPECT_EQ(24, glslang::IntLog2(0x1000000));
97   EXPECT_EQ(25, glslang::IntLog2(0x2000000));
98   EXPECT_EQ(26, glslang::IntLog2(0x4000000));
99   EXPECT_EQ(27, glslang::IntLog2(0x8000000));
100   EXPECT_EQ(28, glslang::IntLog2(0x10000000));
101   EXPECT_EQ(29, glslang::IntLog2(0x20000000));
102   EXPECT_EQ(30, glslang::IntLog2(0x40000000));
103 }
104
105 TEST(IntLog2, Uint) {
106   EXPECT_EQ(0, glslang::IntLog2(1u));
107   EXPECT_EQ(1, glslang::IntLog2(2u));
108   EXPECT_EQ(2, glslang::IntLog2(4u));
109   EXPECT_EQ(3, glslang::IntLog2(8u));
110   EXPECT_EQ(4, glslang::IntLog2(16u));
111   EXPECT_EQ(5, glslang::IntLog2(32u));
112   EXPECT_EQ(6, glslang::IntLog2(64u));
113   EXPECT_EQ(7, glslang::IntLog2(128u));
114   EXPECT_EQ(8, glslang::IntLog2(256u));
115   EXPECT_EQ(9, glslang::IntLog2(512u));
116   EXPECT_EQ(10, glslang::IntLog2(1024u));
117   EXPECT_EQ(11, glslang::IntLog2(2048u));
118   EXPECT_EQ(12, glslang::IntLog2(0x1000u));
119   EXPECT_EQ(13, glslang::IntLog2(0x2000u));
120   EXPECT_EQ(14, glslang::IntLog2(0x4000u));
121   EXPECT_EQ(15, glslang::IntLog2(0x8000u));
122   EXPECT_EQ(16, glslang::IntLog2(0x10000u));
123   EXPECT_EQ(17, glslang::IntLog2(0x20000u));
124   EXPECT_EQ(18, glslang::IntLog2(0x40000u));
125   EXPECT_EQ(19, glslang::IntLog2(0x80000u));
126   EXPECT_EQ(20, glslang::IntLog2(0x100000u));
127   EXPECT_EQ(21, glslang::IntLog2(0x200000u));
128   EXPECT_EQ(22, glslang::IntLog2(0x400000u));
129   EXPECT_EQ(23, glslang::IntLog2(0x800000u));
130   EXPECT_EQ(24, glslang::IntLog2(0x1000000u));
131   EXPECT_EQ(25, glslang::IntLog2(0x2000000u));
132   EXPECT_EQ(26, glslang::IntLog2(0x4000000u));
133   EXPECT_EQ(27, glslang::IntLog2(0x8000000u));
134   EXPECT_EQ(28, glslang::IntLog2(0x10000000u));
135   EXPECT_EQ(29, glslang::IntLog2(0x20000000u));
136   EXPECT_EQ(30, glslang::IntLog2(0x40000000u));
137   EXPECT_EQ(31, glslang::IntLog2(0x80000000u));
138 }
139
140 TEST(IntLog2, Int64) {
141   EXPECT_EQ(0, glslang::IntLog2(int64_t(1)));
142   EXPECT_EQ(1, glslang::IntLog2(int64_t(2)));
143   EXPECT_EQ(2, glslang::IntLog2(int64_t(4)));
144   EXPECT_EQ(3, glslang::IntLog2(int64_t(8)));
145   EXPECT_EQ(30, glslang::IntLog2(int64_t(0x40000000u)));
146   EXPECT_EQ(31, glslang::IntLog2(int64_t(0x80000000u)));
147   EXPECT_EQ(32, glslang::IntLog2(int64_t(0x10000) * int64_t(0x10000)));
148   EXPECT_EQ(48, glslang::IntLog2(int64_t(0x10000) * int64_t(0x10000) * int64_t(0x10000)));
149   EXPECT_EQ(62, glslang::IntLog2(int64_t(0x10000) * int64_t(0x10000) * int64_t(0x10000) * int64_t(0x4000)));
150 }
151
152 TEST(IntLog2, Uint64) {
153   EXPECT_EQ(0, glslang::IntLog2(uint64_t(1)));
154   EXPECT_EQ(1, glslang::IntLog2(uint64_t(2)));
155   EXPECT_EQ(2, glslang::IntLog2(uint64_t(4)));
156   EXPECT_EQ(3, glslang::IntLog2(uint64_t(8)));
157   EXPECT_EQ(30, glslang::IntLog2(uint64_t(0x40000000u)));
158   EXPECT_EQ(31, glslang::IntLog2(uint64_t(0x80000000u)));
159   EXPECT_EQ(32, glslang::IntLog2(uint64_t(0x10000) * uint64_t(0x10000)));
160   EXPECT_EQ(48, glslang::IntLog2(uint64_t(0x10000) * uint64_t(0x10000) * uint64_t(0x10000)));
161   EXPECT_EQ(62, glslang::IntLog2(uint64_t(0x10000) * uint64_t(0x10000) * uint64_t(0x10000) * uint64_t(0x4000)));
162   EXPECT_EQ(63, glslang::IntLog2(uint64_t(0x10000) * uint64_t(0x10000) * uint64_t(0x10000) * uint64_t(0x8000)));
163 }
164
165 }  // anonymous namespace