gem5  v22.1.0.0
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  M5_VAR_USED 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  M5_VAR_USED int success = rmdir(dirName.c_str());
114  assert(success == 0);
115  }
116 };
118 
119 } // anonymous namespace
static const char * baseFilename
Definition: serialize.hh:132
Fixture class that handles temporary directory creation.
std::string getDirName() const
Get the name of the directory we have created on SetUp.
std::string getCptPath() const
Get the path to the checkpoint file.
static std::string generateTempDirName()
Generate a temporary directory name.
static unsigned dirNumber
Temporary directory names are generated based on this number, which is updated every time the generat...
void simulateSerialization(std::string contents) const
Create a cpt file with the contents specified by the string.
std::string dirName
The name of the temporary directory.
#define M5_VAR_USED
Definition: compiler.hh:145
Bitfield< 2, 1 > contents
Definition: syscalls.hh:50
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1