gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
serialization_fixture.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 Daniel R. Carvalho
3  * 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
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include <cassert>
32 #include <cerrno>
33 #include <fstream>
34 #include <string>
35 
36 #include "base/compiler.hh"
37 #include "sim/serialize.hh"
38 
39 namespace gem5
40 {
41 
52 class SerializationFixture : public ::testing::Test
53 {
54  private:
59  static unsigned dirNumber;
60 
62  std::string dirName;
63 
64  public:
65  using ::testing::Test::Test;
66 
68  static std::string
70  {
71  return "/tmp/temp_dir_test" + std::to_string(dirNumber++) + "/";
72  }
73 
75  std::string getDirName() const { return dirName; }
76 
78  std::string
79  getCptPath() const
80  {
81  return getDirName() + std::string(CheckpointIn::baseFilename);
82  }
83 
89  void
90  simulateSerialization(std::string contents) const
91  {
92  std::ofstream cp(getCptPath());
93  cp << contents;
94  cp.close();
95  }
96 
97  void
98  SetUp() override
99  {
100  // Create the directory
102  [[maybe_unused]] int success = mkdir(dirName.c_str(), 0775);
103  assert(!(success == -1 && errno != EEXIST));
104  }
105 
106  void
107  TearDown() override
108  {
109  // There may be a cpt file inside, so try to remove it; otherwise,
110  // rmdir does not work
111  std::remove(getCptPath().c_str());
112  // Remove the directory we created on SetUp
113  [[maybe_unused]] int success = rmdir(dirName.c_str());
114  assert(success == 0);
115  }
116 };
118 
119 } // anonymous namespace
gem5::SerializationFixture::getDirName
std::string getDirName() const
Get the name of the directory we have created on SetUp.
Definition: serialization_fixture.hh:75
serialize.hh
gem5::SerializationFixture::dirNumber
static unsigned dirNumber
Temporary directory names are generated based on this number, which is updated every time the generat...
Definition: serialization_fixture.hh:59
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::SerializationFixture::dirName
std::string dirName
The name of the temporary directory.
Definition: serialization_fixture.hh:62
compiler.hh
gem5::SerializationFixture
Fixture class that handles temporary directory creation.
Definition: serialization_fixture.hh:52
gem5::X86ISA::contents
Bitfield< 2, 1 > contents
Definition: syscalls.hh:50
gem5::SerializationFixture::generateTempDirName
static std::string generateTempDirName()
Generate a temporary directory name.
Definition: serialization_fixture.hh:69
gem5::SerializationFixture::TearDown
void TearDown() override
Definition: serialization_fixture.hh:107
gem5::SerializationFixture::SetUp
void SetUp() override
Definition: serialization_fixture.hh:98
gem5::SerializationFixture::simulateSerialization
void simulateSerialization(std::string contents) const
Create a cpt file with the contents specified by the string.
Definition: serialization_fixture.hh:90
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::CheckpointIn::baseFilename
static const char * baseFilename
Definition: serialize.hh:132
gem5::SerializationFixture::getCptPath
std::string getCptPath() const
Get the path to the checkpoint file.
Definition: serialization_fixture.hh:79

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17