Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. |
| 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 <array> |
| 16 | |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 17 | #include "internal/unix_features_aggregator.h" |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 18 | |
| 19 | #include "gtest/gtest.h" |
| 20 | |
| 21 | namespace cpu_features { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | struct Features { |
| 26 | bool a = false; |
| 27 | bool b = false; |
| 28 | bool c = false; |
| 29 | }; |
| 30 | |
| 31 | DECLARE_SETTER(Features, a) |
| 32 | DECLARE_SETTER(Features, b) |
| 33 | DECLARE_SETTER(Features, c) |
| 34 | |
| 35 | class LinuxFeatureAggregatorTest : public testing::Test { |
| 36 | public: |
| 37 | const std::array<CapabilityConfig, 3> kConfigs = { |
| 38 | {{{0b0001, 0b0000}, "a", &set_a}, |
| 39 | {{0b0010, 0b0000}, "b", &set_b}, |
| 40 | {{0b0000, 0b1100}, "c", &set_c}}}; |
| 41 | }; |
| 42 | |
| 43 | TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) { |
| 44 | Features features; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 45 | CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""), |
| 46 | &features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 47 | EXPECT_FALSE(features.a); |
| 48 | EXPECT_FALSE(features.b); |
| 49 | EXPECT_FALSE(features.c); |
| 50 | } |
| 51 | |
| 52 | TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) { |
| 53 | Features features; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 54 | CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"), |
| 55 | &features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 56 | EXPECT_TRUE(features.a); |
| 57 | EXPECT_TRUE(features.b); |
| 58 | EXPECT_TRUE(features.c); |
| 59 | } |
| 60 | |
| 61 | TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) { |
| 62 | Features features; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 63 | CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"), |
| 64 | &features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 65 | EXPECT_TRUE(features.a); |
| 66 | EXPECT_FALSE(features.b); |
| 67 | EXPECT_FALSE(features.c); |
| 68 | } |
| 69 | |
| 70 | TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) { |
| 71 | HardwareCapabilities capability; |
| 72 | capability.hwcaps = 0; // matches none |
| 73 | capability.hwcaps2 = 0; // matches none |
| 74 | Features features; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 75 | CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, |
| 76 | &features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 77 | EXPECT_FALSE(features.a); |
| 78 | EXPECT_FALSE(features.b); |
| 79 | EXPECT_FALSE(features.c); |
| 80 | } |
| 81 | |
| 82 | TEST_F(LinuxFeatureAggregatorTest, FromHwcapsSet) { |
| 83 | HardwareCapabilities capability; |
| 84 | capability.hwcaps = 0b0010; // matches b but not a |
| 85 | capability.hwcaps2 = 0b1111; // matches c |
| 86 | Features features; |
Arvid Gerstmann | d968991 | 2018-05-04 09:32:17 +0200 | [diff] [blame] | 87 | CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability, |
| 88 | &features); |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 89 | EXPECT_FALSE(features.a); |
| 90 | EXPECT_TRUE(features.b); |
| 91 | EXPECT_TRUE(features.c); |
| 92 | } |
| 93 | |
| 94 | } // namespace |
| 95 | } // namespace cpu_features |