tstRTStrFormat.cpp revision 4f585bdc76239449b81a4cdb0d5c5dedc5534a20
/* $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 *
*******************************************************************************/
#include <iprt/initterm.h>
/** See FNRTSTRFORMATTYPE. */
void *pvUser)
{
/* validate */
/* format */
char szNum[64];
return cch;
}
{
char szBuf[512];
RTTestIFailed("at line %d: nested format '%s'\n"
" output: '%s'\n"
" wanted: '%s'\n",
RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
RTTestIFailed("at line %d: nested format '%s'\n"
" output: '%s'\n"
" wanted: '43xxx%syyy43'\n",
RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
}
int main()
{
if (rc)
return rc;
#define BUF_SIZE 120
/* simple */
{
RTTestIFailed("error: '%s'\n"
"wanted 'u32=16 u64=256 u64=0x100'\n", pszBuf);
}
/* just big. */
{
RTTestIFailed("error: '%s'\n"
"wanted 'u64=0x8070605040302010 42=42 u64=8102081627430068240 42=42'\n", pszBuf);
}
/* huge and negative. */
cch = RTStrPrintf(pszBuf, BUF_SIZE, "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(pszBuf, "u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42"))
{
RTTestIFailed("error: '%s'\n"
"wanted 'u64=0x8070605040302010 42=42 u64=9255003132036915216 42=42 u64=-9191740941672636400 42=42'\n", pszBuf);
}
/* 64-bit value bug. */
u64 = 0xa0000000;
RTTestIFailed("error: '%s'\n"
"wanted 'u64=0xa0000000 42=42 u64=2684354560 42=42'\n", pszBuf);
/* uuid */
RTUuidCreate(&Uuid);
char szCorrect[RTUUID_STR_LENGTH];
RTTestIFailed("error: '%s'\n"
"expected: '%s'\n",
/*
* Nested
*/
/*
* allocation
*/
char *psz = (char *)~0;
if (cch2 < 0)
RTTestIFailed("RTStrAPrintf failed\n"
"got : '%s'\n"
"wanted: 'Hey there! This is a test!'\n",
psz);
do { \
RTTestIFailed("at line %d: format '%s'\n" \
" output: '%s'\n" \
" wanted: '%s'\n", \
RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n", \
} while (0)
RTTestIFailed("error: '%s'\n" \
/*
* Runtime extensions.
*/
#if GC_ARCH_BITS == 32
#else
#endif
if (sizeof(void*) == 8)
{
}
else
{
}
if (sizeof(RTUINTPTR) == 8)
{
}
else
{
}
if (sizeof(RTCCUINTREG) == 8)
{
}
else
{
}
if (sizeof(RTSEMEVENT) == 8)
{
}
else
{
}
if (sizeof(RTTHREAD) == 8)
{
}
else
{
}
RTUuidCreate(&Uuid);
RTTestIFailed("error: '%s'\n"
"expected: '%s'\n",
/*
* Thousand separators.
*/
RTStrFormatNumber(pszBuf, 1, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 10, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 100, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 1000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 10000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("10 000"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 100000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("100 000"); memset(pszBuf, '!', BUF_SIZE);
RTStrFormatNumber(pszBuf, 1000000, 10, 0, 0, RTSTR_F_THOUSAND_SEP); CHECKSTR("1 000 000"); memset(pszBuf, '!', BUF_SIZE);
/*
* 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");
/*
* Unicode string formatting.
*/
static RTUTF16 s_wszDesc[] = { 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'i', 'o', 'n', 0 }; //assumes ascii.
// 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");
#if 0
#endif
/*
* Custom types.
*/
RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type3", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
CHECKSTR("type3=1");
RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type1", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
CHECKSTR("type3=1 type1=2");
RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type4", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
cch = RTStrPrintf(pszBuf, BUF_SIZE, "%R[type3] %R[type1] %R[type4]", (void *)1, (void *)2, (void *)3);
CHECKSTR("type3=1 type1=2 type4=3");
RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type2", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
cch = RTStrPrintf(pszBuf, BUF_SIZE, "%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");
RTTESTI_CHECK_RC(RTStrFormatTypeRegister("type5", TstType, (void *)((uintptr_t)TstType)), VINF_SUCCESS);
cch = RTStrPrintf(pszBuf, BUF_SIZE, "%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(pszBuf, BUF_SIZE, "%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(pszBuf, BUF_SIZE, "%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(pszBuf, BUF_SIZE, "%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.
*/
return RTTestSummaryAndDestroy(hTest);
}