From 45c4362ae608072a6c29acb490adea9ab3e46ad3 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 4 Jul 2024 06:29:46 +0300 Subject: [PATCH] public: add Q_ato{i,f,v} test --- public/tests/test_atoi.c | 108 +++++++++++++++++++++++++++++++++++++++ public/wscript | 1 + 2 files changed, 109 insertions(+) create mode 100644 public/tests/test_atoi.c diff --git a/public/tests/test_atoi.c b/public/tests/test_atoi.c new file mode 100644 index 00000000..3f1b4393 --- /dev/null +++ b/public/tests/test_atoi.c @@ -0,0 +1,108 @@ +#include "crtlib.h" +#include "xash3d_mathlib.h" + +static int Test_Atoi( void ) +{ + struct { + const char *str; + int result; + } test_data[] = { + { "", 0 }, + { " 123", 123 }, + { "-123", -123 }, + { "0xa1ba", 0xa1ba }, + { "-0xa1ba", -0xa1ba }, + { "0XA1BA", 0xa1ba }, + { "-0XA1BA", -0xa1ba }, + { "'a'", (byte)'a' }, + { "-'c'", - ((byte)'c') }, + }; + int i; + + for( i = 0; i < sizeof( test_data ) / sizeof( test_data[0] ); i++ ) + { + if( Q_atoi( test_data[i].str ) != test_data[i].result ) + return i + 1; + } + + return 0; +} + +static int Test_Atof( void ) +{ + struct { + const char *str; + float result; + } test_data[] = { + { "", 0 }, + { " 123.123", 123.123 }, + { "-123.13 ", -123.13 }, + { "0xa1ba", 0xa1ba }, + { "-0xa1ba", -0xa1ba }, + { "0XA1BA", 0xa1ba }, + { "-0XA1BA", -0xa1ba }, + { "'a'", (byte)'a' }, + { "-'c'", - ((byte)'c') } + }; + int i; + + for( i = 0; i < sizeof( test_data ) / sizeof( test_data[0] ); i++ ) + { + float result = Q_atof( test_data[i].str ); + + if( !Q_equal( result, test_data[i].result )) + return i + 1; + } + + return 0; +} + +static int Test_Atov( void ) +{ + struct { + const char *str; + int N; + float result[3]; + } test_data[] = { + { "1.0 1.2 3", 3, { 1.0f, 1.2f, 3.0f }}, + { "1.234 1.32", 2, { 1.234f, 1.32f }}, + }; + int i; + + for( i = 0; i < sizeof( test_data ) / sizeof( test_data[0] ); i++ ) + { + float result[3]; + int j; + + memset( result, 0, sizeof( result )); + Q_atov( result, test_data[i].str, test_data[i].N ); + + for( j = 0; j < 3; j++ ) // check that Q_atov didn't parsed more than requested + { + if( !Q_equal( result[j], test_data[i].result[j] )) + return i * 4 + j + 1; + } + } + + return 0; +} + +int main( void ) +{ + int ret = Test_Atoi(); + + if( ret > 0 ) + return ret; + + ret = Test_Atof(); + + if( ret > 0 ) + return ret + 32; + + ret = Test_Atov(); + + if( ret > 0 ) + return ret + 64; + + return 0; +} diff --git a/public/wscript b/public/wscript index 863977bd..86581d83 100644 --- a/public/wscript +++ b/public/wscript @@ -118,6 +118,7 @@ def build(bld): 'build': 'tests/test_build.c', 'filebase': 'tests/test_filebase.c', 'efp': 'tests/test_efp.c', + 'atoi': 'tests/test_atoi.c', } for i in tests: