Daniel Carvalho has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/43593 )

Change subject: sim,tests: Add unit test for Globals
......................................................................

sim,tests: Add unit test for Globals

Add a unit test for sim/globals.

Change-Id: Ia47e750df4cbdb91a0ab0498819f4e3451d74830
Signed-off-by: Daniel R. Carvalho <oda...@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/43593
Reviewed-by: Bobby Bruce <bbr...@ucdavis.edu>
Maintainer: Bobby Bruce <bbr...@ucdavis.edu>
Tested-by: kokoro <noreply+kok...@google.com>
---
M src/sim/SConscript
A src/sim/globals.test.cc
2 files changed, 184 insertions(+), 0 deletions(-)

Approvals:
  Bobby Bruce: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass




diff --git a/src/sim/SConscript b/src/sim/SConscript
index 8bf5f5d..371eccd 100644
--- a/src/sim/SConscript
+++ b/src/sim/SConscript
@@ -92,6 +92,8 @@
 env.TagImplies('gem5 serialize', 'gem5 trace')

 GTest('byteswap.test', 'byteswap.test.cc', '../base/types.cc')
+GTest('globals.test', 'globals.test.cc', 'globals.cc',
+    with_tag('gem5 serialize'))
 GTest('guest_abi.test', 'guest_abi.test.cc')
 GTest('port.test', 'port.test.cc', 'port.cc')
 GTest('proxy_ptr.test', 'proxy_ptr.test.cc')
diff --git a/src/sim/globals.test.cc b/src/sim/globals.test.cc
new file mode 100644
index 0000000..8900c19
--- /dev/null
+++ b/src/sim/globals.test.cc
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2022 Daniel R. Carvalho
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <gmock/gmock.h>
+#include <gtest/gtest-spi.h>
+#include <gtest/gtest.h>
+
+#include <string>
+
+#include "base/gtest/cur_tick_fake.hh"
+#include "base/gtest/logging.hh"
+#include "base/gtest/serialization_fixture.hh"
+#include "sim/globals.hh"
+
+// The version tags are declared as extern
+namespace gem5
+{
+std::set<std::string> version_tags;
+} // namespace gem5
+
+using namespace gem5;
+
+// Use the tick handled to manipulate the current tick
+GTestTickHandler tickHandler;
+
+using GlobalsSerializationFixture = SerializationFixture;
+using GlobalsSerializationFixtureDeathTest = GlobalsSerializationFixture;
+
+/** Test serialization. */
+TEST_F(GlobalsSerializationFixture, Serialization)
+{
+    Globals globals;
+    tickHandler.setCurTick(1234);
+ version_tags = { "first-tag", "second-tag", "third-tag", "fourth-tag" };
+
+    // Serialization
+    std::ofstream cp(getCptPath());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+    globals.serialize(cp);
+
+    // The checkpoint must be flushed, otherwise the file may not be up-
+    // to-date and the assertions below will fail
+    cp.close();
+
+    // Verify the output
+    std::ifstream is(getCptPath());
+    assert(is.good());
+    std::string str = std::string(std::istreambuf_iterator<char>(is),
+        std::istreambuf_iterator<char>());
+    ASSERT_THAT(str, ::testing::StrEq("\n[Section1]\ncurTick=1234\n"
+        "version_tags=first-tag fourth-tag second-tag third-tag\n"));
+}
+
+/** Test unserialization. */
+TEST_F(GlobalsSerializationFixture, Unserialization)
+{
+    version_tags = { "first-tag-un", "second-tag-un", "third-tag-un",
+        "fourth-tag-un" };
+    simulateSerialization("\n[Section1]\ncurTick=1111\nversion_tags="
+        "first-tag-un second-tag-un third-tag-un fourth-tag-un\n");
+
+    Globals globals;
+    CheckpointIn cp(getDirName());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+
+    gtestLogOutput.str("");
+    globals.unserialize(cp);
+    ASSERT_THAT(gtestLogOutput.str(), ::testing::StrEq(""));
+    ASSERT_EQ(globals.unserializedCurTick, 1111);
+}
+
+/**
+ * Test that unserialization fails when there are no version tags in the
+ * checkpoint.
+ */
+TEST_F(GlobalsSerializationFixture, UnserializationCptNoVersionTags)
+{
+    version_tags = {};
+    simulateSerialization("\n[Section1]\ncurTick=2222\n");
+
+    // Unserialization
+    Globals globals;
+    CheckpointIn cp(getDirName());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+
+    gtestLogOutput.str("");
+    globals.unserialize(cp);
+    ASSERT_THAT(gtestLogOutput.str(),
+        ::testing::HasSubstr("Checkpoint uses an old versioning scheme."));
+    ASSERT_EQ(globals.unserializedCurTick, 2222);
+}
+
+/** Test that a warning is thrown when the cpt misses any of gem5's tags. */
+TEST_F(GlobalsSerializationFixture, UnserializationCptMissingVersionTags)
+{
+    version_tags = { "first-tag-un", "second-tag-un", "third-tag-un",
+        "fourth-tag-un" };
+    simulateSerialization("\n[Section1]\ncurTick=3333\n"
+        "version_tags=second-tag-un fourth-tag-un\n");
+
+    Globals globals;
+    CheckpointIn cp(getDirName());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+
+    gtestLogOutput.str("");
+    globals.unserialize(cp);
+    ASSERT_THAT(gtestLogOutput.str(), ::testing::HasSubstr(
+        "warn:   first-tag-un\nwarn:   third-tag-un\n"));
+    ASSERT_EQ(globals.unserializedCurTick, 3333);
+}
+
+/** Test that a warning is thrown when gem5 misses any of the cpt's tags. */
+TEST_F(GlobalsSerializationFixture, UnserializationGem5MissingVersionTags)
+{
+    version_tags = { "first-tag-un", "second-tag-un", "third-tag-un" };
+    simulateSerialization("\n[Section1]\ncurTick=4444\nversion_tags="
+        "first-tag-un second-tag-un third-tag-un fourth-tag-un\n");
+
+    Globals globals;
+    CheckpointIn cp(getDirName());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+
+    gtestLogOutput.str("");
+    globals.unserialize(cp);
+    ASSERT_THAT(gtestLogOutput.str(),
+        ::testing::HasSubstr("warn:   fourth-tag-un\n"));
+    ASSERT_EQ(globals.unserializedCurTick, 4444);
+}
+
+/**
+ * Test that unserialization fails when there are is no cur tick in the
+ * checkpoint.
+ */
+TEST_F(GlobalsSerializationFixtureDeathTest, UnserializationCptNoCurTick)
+{
+    simulateSerialization("\n[Section1]\n");
+
+    Globals globals;
+    CheckpointIn cp(getDirName());
+    Serializable::ScopedCheckpointSection scs(cp, "Section1");
+    ASSERT_ANY_THROW(globals.unserialize(cp));
+}

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/43593
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ia47e750df4cbdb91a0ab0498819f4e3451d74830
Gerrit-Change-Number: 43593
Gerrit-PatchSet: 15
Gerrit-Owner: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Bobby Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Jason Lowe-Power <ja...@lowepower.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-CC: Giacomo Travaglini <giacomo.travagl...@arm.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to