Refactoring: move GUID operations out of Joystick

- SDL_JoystickGUID -> SDL_GUID (though we retain a type alias)
- Operations for GUID <-> String ops are now in
  src/SDL_guid.c and include/SDL_guid.h
- The corresponding Joystick operations delegate to SDL_guid.c
- Added test/testguid.c
This commit is contained in:
Christoph Reichenbach
2022-06-04 20:16:28 +00:00
committed by Sam Lantinga
parent 4e07d4722d
commit 3a20274ddf
18 changed files with 422 additions and 89 deletions

View File

@@ -85,6 +85,7 @@ add_executable(testgesture testgesture.c)
add_executable(testgl2 testgl2.c)
add_executable(testgles testgles.c)
add_executable(testgles2 testgles2.c)
add_executable(testguid testguid.c)
add_executable(testhaptic testhaptic.c)
add_executable(testhotplug testhotplug.c)
add_executable(testrumble testrumble.c)
@@ -166,6 +167,7 @@ SET(ALL_TESTS
testgl2
testgles
testgles2
testguid
testhaptic
testhittesting
testhotplug
@@ -214,6 +216,7 @@ set(NONINTERACTIVE
testatomic
testerror
testfilesystem
testguid
testlocale
testplatform
testpower
@@ -313,35 +316,36 @@ if(PSP)
# Build EBOOT files if building for PSP
set(BUILD_EBOOT
${NEEDS_RESOURCES}
testbounds
testgl2
testsem
testdisplayinfo
teststreaming
testgeometry
testfile
testdraw2
testviewport
testhittesting
testoverlay2
testver
testdrawchessboard
testsurround
testintersections
testmessage
testaudiocapture
testerror
testatomic
testjoystick
testiconv
testfilesystem
testplatform
testthread
testqsort
testaudiocapture
testaudioinfo
testbounds
testdisplayinfo
testdraw2
testdrawchessboard
testerror
testfile
testfilesystem
testgeometry
testgl2
testguid
testhittesting
testiconv
testintersections
testjoystick
testlock
testtimer
testmessage
testoverlay2
testplatform
testpower
testqsort
testsem
teststreaming
testsurround
testthread
testtimer
testver
testviewport
testwm2
torturethread
)

View File

@@ -40,6 +40,7 @@ TARGETS = \
testgamecontroller$(EXE) \
testgeometry$(EXE) \
testgesture$(EXE) \
testguid$(EXE) \
testhaptic$(EXE) \
testhittesting$(EXE) \
testhotplug$(EXE) \
@@ -201,7 +202,7 @@ testgeometry$(EXE): $(srcdir)/testgeometry.c $(srcdir)/testutils.c
testgesture$(EXE): $(srcdir)/testgesture.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
testgl2$(EXE): $(srcdir)/testgl2.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
@@ -214,6 +215,9 @@ testgles2$(EXE): $(srcdir)/testgles2.c
testgles2_sdf$(EXE): $(srcdir)/testgles2_sdf.c $(srcdir)/testutils.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS) @MATHLIB@
testguid$(EXE): $(srcdir)/testguid.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
testhaptic$(EXE): $(srcdir)/testhaptic.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)

169
test/testguid.c Normal file
View File

@@ -0,0 +1,169 @@
/*
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/**
* Automated tests for GUID processing
*/
#include <stdio.h>
#include "SDL.h"
/* Helpers */
static int _error_count = 0;
static int
_require_eq(Uint64 expected, Uint64 actual, int line, char *msg)
{
if (expected != actual) {
_error_count += 1;
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "[%s, L%d] %s: Actual %ld (0x%lx) != expected %ld (0x%lx)",
__FILE__, line, msg, actual, actual, expected, expected);
return 0;
}
return 1;
}
#define ASSERT_EQ(MSG, EXPECTED, ACTUAL) _require_eq((EXPECTED), (ACTUAL), __LINE__, (MSG))
/* Helpers */
#define NUM_TEST_GUIDS 5
static struct {
char *str;
Uint64 upper, lower;
} test_guids[NUM_TEST_GUIDS] = {
{ "0000000000000000" "ffffffffffffffff",
0x0000000000000000, 0xfffffffffffffffflu },
{ "0011223344556677" "8091a2b3c4d5e6f0",
0x0011223344556677lu, 0x8091a2b3c4d5e6f0lu },
{ "a011223344556677" "8091a2b3c4d5e6f0",
0xa011223344556677lu, 0x8091a2b3c4d5e6f0lu },
{ "a011223344556677" "8091a2b3c4d5e6f1",
0xa011223344556677lu, 0x8091a2b3c4d5e6f1lu },
{ "a011223344556677" "8191a2b3c4d5e6f0",
0xa011223344556677lu, 0x8191a2b3c4d5e6f0lu },
};
static void
upper_lower_to_bytestring(Uint8* out, Uint64 upper, Uint64 lower)
{
Uint64 values[2];
int i, k;
values[0] = upper;
values [1] = lower;
for (i = 0; i < 2; ++i) {
Uint64 v = values[i];
for (k = 0; k < 8; ++k) {
*out++ = v >> 56;
v <<= 8;
}
}
}
/* ================= Test Case Implementation ================== */
/**
* @brief Check String-to-GUID conversion
*
* @sa SDL_GUIDFromString
*/
static void
TestGuidFromString(void)
{
int i;
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);
if (!ASSERT_EQ("GUID from string", 0, SDL_memcmp(expected, guid.data, 16))) {
SDL_Log(" GUID was: '%s'", test_guids[i].str);
}
}
}
/**
* @brief Check GUID-to-String conversion
*
* @sa SDL_GUIDToString
*/
static void
TestGuidToString(void)
{
int i;
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 */
expected_prefix = fill_char | (fill_char << 8) | (fill_char << 16) | (fill_char << 24);
SDL_memcpy(&actual_prefix, guid_str_buf, 4);
if (!ASSERT_EQ("String buffer memory before output untouched: ", expected_prefix, actual_prefix)) {
SDL_Log(" at size=%d", size);
}
/* Check that we did not overwrite too much */
written_size = 0;
while ((guid_str[written_size] & 0xff) != fill_char && written_size < 256) {
++written_size;
}
if (!ASSERT_EQ("Output length is within expected bounds", 1, written_size <= size)) {
SDL_Log(" with lenght %d: wrote %d of %d permitted bytes",
size, written_size, size);
}
if (size >= 33) {
if (!ASSERT_EQ("GUID string equality", 0, SDL_strcmp(guid_str, test_guids[i].str))) {
SDL_Log(" from string: %s", test_guids[i].str);
}
}
}
}
}
int
main(int argc, char *argv[])
{
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
TestGuidFromString();
TestGuidToString();
return _error_count > 0;
}

View File

@@ -11,7 +11,7 @@ TARGETS = testatomic.exe testdisplayinfo.exe testbounds.exe testdraw2.exe &
testdrawchessboard.exe testdropfile.exe testerror.exe testfile.exe &
testfilesystem.exe testgamecontroller.exe testgeometry.exe testgesture.exe &
testhittesting.exe testhotplug.exe testiconv.exe testime.exe testlocale.exe &
testintersections.exe testjoystick.exe testkeys.exe testloadso.exe &
testguid.exe testintersections.exe testjoystick.exe testkeys.exe testloadso.exe &
testlock.exe testmessage.exe testoverlay2.exe testplatform.exe &
testpower.exe testsensor.exe testrelative.exe testrendercopyex.exe &
testrendertarget.exe testrumble.exe testscale.exe testsem.exe &