1 // Copyright 2017 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "internal/linux_features_aggregator.h"
19 #include "gtest/gtest.h"
21 namespace cpu_features {
31 DECLARE_SETTER(Features, a)
32 DECLARE_SETTER(Features, b)
33 DECLARE_SETTER(Features, c)
35 class LinuxFeatureAggregatorTest : public testing::Test {
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}}};
43 TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) {
45 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""),
47 EXPECT_FALSE(features.a);
48 EXPECT_FALSE(features.b);
49 EXPECT_FALSE(features.c);
52 TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) {
54 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"),
56 EXPECT_TRUE(features.a);
57 EXPECT_TRUE(features.b);
58 EXPECT_TRUE(features.c);
61 TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) {
63 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"),
65 EXPECT_TRUE(features.a);
66 EXPECT_FALSE(features.b);
67 EXPECT_FALSE(features.c);
70 TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) {
71 HardwareCapabilities capability;
72 capability.hwcaps = 0; // matches none
73 capability.hwcaps2 = 0; // matches none
75 CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
77 EXPECT_FALSE(features.a);
78 EXPECT_FALSE(features.b);
79 EXPECT_FALSE(features.c);
82 TEST_F(LinuxFeatureAggregatorTest, FromHwcapsSet) {
83 HardwareCapabilities capability;
84 capability.hwcaps = 0b0010; // matches b but not a
85 capability.hwcaps2 = 0b1111; // matches c
87 CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
89 EXPECT_FALSE(features.a);
90 EXPECT_TRUE(features.b);
91 EXPECT_TRUE(features.c);
95 } // namespace cpu_features