File: linux_features_aggregator_test.cc

package info (click to toggle)
anbox 0.0~git20190124-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 22,000 kB
  • sloc: cpp: 76,190; ansic: 7,434; sh: 1,350; xml: 818; java: 780; python: 390; makefile: 35; lisp: 7
file content (95 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>

#include "internal/linux_features_aggregator.h"

#include "gtest/gtest.h"

namespace cpu_features {

namespace {

struct Features {
  bool a = false;
  bool b = false;
  bool c = false;
};

DECLARE_SETTER(Features, a)
DECLARE_SETTER(Features, b)
DECLARE_SETTER(Features, c)

class LinuxFeatureAggregatorTest : public testing::Test {
 public:
  const std::array<CapabilityConfig, 3> kConfigs = {
      {{{0b0001, 0b0000}, "a", &set_a},
       {{0b0010, 0b0000}, "b", &set_b},
       {{0b0000, 0b1100}, "c", &set_c}}};
};

TEST_F(LinuxFeatureAggregatorTest, FromFlagsEmpty) {
  Features features;
  CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str(""),
                           &features);
  EXPECT_FALSE(features.a);
  EXPECT_FALSE(features.b);
  EXPECT_FALSE(features.c);
}

TEST_F(LinuxFeatureAggregatorTest, FromFlagsAllSet) {
  Features features;
  CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a c b"),
                           &features);
  EXPECT_TRUE(features.a);
  EXPECT_TRUE(features.b);
  EXPECT_TRUE(features.c);
}

TEST_F(LinuxFeatureAggregatorTest, FromFlagsOnlyA) {
  Features features;
  CpuFeatures_SetFromFlags(kConfigs.size(), kConfigs.data(), str("a"),
                           &features);
  EXPECT_TRUE(features.a);
  EXPECT_FALSE(features.b);
  EXPECT_FALSE(features.c);
}

TEST_F(LinuxFeatureAggregatorTest, FromHwcapsNone) {
  HardwareCapabilities capability;
  capability.hwcaps = 0;   // matches none
  capability.hwcaps2 = 0;  // matches none
  Features features;
  CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
                                 &features);
  EXPECT_FALSE(features.a);
  EXPECT_FALSE(features.b);
  EXPECT_FALSE(features.c);
}

TEST_F(LinuxFeatureAggregatorTest, FromHwcapsSet) {
  HardwareCapabilities capability;
  capability.hwcaps = 0b0010;   // matches b but not a
  capability.hwcaps2 = 0b1111;  // matches c
  Features features;
  CpuFeatures_OverrideFromHwCaps(kConfigs.size(), kConfigs.data(), capability,
                                 &features);
  EXPECT_FALSE(features.a);
  EXPECT_TRUE(features.b);
  EXPECT_TRUE(features.c);
}

}  // namespace
}  // namespace cpu_features