1 | /* $Id: tstStrToVer.cpp 24658 2009-11-14 23:31:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - String To Version Number Conversion.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/stream.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | struct TstU32
|
---|
41 | {
|
---|
42 | const char *psz;
|
---|
43 | int rc;
|
---|
44 | uint32_t Result;
|
---|
45 | };
|
---|
46 |
|
---|
47 |
|
---|
48 | #define TEST(Test, Type, Fmt, Fun, iTest) \
|
---|
49 | do \
|
---|
50 | { \
|
---|
51 | Type Result; \
|
---|
52 | int rc = Fun(Test.psz, &Result); \
|
---|
53 | if (Result != Test.Result) \
|
---|
54 | { \
|
---|
55 | RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
|
---|
56 | cErrors++; \
|
---|
57 | } \
|
---|
58 | else if (rc != Test.rc) \
|
---|
59 | { \
|
---|
60 | RTPrintf("failure: '%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
|
---|
61 | cErrors++; \
|
---|
62 | } \
|
---|
63 | } while (0)
|
---|
64 |
|
---|
65 |
|
---|
66 | #define RUN_TESTS(aTests, Type, Fmt, Fun) \
|
---|
67 | do \
|
---|
68 | { \
|
---|
69 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
|
---|
70 | { \
|
---|
71 | TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
|
---|
72 | } \
|
---|
73 | } while (0)
|
---|
74 |
|
---|
75 |
|
---|
76 | int main()
|
---|
77 | {
|
---|
78 | /** @todo r=bird: IPRT init is missing. Use the RTTest framework (see any
|
---|
79 | * tstRT*.cpp file for examples of this). */
|
---|
80 | int cErrors = 0;
|
---|
81 |
|
---|
82 | static const struct TstU32 aTstU32[] =
|
---|
83 | {
|
---|
84 | { "asdf", VERR_NO_DIGITS, 0 },
|
---|
85 | { "asdf234", VERR_NO_DIGITS, 0 },
|
---|
86 | { "123", VINF_SUCCESS, 123 },
|
---|
87 | { "45.63", VINF_SUCCESS, 4563 },
|
---|
88 | { "68.54.123", VINF_SUCCESS, 6854123 },
|
---|
89 | { "1.0.3-asdf", VINF_SUCCESS, 103 },
|
---|
90 | { "aasdf-1", VINF_SUCCESS, 1 },
|
---|
91 | { "qwer-123.34", VINF_SUCCESS, 12334 },
|
---|
92 | { "aasdf45-r4545-5", VINF_SUCCESS, 5 },
|
---|
93 | { "foo41-r2431-6.9.8", VINF_SUCCESS, 698 },
|
---|
94 | { "bar43-r3517-7.1.2-beta", VINF_SUCCESS, 712 },
|
---|
95 | { "bar43-r3517-7.1.2.53412344556-beta", VWRN_NUMBER_TOO_BIG, 0 },
|
---|
96 | };
|
---|
97 | RUN_TESTS(aTstU32, uint32_t, "%#ld", RTStrVersionToUInt32);
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Summary.
|
---|
101 | */
|
---|
102 | if (!cErrors)
|
---|
103 | RTPrintf("tstStrToVer: SUCCESS\n");
|
---|
104 | else
|
---|
105 | RTPrintf("tstStrToVer: FAILURE - %d errors\n", cErrors);
|
---|
106 | return !!cErrors;
|
---|
107 | }
|
---|