Exemple #1
0
		void testAutocasting() {
			cout << "testAutocasting" << endl;

			BSONObj o;
			o.add("long", (__int64)1L);
			o.add("double", 2.0);
			o.add("int", 1);
			o.add("char*", (char*)"Test");

			BSONObj inner;
			inner.add("text", "text");
			o.add("inner", inner);

			TEST_ASSERT(o.getContent("long") != NULL);
			TEST_ASSERT((__int64)*o.getContent("long") == 1L);

			TEST_ASSERT(o.getContent("double") != NULL);
			TEST_ASSERT((double)*o.getContent("double") == 2.0);

			TEST_ASSERT(o.getContent("int") != NULL);
			TEST_ASSERT((int)*o.getContent("int") == 1);

			TEST_ASSERT(o.getContent("inner") != NULL);
			BSONObj* obj = *o.getContent("inner");
			TEST_ASSERT(strcmp(obj->getString("text"), "text") == 0);
		}
Exemple #2
0
bool BSONObj::operator !=(const BSONObj& obj) const {
	if (this->has("_id") && obj.has("_id")) {
		BSONContent* idThis = this->getContent("_id");
		BSONContent* idOther = obj.getContent("_id");

		return (*idThis != *idOther);
	}

	// Element count
	if (this->length() != obj.length()) {
		return true;
	}
	for (BSONObj::const_iterator it = this->begin(); it != this->end(); it++) {
		std::string key = it->first;
		BSONContent* content = it->second;

		BSONContent* other = obj.getContent(key);
		if (other == NULL) {
			return true;
		}
		if (content->type() != other->type()) {
			return true;
		}
		bool result;
		switch (content->type()) {
			case BOOL_TYPE: {
									result = *((BSONContentBoolean*)content) != *((BSONContentBoolean*)other);
									break;
								}
			case INT_TYPE: {
									result = *((BSONContentInt*)content) != *((BSONContentInt*)other);
									break;
								}
			case DOUBLE_TYPE: {
										result = *((BSONContentDouble*)content) != *((BSONContentDouble*)other);
										break;
									}
			case LONG64_TYPE: 
			case LONG_TYPE: {
									 result = *((BSONContentLong*)content) != *((BSONContentLong*)other);
									 break;
								 }
			case PTRCHAR_TYPE: 
			case STRING_TYPE: {
										result = *((BSONContentString*)content) != *((BSONContentString*)other);
										break;
									}
			case BSON_TYPE: {
									 result = *((BSONContentBSON*)content) != *((BSONContentBSON*)other);
									 break;
								 }
			case BSONARRAY_TYPE: {
											result = *((BSONContentBSONArray*)content) != *((BSONContentBSONArray*)other);
											break;
										}
			case NULL_TYPE: {
									 result = true;
									 break;
								 }
			case UNKNOWN_TYPE: 
			default:
										{
											result = *((BSONContentInt*)content) != *((BSONContentInt*)other);
											break;
										}
		}
		if (result) {
			return result;
		}
	}
	return false;
}