Skip to content

Commit

Permalink
Fixed wasm tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Dec 6, 2024
1 parent 32e578b commit b97e679
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
6 changes: 5 additions & 1 deletion tests/os_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

TEST_CASE("getProcessMemoryUsed", "")
{
if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
{
SKIP("Not supported by wasm.");
}

REQUIRE(0 != bx::getProcessMemoryUsed() );
// DBG("bx::getProcessMemoryUsed %d", bx::getProcessMemoryUsed() );
}

#if BX_CONFIG_SUPPORTS_THREADING
Expand Down
17 changes: 15 additions & 2 deletions tests/run_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ bool testAssertHandler(const bx::Location& _location, const char* _format, va_li
return true;
}

int runAllTests(int _argc, const char* _argv[])
int runAllTests(int32_t _argc, const char* _argv[])
{
bx::setAssertHandler(testAssertHandler);

Expand Down Expand Up @@ -58,12 +58,25 @@ int runAllTests(int _argc, const char* _argv[])
"\n\n"
);

bx::printf("Args:\n");

for (int32_t ii = 0; ii < _argc; ++ii)
{
bx::printf("\t%2d: \"%s\"", ii, _argv[ii]);
}

bx::printf("\n\n");

using namespace Catch;

Session session;

ConfigData config;
config.defaultColourMode = BX_PLATFORM_EMSCRIPTEN ? ColourMode::None : ColourMode::PlatformDefault;
config.defaultColourMode = BX_PLATFORM_EMSCRIPTEN
? ColourMode::None
: ColourMode::PlatformDefault
;
config.showDurations = ShowDurations::Always;

session.useConfigData(config);

Expand Down
35 changes: 25 additions & 10 deletions tests/vsnprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

TEST_CASE("No output buffer provided.", "[string][printf]")
{
REQUIRE(4 == bx::snprintf(NULL, 0, "test") );

REQUIRE(1 == bx::snprintf(NULL, 0, "%d", 1) );
REQUIRE(4 == bx::snprintf(NULL, 0, "test") );
REQUIRE(4 == bx::snprintf(NULL, 10, "test") );
REQUIRE(1 == bx::snprintf(NULL, 0, "%d", 1) );
REQUIRE(1 == bx::snprintf(NULL, 10, "%d", 1) );
}

TEST_CASE("Truncated output buffer.", "[string][printf]")
Expand Down Expand Up @@ -58,15 +59,16 @@ TEST_CASE("Truncated output buffer.", "[string][printf]")
template<bool StdCompliantT>
static bool test(const char* _expected, const char* _format, va_list _argList)
{
int32_t max = (int32_t)bx::strLen(_expected) + 1;
const int32_t expectedLen = bx::strLen(_expected);
int32_t max = expectedLen + 1024;
char* bxTemp = (char*)alloca(max);

va_list argList;
va_copy(argList, _argList);
const int32_t bxLen = bx::vsnprintf(bxTemp, max, _format, argList);

bool result = true
&& bxLen == max-1
&& bxLen == expectedLen
&& 0 == bx::strCmp(_expected, bxTemp)
;

Expand Down Expand Up @@ -96,8 +98,8 @@ static bool test(const char* _expected, const char* _format, va_list _argList)
{
printf("---\n");
printf("printf format '%s'\n", _format);
printf(" result (%4d) '%s'\n", bxLen, bxTemp);
printf(" expected (%4d) '%s'\n", max-1, _expected);
printf(" bx result (%4d) '%s'\n", bxLen, bxTemp);
printf(" expected (%4d) '%s'\n", expectedLen, _expected);
printf("CRT vsnprintf (%4d) '%s'\n", crtLen, crtTemp);
}

Expand Down Expand Up @@ -133,6 +135,9 @@ TEST_CASE("Format %f", "[string][printf]")
REQUIRE(test(" 13.370", "%*.*f", 8, 3, 13.37) );
REQUIRE(test("13.370 ", "%-8.3f", 13.37) );
REQUIRE(test("13.370 ", "%*.*f", -8, 3, 13.37) );
REQUIRE(test(" -13.370", "% 8.3f", -13.37) );
REQUIRE(test(" 13.370", "% 16.3f", 13.37) );
REQUIRE(test(" -13.370", "% 16.3f", -13.37) );

REQUIRE(test("nan ", "%-8f", std::numeric_limits<double>::quiet_NaN() ) );
REQUIRE(test(" nan", "%8f", std::numeric_limits<double>::quiet_NaN() ) );
Expand Down Expand Up @@ -216,8 +221,10 @@ TEST_CASE("Format %f", "[string][printf]")
TEST_CASE("Format %d, %i, %o, %u, %x", "[string][printf]")
{
REQUIRE(test("1337", "%d", 1337) );
REQUIRE(test("-1337", "% d", -1337) );
REQUIRE(test("1337 ", "%-20d", 1337) );
REQUIRE(test("-1337 ", "%-20d", -1337) );
REQUIRE(test(" -1337", "% 20d", -1337) );

REQUIRE(test("1337", "%i", 1337) );
REQUIRE(test("1337 ", "%-20i", 1337) );
Expand Down Expand Up @@ -273,7 +280,7 @@ TEST_CASE("Format %d, %i, %o, %u, %x", "[string][printf]")
REQUIRE(test("-001", "%04i", -1) );
REQUIRE(test("+001", "%+04i", 1) );

if (BX_ENABLED(BX_ARCH_32BIT) )
if (sizeof(intmax_t) == 4)
{
REQUIRE(test("2147483647", "%jd", INTMAX_MAX) );
}
Expand Down Expand Up @@ -318,11 +325,18 @@ TEST_CASE("Format %s", "[string][printf]")

TEST_CASE("Format %td", "[string][printf]")
{
size_t size = size_t(-1);
ptrdiff_t size = ptrdiff_t(-1);

REQUIRE(test("-1", "%td", size) );

REQUIRE(test("3221225472", "%td", size_t(3221225472) ) );
if (4 == sizeof(ptrdiff_t) )
{
REQUIRE(test("-1073741824", "%td", ptrdiff_t(3221225472) ) );
}
else
{
REQUIRE(test("3221225472", "%td", ptrdiff_t(3221225472) ) );
}
}

TEST_CASE("Format %n", "[string][printf]")
Expand Down Expand Up @@ -353,6 +367,7 @@ TEST_CASE("Format %c, %s, %S", "[string][printf]")
REQUIRE(test("x ", "%-20c", 'x') );

REQUIRE(test("hello ", "%-20s", "hello") );
REQUIRE(test(" hello", "%10s", "hello") );
REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") );

REQUIRE(testNotStdCompliant("h", "%1s", "hello") );
Expand Down

0 comments on commit b97e679

Please sign in to comment.