yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
aefd1e3e0
master
1#pragma once 2 3#include "core/slang-render-api-util.h" 4#include "slang.h" 5 6enum class TestResult 7{ 8// NOTE! Must keep in order such that combine is meaningful. That is larger values are higher 9// precident - and a series of tests that has lots of passes and a fail, is still a fail 10// overall. 11Ignored , 12Pass , 13ExpectedFail , 14Fail , 15}; 16 17enum class TestMessageType 18{ 19Info ,///< General info (may not be shown depending on verbosity setting) 20TestFailure ,///< Describes how a test failure took place 21RunError ,///< Describes an error that caused a test not to actually correctly run 22}; 23 24class ITestReporter 25{ 26public : 27virtual SLANG_NO_THROW void SLANG_MCALL startTest (const char * testName )= 0 ; 28virtual SLANG_NO_THROW void SLANG_MCALL addResult (TestResult result )= 0 ; 29virtual SLANG_NO_THROW void SLANG_MCALL 30addResultWithLocation (TestResult result ,const char * testText ,const char * file ,int line )= 0 ; 31virtual SLANG_NO_THROW void SLANG_MCALL 32addResultWithLocation (bool testSucceeded ,const char * testText ,const char * file ,int line )= 0 ; 33virtual SLANG_NO_THROW void SLANG_MCALL addExecutionTime (double time )= 0 ; 34virtual SLANG_NO_THROW void SLANG_MCALL message (TestMessageType type ,const char * message )= 0 ; 35virtual SLANG_NO_THROW void SLANG_MCALL endTest ()= 0 ; 36}; 37 38ITestReporter * getTestReporter (); 39 40namespace rhi 41{ 42class IDebugCallback ; 43} 44 45struct UnitTestContext 46{ 47slang ::IGlobalSession * slangGlobalSession ; 48const char * workDirectory ; 49const char * executableDirectory ; 50Slang ::RenderApiFlags enabledApis ; 51bool enableDebugLayers ; 52rhi ::IDebugCallback * debugCallback = nullptr; 53}; 54 55typedef void (* UnitTestFunc )(UnitTestContext * ); 56 57class IUnitTestModule 58{ 59public : 60virtual SLANG_NO_THROW SlangInt SLANG_MCALL getTestCount ()= 0 ; 61virtual SLANG_NO_THROW const char * SLANG_MCALL getTestName (SlangInt index )= 0 ; 62virtual SLANG_NO_THROW UnitTestFunc SLANG_MCALL getTestFunc (SlangInt index )= 0 ; 63virtual SLANG_NO_THROW void SLANG_MCALL setTestReporter (ITestReporter * reporter )= 0 ; 64virtual SLANG_NO_THROW void SLANG_MCALL destroy ()= 0 ; 65}; 66 67class UnitTestRegisterHelper 68{ 69public : 70UnitTestRegisterHelper (const char * name ,UnitTestFunc testFunc ); 71}; 72 73class AbortTestException 74{ 75}; 76 77typedef IUnitTestModule * (* UnitTestGetModuleFunc )(); 78 79#define SLANG_UNIT_TEST (name ) \ 80 void _##name##_impl(UnitTestContext* unitTestContext); \ 81 void name(UnitTestContext* unitTestContext) \ 82 { \ 83 try \ 84 { \ 85 _##name##_impl(unitTestContext); \ 86 } \ 87 catch (AbortTestException&) \ 88 { \ 89 } \ 90 } \ 91 UnitTestRegisterHelper _##name##RegisterHelper(#name, name); \ 92 void _##name##_impl(UnitTestContext* unitTestContext) 93 94#define SLANG_CHECK (x ) getTestReporter()->addResultWithLocation((x), #x, __FILE__, __LINE__); 95#define SLANG_CHECK_ABORT (x ) \ 96 { \ 97 bool _slang_check_result = (x); \ 98 getTestReporter()->addResultWithLocation(_slang_check_result, #x, __FILE__, __LINE__); \ 99 if (!_slang_check_result) \ 100 throw AbortTestException(); \ 101 } 102#define SLANG_IGNORE_TEST \ 103 getTestReporter()->addResult(TestResult::Ignored); \ 104 throw AbortTestException(); 105#define SLANG_CHECK_MSG (condition ,message ) \ 106 getTestReporter() \ 107 ->addResultWithLocation((condition), #condition " " message, __FILE__, __LINE__)