gem5 v24.0.0.0
Loading...
Searching...
No Matches
globals.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2022 Daniel R. Carvalho
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gmock/gmock.h>
29#include <gtest/gtest-spi.h>
30#include <gtest/gtest.h>
31
32#include <string>
33
35#include "base/gtest/logging.hh"
37#include "sim/globals.hh"
38
39// The version tags are declared as extern
40namespace gem5
41{
42std::set<std::string> version_tags;
43} // namespace gem5
44
45using namespace gem5;
46
47// Use the tick handled to manipulate the current tick
49
52
55{
56 Globals globals;
58 version_tags = { "first-tag", "second-tag", "third-tag", "fourth-tag" };
59
60 // Serialization
61 std::ofstream cp(getCptPath());
63 globals.serialize(cp);
64
65 // The checkpoint must be flushed, otherwise the file may not be up-
66 // to-date and the assertions below will fail
67 cp.close();
68
69 // Verify the output
70 std::ifstream is(getCptPath());
71 assert(is.good());
72 std::string str = std::string(std::istreambuf_iterator<char>(is),
73 std::istreambuf_iterator<char>());
74 ASSERT_THAT(str, ::testing::StrEq("\n[Section1]\ncurTick=1234\n"
75 "version_tags=first-tag fourth-tag second-tag third-tag\n"));
76}
77
80{
81 version_tags = { "first-tag-un", "second-tag-un", "third-tag-un",
82 "fourth-tag-un" };
83 simulateSerialization("\n[Section1]\ncurTick=1111\nversion_tags="
84 "first-tag-un second-tag-un third-tag-un fourth-tag-un\n");
85
86 Globals globals;
87 CheckpointIn cp(getDirName());
89
90 gtestLogOutput.str("");
91 globals.unserialize(cp);
92 ASSERT_THAT(gtestLogOutput.str(), ::testing::StrEq(""));
93 ASSERT_EQ(globals.unserializedCurTick, 1111);
94}
95
100TEST_F(GlobalsSerializationFixture, UnserializationCptNoVersionTags)
101{
102 version_tags = {};
103 simulateSerialization("\n[Section1]\ncurTick=2222\n");
104
105 // Unserialization
106 Globals globals;
107 CheckpointIn cp(getDirName());
109
110 gtestLogOutput.str("");
111 globals.unserialize(cp);
112 ASSERT_THAT(gtestLogOutput.str(),
113 ::testing::HasSubstr("Checkpoint uses an old versioning scheme."));
114 ASSERT_EQ(globals.unserializedCurTick, 2222);
115}
116
118TEST_F(GlobalsSerializationFixture, UnserializationCptMissingVersionTags)
119{
120 version_tags = { "first-tag-un", "second-tag-un", "third-tag-un",
121 "fourth-tag-un" };
122 simulateSerialization("\n[Section1]\ncurTick=3333\n"
123 "version_tags=second-tag-un fourth-tag-un\n");
124
125 Globals globals;
126 CheckpointIn cp(getDirName());
128
129 gtestLogOutput.str("");
130 globals.unserialize(cp);
131 ASSERT_THAT(gtestLogOutput.str(), ::testing::HasSubstr(
132 "warn: first-tag-un\nwarn: third-tag-un\n"));
133 ASSERT_EQ(globals.unserializedCurTick, 3333);
134}
135
137TEST_F(GlobalsSerializationFixture, UnserializationGem5MissingVersionTags)
138{
139 version_tags = { "first-tag-un", "second-tag-un", "third-tag-un" };
140 simulateSerialization("\n[Section1]\ncurTick=4444\nversion_tags="
141 "first-tag-un second-tag-un third-tag-un fourth-tag-un\n");
142
143 Globals globals;
144 CheckpointIn cp(getDirName());
146
147 gtestLogOutput.str("");
148 globals.unserialize(cp);
149 ASSERT_THAT(gtestLogOutput.str(),
150 ::testing::HasSubstr("warn: fourth-tag-un\n"));
151 ASSERT_EQ(globals.unserializedCurTick, 4444);
152}
153
158TEST_F(GlobalsSerializationFixtureDeathTest, UnserializationCptNoCurTick)
159{
160 simulateSerialization("\n[Section1]\n");
161
162 Globals globals;
163 CheckpointIn cp(getDirName());
165 ASSERT_ANY_THROW(globals.unserialize(cp));
166}
void setCurTick(Tick tick)
Assign a value to the current simulation tick.
Container for serializing global variables (not associated with any serialized object).
Definition globals.hh:55
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition globals.cc:59
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition globals.cc:66
Tick unserializedCurTick
Definition globals.hh:63
Fixture class that handles temporary directory creation.
TEST_F(GlobalsSerializationFixture, Serialization)
Test serialization.
SerializationFixture GlobalsSerializationFixture
GTestTickHandler tickHandler
Bitfield< 24, 22 > is
Bitfield< 18 > scs
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::set< std::string > version_tags
The version tags for this build of the simulator, to be stored in the Globals section during serializ...
thread_local GTestLogOutput gtestLogOutput
Definition logging.cc:33

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