gem5 v24.0.0.0
Loading...
Searching...
No Matches
extensible.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2023 Google, Inc.
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 <gtest/gtest.h>
29
30#include <memory>
31
32#include "base/extensible.hh"
33
34using namespace gem5;
35
36namespace {
37
38class TestTarget : public Extensible<TestTarget>
39{
40};
41
42class IntegerExtension : public Extension<TestTarget, IntegerExtension>
43{
44 public:
45 explicit IntegerExtension(uint32_t data)
46 : data_(data) {}
47
48 std::unique_ptr<gem5::ExtensionBase> clone() const override
49 {
50 return std::unique_ptr<IntegerExtension>(new IntegerExtension(data_));
51 }
52
53 uint32_t getData() const { return data_; }
54
55 private:
56 uint32_t data_;
57};
58
59class BoolExtension : public Extension<TestTarget, BoolExtension>
60{
61 public:
62 explicit BoolExtension(bool data)
63 : data_(data) {}
64
65 std::unique_ptr<gem5::ExtensionBase> clone() const override
66 {
67 return std::unique_ptr<BoolExtension>(new BoolExtension(data_));
68 }
69
70 bool getData() const { return data_; }
71
72 private:
73 bool data_;
74};
75
76} // namespace
77
78TEST(ExtensibleTest, ExtensionID)
79{
80 std::shared_ptr<IntegerExtension> ext1(new IntegerExtension(0xabcd));
81 EXPECT_EQ(0, ext1->getExtensionID());
82
83 std::shared_ptr<BoolExtension> ext2(new BoolExtension(true));
84 EXPECT_EQ(1, ext2->getExtensionID());
85}
86
87TEST(ExtensibleTest, SetAndRemoveExtension)
88{
89 const uint32_t data = 0xbeef;
90 std::shared_ptr<IntegerExtension> ext(new IntegerExtension(data));
91 std::unique_ptr<TestTarget> target(new TestTarget);
92 target->setExtension(ext);
93 EXPECT_EQ(data, target->getExtension<IntegerExtension>()->getData());
94
95 target->removeExtension<IntegerExtension>();
96 EXPECT_EQ(nullptr, target->getExtension<IntegerExtension>());
97}
98
99TEST(ExtensibleTest, ReplaceExtension)
100{
101 const uint32_t data = 0xbeef;
102 std::shared_ptr<IntegerExtension> ext(new IntegerExtension(data));
103 std::unique_ptr<TestTarget> target(new TestTarget);
104 target->setExtension(ext);
105 EXPECT_EQ(data, target->getExtension<IntegerExtension>()->getData());
106
107 const uint32_t new_data = 0xa5a5;
108 std::shared_ptr<IntegerExtension> new_ext(new IntegerExtension(new_data));
109 target->setExtension(new_ext);
110 EXPECT_EQ(new_data, target->getExtension<IntegerExtension>()->getData());
111}
const char data[]
This is the extension for carrying additional information.
TEST(ExtensibleTest, ExtensionID)
Bitfield< 12 > ext
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36

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