aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/test
diff options
context:
space:
mode:
authorGravatar rsgowman <rgowman@google.com>2018-02-22 09:42:40 -0500
committerGravatar GitHub <noreply@github.com>2018-02-22 09:42:40 -0500
commit6ce954a791a73abc8d32765e2695ed153e120c47 (patch)
treee9fe172b309156e2e4bc147c0452ea712a0102b5 /Firestore/core/test
parent41dcd4bd17708779b45982d9547215924f2e5fd5 (diff)
Serialize (and deserialize) int64 values (#818) (#829)
Diffstat (limited to 'Firestore/core/test')
-rw-r--r--Firestore/core/test/firebase/firestore/model/field_value_test.cc2
-rw-r--r--Firestore/core/test/firebase/firestore/remote/serializer_test.cc71
2 files changed, 72 insertions, 1 deletions
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 <pb.h>
#include <pb_encode.h>
+#include <limits>
#include <vector>
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
@@ -134,6 +135,76 @@ TEST_F(SerializerTest, EncodesBoolProtoToBytes) {
}
}
+TEST_F(SerializerTest, EncodesIntegersModelToProto) {
+ std::vector<int64_t> testCases{0,
+ 1,
+ -1,
+ 100,
+ -100,
+ std::numeric_limits<int64_t>::min(),
+ std::numeric_limits<int64_t>::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<T> 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<int64_t>::min()
+ // 9223372036854775807
+ // == 7FFFFFFFFFFFFFFF
+ // == numeric_limits<int64_t>::max()
+ //
+ // For now, lets at least verify these values:
+ EXPECT_EQ(-9223372036854775807 - 1, std::numeric_limits<int64_t>::min());
+ EXPECT_EQ(9223372036854775807, std::numeric_limits<int64_t>::max());
+
+ struct TestCase {
+ int64_t value;
+ std::vector<uint8_t> bytes;
+ };
+
+ std::vector<TestCase> 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.