VirtualBox

Changeset 25014 in vbox


Ignore:
Timestamp:
Nov 26, 2009 3:26:36 PM (15 years ago)
Author:
vboxsync
Message:

RTStrVersionCompare: made it return the same as RTStrICmp (-1,0,1).

Location:
trunk
Files:
4 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/string.h

    r25011 r25014  
    10531053
    10541054/**
    1055  * Compares two version strings stricmp fashion.
     1055 * Compares two version strings RTStrICmp fashion.
    10561056 *
    10571057 * The version string is split up into sections at punctuation, spaces,
     
    10601060 * in a numeric or case insesntivie fashion depending on what they are.
    10611061 *
    1062  * The following strings are considered to be equal: "1.0.0", "1.0", "1".
    1063  * There aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3"
    1064  *
    1065  * @returns integer value indicating the relationship between the versions:
    1066  * @retval  0, if both versions are equal.
    1067  * @retval  1, if pszVer1 is greater.
    1068  * @retval  2, if pszVer2 is greater.
     1062 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
     1063 * "1".  These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
     1064 *
     1065 * @returns < 0 if the first string less than the second string.
     1066 * @returns 0 if the first string identical to the second string.
     1067 * @returns > 0 if the first string greater than the second string.
    10691068 *
    10701069 * @param   pszVer1     First version string to compare.
  • trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibHostVersion.cpp

    r25005 r25014  
    133133    if (RT_SUCCESS(rc) && *pfUpdate)
    134134    {
    135         if (RTStrVersionCompare(*ppszHostVersion, *ppszGuestVersion) == 1) /* Is host version greater than guest add version? */
     135        if (RTStrVersionCompare(*ppszHostVersion, *ppszGuestVersion) > 0) /* Is host version greater than guest add version? */
    136136        {
    137137            /* Yay, we have an update! */
  • trunk/src/VBox/Runtime/common/string/strversion.cpp

    r25005 r25014  
    111111     * Do a parallel parse of the strings.
    112112     */
    113     int iRes = 0;
    114113    while (*pszVer1 || *pszVer2)
    115114    {
     
    127126        {
    128127            if (uVal1 != uVal2)
    129             {
    130                 iRes = uVal1 > uVal2 ? 1 : 2;
    131                 break;
    132             }
     128                return uVal1 < uVal2 ? -1 : 1;
    133129        }
    134130        else if (   !fNumeric1 && fNumeric2 && uVal2 == 0 && cchBlock1 == 0
     
    144140                iDiff = cchBlock1 < cchBlock2 ? -1 : 1;
    145141            if (iDiff)
    146             {
    147                 iRes = iDiff > 0 ? 1 : 2;
    148                 break;
    149             }
     142                return iDiff < 0 ? -1 : 1;
    150143        }
    151144    }
    152     return iRes;
     145    return 0;
    153146}
    154147RT_EXPORT_SYMBOL(RTStrVersionCompare);
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r24633 r25014  
    9696        tstStrSimplePattern \
    9797        tstStrToNum \
    98         tstStrToVer \
     98        tstRTStrVersion \
    9999        tstSystemQueryOsInfo \
    100100        tstRTTemp \
     
    375375tstStrToNum_SOURCES = tstStrToNum.cpp
    376376
    377 tstStrToVer_SOURCES = tstStrToVer.cpp
     377tstRTStrVersion_TEMPLATE = VBOXR3TSTEXE
     378tstRTStrVersion_SOURCES = tstRTStrVersion.cpp
    378379
    379380tstSystemQueryOsInfo_SOURCES = tstSystemQueryOsInfo.cpp
  • trunk/src/VBox/Runtime/testcase/tstRTStrVersion.cpp

    r25010 r25014  
    2929 */
    3030
     31/*******************************************************************************
     32*   Header Files                                                               *
     33*******************************************************************************/
     34#include <iprt/string.h>
    3135
    32 #include <iprt/initterm.h>
    33 #include <iprt/string.h>
     36#include <iprt/test.h>
    3437#include <iprt/stream.h>
    3538
    3639
    37 struct TstU8
    38 {
    39     const char *pszVer1;
    40     const char *pszVer2;
    41     uint8_t     Result;
    42 };
    43 
    44 
    45 #define TEST(Test, Type, Fmt, Fun, iTest) \
    46     do \
    47     { \
    48         Type Result = Fun(Test.pszVer1, Test.pszVer2); \
    49         if (Result != Test.Result) \
    50         { \
    51             RTPrintf("failure: '%s' <-> '%s' -> " Fmt ", expected " Fmt ". (%s/%u)\n", Test.pszVer1, Test.pszVer2, Result, Test.Result, #Fun, iTest); \
    52             cErrors++; \
    53         } \
    54     } while (0)
    55 
    56 #define RUN_TESTS(aTests, Type, Fmt, Fun) \
    57     do \
    58     { \
    59         for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) \
    60         { \
    61             TEST(aTests[iTest], Type, Fmt, Fun, iTest); \
    62         } \
    63     } while (0)
    64 
    6540int main()
    6641{
    67     RTR3Init();
     42    RTTEST hTest;
     43    int rc = RTTestInitAndCreate("tstRTStrVersion", &hTest);
     44    if (rc)
     45        return rc;
     46    RTTestBanner(hTest);
    6847
    69     int cErrors = 0;
    70     static const struct TstU8 aTstU8[] =
     48    RTTestSub(hTest, "RTStrVersionCompare");
     49    static struct
     50    {
     51        const char *pszVer1;
     52        const char *pszVer2;
     53        int         iResult;
     54    } const aTests[] =
    7155    {
    7256        { "", "",                         0 },
     
    7559        { "12.foo006", "12.6",            1 }, /* "12.foo006" is bigger than "12.6" */
    7660        { "1", "1",                       0 },
    77         { "1", "100",                     2 },
     61        { "1", "100",                     -1},
    7862        { "100", "1",                     1 },
    79         { "3", "4",                       2 },
     63        { "3", "4",                       -1},
    8064        { "1", "0.1",                     1 },
    8165        { "1", "0.0.0.0.10000",           1 },
    8266        { "0100", "100",                  0 },
    8367        { "1.0.0", "1",                   0 },
    84         { "1.0.0", "100.0.0",             2 },
    85         { "1", "1.0.3.0",                 2 },
     68        { "1.0.0", "100.0.0",             -1},
     69        { "1", "1.0.3.0",                 -1},
    8670        { "1.4.5", "1.2.3",               1 },
    87         { "1.2.3", "1.4.5",               2 },
    88         { "1.2.3", "4.5.6",               2 },
     71        { "1.2.3", "1.4.5",               -1},
     72        { "1.2.3", "4.5.6",               -1},
    8973        { "1.0.4", "1.0.3",               1 },
    9074        { "0.1", "0.0.1",                 1 },
    91         { "0.0.1", "0.1.1",               2 },
     75        { "0.0.1", "0.1.1",               -1},
    9276        { "3.1.0", "3.0.14",              1 },
    93         { "2.0.12", "3.0.14",             2 },
     77        { "2.0.12", "3.0.14",             -1},
    9478        { "3.1", "3.0.22",                1 },
    95         { "3.0.14", "3.1.0",              2 },
     79        { "3.0.14", "3.1.0",              -1},
    9680        { "45.63", "04.560.30",           1 },
    9781        { "45.006", "45.6",               0 },
    9882        { "23.206", "23.06",              1 },
    99         { "23.2", "23.060",               2 },
     83        { "23.2", "23.060",               -1},
    10084
    101         { "VirtualBox-2.0.8-Beta2", "VirtualBox-2.0.8_Beta3-r12345", 2 },
     85        { "VirtualBox-2.0.8-Beta2", "VirtualBox-2.0.8_Beta3-r12345", -1},
    10286        { "VirtualBox-2.2.4-Beta2", "VirtualBox-2.2.2", 1 },
    10387        { "VirtualBox-2.2.4-Beta3", "VirtualBox-2.2.2-Beta4", 1 },
    104         { "VirtualBox-3.1.8-Alpha1", "VirtualBox-3.1.8-Alpha1-r61454", 2 },
    105         { "VirtualBox-3.1.0", "VirtualBox-3.1.2_Beta1", 2 },
    106         { "3.1.0_BETA-r12345", "3.1.2", 2 },
     88        { "VirtualBox-3.1.8-Alpha1", "VirtualBox-3.1.8-Alpha1-r61454", -1},
     89        { "VirtualBox-3.1.0", "VirtualBox-3.1.2_Beta1", -1},
     90        { "3.1.0_BETA-r12345", "3.1.2", -1},
    10791    };
    108     RUN_TESTS(aTstU8, int, "%#d", RTStrVersionCompare);
     92    for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
     93    {
     94        int iResult = RTStrVersionCompare(aTests[iTest].pszVer1, aTests[iTest].pszVer2);
     95        if (iResult != aTests[iTest].iResult)
     96            RTTestFailed(hTest, "#%u: '%s' <-> '%s' -> %d, expected %d",
     97                         iTest, aTests[iTest].pszVer1, aTests[iTest].pszVer2, iResult, aTests[iTest].iResult);
     98    }
    10999
    110100    /*
    111101     * Summary.
    112102     */
    113     if (!cErrors)
    114         RTPrintf("tstStrToVer: SUCCESS\n");
    115     else
    116         RTPrintf("tstStrToVer: FAILURE - %d errors\n", cErrors);
    117     return !!cErrors;
     103    return RTTestSummaryAndDestroy(hTest);
    118104}
     105
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette