Skip to content

Commit 3edb738

Browse files
Decrease number of classes deriving from Program 2/n
Change-Id: I67a6e26f62c4bfdcdcea591ca4af3d4ce4f9980e Signed-off-by: Maciej Dziuban <[email protected]>
1 parent f80b020 commit 3edb738

File tree

5 files changed

+76
-35
lines changed

5 files changed

+76
-35
lines changed

unit_tests/mocks/mock_program.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class MockProgram : public Program {
4242
using Program::isKernelDebugEnabled;
4343
using Program::rebuildProgramFromIr;
4444

45+
using Program::sourceCode;
46+
4547
MockProgram() : Program() {}
4648
MockProgram(Context *context, bool isBuiltinKernel) : Program(context, isBuiltinKernel) {}
4749
~MockProgram() {
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -22,51 +22,59 @@
2222

2323
#include "runtime/helpers/string.h"
2424
#include "runtime/program/program.h"
25+
26+
#include "unit_tests/mocks/mock_program.h"
27+
2528
#include "gtest/gtest.h"
2629

2730
using namespace OCLRT;
2831

29-
class ProcessSpirBinaryTests : public Program,
30-
public ::testing::Test {
32+
class ProcessSpirBinaryTests : public ::testing::Test {
33+
public:
34+
void SetUp() override {
35+
program = std::make_unique<MockProgram>();
36+
}
37+
38+
std::unique_ptr<MockProgram> program;
3139
};
3240

3341
TEST_F(ProcessSpirBinaryTests, NullBinary) {
34-
auto retVal = processSpirBinary(nullptr, 0, false);
42+
auto retVal = program->processSpirBinary(nullptr, 0, false);
3543

3644
EXPECT_EQ(CL_SUCCESS, retVal);
37-
EXPECT_EQ(true, sourceCode.empty());
45+
EXPECT_EQ(true, program->sourceCode.empty());
3846
}
3947

4048
TEST_F(ProcessSpirBinaryTests, InvalidSizeBinary) {
4149
char pBinary[] = "somebinary\0";
4250
size_t binarySize = 1;
43-
auto retVal = processSpirBinary(pBinary, binarySize, false);
51+
auto retVal = program->processSpirBinary(pBinary, binarySize, false);
4452

4553
EXPECT_EQ(CL_SUCCESS, retVal);
46-
EXPECT_EQ(binarySize, sourceCode.size());
54+
EXPECT_EQ(binarySize, program->sourceCode.size());
4755
}
4856

4957
TEST_F(ProcessSpirBinaryTests, SomeBinary) {
5058
char pBinary[] = "somebinary\0";
5159
size_t binarySize = strnlen_s(pBinary, 11);
52-
auto retVal = processSpirBinary(pBinary, binarySize, false);
60+
auto retVal = program->processSpirBinary(pBinary, binarySize, false);
5361

5462
EXPECT_EQ(CL_SUCCESS, retVal);
55-
EXPECT_EQ(0, strcmp(pBinary, sourceCode.c_str()));
56-
EXPECT_EQ(binarySize, sourceCode.size());
63+
EXPECT_EQ(0, strcmp(pBinary, program->sourceCode.c_str()));
64+
EXPECT_EQ(binarySize, program->sourceCode.size());
5765

5866
// Verify no built log is available
59-
auto pBuildLog = getBuildLog(pDevice);
67+
auto pBuildLog = program->getBuildLog(program->getDevicePtr());
6068
EXPECT_EQ(nullptr, pBuildLog);
6169
}
6270

6371
TEST_F(ProcessSpirBinaryTests, SpirvBinary) {
6472
const uint32_t pBinary[2] = {0x03022307, 0x07230203};
6573
size_t binarySize = sizeof(pBinary);
6674

67-
processSpirBinary(pBinary, binarySize, false);
68-
EXPECT_FALSE(this->isSpirV);
75+
program->processSpirBinary(pBinary, binarySize, false);
76+
EXPECT_FALSE(program->getIsSpirV());
6977

70-
processSpirBinary(pBinary, binarySize, true);
71-
EXPECT_TRUE(this->isSpirV);
78+
program->processSpirBinary(pBinary, binarySize, true);
79+
EXPECT_TRUE(program->getIsSpirV());
7280
}

unit_tests/program/program_tests.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,37 @@
1919
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2020
* OTHER DEALINGS IN THE SOFTWARE.
2121
*/
22-
#include "runtime/kernel/kernel.h"
22+
23+
#include "program_tests.h"
24+
25+
#include "elf/reader.h"
2326
#include "runtime/command_stream/command_stream_receiver_hw.h"
2427
#include "runtime/compiler_interface/compiler_options.h"
25-
#include "unit_tests/libult/ult_command_stream_receiver.h"
26-
#include "runtime/indirect_heap/indirect_heap.h"
2728
#include "runtime/helpers/aligned_memory.h"
2829
#include "runtime/helpers/hash.h"
2930
#include "runtime/helpers/hw_helper.h"
3031
#include "runtime/helpers/kernel_commands.h"
3132
#include "runtime/helpers/ptr_math.h"
3233
#include "runtime/helpers/string.h"
34+
#include "runtime/indirect_heap/indirect_heap.h"
35+
#include "runtime/kernel/kernel.h"
3336
#include "runtime/memory_manager/graphics_allocation.h"
3437
#include "runtime/memory_manager/surface.h"
3538
#include "runtime/program/create.inl"
36-
#include "program_tests.h"
39+
#include "unit_tests/fixtures/device_fixture.h"
3740
#include "unit_tests/fixtures/program_fixture.inl"
3841
#include "unit_tests/global_environment.h"
3942
#include "unit_tests/helpers/debug_manager_state_restore.h"
4043
#include "unit_tests/helpers/kernel_binary_helper.h"
44+
#include "unit_tests/libult/ult_command_stream_receiver.h"
4145
#include "unit_tests/mocks/mock_kernel.h"
46+
#include "unit_tests/mocks/mock_program.h"
4247
#include "unit_tests/program/program_from_binary.h"
4348
#include "unit_tests/program/program_with_source.h"
44-
#include "test.h"
45-
#include "unit_tests/fixtures/device_fixture.h"
46-
#include "unit_tests/mocks/mock_program.h"
49+
#include "unit_tests/utilities/base_object_utils.h"
4750
#include "gtest/gtest.h"
4851
#include "gmock/gmock.h"
49-
#include "elf/reader.h"
52+
#include "test.h"
5053

5154
#include <map>
5255
#include <memory>
@@ -2888,9 +2891,6 @@ TEST_F(ProgramTests, givenCompilerInterfaceWhenCompileIsCalledThenProperIntermed
28882891
}
28892892
};
28902893

2891-
struct SmallMockProgram : public Program {};
2892-
using ProgramAutoPtr = std::unique_ptr<SmallMockProgram, void (*)(SmallMockProgram *)>;
2893-
28942894
auto device = castToObject<Device>(pContext->getDevice(0));
28952895

28962896
TranslationArgs input = {};
@@ -2908,32 +2908,27 @@ TEST_F(ProgramTests, givenCompilerInterfaceWhenCompileIsCalledThenProperIntermed
29082908
compilerMain->setDefaultCreatorFunc<OCLRT::MockFclOclDeviceCtx>(OCLRT::MockFclOclDeviceCtx::Create);
29092909

29102910
compilerInterface.useLlvmText = true;
2911-
ProgramAutoPtr programLlvmText{new SmallMockProgram(), [](SmallMockProgram *p) { p->release(); }};
2911+
auto programLlvmText = wrapReleasableObjectWithUniquePtr(new MockProgram());
29122912
programLlvmText->setDevice(device);
29132913
compilerInterface.intermediateRepresentation = IGC::CodeType::spirV;
29142914
compilerInterface.compile(*programLlvmText, input);
29152915
EXPECT_FALSE(programLlvmText->getIsSpirV());
29162916

29172917
compilerInterface.useLlvmText = false;
2918-
ProgramAutoPtr programSpirV{new SmallMockProgram(), [](SmallMockProgram *p) { p->release(); }};
2918+
auto programSpirV = wrapReleasableObjectWithUniquePtr(new MockProgram());
29192919
programSpirV->setDevice(device);
29202920
compilerInterface.intermediateRepresentation = IGC::CodeType::spirV;
29212921
compilerInterface.compile(*programSpirV, input);
29222922
EXPECT_TRUE(programSpirV->getIsSpirV());
29232923

2924-
ProgramAutoPtr programLlvmBc{new SmallMockProgram(), [](SmallMockProgram *p) { p->release(); }};
2924+
auto programLlvmBc = wrapReleasableObjectWithUniquePtr(new MockProgram());
29252925
programLlvmBc->setDevice(device);
29262926
compilerInterface.intermediateRepresentation = IGC::CodeType::llvmBc;
29272927
compilerInterface.compile(*programLlvmBc, input);
29282928
EXPECT_FALSE(programLlvmBc->getIsSpirV());
29292929
}
29302930

29312931
TEST_F(ProgramTests, givenProgramWithSpirvWhenRebuildProgramIsCalledThenSpirvPathIsTaken) {
2932-
struct SmallMockProgram : public Program {
2933-
using Program::rebuildProgramFromIr;
2934-
};
2935-
using ProgramAutoPtr = std::unique_ptr<SmallMockProgram, void (*)(SmallMockProgram *)>;
2936-
29372932
auto device = castToObject<Device>(pContext->getDevice(0));
29382933

29392934
MockCompilerInterface compilerInterface;
@@ -2952,7 +2947,7 @@ TEST_F(ProgramTests, givenProgramWithSpirvWhenRebuildProgramIsCalledThenSpirvPat
29522947
gEnvironment->igcPushDebugVars(debugVars);
29532948
std::unique_ptr<void, void (*)(void *)> igcDebugVarsAutoPop{&gEnvironment, [](void *) { gEnvironment->igcPopDebugVars(); }};
29542949

2955-
ProgramAutoPtr program{new SmallMockProgram(), [](SmallMockProgram *p) { p->release(); }};
2950+
auto program = wrapReleasableObjectWithUniquePtr(new MockProgram());
29562951
program->setDevice(device);
29572952
uint32_t spirv[16] = {0x03022307, 0x23471113, 0x17192329};
29582953
program->storeIrBinary(spirv, sizeof(spirv), true);

unit_tests/utilities/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# OTHER DEALINGS IN THE SOFTWARE.
2020

2121
set(IGDRCL_SRCS_tests_utilities
22+
${CMAKE_CURRENT_SOURCE_DIR}/base_object_utils.h
2223
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
2324
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests.cpp
2425
${CMAKE_CURRENT_SOURCE_DIR}/containers_tests_helpers
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#pragma once
24+
#include <memory>
25+
26+
namespace OCLRT {
27+
28+
template <typename T>
29+
using ReleaseableObjectPtr = std::unique_ptr<T, void (*)(T *)>;
30+
31+
template <typename T>
32+
static ReleaseableObjectPtr<T> wrapReleasableObjectWithUniquePtr(T *object) {
33+
return ReleaseableObjectPtr<T>{object, [](T *p) { p->release(); }};
34+
}
35+
} // namespace OCLRT

0 commit comments

Comments
 (0)