2022-06-04 20:16:28 +00:00
/**
2022-07-01 23:27:51 +00:00
* GUID test suite
2022-06-04 20:16:28 +00:00
*/
# include "SDL.h"
2022-07-01 23:27:51 +00:00
# include "SDL_test.h"
2022-06-04 20:16:28 +00:00
2023-05-31 01:50:02 +00:00
# ifdef HAVE_STDINT_H
# include <stdint.h>
# endif
2022-07-01 23:27:51 +00:00
/* ================= Test Case Implementation ================== */
2022-06-04 20:16:28 +00:00
2022-07-01 23:27:51 +00:00
/* Helper functions */
2022-06-04 20:16:28 +00:00
# define NUM_TEST_GUIDS 5
2023-05-31 01:50:02 +00:00
# ifndef UINT64_C
# ifdef _MSC_VER
# define UINT64_C(x) x##ui64
# elif defined(_LP64)
# define UINT64_C(x) x##UL
# else
# define UINT64_C(x) x##ULL
# endif
# endif
2022-11-30 20:51:59 +00:00
static struct
{
2022-06-04 20:16:28 +00:00
char * str ;
Uint64 upper , lower ;
} test_guids [ NUM_TEST_GUIDS ] = {
2022-11-30 20:51:59 +00:00
{ " 0000000000000000 "
" ffffffffffffffff " ,
2023-05-31 01:50:02 +00:00
UINT64_C ( 0x0000000000000000 ) , UINT64_C ( 0xffffffffffffffff ) } ,
2022-11-30 20:51:59 +00:00
{ " 0011223344556677 "
" 8091a2b3c4d5e6f0 " ,
2023-05-31 01:50:02 +00:00
UINT64_C ( 0x0011223344556677 ) , UINT64_C ( 0x8091a2b3c4d5e6f0 ) } ,
2022-11-30 20:51:59 +00:00
{ " a011223344556677 "
" 8091a2b3c4d5e6f0 " ,
2023-05-31 01:50:02 +00:00
UINT64_C ( 0xa011223344556677 ) , UINT64_C ( 0x8091a2b3c4d5e6f0 ) } ,
2022-11-30 20:51:59 +00:00
{ " a011223344556677 "
" 8091a2b3c4d5e6f1 " ,
2023-05-31 01:50:02 +00:00
UINT64_C ( 0xa011223344556677 ) , UINT64_C ( 0x8091a2b3c4d5e6f1 ) } ,
2022-11-30 20:51:59 +00:00
{ " a011223344556677 "
" 8191a2b3c4d5e6f0 " ,
2023-05-31 01:50:02 +00:00
UINT64_C ( 0xa011223344556677 ) , UINT64_C ( 0x8191a2b3c4d5e6f0 ) } ,
2022-06-04 20:16:28 +00:00
} ;
static void
2022-11-30 20:51:59 +00:00
upper_lower_to_bytestring ( Uint8 * out , Uint64 upper , Uint64 lower )
2022-06-04 20:16:28 +00:00
{
Uint64 values [ 2 ] ;
int i , k ;
values [ 0 ] = upper ;
2022-11-30 20:51:59 +00:00
values [ 1 ] = lower ;
2022-06-04 20:16:28 +00:00
for ( i = 0 ; i < 2 ; + + i ) {
Uint64 v = values [ i ] ;
for ( k = 0 ; k < 8 ; + + k ) {
* out + + = v > > 56 ;
v < < = 8 ;
}
}
}
2022-07-01 23:27:51 +00:00
/* Test case functions */
2022-06-04 20:16:28 +00:00
/**
* @ brief Check String - to - GUID conversion
*
* @ sa SDL_GUIDFromString
*/
2022-07-01 23:27:51 +00:00
static int
TestGuidFromString ( void * arg )
2022-06-04 20:16:28 +00:00
{
int i ;
2022-07-01 23:27:51 +00:00
SDLTest_AssertPass ( " Call to SDL_GUIDFromString " ) ;
2022-06-04 20:16:28 +00:00
for ( i = 0 ; i < NUM_TEST_GUIDS ; + + i ) {
Uint8 expected [ 16 ] ;
SDL_GUID guid ;
upper_lower_to_bytestring ( expected ,
test_guids [ i ] . upper , test_guids [ i ] . lower ) ;
guid = SDL_GUIDFromString ( test_guids [ i ] . str ) ;
2022-07-01 23:27:51 +00:00
SDLTest_AssertCheck ( SDL_memcmp ( expected , guid . data , 16 ) = = 0 , " GUID from string, GUID was: '%s' " , test_guids [ i ] . str ) ;
2022-06-04 20:16:28 +00:00
}
2022-07-01 23:27:51 +00:00
return TEST_COMPLETED ;
2022-06-04 20:16:28 +00:00
}
/**
* @ brief Check GUID - to - String conversion
*
* @ sa SDL_GUIDToString
*/
2022-07-01 23:27:51 +00:00
static int
TestGuidToString ( void * arg )
2022-06-04 20:16:28 +00:00
{
int i ;
2022-07-01 23:27:51 +00:00
SDLTest_AssertPass ( " Call to SDL_GUIDToString " ) ;
2022-06-04 20:16:28 +00:00
for ( i = 0 ; i < NUM_TEST_GUIDS ; + + i ) {
const int guid_str_offset = 4 ;
char guid_str_buf [ 64 ] ;
char * guid_str = guid_str_buf + guid_str_offset ;
SDL_GUID guid ;
int size ;
upper_lower_to_bytestring ( guid . data ,
test_guids [ i ] . upper , test_guids [ i ] . lower ) ;
/* Serialise to limited-length buffers */
for ( size = 0 ; size < = 36 ; + + size ) {
const Uint8 fill_char = size + 0xa0 ;
Uint32 expected_prefix ;
Uint32 actual_prefix ;
int written_size ;
SDL_memset ( guid_str_buf , fill_char , sizeof ( guid_str_buf ) ) ;
SDL_GUIDToString ( guid , guid_str , size ) ;
/* Check bytes before guid_str_buf */
2024-01-11 08:26:55 +00:00
expected_prefix = fill_char | ( fill_char < < 8 ) | ( fill_char < < 16 ) | ( ( ( Uint32 ) fill_char ) < < 24 ) ;
2022-06-04 20:16:28 +00:00
SDL_memcpy ( & actual_prefix , guid_str_buf , 4 ) ;
2022-10-10 05:31:14 +00:00
SDLTest_AssertCheck ( expected_prefix = = actual_prefix , " String buffer memory before output untouched, expected: % " SDL_PRIu32 " , got: % " SDL_PRIu32 " , at size=%d " , expected_prefix , actual_prefix , size ) ;
2022-06-04 20:16:28 +00:00
/* Check that we did not overwrite too much */
written_size = 0 ;
while ( ( guid_str [ written_size ] & 0xff ) ! = fill_char & & written_size < 256 ) {
+ + written_size ;
}
2022-07-01 23:27:51 +00:00
SDLTest_AssertCheck ( written_size < = size , " Output length is within expected bounds, with length %d: wrote %d of %d permitted bytes " , size , written_size , size ) ;
2022-06-04 20:16:28 +00:00
if ( size > = 33 ) {
2022-07-01 23:27:51 +00:00
SDLTest_AssertCheck ( SDL_strcmp ( guid_str , test_guids [ i ] . str ) = = 0 , " GUID string equality, from string: %s " , test_guids [ i ] . str ) ;
2022-06-04 20:16:28 +00:00
}
}
}
2022-07-01 23:27:51 +00:00
return TEST_COMPLETED ;
2022-06-04 20:16:28 +00:00
}
2022-07-01 23:27:51 +00:00
/* ================= Test References ================== */
2022-06-04 20:16:28 +00:00
2022-07-01 23:27:51 +00:00
/* GUID routine test cases */
2022-11-30 20:51:59 +00:00
static const SDLTest_TestCaseReference guidTest1 = {
( SDLTest_TestCaseFp ) TestGuidFromString , " TestGuidFromString " , " Call to SDL_GUIDFromString " , TEST_ENABLED
} ;
2022-06-04 20:16:28 +00:00
2022-11-30 20:51:59 +00:00
static const SDLTest_TestCaseReference guidTest2 = {
( SDLTest_TestCaseFp ) TestGuidToString , " TestGuidToString " , " Call to SDL_GUIDToString " , TEST_ENABLED
} ;
2022-07-01 23:27:51 +00:00
/* Sequence of GUID routine test cases */
2022-11-30 20:51:59 +00:00
static const SDLTest_TestCaseReference * guidTests [ ] = {
2022-07-01 23:27:51 +00:00
& guidTest1 ,
& guidTest2 ,
NULL
} ;
/* GUID routine test suite (global) */
SDLTest_TestSuiteReference guidTestSuite = {
" GUID " ,
NULL ,
guidTests ,
NULL
} ;