Skip to content

Commit

Permalink
[RFC] Move logic for test registration to the test harness (KhronosGr…
Browse files Browse the repository at this point in the history
…oup#2151)

And use in device_timer suite.

Signed-off-by: Kévin Petit <[email protected]>
  • Loading branch information
kpet authored Nov 26, 2024
1 parent df5e87b commit 0a1456d
Show file tree
Hide file tree
Showing 36 changed files with 307 additions and 352 deletions.
21 changes: 21 additions & 0 deletions test_common/harness/testHarness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ bool gCoreILProgram = true;

#define DEFAULT_NUM_ELEMENTS 0x4000

test_definition *test_registry::definitions() { return &m_definitions[0]; }

size_t test_registry::num_tests() { return m_definitions.size(); }

void test_registry::add_test(test *t, const char *name, Version version)
{

m_tests.push_back(t);
test_definition testDef;
testDef.func = t->getFunction();
testDef.name = name;
testDef.min_version = version;
m_definitions.push_back(testDef);
}

test_registry &test_registry::getInstance()
{
static test_registry instance;
return instance;
}

static int saveResultsToJson(const char *suiteName, test_definition testList[],
unsigned char selectedTestList[],
test_status resultTestList[], int testNum)
Expand Down
48 changes: 48 additions & 0 deletions test_common/harness/testHarness.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sstream>

#include <string>
#include <vector>

class Version {
public:
Expand Down Expand Up @@ -99,6 +100,53 @@ struct test_harness_config
unsigned numWorkerThreads;
};


struct test
{
virtual test_function_pointer getFunction() = 0;
};

class test_registry {
private:
std::vector<test *> m_tests;
std::vector<test_definition> m_definitions;

public:
static test_registry &getInstance();

test_definition *definitions();

size_t num_tests();

void add_test(test *t, const char *name, Version version);
test_registry() {}
};

template <typename T> T *register_test(const char *name, Version version)
{
T *t = new T();
test_registry::getInstance().add_test((test *)t, name, version);
return t;
}

#define REGISTER_TEST_VERSION(name, version) \
extern int test_##name(cl_device_id deviceID, cl_context context, \
cl_command_queue queue, int num_elements); \
class test_##name##_class : public test { \
private: \
test_function_pointer fn; \
\
public: \
test_##name##_class(): fn(test_##name) {} \
test_function_pointer getFunction() { return fn; } \
}; \
test_##name##_class *var_##name = \
register_test<test_##name##_class>(#name, version); \
int test_##name(cl_device_id deviceID, cl_context context, \
cl_command_queue queue, int num_elements)

#define REGISTER_TEST(name) REGISTER_TEST_VERSION(name, Version(1, 2))

extern int gFailCount;
extern int gTestCount;
extern cl_uint gReSeed;
Expand Down
14 changes: 3 additions & 11 deletions test_conformance/device_timer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
#include <unistd.h>
#endif

#include "procs.h"

test_definition test_list[] = {
ADD_TEST( timer_resolution_queries ),
ADD_TEST( device_and_host_timers ),
};

test_status InitCL(cl_device_id device)
{
auto version = get_device_cl_version(device);
Expand Down Expand Up @@ -70,11 +63,10 @@ test_status InitCL(cl_device_id device)
return TEST_PASS;
}


const int test_num = ARRAY_SIZE( test_list );

int main(int argc, const char *argv[])
{
return runTestHarnessWithCheck( argc, argv, test_num, test_list, false, 0, InitCL );
return runTestHarnessWithCheck(
argc, argv, test_registry::getInstance().num_tests(),
test_registry::getInstance().definitions(), false, 0, InitCL);
}

24 changes: 0 additions & 24 deletions test_conformance/device_timer/procs.h

This file was deleted.

5 changes: 3 additions & 2 deletions test_conformance/device_timer/test_device_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
#include <CL/cl.h>
#include "harness/errorHelpers.h"
#include "harness/compat.h"
#include "harness/testHarness.h"

#if !defined(_WIN32)
#include "unistd.h" // For "sleep"
#endif

#define ALLOWED_ERROR 0.005f

