• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Tint 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 "src/utils/math.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace tint {
20 namespace utils {
21 namespace {
22 
TEST(MathTests,RoundUp)23 TEST(MathTests, RoundUp) {
24   EXPECT_EQ(RoundUp(1, 0), 0);
25   EXPECT_EQ(RoundUp(1, 1), 1);
26   EXPECT_EQ(RoundUp(1, 2), 2);
27 
28   EXPECT_EQ(RoundUp(1, 1), 1);
29   EXPECT_EQ(RoundUp(2, 1), 2);
30   EXPECT_EQ(RoundUp(3, 1), 3);
31   EXPECT_EQ(RoundUp(4, 1), 4);
32 
33   EXPECT_EQ(RoundUp(1, 2), 2);
34   EXPECT_EQ(RoundUp(2, 2), 2);
35   EXPECT_EQ(RoundUp(3, 2), 3);
36   EXPECT_EQ(RoundUp(4, 2), 4);
37 
38   EXPECT_EQ(RoundUp(1, 3), 3);
39   EXPECT_EQ(RoundUp(2, 3), 4);
40   EXPECT_EQ(RoundUp(3, 3), 3);
41   EXPECT_EQ(RoundUp(4, 3), 4);
42 
43   EXPECT_EQ(RoundUp(1, 4), 4);
44   EXPECT_EQ(RoundUp(2, 4), 4);
45   EXPECT_EQ(RoundUp(3, 4), 6);
46   EXPECT_EQ(RoundUp(4, 4), 4);
47 }
48 
TEST(MathTests,IsPowerOfTwo)49 TEST(MathTests, IsPowerOfTwo) {
50   EXPECT_EQ(IsPowerOfTwo(1), true);
51   EXPECT_EQ(IsPowerOfTwo(2), true);
52   EXPECT_EQ(IsPowerOfTwo(3), false);
53   EXPECT_EQ(IsPowerOfTwo(4), true);
54   EXPECT_EQ(IsPowerOfTwo(5), false);
55   EXPECT_EQ(IsPowerOfTwo(6), false);
56   EXPECT_EQ(IsPowerOfTwo(7), false);
57   EXPECT_EQ(IsPowerOfTwo(8), true);
58   EXPECT_EQ(IsPowerOfTwo(9), false);
59 }
60 
TEST(MathTests,MaxAlignOf)61 TEST(MathTests, MaxAlignOf) {
62   EXPECT_EQ(MaxAlignOf(0u), 1u);
63   EXPECT_EQ(MaxAlignOf(1u), 1u);
64   EXPECT_EQ(MaxAlignOf(2u), 2u);
65   EXPECT_EQ(MaxAlignOf(3u), 1u);
66   EXPECT_EQ(MaxAlignOf(4u), 4u);
67   EXPECT_EQ(MaxAlignOf(5u), 1u);
68   EXPECT_EQ(MaxAlignOf(6u), 2u);
69   EXPECT_EQ(MaxAlignOf(7u), 1u);
70   EXPECT_EQ(MaxAlignOf(8u), 8u);
71   EXPECT_EQ(MaxAlignOf(9u), 1u);
72   EXPECT_EQ(MaxAlignOf(10u), 2u);
73   EXPECT_EQ(MaxAlignOf(11u), 1u);
74   EXPECT_EQ(MaxAlignOf(12u), 4u);
75   EXPECT_EQ(MaxAlignOf(13u), 1u);
76   EXPECT_EQ(MaxAlignOf(14u), 2u);
77   EXPECT_EQ(MaxAlignOf(15u), 1u);
78   EXPECT_EQ(MaxAlignOf(16u), 16u);
79 }
80 
81 }  // namespace
82 }  // namespace utils
83 }  // namespace tint
84