File: reference_tracked_object_tests.cpp

package info (click to toggle)
intel-compute-runtime 22.43.24595.41-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,740 kB
  • sloc: cpp: 631,142; lisp: 3,515; sh: 470; makefile: 76; python: 21
file content (249 lines) | stat: -rw-r--r-- 8,092 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (C) 2018-2022 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/utilities/reference_tracked_object.h"

#include "gtest/gtest.h"

#include <thread>

namespace NEO {
TEST(RefCounter, WhenIncrementingAndDecrementingThenCounterIsCorrect) {
    RefCounter<> rc;
    ASSERT_EQ(0, rc.peek());
    int max = 7;
    for (int i = 0; i < max; ++i) {
        rc.inc();
    }
    ASSERT_EQ(max, rc.peek());
    for (int i = 0; i < max - 1; ++i) {
        rc.dec();
    }
    ASSERT_EQ(1, rc.peek());
    rc.dec();
    ASSERT_EQ(0, rc.peek());
}

TEST(RefCounter, givenReferenceTrackedObjectWhenDecAndReturnCurrentIsCalledThenMinusOneIsReturned) {
    RefCounter<> rc;
    EXPECT_EQ(-1, rc.decAndReturnCurrent());
}

TEST(UniquePtrIfUnused, GivenDefaultWhenCreatedThenReturnNullptr) {
    unique_ptr_if_unused<int> uptr;
    ASSERT_EQ(nullptr, uptr.get());
    ASSERT_FALSE(uptr.isUnused());
}

TEST(UniquePtrIfUnused, WhenDeletingThenDeletionIsDeferred) {
    struct PrimitivObject {
        PrimitivObject(int v, bool *wasDeletedFlag)
            : memb(v), wasDeletedFlag(wasDeletedFlag) {
            if (wasDeletedFlag != nullptr) {
                *wasDeletedFlag = false;
            }
        }
        ~PrimitivObject() {
            if (wasDeletedFlag != nullptr) {
                *wasDeletedFlag = true;
            }
        }
        int memb;
        bool *wasDeletedFlag;
    };

    {
        bool wasDeleted = false;
        auto a = new PrimitivObject(5, &wasDeleted);
        {
            unique_ptr_if_unused<PrimitivObject> shouldNotDelete(a, false);
            EXPECT_FALSE(wasDeleted);
            EXPECT_FALSE(shouldNotDelete.isUnused());
            EXPECT_EQ(a, shouldNotDelete.get());
            EXPECT_EQ(a, &*shouldNotDelete);
            EXPECT_EQ(&a->memb, &shouldNotDelete->memb);
        }
        EXPECT_FALSE(wasDeleted);
        delete a;
        EXPECT_TRUE(wasDeleted);
    }

    {
        bool wasDeleted = false;
        auto a = new PrimitivObject(5, &wasDeleted);
        {
            unique_ptr_if_unused<PrimitivObject> shouldDelete(a, true);
            EXPECT_FALSE(wasDeleted);
            EXPECT_TRUE(shouldDelete.isUnused());
            EXPECT_EQ(a, shouldDelete.get());
            EXPECT_EQ(a, &*shouldDelete);
            EXPECT_EQ(&a->memb, &shouldDelete->memb);
        }
        EXPECT_TRUE(wasDeleted);
    }
}

TEST(UniquePtrIfUnused, GivenNoCustomDeleterAtCreationWhenDeletingThenUseDefaultDeleter) {
    bool deleterWasCalled{false};
    struct DefaultDeleterTestStruct {
        DefaultDeleterTestStruct(bool *deleterWasCalledFlag)
            : deleterWasCalledFlag(deleterWasCalledFlag) {}
        ~DefaultDeleterTestStruct() {
            *deleterWasCalledFlag = true;
        }
        bool *deleterWasCalledFlag;
    };

    {
        unique_ptr_if_unused<DefaultDeleterTestStruct> uptr(new DefaultDeleterTestStruct(&deleterWasCalled), true);
    }

    ASSERT_TRUE(deleterWasCalled);
}

TEST(UniquePtrIfUnused, GivenCustomDeleterAtCreationWhenDeletingThenUseProvidedDeleter) {
    struct CustomDeleterTestStruct {
        bool customDeleterWasCalled{false};
        static void deleter(CustomDeleterTestStruct *ptr) {
            ptr->customDeleterWasCalled = true;
        }
    } customDeleterObj;
    {
        unique_ptr_if_unused<CustomDeleterTestStruct> uptr(&customDeleterObj, true, &CustomDeleterTestStruct::deleter);
    }
    ASSERT_TRUE(customDeleterObj.customDeleterWasCalled);
}

TEST(UniquePtrIfUnused, GivenIntializedWithDerivativeOfReferenceCounterWhenDestroyingThenUseObtainedDeleter) {
    struct ObtainedDeleterTestStruct : public ReferenceTrackedObject<ObtainedDeleterTestStruct> {
        DeleterFuncType getCustomDeleter() const {
            return &ObtainedDeleterTestStruct::deleter;
        }

        bool obtainedDeleterWasCalled{false};
        static void deleter(ObtainedDeleterTestStruct *ptr) {
            ptr->obtainedDeleterWasCalled = true;
        }
    } obtainedDeleterObj;
    {
        unique_ptr_if_unused<ObtainedDeleterTestStruct> uptr(&obtainedDeleterObj, true);
    }
    ASSERT_TRUE(obtainedDeleterObj.obtainedDeleterWasCalled);

    {
        // make sure that nullptr for types that declare custom deleters don't cause problems
        unique_ptr_if_unused<ObtainedDeleterTestStruct>{nullptr, true};
        unique_ptr_if_unused<ObtainedDeleterTestStruct>{nullptr, false};
    }
}

TEST(ReferenceTrackedObject, GivenInternalAndApiReferenceCountWhenDecrementingThenDeletionBehavesCorrectly) {
    struct PrimitiveReferenceTrackedObject : ReferenceTrackedObject<PrimitiveReferenceTrackedObject> {
        PrimitiveReferenceTrackedObject(int v, bool *wasDeletedFlag)
            : memb(v), wasDeletedFlag(wasDeletedFlag) {
            if (wasDeletedFlag != nullptr) {
                *wasDeletedFlag = false;
            }
        }
        ~PrimitiveReferenceTrackedObject() override {
            if (wasDeletedFlag != nullptr) {
                *wasDeletedFlag = true;
            }
        }
        int memb;
        bool *wasDeletedFlag;
    };

    PrimitiveReferenceTrackedObject *a = nullptr;

    // 1. Test simple inc/dec api ref count
    {
        bool wasDeleted = false;
        a = new PrimitiveReferenceTrackedObject(5, &wasDeleted);
        ASSERT_EQ(0, a->getRefInternalCount());
        a->incRefApi();
        ASSERT_EQ(1, a->getRefInternalCount());
        {
            ASSERT_FALSE(wasDeleted);
            auto autoDeleter = a->decRefApi();
            EXPECT_TRUE(autoDeleter.isUnused());
            EXPECT_EQ(a, autoDeleter.get());
            EXPECT_EQ(a, &*autoDeleter);
            EXPECT_EQ(&a->memb, &autoDeleter->memb);
            ASSERT_FALSE(wasDeleted);
        }
        ASSERT_TRUE(wasDeleted);
    }

    // 2. Test zero api ref count, but internal ref count > 0
    {
        bool wasDeleted = false;
        a = new PrimitiveReferenceTrackedObject(5, &wasDeleted);
        ASSERT_EQ(0, a->getRefInternalCount());
        a->incRefInternal();
        ASSERT_EQ(1, a->getRefInternalCount());
        a->incRefApi();
        {
            {
                ASSERT_FALSE(wasDeleted);
                auto autoDeleter = a->decRefApi();
                EXPECT_FALSE(autoDeleter.isUnused());
                EXPECT_EQ(a, autoDeleter.get());
                EXPECT_EQ(a, &*autoDeleter);
                EXPECT_EQ(&a->memb, &autoDeleter->memb);
                ASSERT_FALSE(wasDeleted);
            }
            // 3. Test api ref count 0 and dec internal ref count to 0
            ASSERT_FALSE(wasDeleted);
            auto autoDeleter = a->decRefInternal(); // NOLINT(clang-analyzer-cplusplus.NewDelete)
            EXPECT_TRUE(autoDeleter.isUnused());
            EXPECT_EQ(a, autoDeleter.get());
            EXPECT_EQ(a, &*autoDeleter);
            EXPECT_EQ(&a->memb, &autoDeleter->memb);
            ASSERT_FALSE(wasDeleted);
        }
        ASSERT_TRUE(wasDeleted);
    }

    // 4. Test that api refcount is not affected by internal refcount
    bool wasDeleted = false;
    a = new PrimitiveReferenceTrackedObject(5, &wasDeleted);
    static const int refApiTop = 7;
    static const int refIntTop = 13;
    for (int i = 0; i < refApiTop; ++i) {
        a->incRefApi();
    }
    for (int i = 0; i < refIntTop; ++i) {
        a->incRefInternal();
    }

    EXPECT_EQ(refApiTop, a->getRefApiCount());
    EXPECT_EQ(refIntTop + refApiTop, a->getRefInternalCount());

    while (a->getRefApiCount() > 0) {
        a->decRefApi();
        ASSERT_FALSE(wasDeleted);
    }

    while (a->getRefInternalCount() > 1) {
        a->decRefInternal();
        ASSERT_FALSE(wasDeleted);
    }

    a->decRefInternal();
    ASSERT_TRUE(wasDeleted);
}

TEST(ReferenceTrackedObject, whenNewReferenceTrackedObjectIsCreatedThenRefcountsAreZero) {
    struct PrimitiveReferenceTrackedObject : ReferenceTrackedObject<PrimitiveReferenceTrackedObject> {
    };

    PrimitiveReferenceTrackedObject obj;
    EXPECT_EQ(0, obj.getRefApiCount());
    EXPECT_EQ(0, obj.getRefInternalCount());
}
} // namespace NEO