blob: 61d59a6da513a299c4993d08fc1cbc19388e07d4 [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2019 IBM Corporation
3
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +08004#include "extensions/openpower-pels/json_utils.hpp"
Matt Spinlerb832aa52023-03-21 15:32:34 -05005#include "extensions/openpower-pels/paths.hpp"
6
7#include <nlohmann/json.hpp>
8
9#include <filesystem>
Matt Spinler8a09b982025-05-09 14:09:10 -050010#include <fstream>
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080011
12#include <gtest/gtest.h>
13
14using namespace openpower::pels;
15
16TEST(JsonUtilsTest, TrimEndTest)
17{
18 std::string testStr("Test string 1");
19 EXPECT_EQ(trimEnd(testStr), "Test string 1");
20 testStr = "Test string 2 ";
21 EXPECT_EQ(trimEnd(testStr), "Test string 2");
22 testStr = " Test string 3 ";
23 EXPECT_EQ(trimEnd(testStr), " Test string 3");
24}
25
26TEST(JsonUtilsTest, NumberToStringTest)
27{
28 size_t number = 123;
29 EXPECT_EQ(getNumberString("%d", number), "123");
30 EXPECT_EQ(getNumberString("%03X", number), "07B");
31 EXPECT_EQ(getNumberString("0x%X", number), "0x7B");
Patrick Williams23867bc2021-04-22 09:26:47 -050032 EXPECT_THROW(getNumberString("%123", number), std::invalid_argument);
Harisuddin Mohamed Isae2d1bf32020-02-06 17:32:38 +080033}
34
35TEST(JsonUtilsTest, JsonInsertTest)
36{
37 std::string json;
38 jsonInsert(json, "Key", "Value1", 1);
39 EXPECT_EQ(json, " \"Key\": \"Value1\",\n");
40 jsonInsert(json, "Keyxxxxxxxxxxxxxxxxxxxxxxxxxx", "Value2", 2);
41 EXPECT_EQ(json, " \"Key\": \"Value1\",\n"
42 " \"Keyxxxxxxxxxxxxxxxxxxxxxxxxxx\": \"Value2\",\n");
43}
Matt Spinlerb832aa52023-03-21 15:32:34 -050044
45TEST(JsonUtilsTest, GetComponentNameTest)
46{
47 const auto compIDs = R"(
48 {
49 "1000": "some comp",
50 "2222": "another comp"
51 })"_json;
52
53 auto dataPath = getPELReadOnlyDataPath();
54 std::ofstream file{dataPath / "O_component_ids.json"};
55 file << compIDs;
56 file.close();
57
58 // The component ID file exists
59 EXPECT_EQ(getComponentName(0x1000, 'O'), "some comp");
60 EXPECT_EQ(getComponentName(0x2222, 'O'), "another comp");
61 EXPECT_EQ(getComponentName(0x0001, 'O'), "0x0001");
62
63 // No component ID file
64 EXPECT_EQ(getComponentName(0x3456, 'B'), "0x3456");
65
66 // PHYP, uses characters if both bytes nonzero
67 EXPECT_EQ(getComponentName(0x4552, 'H'), "ER");
68 EXPECT_EQ(getComponentName(0x584D, 'H'), "XM");
69 EXPECT_EQ(getComponentName(0x5800, 'H'), "0x5800");
70 EXPECT_EQ(getComponentName(0x0058, 'H'), "0x0058");
71
72 std::filesystem::remove_all(dataPath);
73}