Browse Source

engine: merge tests for imagelib

pull/2/head
Alibek Omarov 3 years ago
parent
commit
91ee9bd32a
  1. 1
      engine/common/common.h
  2. 19
      engine/common/host.c
  3. 75
      engine/common/imagelib/img_main.c
  4. 11
      engine/common/imagelib/img_utils.c
  5. 3
      engine/common/tests.h
  6. 24
      public/tests/test_unittest.c

1
engine/common/common.h

@ -581,6 +581,7 @@ fs_offset_t FS_FileLength( file_t *f ); @@ -581,6 +581,7 @@ fs_offset_t FS_FileLength( file_t *f );
//
#include "com_image.h"
void Image_Setup( void );
void Image_Init( void );
void Image_Shutdown( void );
void Image_AddCmdFlags( uint flags );

19
engine/common/host.c

@ -776,15 +776,19 @@ void Host_Userconfigd_f( void ) @@ -776,15 +776,19 @@ void Host_Userconfigd_f( void )
}
#if XASH_ENGINE_TESTS
static void Host_RunTests( void )
static void Host_RunTests( int stage )
{
switch( stage )
{
case 0: // early engine load
memset( &tests_stats, 0, sizeof( tests_stats ));
Test_RunLibCommon();
break;
case 1: // after FS load
Test_RunImagelib();
Msg( "Done! %d passed, %d failed\n", tests_stats.passed, tests_stats.failed );
Sys_Quit();
}
}
#endif
@ -923,7 +927,7 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha @@ -923,7 +927,7 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha
#if XASH_ENGINE_TESTS
if( Sys_CheckParm( "-runtests" ))
Host_RunTests();
Host_RunTests( 0 );
#endif
Platform_Init();
@ -991,6 +995,11 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha @@ -991,6 +995,11 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha
Image_Init();
Sound_Init();
#if XASH_ENGINE_TESTS
if( Sys_CheckParm( "-runtests" ))
Host_RunTests( 1 );
#endif
FS_LoadGameInfo( NULL );
Q_strncpy( host.gamefolder, GI->gamefolder, sizeof( host.gamefolder ));

75
engine/common/imagelib/img_main.c

@ -491,3 +491,78 @@ rgbdata_t *FS_CopyImage( rgbdata_t *in ) @@ -491,3 +491,78 @@ rgbdata_t *FS_CopyImage( rgbdata_t *in )
return out;
}
#if XASH_ENGINE_TESTS
#include "tests.h"
static void GeneratePixel( byte *pix, uint i, uint j, uint w, uint h, qboolean genAlpha )
{
double x = ( j / (double)w ) - 0.5;
double y = ( i / (double)h ) - 0.5;
double d = sqrt( x * x + y * y );
pix[0] = (byte)(( sin( d * 30.0 ) + 1.0 ) * 126 );
pix[1] = (byte)(( sin( d * 27.723 ) + 1.0 ) * 126 );
pix[2] = (byte)(( sin( d * 42.41 ) + 1.0 ) * 126 );
pix[3] = genAlpha ? (byte)(( cos( d * 2.0 ) + 1.0 ) * 126 ) : 255;
}
static void Test_CheckImage( const char *name, rgbdata_t *rgb )
{
rgbdata_t *load;
// test reading
load = FS_LoadImage( name, NULL, 0 );
TASSERT( load->width == rgb->width )
TASSERT( load->height == rgb->height )
TASSERT( load->type == rgb->type )
TASSERT( ( load->flags & rgb->flags ) != 0 )
TASSERT( load->size == rgb->size )
TASSERT( memcmp(load->buffer, rgb->buffer, rgb->size ) == 0 )
Mem_Free( load );
}
void Test_RunImagelib( void )
{
rgbdata_t rgb = { 0 };
byte *buf;
const char *extensions[] = { "tga", "png", "bmp" };
uint i, j;
Image_Setup();
// generate image
rgb.width = 256;
rgb.height = 512;
rgb.type = PF_RGBA_32;
rgb.flags = IMAGE_HAS_ALPHA;
rgb.size = rgb.width * rgb.height * 4;
buf = rgb.buffer = Z_Malloc( rgb.size );
for( i = 0; i < rgb.height; i++ )
{
for( j = 0; j < rgb.width; j++ )
{
GeneratePixel( buf, i, j, rgb.width, rgb.height, true );
buf += 4;
}
}
for( i = 0; i < sizeof(extensions) / sizeof(extensions[0]); i++ )
{
const char *name = va( "test_gen.%s", extensions[i] );
// test saving
qboolean ret = FS_SaveImage( name, &rgb );
Con_Printf( "Checking if we can save images in '%s' format...\n", extensions[i] );
ASSERT(ret == true);
// test reading
Con_Printf( "Checking if we can read images in '%s' format...\n", extensions[i] );
Test_CheckImage( name, &rgb );
}
Z_Free( rgb.buffer );
}
#endif /* XASH_ENGINE_TESTS */

11
engine/common/imagelib/img_utils.c

@ -139,6 +139,13 @@ static const savepixformat_t save_game[] = @@ -139,6 +139,13 @@ static const savepixformat_t save_game[] =
{ NULL, NULL, NULL }
};
void Image_Setup( void )
{
image.cmd_flags = IL_USE_LERPING|IL_ALLOW_OVERWRITE;
image.loadformats = load_game;
image.saveformats = save_game;
}
void Image_Init( void )
{
// init pools
@ -148,9 +155,7 @@ void Image_Init( void ) @@ -148,9 +155,7 @@ void Image_Init( void )
switch( host.type )
{
case HOST_NORMAL:
image.cmd_flags = IL_USE_LERPING|IL_ALLOW_OVERWRITE;
image.loadformats = load_game;
image.saveformats = save_game;
Image_Setup( );
break;
case HOST_DEDICATED:
image.cmd_flags = 0;

3
engine/common/tests.h

@ -17,10 +17,11 @@ extern struct tests_stats_s tests_stats; @@ -17,10 +17,11 @@ extern struct tests_stats_s tests_stats;
if(!( exp )) \
{ \
tests_stats.failed++; \
Msg( "assert failed at %s:%i\n", __FILE__, __LINE__ ) \
Msg( "assert failed at %s:%i\n", __FILE__, __LINE__ ); \
} \
else tests_stats.passed++;
void Test_RunImagelib( void );
void Test_RunLibCommon( void );
#endif

24
public/tests/test_unittest.c

@ -1,24 +0,0 @@ @@ -1,24 +0,0 @@
#include "unittest.h"
#include <math.h>
TEST_FIRST(helloworld)
{
}
TEST3(hw2, helloworld, "Hello, World")
{
}
TEST(nonsense, hw2)
{
if( !( sin( 0 ) != 0.0 ))
_self->status = 1;
}
TEST(sense, nonsense)
{
if( !( sin( 0 ) == 0.0 ) )
_self->status = 1;
}
IMPLEMENT_MAIN(sense, "self-testing")
Loading…
Cancel
Save