filesystem: tests: reduce boilerplate, add filecopy test

This commit is contained in:
Alibek Omarov
2026-05-27 00:38:53 +05:00
parent 8ee3544507
commit 4f342fb19a
6 changed files with 273 additions and 131 deletions

View File

@@ -1,39 +1,4 @@
#include "port.h"
#include "build.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "filesystem.h"
#if XASH_POSIX
#include <dlfcn.h>
#define LoadLibrary( x ) dlopen( x, RTLD_NOW )
#define GetProcAddress( x, y ) dlsym( x, y )
#define FreeLibrary( x ) dlclose( x )
#elif XASH_WIN32
#include <windows.h>
#endif
void *g_hModule;
FSAPI g_pfnGetFSAPI;
fs_api_t g_fs;
fs_globals_t *g_nullglobals;
static qboolean LoadFilesystem( void )
{
g_hModule = LoadLibrary( "filesystem_stdio." OS_LIB_EXT );
if( !g_hModule )
return false;
g_pfnGetFSAPI = (void*)GetProcAddress( g_hModule, GET_FS_API );
if( !g_pfnGetFSAPI )
return false;
if( !g_pfnGetFSAPI( FS_API_VERSION, &g_fs, &g_nullglobals, NULL ))
return false;
return true;
}
#include "filesystem_test_common.h"
static qboolean CheckFileContents( const char *path, const void *buf, fs_offset_t size )
{

206
filesystem/tests/filecopy.c Normal file
View File

@@ -0,0 +1,206 @@
#include "filesystem_test_common.h"
static qboolean WriteSourceFile( const char *path, const byte *data, fs_offset_t size )
{
file_t *f = g_fs.Open( path, "wb", true );
if( !f )
{
printf( "WriteSourceFile: Open(%s) failed\n", path );
return false;
}
if( g_fs.Write( f, data, size ) != size )
{
printf( "WriteSourceFile: short write\n" );
g_fs.Close( f );
return false;
}
g_fs.Close( f );
return true;
}
static qboolean VerifyFile( const char *path, const byte *expected, fs_offset_t size )
{
fs_offset_t len;
byte *data = g_fs.LoadFile( path, &len, true );
if( !data )
{
printf( "VerifyFile: LoadFile(%s) failed\n", path );
return false;
}
if( len != size )
{
printf( "VerifyFile: size mismatch (%lld != %lld)\n", (long long)len, (long long)size );
free( data );
return false;
}
if( memcmp( data, expected, size ) != 0 )
{
printf( "VerifyFile: content mismatch\n" );
free( data );
return false;
}
free( data );
return true;
}
static qboolean DoCopy( const char *src, const char *dst, fs_offset_t size )
{
file_t *fin = g_fs.Open( src, "rb", true );
if( !fin )
{
printf( "DoCopy: Open(%s) failed\n", src );
return false;
}
file_t *fout = g_fs.Open( dst, "wb", true );
if( !fout )
{
printf( "DoCopy: Open(%s) failed\n", dst );
g_fs.Close( fin );
return false;
}
qboolean ok = g_fs.FileCopy( fout, fin, (int)size );
g_fs.Close( fout );
g_fs.Close( fin );
return ok;
}
static qboolean TestBasic( const byte *payload, fs_offset_t size )
{
if( !WriteSourceFile( "fc_src.bin", payload, size ))
return false;
if( !DoCopy( "fc_src.bin", "fc_dst.bin", size ))
{
printf( "TestBasic(%lld): FileCopy returned false\n", (long long)size );
return false;
}
if( !VerifyFile( "fc_dst.bin", payload, size ))
{
printf( "TestBasic(%lld): verify failed\n", (long long)size );
return false;
}
g_fs.Delete( "fc_src.bin" );
g_fs.Delete( "fc_dst.bin" );
return true;
}
// drains some bytes via Read first, leaving the input file_t's userspace buffer
// dirty (or its position past the start). FS_FileCopy's sendfile fast-path must
// gate this case off; the buffered fallback must still produce the right bytes.
static qboolean TestPartialReadThenCopy( const byte *payload, fs_offset_t size, fs_offset_t skip )
{
byte scratch[64];
if( skip > (fs_offset_t)sizeof( scratch ))
skip = sizeof( scratch );
if( !WriteSourceFile( "fc_src.bin", payload, size ))
return false;
file_t *fin = g_fs.Open( "fc_src.bin", "rb", true );
if( !fin )
return false;
if( g_fs.Read( fin, scratch, skip ) != skip )
{
printf( "TestPartialReadThenCopy: short read\n" );
g_fs.Close( fin );
return false;
}
if( memcmp( scratch, payload, skip ) != 0 )
{
printf( "TestPartialReadThenCopy: pre-read mismatch\n" );
g_fs.Close( fin );
return false;
}
file_t *fout = g_fs.Open( "fc_dst.bin", "wb", true );
if( !fout )
{
g_fs.Close( fin );
return false;
}
qboolean ok = g_fs.FileCopy( fout, fin, (int)( size - skip ));
g_fs.Close( fout );
g_fs.Close( fin );
if( !ok )
{
printf( "TestPartialReadThenCopy: FileCopy returned false\n" );
return false;
}
if( !VerifyFile( "fc_dst.bin", payload + skip, size - skip ))
return false;
g_fs.Delete( "fc_src.bin" );
g_fs.Delete( "fc_dst.bin" );
return true;
}
static qboolean TestFileCopy( void )
{
enum { BIG_SIZE = 2 * 1024 * 1024 + 7777 }; // > FILE_COPY_SIZE to force multi-iteration
g_fs.AddGameDirectory( "./", FS_GAMEDIR_PATH );
byte *payload = malloc( BIG_SIZE );
if( !payload )
return false;
for( fs_offset_t i = 0; i < BIG_SIZE; i++ )
payload[i] = (byte)( ( i * 2654435761u ) >> 24 );
if( !TestBasic( payload, 0 ))
goto fail;
if( !TestBasic( payload, 1 ))
goto fail;
if( !TestBasic( payload, 4095 ))
goto fail;
if( !TestBasic( payload, BIG_SIZE ))
goto fail;
if( !TestPartialReadThenCopy( payload, BIG_SIZE, 33 ))
goto fail;
free( payload );
return true;
fail:
free( payload );
g_fs.Delete( "fc_src.bin" );
g_fs.Delete( "fc_dst.bin" );
return false;
}
int main( void )
{
if( !LoadFilesystem() )
return EXIT_FAILURE;
srand( time( NULL ));
if( !TestFileCopy())
return EXIT_FAILURE;
FreeLibrary( g_hModule );
printf( "success\n" );
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,62 @@
#ifndef FILESYSTEM_TEST_COMMON_H
#define FILESYSTEM_TEST_COMMON_H
#include "port.h"
#include "build.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "filesystem.h"
#if XASH_POSIX
#include <dlfcn.h>
#define LoadLibrary( x ) dlopen( x, RTLD_NOW )
#define GetProcAddress( x, y ) dlsym( x, y )
#define FreeLibrary( x ) dlclose( x )
#elif XASH_WIN32
#include <windows.h>
#endif
// each test program includes this header exactly once, so static linkage is fine
static void *g_hModule;
static FSAPI g_pfnGetFSAPI;
static fs_api_t g_fs;
static fs_globals_t *g_nullglobals;
#ifdef __cplusplus
static pfnCreateInterface_t g_pfnCreateInterface;
#endif
static qboolean LoadFilesystem( void )
{
g_hModule = LoadLibrary( "filesystem_stdio." OS_LIB_EXT );
if( !g_hModule )
return false;
g_pfnGetFSAPI = (FSAPI)GetProcAddress( g_hModule, GET_FS_API );
if( !g_pfnGetFSAPI )
return false;
if( !g_pfnGetFSAPI( FS_API_VERSION, &g_fs, &g_nullglobals, NULL ))
return false;
#ifdef __cplusplus
if( !g_nullglobals )
return false;
g_pfnCreateInterface = (pfnCreateInterface_t)GetProcAddress( g_hModule, "CreateInterface" );
if( !g_pfnCreateInterface )
return false;
int temp = -1;
if( !g_pfnCreateInterface( FILESYSTEM_INTERFACE_VERSION, &temp ) || temp != 0 )
return false;
temp = -1;
if( !g_pfnCreateInterface( FS_API_CREATEINTERFACE_TAG, &temp ) || temp != 0 )
return false;
#endif
return true;
}
#endif // FILESYSTEM_TEST_COMMON_H

View File

@@ -1,62 +1,5 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "port.h"
#include "build.h"
#include "filesystem_test_common.h"
#include "VFileSystem009.h"
#include "filesystem.h"
#if XASH_POSIX
#include <dlfcn.h>
#define LoadLibrary( x ) dlopen( x, RTLD_NOW )
#define GetProcAddress( x, y ) dlsym( x, y )
#define FreeLibrary( x ) dlclose( x )
typedef void *HMODULE;
#elif XASH_WIN32
#include <windows.h>
#endif
HMODULE g_hModule;
FSAPI g_pfnGetFSAPI;
pfnCreateInterface_t g_pfnCreateInterface;
fs_api_t g_fs;
fs_globals_t *g_nullglobals;
static bool LoadFilesystem()
{
int temp = -1;
g_hModule = LoadLibrary( "filesystem_stdio." OS_LIB_EXT );
if( !g_hModule )
return false;
// check our C-style interface existence
g_pfnGetFSAPI = reinterpret_cast<FSAPI>( GetProcAddress( g_hModule, GET_FS_API ));
if( !g_pfnGetFSAPI )
return false;
g_nullglobals = NULL;
if( !g_pfnGetFSAPI( FS_API_VERSION, &g_fs, &g_nullglobals, NULL ))
return false;
if( !g_nullglobals )
return false;
// check Valve-style interface existence
g_pfnCreateInterface = reinterpret_cast<pfnCreateInterface_t>( GetProcAddress( g_hModule, "CreateInterface" ));
if( !g_pfnCreateInterface )
return false;
if( !g_pfnCreateInterface( FILESYSTEM_INTERFACE_VERSION, &temp ) || temp != 0 )
return false;
temp = -1;
if( !g_pfnCreateInterface( FS_API_CREATEINTERFACE_TAG, &temp ) || temp != 0 )
return false;
return true;
}
int main()
{

View File

@@ -1,39 +1,4 @@
#include "port.h"
#include "build.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "filesystem.h"
#if XASH_POSIX
#include <dlfcn.h>
#define LoadLibrary( x ) dlopen( x, RTLD_NOW )
#define GetProcAddress( x, y ) dlsym( x, y )
#define FreeLibrary( x ) dlclose( x )
#elif XASH_WIN32
#include <windows.h>
#endif
void *g_hModule;
FSAPI g_pfnGetFSAPI;
fs_api_t g_fs;
fs_globals_t *g_nullglobals;
static qboolean LoadFilesystem( void )
{
g_hModule = LoadLibrary( "filesystem_stdio." OS_LIB_EXT );
if( !g_hModule )
return false;
g_pfnGetFSAPI = (void*)GetProcAddress( g_hModule, GET_FS_API );
if( !g_pfnGetFSAPI )
return false;
if( !g_pfnGetFSAPI( FS_API_VERSION, &g_fs, &g_nullglobals, NULL ))
return false;
return true;
}
#include "filesystem_test_common.h"
static int TestNoInit( void )
{

View File

@@ -51,7 +51,8 @@ def build(bld):
tests = {
'interface' : 'tests/interface.cpp',
'caseinsensitive' : 'tests/caseinsensitive.c',
'no-init': 'tests/no-init.c'
'no-init': 'tests/no-init.c',
'filecopy': 'tests/filecopy.c'
}
for i in tests: