blob: feb285992467714b4b63ecf831c629113d4afe18 [file] [log] [blame]
Harvey Yang9260b662019-08-12 15:48:03 +08001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <gtest/gtest.h>
6
7#include "libmems/common_types.h"
8#include "libmems/test_fakes.h"
9
10namespace libmems {
11
12namespace {
13
Gwendal Grignoua1446472020-06-30 18:00:05 -070014constexpr char kFakeChannelName1[] = "fake_channel1";
15constexpr char kFakeChannelName2[] = "fake_channel2";
16
Harvey Yang9260b662019-08-12 15:48:03 +080017constexpr char kFakeDeviceName[] = "iio:device0";
18constexpr int kFakeDeviceId = 0;
19
20constexpr char kTrigger0Attr[] = "trigger0";
21constexpr char kTrigger1Attr[] = "trigger1";
22constexpr char kTrigger12Attr[] = "trigger12";
23
24constexpr char kDevice0Attr[] = "iio:device0";
25constexpr char kDevice1Attr[] = "iio:device1";
26constexpr char kDevice12Attr[] = "iio:device12";
27
28class IioDeviceTest : public fakes::FakeIioDevice, public ::testing::Test {
29 public:
30 static base::Optional<int> GetIdAfterPrefix(const char* id_str,
31 const char* prefix) {
32 return IioDevice::GetIdAfterPrefix(id_str, prefix);
33 }
34
35 IioDeviceTest()
36 : fakes::FakeIioDevice(nullptr, kFakeDeviceName, kFakeDeviceId),
37 ::testing::Test() {}
Gwendal Grignoua1446472020-06-30 18:00:05 -070038
39 protected:
40 void SetUp() override {
41 auto channel1 =
42 std::make_unique<fakes::FakeIioChannel>(kFakeChannelName1, false);
43 channel1_ = channel1.get();
44 AddChannel(std::move(channel1));
45
46 auto channel2 =
47 std::make_unique<fakes::FakeIioChannel>(kFakeChannelName2, false);
48 channel2_ = channel2.get();
49 AddChannel(std::move(channel2));
50 }
51
52 fakes::FakeIioChannel* channel1_;
53 fakes::FakeIioChannel* channel2_;
Harvey Yang9260b662019-08-12 15:48:03 +080054};
55
56TEST_F(IioDeviceTest, GetIdAfterPrefixTest) {
57 EXPECT_EQ(GetIdAfterPrefix(kTrigger0Attr, kTriggerIdPrefix), 0);
58 EXPECT_EQ(GetIdAfterPrefix(kTrigger1Attr, kTriggerIdPrefix), 1);
59 EXPECT_EQ(GetIdAfterPrefix(kTrigger12Attr, kTriggerIdPrefix), 12);
60
61 EXPECT_EQ(GetIdAfterPrefix(kDevice0Attr, kDeviceIdPrefix), 0);
62 EXPECT_EQ(GetIdAfterPrefix(kDevice1Attr, kDeviceIdPrefix), 1);
63 EXPECT_EQ(GetIdAfterPrefix(kDevice12Attr, kDeviceIdPrefix), 12);
64}
65
Gwendal Grignoua1446472020-06-30 18:00:05 -070066TEST_F(IioDeviceTest, GetAllChannels) {
67 auto channels = GetAllChannels();
68 EXPECT_EQ(channels.size(), 2);
69 EXPECT_EQ(channels[0], channel1_);
70 EXPECT_EQ(channels[1], channel2_);
71}
72
73TEST_F(IioDeviceTest, GetChannelByIndex) {
74 EXPECT_EQ(GetChannel(0), channel1_);
75 EXPECT_EQ(GetChannel(1), channel2_);
76}
77
78TEST_F(IioDeviceTest, GetChannelByName) {
79 EXPECT_EQ(GetChannel(kFakeChannelName1), channel1_);
80 EXPECT_EQ(GetChannel(kFakeChannelName2), channel2_);
81}
82
Harvey Yang98ff1b32020-10-28 14:45:35 +080083class IioDeviceTestOnMinMaxFrequencyWithParam
84 : public ::testing::TestWithParam<
85 std::tuple<std::string, bool, double, double>> {
86 protected:
87 void SetUp() override {
88 device_ = std::make_unique<libmems::fakes::FakeIioDevice>(
89 nullptr, kFakeDeviceName, kFakeDeviceId);
90
91 EXPECT_TRUE(device_->WriteStringAttribute(kSamplingFrequencyAvailable,
92 std::get<0>(GetParam())));
93
94 result_ = device_->GetMinMaxFrequency(&min_freq_, &max_freq_);
95 }
96
97 std::unique_ptr<libmems::fakes::FakeIioDevice> device_;
98 bool result_;
99 double min_freq_ = -1;
100 double max_freq_ = -1;
101};
102
103TEST_P(IioDeviceTestOnMinMaxFrequencyWithParam, ParseMinMaxFrequency) {
104 EXPECT_EQ(result_, std::get<1>(GetParam()));
105 if (!result_)
106 return;
107
108 EXPECT_EQ(min_freq_, std::get<2>(GetParam()));
109 EXPECT_EQ(max_freq_, std::get<3>(GetParam()));
110}
111
112INSTANTIATE_TEST_SUITE_P(
113 IioDeviceTestOnMinMaxFrequencyWithParamRun,
114 IioDeviceTestOnMinMaxFrequencyWithParam,
115 ::testing::Values(
116 std::make_tuple(" ", false, 0.0, 0.0),
117 std::make_tuple(" 0abc ", false, 0.0, 0.0),
118 std::make_tuple(" 0.0001 ", false, 0.0, 0.0),
119 std::make_tuple("0.5 ", true, 0.5, 0.5),
120 std::make_tuple(" 1000 ", true, 1000.0, 1000.0),
121 std::make_tuple("1.0 100.0 ", true, 1.0, 100.0),
122 std::make_tuple("1.0 10.0 100.0 ", true, 1.0, 100.0),
123 std::make_tuple("1.0 a b c 100.0 ", true, 1.0, 100.0),
124 std::make_tuple("0.0 a b c 100.0 ", false, 0.0, 0.0),
125 std::make_tuple("0.0 1.0 100.0 ", true, 1.0, 100.0),
126 std::make_tuple("0.0 2.0 a b c 100.0 ", true, 2.0, 100.0)));
127
Harvey Yang9260b662019-08-12 15:48:03 +0800128} // namespace
129
130} // namespace libmems