1*3828e951SShawn McCarney /**
2*3828e951SShawn McCarney  * Copyright © 2024 IBM Corporation
3*3828e951SShawn McCarney  *
4*3828e951SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5*3828e951SShawn McCarney  * you may not use this file except in compliance with the License.
6*3828e951SShawn McCarney  * You may obtain a copy of the License at
7*3828e951SShawn McCarney  *
8*3828e951SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9*3828e951SShawn McCarney  *
10*3828e951SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11*3828e951SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12*3828e951SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3828e951SShawn McCarney  * See the License for the specific language governing permissions and
14*3828e951SShawn McCarney  * limitations under the License.
15*3828e951SShawn McCarney  */
16*3828e951SShawn McCarney 
17*3828e951SShawn McCarney #include "format_utils.hpp"
18*3828e951SShawn McCarney 
19*3828e951SShawn McCarney #include <array>
20*3828e951SShawn McCarney #include <span>
21*3828e951SShawn McCarney #include <string>
22*3828e951SShawn McCarney #include <vector>
23*3828e951SShawn McCarney 
24*3828e951SShawn McCarney #include <gtest/gtest.h>
25*3828e951SShawn McCarney 
26*3828e951SShawn McCarney using namespace phosphor::power::format_utils;
27*3828e951SShawn McCarney 
TEST(FormatUtilsTests,toString)28*3828e951SShawn McCarney TEST(FormatUtilsTests, toString)
29*3828e951SShawn McCarney {
30*3828e951SShawn McCarney     // Test with spans from integer vector
31*3828e951SShawn McCarney     {
32*3828e951SShawn McCarney         std::vector<int> vec{1, 3, 5, 7, 9, 11};
33*3828e951SShawn McCarney         std::span<int> spn{vec};
34*3828e951SShawn McCarney         EXPECT_EQ(toString(spn), "[1, 3, 5, 7, 9, 11]");
35*3828e951SShawn McCarney         EXPECT_EQ(toString(spn.subspan(0, 4)), "[1, 3, 5, 7]");
36*3828e951SShawn McCarney         EXPECT_EQ(toString(spn.subspan(3, 3)), "[7, 9, 11]");
37*3828e951SShawn McCarney         EXPECT_EQ(toString(spn.subspan(5, 1)), "[11]");
38*3828e951SShawn McCarney         EXPECT_EQ(toString(spn.subspan(0, 0)), "[]");
39*3828e951SShawn McCarney     }
40*3828e951SShawn McCarney 
41*3828e951SShawn McCarney     // Test with spans from double vector
42*3828e951SShawn McCarney     {
43*3828e951SShawn McCarney         std::vector<double> vec{2.1, -3.9, 21.03};
44*3828e951SShawn McCarney         std::span<double> spn{vec};
45*3828e951SShawn McCarney         EXPECT_EQ(toString(spn), "[2.1, -3.9, 21.03]");
46*3828e951SShawn McCarney     }
47*3828e951SShawn McCarney 
48*3828e951SShawn McCarney     // Test with span from empty vector
49*3828e951SShawn McCarney     {
50*3828e951SShawn McCarney         std::vector<int> vec{};
51*3828e951SShawn McCarney         std::span<int> spn{vec};
52*3828e951SShawn McCarney         EXPECT_EQ(toString(spn), "[]");
53*3828e951SShawn McCarney     }
54*3828e951SShawn McCarney 
55*3828e951SShawn McCarney     // Test with spans from string array
56*3828e951SShawn McCarney     {
57*3828e951SShawn McCarney         std::array<std::string, 3> ary{"cow", "horse", "zebra"};
58*3828e951SShawn McCarney         std::span<std::string> spn{ary};
59*3828e951SShawn McCarney         EXPECT_EQ(toString(spn), "[cow, horse, zebra]");
60*3828e951SShawn McCarney         EXPECT_EQ(toString(spn.subspan(1, 2)), "[horse, zebra]");
61*3828e951SShawn McCarney     }
62*3828e951SShawn McCarney }
63