Shawn McCarney | fe44e83 | 2024-04-18 16:03:49 -0500 | [diff] [blame] | 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 | |
| 17 | #include "format_utils.hpp" |
| 18 | |
| 19 | #include <array> |
| 20 | #include <span> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <gtest/gtest.h> |
| 25 | |
Shawn McCarney | 3828e95 | 2024-08-07 10:13:29 -0500 | [diff] [blame] | 26 | using namespace phosphor::power::format_utils; |
Shawn McCarney | fe44e83 | 2024-04-18 16:03:49 -0500 | [diff] [blame] | 27 | |
| 28 | TEST(FormatUtilsTests, toString) |
| 29 | { |
| 30 | // Test with spans from integer vector |
| 31 | { |
| 32 | std::vector<int> vec{1, 3, 5, 7, 9, 11}; |
| 33 | std::span<int> spn{vec}; |
| 34 | EXPECT_EQ(toString(spn), "[1, 3, 5, 7, 9, 11]"); |
| 35 | EXPECT_EQ(toString(spn.subspan(0, 4)), "[1, 3, 5, 7]"); |
| 36 | EXPECT_EQ(toString(spn.subspan(3, 3)), "[7, 9, 11]"); |
| 37 | EXPECT_EQ(toString(spn.subspan(5, 1)), "[11]"); |
| 38 | EXPECT_EQ(toString(spn.subspan(0, 0)), "[]"); |
| 39 | } |
| 40 | |
| 41 | // Test with spans from double vector |
| 42 | { |
| 43 | std::vector<double> vec{2.1, -3.9, 21.03}; |
| 44 | std::span<double> spn{vec}; |
| 45 | EXPECT_EQ(toString(spn), "[2.1, -3.9, 21.03]"); |
| 46 | } |
| 47 | |
| 48 | // Test with span from empty vector |
| 49 | { |
| 50 | std::vector<int> vec{}; |
| 51 | std::span<int> spn{vec}; |
| 52 | EXPECT_EQ(toString(spn), "[]"); |
| 53 | } |
| 54 | |
| 55 | // Test with spans from string array |
| 56 | { |
| 57 | std::array<std::string, 3> ary{"cow", "horse", "zebra"}; |
| 58 | std::span<std::string> spn{ary}; |
| 59 | EXPECT_EQ(toString(spn), "[cow, horse, zebra]"); |
| 60 | EXPECT_EQ(toString(spn.subspan(1, 2)), "[horse, zebra]"); |
| 61 | } |
| 62 | } |