From 6ce954a791a73abc8d32765e2695ed153e120c47 Mon Sep 17 00:00:00 2001 From: rsgowman Date: Thu, 22 Feb 2018 09:42:40 -0500 Subject: Serialize (and deserialize) int64 values (#818) (#829) --- .../firebase/firestore/model/field_value_test.cc | 2 +- .../firebase/firestore/remote/serializer_test.cc | 71 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) (limited to 'Firestore/core/test') diff --git a/Firestore/core/test/firebase/firestore/model/field_value_test.cc b/Firestore/core/test/firebase/firestore/model/field_value_test.cc index 86eb804..5a64d59 100644 --- a/Firestore/core/test/firebase/firestore/model/field_value_test.cc +++ b/Firestore/core/test/firebase/firestore/model/field_value_test.cc @@ -57,7 +57,7 @@ TEST(FieldValue, NumberType) { const FieldValue integer_value = FieldValue::IntegerValue(10L); const FieldValue double_value = FieldValue::DoubleValue(10.1); EXPECT_EQ(Type::Double, nan_value.type()); - EXPECT_EQ(Type::Long, integer_value.type()); + EXPECT_EQ(Type::Integer, integer_value.type()); EXPECT_EQ(Type::Double, double_value.type()); EXPECT_TRUE(nan_value < integer_value); EXPECT_TRUE(nan_value < double_value); diff --git a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc index 79aba85..7607911 100644 --- a/Firestore/core/test/firebase/firestore/remote/serializer_test.cc +++ b/Firestore/core/test/firebase/firestore/remote/serializer_test.cc @@ -38,6 +38,7 @@ #include #include +#include #include #include "Firestore/core/src/firebase/firestore/model/field_value.h" @@ -134,6 +135,76 @@ TEST_F(SerializerTest, EncodesBoolProtoToBytes) { } } +TEST_F(SerializerTest, EncodesIntegersModelToProto) { + std::vector testCases{0, + 1, + -1, + 100, + -100, + std::numeric_limits::min(), + std::numeric_limits::max()}; + for (int64_t test : testCases) { + FieldValue model = FieldValue::IntegerValue(test); + Serializer::TypedValue proto{FieldValue::Type::Integer, + google_firestore_v1beta1_Value_init_default}; + proto.value.integer_value = test; + ExpectRoundTrip(model, proto, FieldValue::Type::Integer); + } +} + +TEST_F(SerializerTest, EncodesIntegersProtoToBytes) { + // NB: because we're calculating the bytes via protoc (instead of + // libprotobuf) we have to look at values from numeric_limits ahead of + // time. So these test cases make the following assumptions about + // numeric_limits: (linking libprotobuf is starting to sound like a better + // idea. :) + // -9223372036854775808 + // == -8000000000000000 + // == numeric_limits::min() + // 9223372036854775807 + // == 7FFFFFFFFFFFFFFF + // == numeric_limits::max() + // + // For now, lets at least verify these values: + EXPECT_EQ(-9223372036854775807 - 1, std::numeric_limits::min()); + EXPECT_EQ(9223372036854775807, std::numeric_limits::max()); + + struct TestCase { + int64_t value; + std::vector bytes; + }; + + std::vector cases{ + // TEXT_FORMAT_PROTO: 'integer_value: 0' + TestCase{0, {0x10, 0x00}}, + // TEXT_FORMAT_PROTO: 'integer_value: 1' + TestCase{1, {0x10, 0x01}}, + // TEXT_FORMAT_PROTO: 'integer_value: -1' + TestCase{ + -1, + {0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}}, + // TEXT_FORMAT_PROTO: 'integer_value: 100' + TestCase{100, {0x10, 0x64}}, + // TEXT_FORMAT_PROTO: 'integer_value: -100' + TestCase{ + -100, + {0x10, 0x9c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}}, + // TEXT_FORMAT_PROTO: 'integer_value: -9223372036854775808' + TestCase{ + -9223372036854775807 - 1, + {0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}}, + // TEXT_FORMAT_PROTO: 'integer_value: 9223372036854775807' + TestCase{9223372036854775807, + {0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}}}; + + for (const TestCase& test : cases) { + Serializer::TypedValue proto{FieldValue::Type::Integer, + google_firestore_v1beta1_Value_init_default}; + proto.value.integer_value = test.value; + ExpectRoundTrip(proto, test.bytes, FieldValue::Type::Integer); + } +} + // TODO(rsgowman): Test [en|de]coding multiple protos into the same output // vector. -- cgit v1.2.3