1 /**
2 * Copyright © 2024 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "../utils.hpp"
17
18 #include <stdexcept>
19 #include <tuple>
20
21 #include <gtest/gtest.h>
22
23 using namespace utils;
24
TEST(TestUtils,getDeviceName)25 TEST(TestUtils, getDeviceName)
26 {
27 auto ret = getDeviceName("");
28 EXPECT_TRUE(ret.empty());
29
30 ret = getDeviceName("/sys/bus/i2c/devices/3-0069");
31 EXPECT_EQ("3-0069", ret);
32
33 ret = getDeviceName("/sys/bus/i2c/devices/3-0069/");
34 EXPECT_EQ("3-0069", ret);
35 }
36
TEST(TestUtils,parseDeviceName)37 TEST(TestUtils, parseDeviceName)
38 {
39 auto [id, addr] = parseDeviceName("3-0068");
40 EXPECT_EQ(3, id);
41 EXPECT_EQ(0x68, addr);
42
43 std::tie(id, addr) = parseDeviceName("11-0069");
44 EXPECT_EQ(11, id);
45 EXPECT_EQ(0x69, addr);
46
47 EXPECT_THROW(parseDeviceName("no-number"), std::invalid_argument);
48
49 EXPECT_DEATH(parseDeviceName("invalid"), "");
50 }
51