tstStrFormat.cpp revision 5b281ba489ca18f0380d7efc7a5108b606cce449
/* $Id$ */
/** @file
* IPRT Testcase - String formatting.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static int g_cErrors = 0;
/** See FNRTSTRFORMATTYPE. */
void *pvUser)
{
/* validate */
{
g_cErrors++;
}
{
g_cErrors++;
}
/* format */
char szNum[64];
return cch;
}
int main()
{
RTR3Init();
char szStr[120];
/* simple */
{
RTPrintf("error: '%s'\n"
"wanted 'u32=16 u64=256 u64=0x100'\n", szStr);
g_cErrors++;
}
/* just big. */
{
RTPrintf("error: '%s'\n"
"wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", szStr);
g_cErrors++;
}
/* huge and negative. */
cch = RTStrPrintf(szStr, sizeof(szStr), "u64=%#llx 42=%d u64=%llu 42=%d u64=%lld 42=%d", u64, 42, u64, 42, u64, 42);
/* Not sure if this is the correct decimal representation... But both */
if (strcmp(szStr, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
{
RTPrintf("error: '%s'\n"
"wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", szStr);
g_cErrors++;
}
/* 64-bit value bug. */
u64 = 0xa0000000;
{
RTPrintf("error: '%s'\n"
"wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", szStr);
g_cErrors++;
}
/* uuid */
RTUuidCreate(&Uuid);
char szCorrect[RTUUID_STR_LENGTH];
{
RTPrintf("error: '%s'\n"
"expected: '%s'\n",
g_cErrors++;
}
/* allocation */
char *psz = (char *)~0;
if (cch2 < 0)
{
g_cErrors++;
}
{
RTPrintf("error: RTStrAPrintf failed\n"
"got : '%s'\n"
"wanted: 'Hey there! This is a test!'\n",
psz);
g_cErrors++;
}
{
g_cErrors++;
}
do { \
{ \
RTPrintf("error(%d): format '%s'\n" \
" output: '%s'\n" \
" wanted: '%s'\n", \
g_cErrors++; \
} \
{ \
RTPrintf("error(%d): Invalid length %d returned, expected %u!\n", \
g_cErrors++; \
} \
} while (0)
/*
* Runtime extensions.
*/
if (sizeof(void*) == 8)
{
}
else
{
}
if (sizeof(RTUINTPTR) == 8)
{
}
else
{
}
if (sizeof(RTUINTREG) == 8)
{
}
else
{
}
if (sizeof(RTSEMEVENT) == 8)
{
}
else
{
}
if (sizeof(RTTHREAD) == 8)
{
}
else
{
}
RTUuidCreate(&Uuid);
{
RTPrintf("error: '%s'\n"
"expected: '%s'\n",
g_cErrors++;
}
{ \
RTPrintf("error: '%s'\n" \
g_cErrors++; \
}
/*
* String formatting.
*/
// 0 1 2 3 4 5 6 7
// 0....5....0....5....0....5....0....5....0....5....0....5....0....5....0
CHECKSTR("cmd args description");
CHECKSTR("cmd description");
CHECKSTR("");
/* automatic conversions. */
static RTUNICP s_usz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
static RTUTF16 s_wsz1[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0 }; //assumes ascii.
CHECKSTR("hello world");
CHECKSTR("hello world");
CHECKSTR("hello");
CHECKSTR("hello");
#if 0
#endif
/*
* Custom types.
*/
#define CHECK(expr) do { if (!(expr)) { RTPrintf("tstEnv: error line %d: %s\n", __LINE__, #expr); g_cErrors++; } } while (0)
#define CHECK_RC(expr, rc) do { int rc2 = expr; if (rc2 != (rc)) { RTPrintf("tstEnv: error line %d: %s -> %Rrc expected %Rrc\n", __LINE__, #expr, rc2, rc); g_cErrors++; } } while (0)
CHECKSTR("type3=1");
CHECKSTR("type3=1 type1=2");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
CHECKSTR("type3=1 type1=2 type4=3");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2]", (void *)1, (void *)2, (void *)3, (void *)4);
CHECKSTR("type3=1 type1=2 type4=3 type2=4");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)1, (void *)2, (void *)3, (void *)4, (void *)5);
CHECKSTR("type3=1 type1=2 type4=3 type2=4 type5=5");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type2] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40, (void *)50);
CHECKSTR("type3=10 type1=20 type4=30 type2=40 type5=50");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4] %R[type5]", (void *)10, (void *)20, (void *)30, (void *)40);
CHECKSTR("type3=10 type1=20 type4=30 type5=40");
cch = RTStrPrintf(szStr, sizeof(szStr), "%R[type3] %R[type1] %R[type4]", (void *)10, (void *)20, (void *)30);
CHECKSTR("type3=10 type1=20 type4=30");
CHECKSTR("type3=10 type1=20");
CHECKSTR("type3=10");
/*
* Summarize and exit.
*/
if (!g_cErrors)
RTPrintf("tstStrFormat: SUCCESS\n");
else
return !!g_cErrors;
}