VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTExprEval.cpp@ 99775

Last change on this file since 99775 was 98103, checked in by vboxsync, 22 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: tstRTExprEval.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTExprEval
4 */
5
6/*
7 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/expreval.h>
42
43#include <iprt/errcore.h>
44#include <iprt/string.h>
45#include <iprt/test.h>
46
47
48/*********************************************************************************************************************************
49* Global Variables *
50*********************************************************************************************************************************/
51static RTTEST g_hTest;
52static bool g_fQueryVariableExpected = false;
53
54
55
56/** @callbackmethodimpl{PFNRTEXPREVALQUERYVARIABLE} */
57static DECLCALLBACK(int) tstBasicQueryVariable(const char *pchName, size_t cchName, void *pvUser, char **ppszValue)
58{
59 RTTESTI_CHECK(pvUser == (void *)g_hTest);
60 RTTESTI_CHECK_RET(g_fQueryVariableExpected, VERR_WRONG_ORDER);
61
62#define MATCH_VAR(a_szVariable) \
63 (cchName == sizeof(a_szVariable) - 1 && memcmp(pchName, a_szVariable, sizeof(a_szVariable) - 1) == 0)
64 const char *pszValue;
65 if (MATCH_VAR("MYVAR1"))
66 pszValue = "42";
67 else if (MATCH_VAR("MYVAR2"))
68 pszValue = "string";
69 else if (MATCH_VAR("MYNESTED1"))
70 pszValue = "MYVAR1";
71 else
72 return VERR_NOT_FOUND;
73
74 if (ppszValue)
75 return RTStrDupEx(ppszValue, pszValue);
76 return VINF_SUCCESS;
77}
78
79
80static void tstBasic(void)
81{
82 RTTestISub("Basics");
83
84 RTEXPREVAL hExprEval;
85 RTTESTI_CHECK_RC_RETV(RTExprEvalCreate(&hExprEval, 0, "basics", NULL, NULL), VINF_SUCCESS);
86 RTTESTI_CHECK_RETV(hExprEval != NIL_RTEXPREVAL);
87 RTTESTI_CHECK_RETV(RTExprEvalRelease(hExprEval) == 0);
88
89 RTTESTI_CHECK_RC_RETV(RTExprEvalCreate(&hExprEval, 0, "basics", g_hTest, tstBasicQueryVariable), VINF_SUCCESS);
90 RTTESTI_CHECK_RETV(hExprEval != NIL_RTEXPREVAL);
91
92 bool fResult;
93#define CHECK_fResult(a_fExpect) do { \
94 if (fResult != (a_fExpect)) RTTestIFailed("line %u: fResult=%RTbool, expected %RTbool", __LINE__, fResult, (a_fExpect)); \
95 } while (0)
96 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1"), &fResult, NULL), VINF_SUCCESS);
97 CHECK_fResult(true);
98 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("0"), &fResult, NULL), VINF_SUCCESS);
99 CHECK_fResult(false);
100
101 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("true"), &fResult, NULL), VINF_SUCCESS);
102 CHECK_fResult(true);
103 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("false"), &fResult, NULL), VINF_SUCCESS);
104 CHECK_fResult(false);
105
106 g_fQueryVariableExpected = true;
107 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("defined(MYVAR1)"), &fResult, NULL), VINF_SUCCESS);
108 CHECK_fResult(true);
109 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("defined(NO_SUCH_VARIABLE)"), &fResult, NULL), VINF_SUCCESS);
110 CHECK_fResult(false);
111 g_fQueryVariableExpected = false;
112
113 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 2.0"), &fResult, NULL), VINF_SUCCESS);
114 CHECK_fResult(true);
115 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 1.0"), &fResult, NULL), VINF_SUCCESS);
116 CHECK_fResult(false);
117 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 1.0.1"), &fResult, NULL), VINF_SUCCESS);
118 CHECK_fResult(true);
119
120 int64_t iResult;
121#define CHECK_iResult(a_iExpect) do { \
122 if (iResult != (a_iExpect)) RTTestIFailed("line %u: iResult=%#RX64, expected %#RX64", __LINE__, iResult, (a_iExpect)); \
123 } while (0)
124 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("1"), &iResult, NULL), VINF_SUCCESS);
125 CHECK_iResult(1);
126 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("0"), &iResult, NULL), VINF_SUCCESS);
127 CHECK_iResult(0);
128 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("123459876"), &iResult, NULL), VINF_SUCCESS);
129 CHECK_iResult(123459876);
130 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("123459876"), &iResult, NULL), VINF_SUCCESS);
131 CHECK_iResult(123459876);
132 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("-123459876"), &iResult, NULL), VINF_SUCCESS);
133 CHECK_iResult(-123459876);
134 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("0x7fffffffffffffff"), &iResult, NULL), VINF_SUCCESS);
135 CHECK_iResult(INT64_MAX);
136 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("-9223372036854775808"), &iResult, NULL), VINF_SUCCESS);
137 CHECK_iResult(INT64_MIN);
138
139 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("true"), &iResult, NULL), VINF_SUCCESS);
140 CHECK_iResult(1);
141 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false"), &iResult, NULL), VINF_SUCCESS);
142 CHECK_iResult(0);
143 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false + 2"), &iResult, NULL), VINF_SUCCESS);
144 CHECK_iResult(2);
145 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false - true"), &iResult, NULL), VINF_SUCCESS);
146 CHECK_iResult(-1);
147 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false - ((true))"), &iResult, NULL), VINF_SUCCESS);
148 CHECK_iResult(-1);
149 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false true - "), &iResult, NULL), VERR_PARSE_ERROR);
150 CHECK_iResult(INT64_MAX);
151 g_fQueryVariableExpected = true;
152 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("${MYVAR1} + 0"), &iResult, NULL), VINF_SUCCESS);
153 CHECK_iResult(42);
154 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("${${MYNESTED1}} + 2"), &iResult, NULL), VINF_SUCCESS);
155 CHECK_iResult(44);
156 g_fQueryVariableExpected = false;
157
158 char *pszResult;
159#define CHECK_FREE_pszResult(a_pszExpect) do { \
160 const char *pszMacroExpect = (a_pszExpect); \
161 if ( (pszResult != NULL || pszMacroExpect != NULL) \
162 && RTStrCmp(pszResult, pszMacroExpect) != 0 ) \
163 RTTestIFailed("line %u: pszResult=%s, expected %s", __LINE__, pszResult, pszMacroExpect); \
164 RTStrFree(pszResult); \
165 } while (0)
166 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("true"), &pszResult, NULL), VINF_SUCCESS);
167 CHECK_FREE_pszResult("true");
168 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("false"), &pszResult, NULL), VINF_SUCCESS);
169 CHECK_FREE_pszResult("false");
170 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("1+2"), &pszResult, NULL), VINF_SUCCESS);
171 CHECK_FREE_pszResult("3");
172
173 RTTESTI_CHECK_RETV(RTExprEvalRelease(hExprEval) == 0);
174}
175
176
177
178int main()
179{
180 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTExprEval", &g_hTest);
181 if (rcExit != RTEXITCODE_SUCCESS)
182 return rcExit;
183
184 tstBasic();
185
186 /*
187 * Summary.
188 */
189 return RTTestSummaryAndDestroy(g_hTest);
190}
191
Note: See TracBrowser for help on using the repository browser.

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