Move majority of utilities to the core dir

Related-To: NEO-3677

Change-Id: If2e876028b765ad3ecf5f75db8755623b82955b8
Signed-off-by: Jobczyk, Lukasz <lukasz.jobczyk@intel.com>
This commit is contained in:
Jobczyk, Lukasz
2019-09-11 09:26:24 +02:00
committed by sys_ocldev
parent 1902523df2
commit 2e8e6bdb18
88 changed files with 113 additions and 135 deletions

View File

@ -5,22 +5,12 @@
#
set(IGDRCL_SRCS_tests_utilities
${CMAKE_CURRENT_SOURCE_DIR}/base_object_utils.h
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests_helpers
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}/debug_file_reader_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/debug_file_reader_tests.inl
${CMAKE_CURRENT_SOURCE_DIR}/debug_settings_reader_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/destructor_counted.h
${CMAKE_CURRENT_SOURCE_DIR}/directory_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/heap_allocator_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/numeric_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/perf_profiler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/reference_tracked_object_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tag_allocator_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/timer_util_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/vec_tests.cpp
)
get_property(NEO_CORE_UTILITIES_TESTS GLOBAL PROPERTY NEO_CORE_UTILITIES_TESTS)

View File

@ -1,34 +0,0 @@
/*
* Copyright (C) 2018-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include <memory>
namespace NEO {
template <typename T>
struct ReleaseObject {
void operator()(T *t) {
if (t != nullptr) {
t->release();
}
}
};
template <typename T>
using ReleaseableObjectPtr = std::unique_ptr<T, ReleaseObject<T>>;
template <typename T>
static ReleaseableObjectPtr<T> clUniquePtr(T *object) {
return ReleaseableObjectPtr<T>{object};
}
template <class _Ty, class... _Types>
inline ReleaseableObjectPtr<_Ty> make_releaseable(_Types &&... _Args) {
return (ReleaseableObjectPtr<_Ty>(new _Ty(std::forward<_Types>(_Args)...)));
}
} // namespace NEO

File diff suppressed because it is too large Load Diff

View File

@ -1,93 +0,0 @@
/*
* Copyright (c) 2017, Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
template <typename Arg1>
size_t countArgs(const Arg1 &arg1) {
return 1;
}
template <typename Arg1, typename... Rest>
size_t countArgs(const Arg1 &arg1, const Rest &... rest) {
return 1 + countArgs(rest...);
}
template <typename NodeObjectType, typename... Args>
int verifySequence(const NodeObjectType *base, int nodeNum, const NodeObjectType *last) {
if (base == last) {
return -1;
} else {
return nodeNum;
}
}
template <typename NodeObjectType, typename... Rest>
int verifySequence(const NodeObjectType *base, int nodeNum, const NodeObjectType *current, const Rest *... rest) {
if (base == nullptr) {
return nodeNum - 1;
} else if (base == current) {
return verifySequence(base->next, nodeNum + 1, rest...);
} else {
return nodeNum;
}
}
// Helper function for testing if nodes are placed exactly in given order
// Negative return values indicates that the sequence is ordered as requested.
// Non-negative value indicates first node that breaks the order.
template <typename NodeObjectType, typename... Rest>
int verifySequence(const NodeObjectType *base, const NodeObjectType *first, const Rest *... rest) {
return verifySequence(base, 0, first, rest...);
}
// Helper function for testing if nodes are placed exactly in given order
// Negative return values indicates that the sequence is ordered as requested.
// Non-negative value indicates first node that breaks the order.
// Note : verifies also "last->tail == nulptr"
template <typename NodeObjectType, typename... Rest>
int verifyFListOrder(const NodeObjectType *base, const NodeObjectType *first, const Rest *... rest) {
NodeObjectType *sentinel = nullptr;
int sequenceRet = verifySequence(base, first, rest..., sentinel);
if (sequenceRet < 0) {
return sequenceRet;
}
int totalReferenceNodes = (int)countArgs(first, rest...);
if (sequenceRet >= totalReferenceNodes) {
// base defines longer sequence than expected
return totalReferenceNodes - 1;
}
return sequenceRet;
}
// Helper function for testing if nodes are ordered exactly in given order
// Negative return values indicates that the sequence is ordered as requested.
// Non-negative value indicates first node that breaks the order.
// Note : verifies also "first->prev == nullptr" and "last->tail == nulptr"
template <typename NodeObjectType, typename... Rest>
int verifyDListOrder(const NodeObjectType *base, const NodeObjectType *first, const Rest *... rest) {
if (base->prev != nullptr) {
return 0;
}
return verifyFListOrder(base, first, rest...);
}

View File

@ -1,27 +0,0 @@
/*
* Copyright (C) 2018-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "test.h"
namespace NEO {
template <typename BaseType, uint32_t ordinal>
struct DestructorCounted : public BaseType {
template <typename... Args>
DestructorCounted(uint32_t &destructorId, Args &&... args) : BaseType(std::forward<Args>(args)...),
destructorId(destructorId) {}
~DestructorCounted() override {
EXPECT_EQ(ordinal, destructorId);
destructorId++;
}
private:
uint32_t &destructorId;
};
} // namespace NEO

View File

@ -1,30 +0,0 @@
/*
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/utilities/directory.h"
#include "test.h"
#include "gtest/gtest.h"
#include <cstdio>
#include <fstream>
using namespace NEO;
using namespace std;
TEST(Directory, GetFiles) {
ofstream tempfile("temp_file_that_does_not_exist.tmp");
tempfile << " ";
tempfile.flush();
tempfile.close();
string path = ".";
vector<string> files = Directory::getFiles(path);
EXPECT_LT(0u, files.size());
remove("temp_file_that_does_not_exist.tmp");
}

File diff suppressed because it is too large Load Diff

View File

@ -1,57 +0,0 @@
/*
* Copyright (C) 2018-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/utilities/numeric.h"
#include "test.h"
#include <type_traits>
using namespace NEO;
TEST(StorageTypeTest, whenGivenNumberOfBitsThenPicksIntegerTypeThatIsLargeEnough) {
static_assert(std::is_same<uint8_t, StorageTypeT<0>>::value, "");
static_assert(std::is_same<uint8_t, StorageTypeT<1>>::value, "");
static_assert(std::is_same<uint8_t, StorageTypeT<8>>::value, "");
static_assert(std::is_same<uint16_t, StorageTypeT<9>>::value, "");
static_assert(std::is_same<uint16_t, StorageTypeT<16>>::value, "");
static_assert(std::is_same<uint32_t, StorageTypeT<17>>::value, "");
static_assert(std::is_same<uint32_t, StorageTypeT<32>>::value, "");
static_assert(std::is_same<uint64_t, StorageTypeT<33>>::value, "");
static_assert(std::is_same<uint64_t, StorageTypeT<60>>::value, "");
static_assert(std::is_same<uint64_t, StorageTypeT<64>>::value, "");
}
TEST(FixedU4D8, whenGetMaxRepresentableFloatingPointValueIsCalledThenProperValueIsReturned) {
constexpr float maxU4D8 = 15.99609375f;
static_assert(maxU4D8 == FixedU4D8::getMaxRepresentableFloat(), "");
}
TEST(FixedU4D8, whenCreatingFromTooBigFloatThenValueIsClamped) {
constexpr float maxU4D8 = FixedU4D8::getMaxRepresentableFloat();
FixedU4D8 u4d8Max{maxU4D8};
FixedU4D8 u4d8MaxPlus1{maxU4D8 + 1};
EXPECT_EQ(u4d8Max.getRawAccess(), u4d8MaxPlus1.getRawAccess());
EXPECT_EQ((1U << (4 + 8)) - 1, u4d8Max.getRawAccess()); // all 12 bits should be set
EXPECT_EQ(maxU4D8, u4d8Max.asFloat());
}
TEST(FixedU4D8, whenCreatingFromNegativeFloatThenValueIsClamped) {
FixedU4D8 u4d8Zero{0.0f};
FixedU4D8 u4d8Minus1{-1.0f};
EXPECT_EQ(u4d8Zero.getRawAccess(), u4d8Minus1.getRawAccess());
EXPECT_EQ(0U, u4d8Zero.getRawAccess()); // all bits should be cleaned
EXPECT_EQ(0.0f, u4d8Zero.asFloat());
}
TEST(FixedU4D8, whenCreatingFromRepresentableFloatThenCorrectValueIsPreserved) {
constexpr float someValue = 3.5f;
FixedU4D8 u4d8{someValue};
EXPECT_EQ(someValue, u4d8.asFloat());
}

View File

@ -1,256 +0,0 @@
/*
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/utilities/reference_tracked_object.h"
#include "gtest/gtest.h"
#include <thread>
namespace NEO {
TEST(RefCounter, referenceCount) {
RefCounter<> rc;
ASSERT_EQ(0, rc.peek());
ASSERT_TRUE(rc.peekIsZero());
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());
ASSERT_FALSE(rc.peekIsZero());
rc.dec();
ASSERT_EQ(0, rc.peek());
ASSERT_TRUE(rc.peekIsZero());
}
TEST(RefCounter, givenReferenceTrackedObjectWhenDecAndReturnCurrentIsCalledThenMinusOneIsReturned) {
RefCounter<> rc;
EXPECT_EQ(-1, rc.decAndReturnCurrent());
}
TEST(unique_ptr_if_unused, InitializedWithDefaultConstructorAtQueryReturnsNullptr) {
unique_ptr_if_unused<int> uptr;
ASSERT_EQ(nullptr, uptr.get());
ASSERT_FALSE(uptr.isUnused());
}
TEST(unique_ptr_if_unused, deferredDeletion) {
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(unique_ptr_if_unused, IntializedWithoutCustomDeleterAtDestructionUsesDefaultDeleter) {
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(unique_ptr_if_unused, IntializedWithCustomDeleterAtDestructionUsesCustomDeleter) {
struct CustomDeleterTestStruct {
bool customDeleterWasCalled;
static void Delete(CustomDeleterTestStruct *ptr) {
ptr->customDeleterWasCalled = true;
}
} customDeleterObj;
customDeleterObj.customDeleterWasCalled = false;
{
unique_ptr_if_unused<CustomDeleterTestStruct> uptr(&customDeleterObj, true, &CustomDeleterTestStruct::Delete);
}
ASSERT_TRUE(customDeleterObj.customDeleterWasCalled);
}
TEST(unique_ptr_if_unused, IntializedWithDerivativeOfReferenceCounterAtDestructionUsesObtainedDeleter) {
struct ObtainedDeleterTestStruct : public ReferenceTrackedObject<ObtainedDeleterTestStruct> {
using DeleterFuncType = void (*)(ObtainedDeleterTestStruct *);
DeleterFuncType getCustomDeleter() const {
return &ObtainedDeleterTestStruct::Delete;
}
bool obtainedDeleterWasCalled;
static void Delete(ObtainedDeleterTestStruct *ptr) {
ptr->obtainedDeleterWasCalled = true;
}
} obtainedDeleterObj;
obtainedDeleterObj.obtainedDeleterWasCalled = false;
{
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, internalAndApiReferenceCount) {
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_TRUE(a->peekHasZeroRefcounts());
a->incRefApi();
ASSERT_FALSE(a->peekHasZeroRefcounts());
{
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_TRUE(a->peekHasZeroRefcounts());
a->incRefInternal();
ASSERT_FALSE(a->peekHasZeroRefcounts());
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();
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, whenNewReferenceTrackedObjectIsCreatedRefcountsAreZero) {
struct PrimitiveReferenceTrackedObject : ReferenceTrackedObject<PrimitiveReferenceTrackedObject> {
};
PrimitiveReferenceTrackedObject obj;
EXPECT_TRUE(obj.peekHasZeroRefcounts());
EXPECT_EQ(0, obj.getRefApiCount());
EXPECT_EQ(0, obj.getRefInternalCount());
}
} // namespace NEO

View File

@ -1,65 +0,0 @@
/*
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/utilities/timer_util.h"
#include "test.h"
#include "gtest/gtest.h"
#include <algorithm>
using namespace NEO;
TEST(TimerTest, Get) {
Timer::setFreq();
Timer timer;
auto loopCount = 100u;
unsigned long long maxDelta = 0;
unsigned long long minDelta = -1;
while (loopCount--) {
timer.start();
timer.end();
unsigned long long currentDelta = timer.get();
maxDelta = std::max(currentDelta, maxDelta);
minDelta = std::min(currentDelta, minDelta);
}
EXPECT_LE(minDelta, 10000u);
//thread switch may cost up to 2s
EXPECT_LE(maxDelta, 2000000000u);
}
TEST(TimerTest, GetStartEnd) {
Timer::setFreq();
Timer timer;
timer.start();
timer.end();
long long start = timer.getStart();
EXPECT_NE(0, start);
long long end = timer.getEnd();
EXPECT_NE(0, end);
EXPECT_GE(end, start);
}
TEST(TimerTest, Assignement) {
Timer::setFreq();
Timer timer1, timer2;
timer1.start();
timer1.end();
timer2 = timer1;
EXPECT_EQ(timer1.getStart(), timer2.getStart());
}

View File

@ -1,54 +0,0 @@
/*
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "core/helpers/vec.h"
#include "gtest/gtest.h"
TEST(VecTest, operators) {
Vec3<size_t> v0(nullptr);
Vec3<size_t> v1({1, 2, 3});
Vec3<size_t> v2(v1);
Vec3<size_t> v3({0, 0, 0});
Vec3<size_t> v4({1, 2, 1});
ASSERT_EQ(v0, v3);
ASSERT_EQ(v1, v2);
ASSERT_NE(v1, v3);
ASSERT_NE(v1, v4);
v3 = v1;
ASSERT_EQ(v2, v3);
size_t arr[3] = {1, 5, 3};
v3 = arr;
ASSERT_NE(v1, v3);
}
TEST(VecTest, getSimpliefiedDim) {
{
Vec3<size_t> v{3, 3, 3};
EXPECT_EQ(3U, v.getSimplifiedDim());
}
{
Vec3<size_t> v{3, 3, 1};
EXPECT_EQ(2U, v.getSimplifiedDim());
}
{
Vec3<size_t> v{1, 1, 1};
EXPECT_EQ(1U, v.getSimplifiedDim());
}
{
Vec3<size_t> v{0, 0, 0};
EXPECT_EQ(0U, v.getSimplifiedDim());
}
}