summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/global/qgetputenv
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2016-08-03 16:04:22 -0700
committerLiang Qi <liang.qi@qt.io>2017-06-01 06:02:10 +0000
commitc214c000ccebda30ed867934a9d56af29cb34264 (patch)
tree7d12d148cdd7c7ac8511816bde77e003a7777e2d /tests/auto/corelib/global/qgetputenv
parente3bc01b0e339aa3730c619fb02201aa09468d557 (diff)
qEnvironmentVariableIntValue: fix the case of a non-numeric value
The documentation says that it's equivalent to qgetenv(varName).toInt() But the implementation wasn't. QByteArray::toInt() verifies that the entire string was consumed, so QByteArray("1a").toInt() == 0, but qstrtoll alone doesn't. That is, qstrtoll("1a", ...) == 1. The implementation also detected the base, a behavior I kept. Instead, I updated the documentation. Change-Id: I0031aa609e714ae983c3fffd14676ea6061a9268 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'tests/auto/corelib/global/qgetputenv')
-rw-r--r--tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
index 66fc578d5f..09abb953ba 100644
--- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
+++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp
@@ -97,17 +97,33 @@ void tst_QGetPutEnv::intValue_data()
QTest::addColumn<int>("expected");
QTest::addColumn<bool>("ok");
- // most non-success cases already tested in getSetCheck()
+ // some repetition from what is tested in getSetCheck()
+ QTest::newRow("empty") << QByteArray() << 0 << false;
+ QTest::newRow("spaces-heading") << QByteArray(" 1") << 1 << true;
+ QTest::newRow("spaces-trailing") << QByteArray("1 ") << 0 << false;
#define ROW(x, i, b) \
QTest::newRow(#x) << QByteArray(#x) << (i) << (b)
ROW(auto, 0, false);
+ ROW(1auto, 0, false);
ROW(0, 0, true);
+ ROW(+0, 0, true);
ROW(1, 1, true);
+ ROW(+1, 1, true);
+ ROW(09, 0, false);
ROW(010, 8, true);
ROW(0x10, 16, true);
+ ROW(0x, 0, false);
+ ROW(0xg, 0, false);
+ ROW(0x1g, 0, false);
+ ROW(000000000000000000000000000000000000000000000000001, 0, false);
+ ROW(+000000000000000000000000000000000000000000000000001, 0, false);
+ ROW(000000000000000000000000000000000000000000000000001g, 0, false);
+ ROW(-0, 0, true);
ROW(-1, -1, true);
ROW(-010, -8, true);
+ ROW(-000000000000000000000000000000000000000000000000001, 0, false);
+ ROW(2147483648, 0, false);
// ROW(0xffffffff, -1, true); // could be expected, but not how QByteArray::toInt() works
ROW(0xffffffff, 0, false);
const int bases[] = {10, 8, 16};
@@ -125,6 +141,7 @@ void tst_QGetPutEnv::intValue_data()
void tst_QGetPutEnv::intValue()
{
+ const int maxlen = (sizeof(int) * CHAR_BIT + 2) / 3;
const char varName[] = "should_not_exist";
QFETCH(QByteArray, value);
@@ -133,6 +150,13 @@ void tst_QGetPutEnv::intValue()
bool actualOk = !ok;
+ // Self-test: confirm that it was like the docs said it should be
+ if (value.length() < maxlen) {
+ QCOMPARE(value.toInt(&actualOk, 0), expected);
+ QCOMPARE(actualOk, ok);
+ }
+
+ actualOk = !ok;
QVERIFY(qputenv(varName, value));
QCOMPARE(qEnvironmentVariableIntValue(varName), expected);
QCOMPARE(qEnvironmentVariableIntValue(varName, &actualOk), expected);