blob: dd491f25a2e3323f6b643f054c88f248f57fceb5 [file] [log] [blame]
Guillaume Chatelet439d3712018-02-01 10:03:09 +01001// 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 Chateletdfdac6a2019-01-17 18:00:21 +010017#include "internal/unix_features_aggregator.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010018
19#include "gtest/gtest.h"
20
21namespace cpu_features {
22
23namespace {
24
25struct Features {
26 bool a = false;
27 bool b = false;
28 bool c = false;
29};
30
31DECLARE_SETTER(Features, a)
32DECLARE_SETTER(Features, b)
33DECLARE_SETTER(Features, c)
34
35class 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
43TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) {
44 Features features;
Arvid Gerstmannd9689912018-05-04 09:32:17 +020045 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""),
46 &features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010047 EXPECT_FALSE(features.a);
48 EXPECT_FALSE(features.b);
49 EXPECT_FALSE(features.c);
50}
51
52TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) {
53 Features features;
Arvid Gerstmannd9689912018-05-04 09:32:17 +020054 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"),
55 &features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010056 EXPECT_TRUE(features.a);
57 EXPECT_TRUE(features.b);
58 EXPECT_TRUE(features.c);
59}
60
61TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) {
62 Features features;
Arvid Gerstmannd9689912018-05-04 09:32:17 +020063 CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"),
64 &features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010065 EXPECT_TRUE(features.a);
66 EXPECT_FALSE(features.b);
67 EXPECT_FALSE(features.c);
68}
69
70TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) {
71 HardwareCapabilities capability;
72 capability.hwcaps = 0; // matches none
73 capability.hwcaps2 = 0; // matches none
74 Features features;
Arvid Gerstmannd9689912018-05-04 09:32:17 +020075 CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
76 &features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010077 EXPECT_FALSE(features.a);
78 EXPECT_FALSE(features.b);
79 EXPECT_FALSE(features.c);
80}
81
82TEST_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 Gerstmannd9689912018-05-04 09:32:17 +020087 CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
88 &features);
Guillaume Chatelet439d3712018-02-01 10:03:09 +010089 EXPECT_FALSE(features.a);
90 EXPECT_TRUE(features.b);
91 EXPECT_TRUE(features.c);
92}
93
94} // namespace
95} // namespace cpu_features