int test_device_and_host_timers(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
REGISTER_TEST(device_and_host_timers)
{
int errors = 0;
cl_int result = CL_SUCCESS;
Expand Down Expand Up @@ -124,7 +125,7 @@ int test_device_and_host_timers(cl_device_id deviceID, cl_context context, cl_co
return errors;
}

int test_timer_resolution_queries(cl_device_id deviceID, cl_context context, cl_command_queue queue, int num_elements)
REGISTER_TEST(timer_resolution_queries)
{
int errors = 0;
cl_int result = CL_SUCCESS;
Expand Down
33 changes: 2 additions & 31 deletions test_conformance/spirv_new/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,6 @@ std::vector<unsigned char> readSPIRV(const char *file_name)
return readBinary(full_name_str.c_str());
}

test_definition *spirvTestsRegistry::getTestDefinitions()
{
return &testDefinitions[0];
}

size_t spirvTestsRegistry::getNumTests()
{
return testDefinitions.size();
}

void spirvTestsRegistry::addTestClass(baseTestClass *test, const char *testName,
Version version)
{

testClasses.push_back(test);
test_definition testDef;
testDef.func = test->getFunction();
testDef.name = testName;
testDef.min_version = version;
testDefinitions.push_back(testDef);
}

spirvTestsRegistry& spirvTestsRegistry::getInstance()
{
static spirvTestsRegistry instance;
return instance;
}

static int offline_get_program_with_il(clProgramWrapper &prog,
const cl_device_id deviceID,
const cl_context context,
Expand Down Expand Up @@ -270,7 +242,6 @@ int main(int argc, const char *argv[])
}

return runTestHarnessWithCheck(
argc, argv, spirvTestsRegistry::getInstance().getNumTests(),
spirvTestsRegistry::getInstance().getTestDefinitions(), false, 0,
InitCL);
argc, argv, test_registry::getInstance().num_tests(),
test_registry::getInstance().definitions(), false, 0, InitCL);
}
50 changes: 0 additions & 50 deletions test_conformance/spirv_new/procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,6 @@
return -1; \
} while (0)


class baseTestClass {
public:
baseTestClass() {}
virtual test_function_pointer getFunction() = 0;
};

class spirvTestsRegistry {
private:
std::vector<baseTestClass *> testClasses;
std::vector<test_definition> testDefinitions;

public:
static spirvTestsRegistry &getInstance();

test_definition *getTestDefinitions();

size_t getNumTests();

void addTestClass(baseTestClass *test, const char *testName,
Version version);
spirvTestsRegistry() {}
};

template <typename T> T *createAndRegister(const char *name, Version version)
{
T *testClass = new T();
spirvTestsRegistry::getInstance().addTestClass((baseTestClass *)testClass,
name, version);
return testClass;
}

#define TEST_SPIRV_FUNC_VERSION(name, version) \
extern int test_##name(cl_device_id deviceID, cl_context context, \
cl_command_queue queue, int num_elements); \
class test_##name##_class : public baseTestClass { \
private: \
test_function_pointer fn; \
\
public: \
test_##name##_class(): fn(test_##name) {} \
test_function_pointer getFunction() { return fn; } \
}; \
test_##name##_class *var_##name = \
createAndRegister<test_##name##_class>(#name, version); \
int test_##name(cl_device_id deviceID, cl_context context, \
cl_command_queue queue, int num_elements)

#define TEST_SPIRV_FUNC(name) TEST_SPIRV_FUNC_VERSION(name, Version(1, 2))

