gem5 v24.0.0.0
Loading...
Searching...
No Matches
ostream_helpers.test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Arteris, Inc. and its applicable licensors and
3 * affiliates. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer; redistributions in binary
9 * form must reproduce the above copyright notice, this list of conditions and
10 * the following disclaimer in the documentation and/or other materials
11 * provided with the distribution; neither the name of the copyright holders
12 * nor the names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gtest/gtest.h>
29
30#include <map>
31#include <sstream>
32#include <string_view>
33#include <vector>
34
36
37
38TEST(OstreamHelpers, pair) {
39 using gem5::stl_helpers::operator<<;
40 auto p = std::make_pair(1, 2);
41 std::ostringstream os;
42 os << p;
43 EXPECT_EQ(os.str(), "(1, 2)");
44}
45
46TEST(OstreamHelpers, tuple) {
47 using gem5::stl_helpers::operator<<;
48 auto t = std::make_tuple(true,
49 std::make_pair("Hello", std::string_view("World")), '!');
50 std::ostringstream os;
51 os << t;
52 EXPECT_EQ(os.str(), "(1, (Hello, World), !)");
53}
54
55TEST(OstreamHelpers, vector) {
56 using gem5::stl_helpers::operator<<;
57 auto v = std::vector<const char*>{"abc", "defg", "hijklm", "\n"};
58 std::ostringstream os;
59 os << v;
60 EXPECT_EQ(os.str(), "[ abc, defg, hijklm, \n, ]");
61}
62
63TEST(OstreamHelpers, map) {
64 using gem5::stl_helpers::operator<<;
65 auto m = std::map<char, int>{{'a', 0}, {'b', 1}, {'c', 2}, {'d', 3}};
66 std::ostringstream os;
67 os << m;
68 EXPECT_EQ(os.str(), "[ (a, 0), (b, 1), (c, 2), (d, 3), ]");
69}
70
71TEST(OstreamHelpers, optional) {
72 using gem5::stl_helpers::operator<<;
73 auto m = std::make_optional<int>(42);
74 std::ostringstream os;
75 os << m;
76 EXPECT_EQ(os.str(), "42");
77 os.str("");
78 m.reset();
79 os << m;
80 EXPECT_EQ(os.str(), "(-)");
81}
82
83TEST(OstreamHelpers, printer) {
84 std::string hello = "Hello";
85 std::ostringstream os;
86 os << hello;
87 EXPECT_EQ(os.str(), hello);
88
89 std::ostringstream os2;
90 os2 << gem5::stl_helpers::Printer(hello);
91 EXPECT_EQ(os2.str(), "[ H, e, l, l, o, ]");
92}
93
94
95TEST(OstreamHelpers, pointers) {
96 auto helped_representation = [](const auto& val) {
97 std::ostringstream os;
99 return os.str();
100 };
101 auto expected_representation = [&](const auto& ptr) {
102 using gem5::stl_helpers::operator<<;
103 std::ostringstream os;
104 auto* raw_ptr = &*ptr;
105 os << '(' << raw_ptr << ": " << *ptr << ')';
106 return os.str();
107 };
108
109 int x = 42;
110 auto* ptr = &x;
111 EXPECT_EQ(helped_representation(ptr), expected_representation(ptr));
112
113 auto uptr = std::make_unique<std::string>("Hello, World!");
114 EXPECT_EQ(helped_representation(uptr), expected_representation(uptr));
115
116 auto sptr = std::make_shared<std::optional<bool>>();
117 EXPECT_EQ(helped_representation(sptr), expected_representation(sptr));
118}
STL vector class.
Definition stl.hh:37
TEST(OstreamHelpers, pair)

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0