summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qstring
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-10-11 15:01:06 +0200
committerMarc Mutz <marc.mutz@kdab.com>2015-12-08 23:48:12 +0000
commit6de49f4ab6e6c8c6e664f7dd6c8c441bf7685a56 (patch)
tree27ea49daa4e7cf5edeb7b0f0660b6d3bc410b52b /tests/auto/corelib/tools/qstring
parent2baeeb4026e55955a329480b2550a4353291f8ca (diff)
QString: where possible, re-use existing capacity in op(QChar/QL1S)
If the LHS is detached and has existing capacity that is large enough to hold the RHS, re-use the memory instead of allocating a new buffer and throwing away the old. Change-Id: I53d42825da92c264c7301e8e771cba9fb35c321b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qstring')
-rw-r--r--tests/auto/corelib/tools/qstring/tst_qstring.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp
index cc9f250be9..907dcf17e1 100644
--- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp
@@ -582,6 +582,7 @@ private slots:
void compareQLatin1Strings();
void fromQLatin1StringWithLength();
void assignQLatin1String();
+ void assignQChar();
void isRightToLeft_data();
void isRightToLeft();
void unicodeStrings();
@@ -6598,6 +6599,71 @@ void tst_QString::assignQLatin1String()
QCOMPARE(foo.size(), latin1subfoo.size());
QCOMPARE(foo, QString::fromLatin1("foo"));
+ // check capacity re-use:
+ QString s;
+ QCOMPARE(s.capacity(), 0);
+
+ // assign to null QString:
+ s = latin1foo;
+ QCOMPARE(s, QString::fromLatin1("foo"));
+ QCOMPARE(s.capacity(), 3);
+
+ // assign to non-null QString with enough capacity:
+ s = QString::fromLatin1("foofoo");
+ const int capacity = s.capacity();
+ s = latin1foo;
+ QCOMPARE(s, QString::fromLatin1("foo"));
+ QCOMPARE(s.capacity(), capacity);
+
+ // assign to shared QString (enough capacity, but can't use):
+ s = QString::fromLatin1("foofoo");
+ QString s2 = s;
+ s = latin1foo;
+ QCOMPARE(s, QString::fromLatin1("foo"));
+ QCOMPARE(s.capacity(), 3);
+
+ // assign to QString with too little capacity:
+ s = QString::fromLatin1("fo");
+ QCOMPARE(s.capacity(), 2);
+ s = latin1foo;
+ QCOMPARE(s, QString::fromLatin1("foo"));
+ QCOMPARE(s.capacity(), 3);
+
+}
+
+void tst_QString::assignQChar()
+{
+ const QChar sp = QLatin1Char(' ');
+ QString s;
+ QCOMPARE(s.capacity(), 0);
+
+ // assign to null QString:
+ s = sp;
+ QCOMPARE(s, QString(sp));
+ QCOMPARE(s.capacity(), 1);
+
+ // assign to non-null QString with enough capacity:
+ s = QLatin1String("foo");
+ const int capacity = s.capacity();
+ QCOMPARE(capacity, 3);
+ s = sp;
+ QCOMPARE(s, QString(sp));
+ QCOMPARE(s.capacity(), capacity);
+
+ // assign to shared QString (enough capacity, but can't use):
+ s = QLatin1String("foo");
+ QString s2 = s;
+ s = sp;
+ QCOMPARE(s, QString(sp));
+ QCOMPARE(s.capacity(), 1);
+
+ // assign to empty QString:
+ s = QString("");
+ s.detach();
+ QCOMPARE(s.capacity(), 0);
+ s = sp;
+ QCOMPARE(s, QString(sp));
+ QCOMPARE(s.capacity(), 1);
}
void tst_QString::isRightToLeft_data()