struct spec_const
{
spec_const(cl_int id = 0, size_t sizet = 0, const void *value = NULL)
Expand Down
2 changes: 1 addition & 1 deletion test_conformance/spirv_new/test_basic_versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

extern bool gVersionSkip;

TEST_SPIRV_FUNC(basic_versions)
REGISTER_TEST(basic_versions)
{
cl_int error = CL_SUCCESS;

Expand Down
4 changes: 2 additions & 2 deletions test_conformance/spirv_new/test_cl_khr_expect_assume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static int test_expect_type(cl_device_id device, cl_context context,
return TEST_PASS;
}

TEST_SPIRV_FUNC(op_expect)
REGISTER_TEST(op_expect)
{
if (!is_extension_available(deviceID, "cl_khr_expect_assume"))
{
Expand All @@ -139,7 +139,7 @@ TEST_SPIRV_FUNC(op_expect)
return result;
}

TEST_SPIRV_FUNC(op_assume)
REGISTER_TEST(op_assume)
{
if (!is_extension_available(deviceID, "cl_khr_expect_assume"))
{
Expand Down
14 changes: 7 additions & 7 deletions test_conformance/spirv_new/test_decorate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,29 @@ int test_decorate_full(cl_device_id deviceID,
return verify_results(deviceID, context, queue, name, prog);
}

TEST_SPIRV_FUNC(decorate_restrict)
REGISTER_TEST(decorate_restrict)
{
return test_decorate_full(deviceID, context, queue, "decorate_restrict");
}

TEST_SPIRV_FUNC(decorate_aliased)
REGISTER_TEST(decorate_aliased)
{
return test_decorate_full(deviceID, context, queue, "decorate_aliased");
}

TEST_SPIRV_FUNC(decorate_alignment)
REGISTER_TEST(decorate_alignment)
{
//TODO: Check for results ? How to ensure buffers are aligned
clProgramWrapper prog;
return get_program_with_il(prog, deviceID, context, "decorate_alignment");
}

TEST_SPIRV_FUNC(decorate_constant)
REGISTER_TEST(decorate_constant)
{
return test_decorate_full(deviceID, context, queue, "decorate_constant");
}

TEST_SPIRV_FUNC(decorate_cpacked)
REGISTER_TEST(decorate_cpacked)
{
PACKED(struct packed_struct_t {
cl_int ival;
Expand Down Expand Up @@ -382,7 +382,7 @@ int test_saturate_full(cl_device_id deviceID,
}

#define TEST_SATURATED_CONVERSION(Ti, Tl, To) \
TEST_SPIRV_FUNC(decorate_saturated_conversion_##Ti##_to_##To) \
REGISTER_TEST(decorate_saturated_conversion_##Ti##_to_##To) \
{ \
typedef cl_##Ti cl_Ti; \
typedef cl_##Tl cl_Tl; \
Expand Down Expand Up @@ -532,7 +532,7 @@ static inline Ti generate_fprounding_input(RandomSeed &seed)
}

#define TEST_SPIRV_FP_ROUNDING_DECORATE(name, func, Ti, To) \
TEST_SPIRV_FUNC(decorate_fp_rounding_mode_##name##_##Ti##_##To) \
REGISTER_TEST(decorate_fp_rounding_mode_##name##_##Ti##_##To) \
{ \
typedef cl_##Ti clTi; \
typedef cl_##To clTo; \
Expand Down
2 changes: 1 addition & 1 deletion test_conformance/spirv_new/test_get_program_il.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const char *sample_kernel_code_single_line[] = {
"}\n"
};

TEST_SPIRV_FUNC(get_program_il)
REGISTER_TEST(get_program_il)
{
clProgramWrapper source_program;
size_t il_size = -1;
Expand Down
8 changes: 4 additions & 4 deletions test_conformance/spirv_new/test_linkage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ static int test_linkage_compile(cl_device_id deviceID,
return 0;
}

TEST_SPIRV_FUNC(linkage_export_function_compile)
REGISTER_TEST(linkage_export_function_compile)
{
clProgramWrapper prog;
return test_linkage_compile(deviceID, context, queue, "linkage_export", prog);
}

TEST_SPIRV_FUNC(linkage_import_function_compile)
REGISTER_TEST(linkage_import_function_compile)
{
clProgramWrapper prog;
return test_linkage_compile(deviceID, context, queue, "linkage_import", prog);
}

TEST_SPIRV_FUNC(linkage_import_function_link)
REGISTER_TEST(linkage_import_function_link)
{
int err = 0;

Expand Down Expand Up @@ -212,7 +212,7 @@ static int test_linkonce_odr_helper(cl_device_id deviceID, cl_context context,
return TEST_PASS;
}

TEST_SPIRV_FUNC(linkage_linkonce_odr)
REGISTER_TEST(linkage_linkonce_odr)
{
if (!is_extension_available(deviceID, "cl_khr_spirv_linkonce_odr"))
{
Expand Down
Loading

0 comments on commit 0a1456d

Please sign in to comment.