diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2011-11-24 14:55:11 +0100 |
---|---|---|
committer | Knoll Lars <lars.knoll@nokia.com> | 2011-11-25 12:54:06 +0100 |
commit | b98703ca4720f82eaa0b6c2b3ab9e3477de04929 (patch) | |
tree | 50ee76b8be80c0ac1491a795da8f9d78906530aa | |
parent | 72b3fda971684ce59c6b02fd2eb61161411584d2 (diff) |
Added more tests for null values, arrays and objects
Also fixed the resulting test failures.
Change-Id: I92739c14dcfb29ca92e766c83babca2eb3ddbdc2
Reviewed-by: Knoll Lars <lars.knoll@nokia.com>
-rw-r--r-- | src/qbinaryjsonarray.cpp | 9 | ||||
-rw-r--r-- | src/qbinaryjsonobject.cpp | 36 | ||||
-rw-r--r-- | src/qbinaryjsonvalue.cpp | 9 | ||||
-rw-r--r-- | src/qbinaryjsonvalue.h | 1 | ||||
-rw-r--r-- | tests/auto/tst_qtbinaryjson.cpp | 85 |
5 files changed, 126 insertions, 14 deletions
diff --git a/src/qbinaryjsonarray.cpp b/src/qbinaryjsonarray.cpp index 2bd7237..a676652 100644 --- a/src/qbinaryjsonarray.cpp +++ b/src/qbinaryjsonarray.cpp @@ -87,7 +87,7 @@ JsonValue JsonArray::first() const JsonValue JsonArray::last() const { - return at(a->length - 1); + return at(a ? (a->length - 1) : 0); } void JsonArray::append(const JsonValue &value) @@ -160,10 +160,13 @@ bool JsonArray::operator==(const JsonArray &other) const if (a == other.a) return true; - if (!a || !other.a || a->length != other.a->length) + if (!a) + return other.a->length == 0; + if (!other.a) + return a->length == 0; + if (a->length != other.a->length) return false; - for (int i = 0; i < a->length; ++i) { if (d->toValue(a->at(i)) != other.d->toValue(other.a->at(i))) return false; diff --git a/src/qbinaryjsonobject.cpp b/src/qbinaryjsonobject.cpp index 93bd237..658e5b3 100644 --- a/src/qbinaryjsonobject.cpp +++ b/src/qbinaryjsonobject.cpp @@ -64,7 +64,7 @@ JsonObject JsonObject::fromData(const QByteArray &data) JsonObject JsonObject::fromVariantMap(const QVariantMap &map) { - // ### this is implemented the trivial way, not the most effiecent way + // ### this is implemented the trivial way, not the most efficient way JsonObject object; for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) @@ -118,8 +118,10 @@ void JsonObject::insert(const QString &key, const JsonValue &value) { Value *v = value.v; + int valueSize = v ? v->size : sizeof(Value); + int valueOffset = sizeof(Entry) + sizeof(QStringData) + key.length()*sizeof(ushort); - int requiredSize = valueOffset + v->size; + int requiredSize = valueOffset + valueSize; detach(requiredSize + sizeof(offset)); // offset for the new index entry @@ -135,7 +137,14 @@ void JsonObject::insert(const QString &key, const JsonValue &value) e->size = requiredSize; createConstString(&e->keyString, key); e->valueOffset = valueOffset; - memcpy(e->value(), v, v->size); + if (v) { + memcpy(e->value(), v, v->size); + } else { + Value *n = e->value(); + n->type = NullValue; + n->val = 0; + n->size = sizeof(Value); + } } void JsonObject::remove(const QString &key) @@ -153,6 +162,9 @@ void JsonObject::remove(const QString &key) JsonValue JsonObject::take(const QString &key) { + if (!o) + return JsonValue(); + int index = o->indexOf(key); if (index < 0) return JsonValue(); @@ -165,17 +177,15 @@ JsonValue JsonObject::take(const QString &key) bool JsonObject::contains(const QString &key) { - for (uint i = 0; i < o->length; ++i) { - Entry *e = o->entryAt(i); - if (e->shallowKey() == key) - return true; - } - return false; + if (!o) + return false; + + return o->indexOf(key) >= 0; } QByteArray JsonObject::data() const { - if (!d->rawData) + if (!d || !d->rawData) return QByteArray(); return QByteArray(d->rawData, d->_header->size); @@ -186,7 +196,11 @@ bool JsonObject::operator==(const JsonObject &other) const if (o == other.o) return true; - if (!o || !other.o || o->length != other.o->length) + if (!o) + return other.o->length == 0; + if (!other.o) + return o->length == 0; + if (o->length != other.o->length) return false; for (uint i = 0; i < o->length; ++i) { diff --git a/src/qbinaryjsonvalue.cpp b/src/qbinaryjsonvalue.cpp index 5288909..3f78a06 100644 --- a/src/qbinaryjsonvalue.cpp +++ b/src/qbinaryjsonvalue.cpp @@ -44,6 +44,15 @@ JsonValue::JsonValue(const QString &s) d->ref.ref(); } +JsonValue::JsonValue(const QLatin1String &s) +{ + // ### FIXME: Avoid creating the temp QString below + v = Value::fromString(QString(s)); + d = new Data((char *)v, v->size); + d->ref.ref(); + +} + JsonValue::JsonValue(const JsonArray &a) : d(0), v(0) { diff --git a/src/qbinaryjsonvalue.h b/src/qbinaryjsonvalue.h index da47b55..e3ca1b5 100644 --- a/src/qbinaryjsonvalue.h +++ b/src/qbinaryjsonvalue.h @@ -13,6 +13,7 @@ public: JsonValue(bool b); JsonValue(double n); JsonValue(const QString &s); + JsonValue(const QLatin1String &s); JsonValue(const JsonArray &a); JsonValue(const JsonObject &o); diff --git a/tests/auto/tst_qtbinaryjson.cpp b/tests/auto/tst_qtbinaryjson.cpp index 9dd0c37..b0c00ab 100644 --- a/tests/auto/tst_qtbinaryjson.cpp +++ b/tests/auto/tst_qtbinaryjson.cpp @@ -64,6 +64,10 @@ private Q_SLOTS: void testObjectNested(); void testArrayNested(); + void nullValues(); + void nullArrays(); + void nullObject(); + void fromVariantMap(); void toVariantMap(); }; @@ -188,6 +192,9 @@ void TestQtBinaryJson::testArraySimple() QCOMPARE(array.size(), size); QCOMPARE(array.last().type(), NullValue); QCOMPARE(array.last(), JsonValue()); + + QCOMPARE(array.at(-1), JsonValue()); + QCOMPARE(array.at(array.size()), JsonValue()); } void TestQtBinaryJson::testValueObject() @@ -297,6 +304,84 @@ void TestQtBinaryJson::testArrayNested() QCOMPARE(outer.last().toArray().last().toArray().at(0).toString(), QString("nested")); } +void TestQtBinaryJson::nullValues() +{ + JsonArray array; + array.append(JsonValue()); + + QCOMPARE(array.size(), 1); + QCOMPARE(array.at(0), JsonValue()); + + JsonObject object; + object.insert(QString("key"), JsonValue()); + QCOMPARE(object.contains("key"), true); + QCOMPARE(object.numKeys(), 1); + QCOMPARE(object.value("key"), JsonValue()); +} + +void TestQtBinaryJson::nullArrays() +{ + JsonArray nullArray; + JsonArray nonNull; + nonNull.append(QLatin1String("bar")); + + QCOMPARE(nullArray, JsonArray()); + QVERIFY(nullArray != nonNull); + QVERIFY(nonNull != nullArray); + + QCOMPARE(nullArray.size(), 0); + QCOMPARE(nullArray.takeAt(0), JsonValue()); + QCOMPARE(nullArray.first(), JsonValue()); + QCOMPARE(nullArray.last(), JsonValue()); + nullArray.removeAt(0); + nullArray.removeAt(-1); + + nullArray.append(QString("bar")); + nullArray.removeAt(0); + + QCOMPARE(nullArray.size(), 0); + QCOMPARE(nullArray.takeAt(0), JsonValue()); + QCOMPARE(nullArray.first(), JsonValue()); + QCOMPARE(nullArray.last(), JsonValue()); + nullArray.removeAt(0); + nullArray.removeAt(-1); + + nullArray.detach(16); + QCOMPARE(nullArray, JsonArray()); +} + +void TestQtBinaryJson::nullObject() +{ + JsonObject nullObject; + JsonObject nonNull; + nonNull.insert(QLatin1String("foo"), QLatin1String("bar")); + + QCOMPARE(nullObject, JsonObject()); + QVERIFY(nullObject != nonNull); + QVERIFY(nonNull != nullObject); + + QCOMPARE(nullObject.numKeys(), 0); + QCOMPARE(nullObject.keys(), QStringList()); + nullObject.remove("foo"); + QCOMPARE(nullObject, JsonObject()); + QCOMPARE(nullObject.take("foo"), JsonValue()); + QCOMPARE(nullObject.contains("foo"), false); + + nullObject.detach(16); + QCOMPARE(nullObject, JsonObject()); + + nullObject.insert("foo", QString("bar")); + nullObject.remove("foo"); + + QCOMPARE(nullObject.numKeys(), 0); + QCOMPARE(nullObject.keys(), QStringList()); + nullObject.remove("foo"); + QCOMPARE(nullObject, JsonObject()); + QCOMPARE(nullObject.take("foo"), JsonValue()); + QCOMPARE(nullObject.contains("foo"), false); +} + + void TestQtBinaryJson::fromVariantMap() { QVariantMap map; |