tstGetOpt.cpp revision b1cf57acefb5d1ce6ae2466f43e225a81083ff34
/* $Id$ */
/** @file
* IPRT Testcase - RTGetOpt
*/
/*
* Copyright (C) 2007-2009 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 *
*******************************************************************************/
int main()
{
if (rc)
return rc;
#define CHECK(expr) do { if (!(expr)) { RTTestIFailed("error line %d (iNext=%d): %s\n", __LINE__, GetState.iNext, #expr); } } while (0)
do { \
if (!(expr)) { \
} \
} while (0)
#define CHECK_pDef(paOpts, i) \
CHECK2(Val.pDef == &(paOpts)[(i)], ("Got #%d (%p) expected #%d\n", (int)(Val.pDef - &(paOpts)[0]), Val.pDef, i));
do { \
CHECK2(GetState.iNext == (iInc) + iPrev, ("iNext=%d expected %d\n", GetState.iNext, (iInc) + iPrev)); \
} while (0)
do { \
CHECK2(GetState.iNext == (iInc) + iPrev, ("iNext=%d expected %d\n", GetState.iNext, (iInc) + iPrev)); \
} while (0)
/*
* The basics.
*/
static const RTGETOPTDEF s_aOpts2[] =
{
};
const char *argv2[] =
{
"-s", "string1",
"--optwithstring", "string2",
"-i", "-42",
"-i:-42",
"-i=-42",
"-i:", "-42",
"-i=", "-42",
"--optwithint", "42",
"--optwithint:42",
"--optwithint=42",
"--optwithint:", "42",
"--optwithint=", "42",
"-v",
"--verbose",
"-q",
"--quiet",
"-novalue",
"-startvm", "myvm",
"nodash",
"nodashval", "string3",
"filename1",
"-q",
"filename2",
"-vqi999",
"-g192.168.1.1",
"-m08:0:27:00:ab:f3",
"--mac:1:::::c",
"--strindex786", "string4",
"--strindex786:string5",
"--strindex786=string6",
"strindex687", "string7",
"strindex687:string8",
"strindex687=string9",
"--intindex137", "1000",
"--macindex138", "08:0:27:00:ab:f3",
"--indexnovalue1",
"--macindexnegative",
"--twovalues", "firstvalue", "secondvalue",
"--twovalues:firstvalue", "secondvalue",
"--twovaluesindex4", "1", "0xA",
"--twovaluesindex5=2", "0xB",
"--threevalues", "1", "0xC", "thirdvalue",
"--boolean", "on",
"--boolean", "off",
"--boolean", "invalid",
"--booleanindex2", "on",
"--booleanindex7", "off",
"--booleanindex9", "invalid",
/* standard options */
"--help",
"-help",
"-?",
"-h",
"--version",
"-version",
"-V",
/* done */
};
CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc2, (char **)argv2, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0, 0 /* fFlags */)));
/* -i */
/* --optwithint */
/* -novalue / -startvm (single dash long options) */
/* no-dash options */
/* non-option, option, non-option */
/* compress short options */
/* IPv4 */
/* Ethernet MAC address. */
/* string with indexed argument */
/* RTGetOptFetchValue tests */
/* standard options. */
/* the end */
/*
* Options first.
*/
const char *argv3[] =
{
"foo1",
"-s", "string1",
"foo2",
"--optwithstring", "string2",
"foo3",
"-i", "-42",
"foo4",
"-i:-42",
"-i=-42",
"foo5",
"foo6",
"foo7",
"-i:", "-42",
"-i=", "-42",
"foo8",
"--twovalues", "firstvalue", "secondvalue",
"foo9",
"--twovalues:firstvalue", "secondvalue",
"foo10",
"--",
"--optwithstring",
"foo11",
"foo12",
/* done */
};
CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc3, (char **)argv3, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0,
/* -s */
/* -i */
/* --twovalues */
CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
/* -- */
/* the end */
/*
* Options first, part 2: No dash-dash.
*/
const char *argv4[] =
{
"foo1",
"-s", "string1",
"foo2",
"--optwithstring", "string2",
"foo3",
"-i", "-42",
"foo4",
"-i:-42",
"-i=-42",
"foo5",
"foo6",
"foo7",
"-i:", "-42",
"-i=", "-42",
"foo8",
"--twovalues", "firstvalue", "secondvalue",
"foo9",
"--twovalues:firstvalue", "secondvalue",
"foo10",
"foo11",
"foo12",
/* done */
};
CHECK(RT_SUCCESS(RTGetOptInit(&GetState, argc4, (char **)argv4, &s_aOpts2[0], RT_ELEMENTS(s_aOpts2), 0,
/* -s */
/* -i */
/* --twovalues */
CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
CHECK_GETOPT_STR(RTGetOptFetchValue(&GetState, &Val, RTGETOPT_REQ_STRING), VINF_SUCCESS, 1, "secondvalue");
/* -- */
/* the end */
/*
* Summary.
*/
return RTTestSummaryAndDestroy(hTest);
}