Marin Shalamanov | e8a663d | 2020-11-24 17:48:00 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Fps.h" |
| 18 | |
| 19 | #include <gmock/gmock.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | TEST(FpsTest, construct) { |
| 25 | Fps fpsDefault; |
| 26 | EXPECT_FALSE(fpsDefault.isValid()); |
| 27 | |
| 28 | Fps fps1(60.0f); |
| 29 | EXPECT_TRUE(fps1.isValid()); |
| 30 | Fps fps2 = Fps::fromPeriodNsecs(static_cast<nsecs_t>(1e9f / 60.0f)); |
| 31 | EXPECT_TRUE(fps2.isValid()); |
| 32 | EXPECT_TRUE(fps1.equalsWithMargin(fps2)); |
| 33 | } |
| 34 | |
| 35 | TEST(FpsTest, compare) { |
| 36 | constexpr float kEpsilon = 1e-4f; |
| 37 | const Fps::EqualsInBuckets equalsInBuckets; |
| 38 | const Fps::EqualsWithMargin equalsWithMargin; |
| 39 | |
| 40 | EXPECT_TRUE(Fps(60.0f).equalsWithMargin(Fps(60.f))); |
| 41 | EXPECT_TRUE(Fps(60.0f).equalsWithMargin(Fps(60.f - kEpsilon))); |
| 42 | EXPECT_TRUE(Fps(60.0f).equalsWithMargin(Fps(60.f + kEpsilon))); |
| 43 | |
| 44 | EXPECT_TRUE(equalsInBuckets(Fps(60.0f), Fps(60.0f))); |
| 45 | EXPECT_TRUE(equalsInBuckets(Fps(60.0f), Fps(60.0f - kEpsilon))); |
| 46 | EXPECT_TRUE(equalsInBuckets(Fps(60.0f), Fps(60.0f + kEpsilon))); |
| 47 | |
| 48 | EXPECT_TRUE(equalsWithMargin(Fps(60.0f), Fps(60.0f))); |
| 49 | EXPECT_TRUE(equalsWithMargin(Fps(60.0f), Fps(60.0f - kEpsilon))); |
| 50 | EXPECT_TRUE(equalsWithMargin(Fps(60.0f), Fps(60.0f + kEpsilon))); |
| 51 | |
| 52 | EXPECT_TRUE(Fps(60.0f).lessThanOrEqualWithMargin(Fps(60.f + kEpsilon))); |
| 53 | EXPECT_TRUE(Fps(60.0f).lessThanOrEqualWithMargin(Fps(60.f))); |
| 54 | EXPECT_TRUE(Fps(60.0f).lessThanOrEqualWithMargin(Fps(60.f - kEpsilon))); |
| 55 | |
| 56 | EXPECT_TRUE(Fps(60.0f).greaterThanOrEqualWithMargin(Fps(60.f + kEpsilon))); |
| 57 | EXPECT_TRUE(Fps(60.0f).greaterThanOrEqualWithMargin(Fps(60.f))); |
| 58 | EXPECT_TRUE(Fps(60.0f).greaterThanOrEqualWithMargin(Fps(60.f - kEpsilon))); |
| 59 | |
| 60 | // Fps with difference of 1 should be different |
| 61 | EXPECT_FALSE(Fps(60.0f).equalsWithMargin(Fps(61.f))); |
| 62 | EXPECT_TRUE(Fps(60.0f).lessThanWithMargin(Fps(61.f))); |
| 63 | EXPECT_TRUE(Fps(60.0f).greaterThanWithMargin(Fps(59.f))); |
| 64 | |
| 65 | // These are common refresh rates which should be different. |
| 66 | EXPECT_FALSE(Fps(60.0f).equalsWithMargin(Fps(59.94f))); |
| 67 | EXPECT_TRUE(Fps(60.0f).greaterThanWithMargin(Fps(59.94f))); |
| 68 | EXPECT_FALSE(equalsInBuckets(Fps(60.0f), Fps(59.94f))); |
| 69 | EXPECT_FALSE(equalsWithMargin(Fps(60.0f), Fps(59.94f))); |
| 70 | EXPECT_NE(std::hash<Fps>()(Fps(60.0f)), std::hash<Fps>()(Fps(59.94f))); |
| 71 | |
| 72 | EXPECT_FALSE(Fps(30.0f).equalsWithMargin(Fps(29.97f))); |
| 73 | EXPECT_TRUE(Fps(30.0f).greaterThanWithMargin(Fps(29.97f))); |
| 74 | EXPECT_FALSE(equalsInBuckets(Fps(30.0f), Fps(29.97f))); |
| 75 | EXPECT_FALSE(equalsWithMargin(Fps(30.0f), Fps(29.97f))); |
| 76 | EXPECT_NE(std::hash<Fps>()(Fps(30.0f)), std::hash<Fps>()(Fps(29.97f))); |
| 77 | } |
| 78 | |
| 79 | TEST(FpsTest, getIntValue) { |
| 80 | EXPECT_EQ(30, Fps(30.1f).getIntValue()); |
| 81 | EXPECT_EQ(31, Fps(30.9f).getIntValue()); |
| 82 | EXPECT_EQ(31, Fps(30.5f).getIntValue()); |
| 83 | } |
| 84 | |
| 85 | TEST(FpsTest, equalsInBucketsImpliesEqualHashes) { |
| 86 | constexpr float kStep = 1e-4f; |
| 87 | const Fps::EqualsInBuckets equals; |
| 88 | for (float fps = 30.0f; fps < 31.0f; fps += kStep) { |
| 89 | const Fps left(fps); |
| 90 | const Fps right(fps + kStep); |
| 91 | if (equals(left, right)) { |
| 92 | ASSERT_EQ(std::hash<Fps>()(left), std::hash<Fps>()(right)) |
| 93 | << "left= " << left << " right=" << right; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | } // namespace android |