1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "google/cloud/spanner/value.h"
16 #include "google/cloud/testing_util/assert_ok.h"
17 #include "google/cloud/testing_util/is_proto_equal.h"
18 #include "google/cloud/testing_util/status_matchers.h"
19 #include "absl/types/optional.h"
20 #include <google/protobuf/text_format.h>
21 #include <gmock/gmock.h>
22 #include <chrono>
23 #include <cmath>
24 #include <ios>
25 #include <limits>
26 #include <string>
27 #include <tuple>
28 #include <type_traits>
29 #include <vector>
30 
31 namespace google {
32 namespace cloud {
33 namespace spanner {
34 inline namespace SPANNER_CLIENT_NS {
35 namespace {
36 
37 using ::google::cloud::testing_util::IsProtoEqual;
38 using ::google::cloud::testing_util::StatusIs;
39 using ::testing::HasSubstr;
40 using ::testing::Not;
41 
42 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
MakeTimePoint(std::time_t sec,std::chrono::nanoseconds::rep nanos)43 MakeTimePoint(std::time_t sec, std::chrono::nanoseconds::rep nanos) {
44   return std::chrono::system_clock::from_time_t(sec) +
45          std::chrono::nanoseconds(nanos);
46 }
47 
48 template <typename T>
TestBasicSemantics(T init)49 void TestBasicSemantics(T init) {
50   Value const default_ctor{};
51   EXPECT_FALSE(default_ctor.get<T>().ok());
52 
53   Value const v{init};
54 
55   EXPECT_STATUS_OK(v.get<T>());
56   EXPECT_EQ(init, *v.get<T>());
57 
58   Value copy = v;
59   EXPECT_EQ(copy, v);
60   Value const moved = std::move(copy);
61   EXPECT_EQ(moved, v);
62 
63   // Tests a null Value of type `T`.
64   Value const null = MakeNullValue<T>();
65 
66   EXPECT_FALSE(null.get<T>().ok());
67   EXPECT_STATUS_OK(null.get<absl::optional<T>>());
68   EXPECT_EQ(absl::optional<T>{}, *null.get<absl::optional<T>>());
69 
70   Value copy_null = null;
71   EXPECT_EQ(copy_null, null);
72   Value const moved_null = std::move(copy_null);
73   EXPECT_EQ(moved_null, null);
74 
75   // Round-trip from Value -> Proto(s) -> Value
76   auto const protos = internal::ToProto(v);
77   EXPECT_EQ(v, internal::FromProto(protos.first, protos.second));
78 
79   // Ensures that the protos for a NULL T have the same "type" as a non-null T.
80   auto const null_protos = internal::ToProto(null);
81   EXPECT_THAT(null_protos.first, IsProtoEqual(protos.first));
82   EXPECT_EQ(null_protos.second.null_value(),
83             google::protobuf::NullValue::NULL_VALUE);
84 
85   Value const not_null{absl::optional<T>(init)};
86   EXPECT_STATUS_OK(not_null.get<T>());
87   EXPECT_EQ(init, *not_null.get<T>());
88   EXPECT_STATUS_OK(not_null.get<absl::optional<T>>());
89   EXPECT_EQ(init, **not_null.get<absl::optional<T>>());
90 }
91 
TEST(Value,BasicSemantics)92 TEST(Value, BasicSemantics) {
93   for (auto x : {false, true}) {
94     SCOPED_TRACE("Testing: bool " + std::to_string(x));
95     TestBasicSemantics(x);
96     TestBasicSemantics(std::vector<bool>(5, x));
97     std::vector<absl::optional<bool>> v(5, x);
98     v.resize(10);
99     TestBasicSemantics(v);
100   }
101 
102   auto const min64 = std::numeric_limits<std::int64_t>::min();
103   auto const max64 = std::numeric_limits<std::int64_t>::max();
104   for (auto x : std::vector<std::int64_t>{min64, -1, 0, 1, max64}) {
105     SCOPED_TRACE("Testing: std::int64_t " + std::to_string(x));
106     TestBasicSemantics(x);
107     TestBasicSemantics(std::vector<std::int64_t>(5, x));
108     std::vector<absl::optional<std::int64_t>> v(5, x);
109     v.resize(10);
110     TestBasicSemantics(v);
111   }
112 
113   // Note: We skip testing the NaN case here because NaN always compares not
114   // equal, even with itself. So NaN is handled in a separate test.
115   auto const inf = std::numeric_limits<double>::infinity();
116   for (auto x : {-inf, -1.0, -0.5, 0.0, 0.5, 1.0, inf}) {
117     SCOPED_TRACE("Testing: double " + std::to_string(x));
118     TestBasicSemantics(x);
119     TestBasicSemantics(std::vector<double>(5, x));
120     std::vector<absl::optional<double>> v(5, x);
121     v.resize(10);
122     TestBasicSemantics(v);
123   }
124 
125   for (auto const& x :
126        std::vector<std::string>{"", "f", "foo", "12345678901234567"}) {
127     SCOPED_TRACE("Testing: std::string " + std::string(x));
128     TestBasicSemantics(x);
129     TestBasicSemantics(std::vector<std::string>(5, x));
130     std::vector<absl::optional<std::string>> v(5, x);
131     v.resize(10);
132     TestBasicSemantics(v);
133   }
134 
135   for (auto const& x : std::vector<Bytes>{Bytes(""), Bytes("f"), Bytes("foo"),
136                                           Bytes("12345678901234567")}) {
137     SCOPED_TRACE("Testing: Bytes " + x.get<std::string>());
138     TestBasicSemantics(x);
139     TestBasicSemantics(std::vector<Bytes>(5, x));
140     std::vector<absl::optional<Bytes>> v(5, x);
141     v.resize(10);
142     TestBasicSemantics(v);
143   }
144 
145   for (auto const& x : {
146            MakeNumeric(-0.9e29).value(),
147            MakeNumeric(-1).value(),
148            MakeNumeric(-1.0e-9).value(),
149            Numeric(),
150            MakeNumeric(1.0e-9).value(),
151            MakeNumeric(1U).value(),
152            MakeNumeric(0.9e29).value(),
153        }) {
154     SCOPED_TRACE("Testing: google::cloud::spanner::Numeric " + x.ToString());
155     TestBasicSemantics(x);
156     TestBasicSemantics(std::vector<Numeric>(5, x));
157     std::vector<absl::optional<Numeric>> v(5, x);
158     v.resize(10);
159     TestBasicSemantics(v);
160   }
161 
162   for (time_t t : {
163            -9223372035LL,   // near the limit of 64-bit/ns system_clock
164            -2147483649LL,   // below min 32-bit int
165            -2147483648LL,   // min 32-bit int
166            -1LL, 0LL, 1LL,  // around the unix epoch
167            1561147549LL,    // contemporary
168            2147483647LL,    // max 32-bit int
169            2147483648LL,    // above max 32-bit int
170            9223372036LL     // near the limit of 64-bit/ns system_clock
171        }) {
172     for (auto nanos : {-1, 0, 1}) {
173       auto ts = MakeTimestamp(MakeTimePoint(t, nanos)).value();
174       SCOPED_TRACE("Testing: google::cloud::spanner::Timestamp " +
175                    internal::TimestampToRFC3339(ts));
176       TestBasicSemantics(ts);
177       std::vector<Timestamp> v(5, ts);
178       TestBasicSemantics(v);
179       std::vector<absl::optional<Timestamp>> ov(5, ts);
180       ov.resize(10);
181       TestBasicSemantics(ov);
182     }
183   }
184 
185   for (auto x : {
186            absl::CivilDay(1582, 10, 15),  // start of Gregorian calendar
187            absl::CivilDay(1677, 9, 21),   // before system_clock limit
188            absl::CivilDay(1901, 12, 13),  // around min 32-bit seconds limit
189            absl::CivilDay(1970, 1, 1),    // the unix epoch
190            absl::CivilDay(2019, 6, 21),   // contemporary
191            absl::CivilDay(2038, 1, 19),   // around max 32-bit seconds limit
192            absl::CivilDay(2262, 4, 12)    // after system_clock limit
193        }) {
194     SCOPED_TRACE("Testing: absl::CivilDay " + absl::FormatCivilTime(x));
195     TestBasicSemantics(x);
196     TestBasicSemantics(std::vector<absl::CivilDay>(5, x));
197     std::vector<absl::optional<absl::CivilDay>> v(5, x);
198     v.resize(10);
199     TestBasicSemantics(v);
200   }
201 
202   // No for-loop because there's only one value of CommitTimestamp
203   auto const x = CommitTimestamp{};
204   TestBasicSemantics(x);
205   TestBasicSemantics(std::vector<CommitTimestamp>(5, x));
206   std::vector<absl::optional<CommitTimestamp>> v(5, x);
207   v.resize(10);
208   TestBasicSemantics(v);
209 }
210 
TEST(Value,Equality)211 TEST(Value, Equality) {
212   std::vector<std::pair<Value, Value>> test_cases = {
213       {Value(false), Value(true)},
214       {Value(0), Value(1)},
215       {Value(3.14), Value(42.0)},
216       {Value("foo"), Value("bar")},
217       {Value(Bytes("foo")), Value(Bytes("bar"))},
218       {Value(MakeNumeric(0).value()), Value(MakeNumeric(1).value())},
219       {Value(absl::CivilDay(1970, 1, 1)), Value(absl::CivilDay(2020, 3, 15))},
220       {Value(std::vector<double>{1.2, 3.4}),
221        Value(std::vector<double>{4.5, 6.7})},
222       {Value(std::make_tuple(false, 123, "foo")),
223        Value(std::make_tuple(true, 456, "bar"))},
224   };
225 
226   for (auto const& tc : test_cases) {
227     EXPECT_EQ(tc.first, tc.first);
228     EXPECT_EQ(tc.second, tc.second);
229     EXPECT_NE(tc.first, tc.second);
230     // Compares tc.first to tc2.second, which ensures that different "kinds" of
231     // value are never equal.
232     for (auto const& tc2 : test_cases) {
233       EXPECT_NE(tc.first, tc2.second);
234     }
235   }
236 }
237 
238 // NOTE: This test relies on unspecified behavior about the moved-from state
239 // of std::string. Specifically, this test relies on the fact that "large"
240 // strings, when moved-from, end up empty. And we use this fact to verify that
241 // spanner::Value::get<T>() correctly handles moves. If this test ever breaks
242 // on some platform, we could probably delete this, unless we can think of a
243 // better way to test move semantics.
TEST(Value,RvalueGetString)244 TEST(Value, RvalueGetString) {
245   using Type = std::string;
246   Type const data = std::string(128, 'x');
247   Value v(data);
248 
249   auto s = v.get<Type>();
250   EXPECT_STATUS_OK(s);
251   EXPECT_EQ(data, *s);
252 
253   s = std::move(v).get<Type>();
254   EXPECT_STATUS_OK(s);
255   EXPECT_EQ(data, *s);
256 
257   // NOLINTNEXTLINE(bugprone-use-after-move)
258   s = v.get<Type>();
259   EXPECT_STATUS_OK(s);
260   EXPECT_EQ("", *s);
261 }
262 
263 // NOTE: This test relies on unspecified behavior about the moved-from state
264 // of std::string. Specifically, this test relies on the fact that "large"
265 // strings, when moved-from, end up empty. And we use this fact to verify that
266 // spanner::Value::get<T>() correctly handles moves. If this test ever breaks
267 // on some platform, we could probably delete this, unless we can think of a
268 // better way to test move semantics.
TEST(Value,RvalueGetOptionalString)269 TEST(Value, RvalueGetOptionalString) {
270   using Type = absl::optional<std::string>;
271   Type const data = std::string(128, 'x');
272   Value v(data);
273 
274   auto s = v.get<Type>();
275   EXPECT_STATUS_OK(s);
276   EXPECT_EQ(*data, **s);
277 
278   s = std::move(v).get<Type>();
279   EXPECT_STATUS_OK(s);
280   EXPECT_EQ(*data, **s);
281 
282   // NOLINTNEXTLINE(bugprone-use-after-move)
283   s = v.get<Type>();
284   EXPECT_STATUS_OK(s);
285   EXPECT_EQ("", **s);
286 }
287 
288 // NOTE: This test relies on unspecified behavior about the moved-from state
289 // of std::string. Specifically, this test relies on the fact that "large"
290 // strings, when moved-from, end up empty. And we use this fact to verify that
291 // spanner::Value::get<T>() correctly handles moves. If this test ever breaks
292 // on some platform, we could probably delete this, unless we can think of a
293 // better way to test move semantics.
TEST(Value,RvalueGetVectorString)294 TEST(Value, RvalueGetVectorString) {
295   using Type = std::vector<std::string>;
296   Type const data(128, std::string(128, 'x'));
297   Value v(data);
298 
299   auto s = v.get<Type>();
300   EXPECT_STATUS_OK(s);
301   EXPECT_EQ(data, *s);
302 
303   s = std::move(v).get<Type>();
304   EXPECT_STATUS_OK(s);
305   EXPECT_EQ(data, *s);
306 
307   // NOLINTNEXTLINE(bugprone-use-after-move)
308   s = v.get<Type>();
309   EXPECT_STATUS_OK(s);
310   EXPECT_EQ(Type(data.size(), ""), *s);
311 }
312 
313 // NOTE: This test relies on unspecified behavior about the moved-from state
314 // of std::string. Specifically, this test relies on the fact that "large"
315 // strings, when moved-from, end up empty. And we use this fact to verify that
316 // spanner::Value::get<T>() correctly handles moves. If this test ever breaks
317 // on some platform, we could probably delete this, unless we can think of a
318 // better way to test move semantics.
TEST(Value,RvalueGetStructString)319 TEST(Value, RvalueGetStructString) {
320   using Type = std::tuple<std::pair<std::string, std::string>, std::string>;
321   Type data{std::make_pair("name", std::string(128, 'x')),
322             std::string(128, 'x')};
323   Value v(data);
324 
325   auto s = v.get<Type>();
326   EXPECT_STATUS_OK(s);
327   EXPECT_EQ(data, *s);
328 
329   s = std::move(v).get<Type>();
330   EXPECT_STATUS_OK(s);
331   EXPECT_EQ(data, *s);
332 
333   // NOLINTNEXTLINE(bugprone-use-after-move)
334   s = v.get<Type>();
335   EXPECT_STATUS_OK(s);
336   EXPECT_EQ(Type({"name", ""}, ""), *s);
337 }
338 
TEST(Value,DoubleNaN)339 TEST(Value, DoubleNaN) {
340   double const nan = std::nan("NaN");
341   Value v{nan};
342   EXPECT_TRUE(std::isnan(*v.get<double>()));
343 
344   // Since IEEE 754 defines that nan is not equal to itself, then a Value with
345   // NaN should not be equal to itself.
346   EXPECT_NE(nan, nan);
347   EXPECT_NE(v, v);
348 }
349 
TEST(Value,BytesDecodingError)350 TEST(Value, BytesDecodingError) {
351   Value const v(Bytes("some data"));
352   auto p = internal::ToProto(v);
353   EXPECT_EQ(v, internal::FromProto(p.first, p.second));
354 
355   // Now we corrupt the Value proto so that it cannot be decoded.
356   p.second.set_string_value("not base64 encoded data");
357   Value bad = internal::FromProto(p.first, p.second);
358   EXPECT_NE(v, bad);
359 
360   // We know the type is Bytes, but we cannot get a value out of it because the
361   // base64 decoding will fail.
362   StatusOr<Bytes> bytes = bad.get<Bytes>();
363   EXPECT_THAT(bytes,
364               StatusIs(Not(StatusCode::kOk), HasSubstr("Invalid base64")));
365 }
366 
TEST(Value,BytesRelationalOperators)367 TEST(Value, BytesRelationalOperators) {
368   Bytes b1(std::string(1, '\x00'));
369   Bytes b2(std::string(1, '\xff'));
370 
371   EXPECT_EQ(b1, b1);
372   EXPECT_NE(b1, b2);
373 }
374 
TEST(Value,ConstructionFromLiterals)375 TEST(Value, ConstructionFromLiterals) {
376   Value v_int64(42);
377   EXPECT_EQ(42, *v_int64.get<std::int64_t>());
378 
379   Value v_string("hello");
380   EXPECT_EQ("hello", *v_string.get<std::string>());
381 
382   std::vector<char const*> vec = {"foo", "bar"};
383   Value v_vec(vec);
384   EXPECT_STATUS_OK(v_vec.get<std::vector<std::string>>());
385 
386   std::tuple<char const*, char const*> tup = std::make_tuple("foo", "bar");
387   Value v_tup(tup);
388   EXPECT_STATUS_OK((v_tup.get<std::tuple<std::string, std::string>>()));
389 
390   auto named_field = std::make_tuple(false, std::make_pair("f1", 42));
391   Value v_named_field(named_field);
392   EXPECT_STATUS_OK(
393       (v_named_field
394            .get<std::tuple<bool, std::pair<std::string, std::int64_t>>>()));
395 }
396 
TEST(Value,MixingTypes)397 TEST(Value, MixingTypes) {
398   using A = bool;
399   using B = std::int64_t;
400 
401   Value a(A{});
402   EXPECT_STATUS_OK(a.get<A>());
403   EXPECT_FALSE(a.get<B>().ok());
404   EXPECT_FALSE(a.get<B>().ok());
405 
406   Value null_a = MakeNullValue<A>();
407   EXPECT_FALSE(null_a.get<A>().ok());
408   EXPECT_FALSE(null_a.get<B>().ok());
409 
410   EXPECT_NE(null_a, a);
411 
412   Value b(B{});
413   EXPECT_STATUS_OK(b.get<B>());
414   EXPECT_FALSE(b.get<A>().ok());
415   EXPECT_FALSE(b.get<A>().ok());
416 
417   EXPECT_NE(b, a);
418   EXPECT_NE(b, null_a);
419 
420   Value null_b = MakeNullValue<B>();
421   EXPECT_FALSE(null_b.get<B>().ok());
422   EXPECT_FALSE(null_b.get<A>().ok());
423 
424   EXPECT_NE(null_b, b);
425   EXPECT_NE(null_b, null_a);
426   EXPECT_NE(null_b, a);
427 }
428 
TEST(Value,SpannerArray)429 TEST(Value, SpannerArray) {
430   using ArrayInt64 = std::vector<std::int64_t>;
431   using ArrayDouble = std::vector<double>;
432 
433   ArrayInt64 const empty = {};
434   Value const ve(empty);
435   EXPECT_EQ(ve, ve);
436   EXPECT_STATUS_OK(ve.get<ArrayInt64>());
437   EXPECT_FALSE(ve.get<ArrayDouble>().ok());
438   EXPECT_EQ(empty, *ve.get<ArrayInt64>());
439 
440   ArrayInt64 const ai = {1, 2, 3};
441   Value const vi(ai);
442   EXPECT_EQ(vi, vi);
443   EXPECT_STATUS_OK(vi.get<ArrayInt64>());
444   EXPECT_FALSE(vi.get<ArrayDouble>().ok());
445   EXPECT_EQ(ai, *vi.get<ArrayInt64>());
446 
447   ArrayDouble const ad = {1.0, 2.0, 3.0};
448   Value const vd(ad);
449   EXPECT_EQ(vd, vd);
450   EXPECT_NE(vi, vd);
451   EXPECT_FALSE(vd.get<ArrayInt64>().ok());
452   EXPECT_STATUS_OK(vd.get<ArrayDouble>());
453   EXPECT_EQ(ad, *vd.get<ArrayDouble>());
454 
455   Value const null_vi = MakeNullValue<ArrayInt64>();
456   EXPECT_EQ(null_vi, null_vi);
457   EXPECT_NE(null_vi, vi);
458   EXPECT_NE(null_vi, vd);
459   EXPECT_FALSE(null_vi.get<ArrayInt64>().ok());
460   EXPECT_FALSE(null_vi.get<ArrayDouble>().ok());
461 
462   Value const null_vd = MakeNullValue<ArrayDouble>();
463   EXPECT_EQ(null_vd, null_vd);
464   EXPECT_NE(null_vd, null_vi);
465   EXPECT_NE(null_vd, vd);
466   EXPECT_NE(null_vd, vi);
467   EXPECT_FALSE(null_vd.get<ArrayDouble>().ok());
468   EXPECT_FALSE(null_vd.get<ArrayInt64>().ok());
469 }
470 
TEST(Value,SpannerStruct)471 TEST(Value, SpannerStruct) {
472   // Using declarations to shorten the tests, making them more readable.
473   using std::int64_t;
474   using std::make_pair;
475   using std::make_tuple;
476   using std::pair;
477   using std::string;
478   using std::tuple;
479 
480   auto tup1 = make_tuple(false, int64_t{123});
481   using T1 = decltype(tup1);
482   Value v1(tup1);
483   EXPECT_STATUS_OK(v1.get<T1>());
484   EXPECT_EQ(tup1, *v1.get<T1>());
485   EXPECT_EQ(v1, v1);
486 
487   // Verify we can extract tuple elements even if they're wrapped in a pair.
488   auto const pair0 = v1.get<tuple<pair<string, bool>, int64_t>>();
489   EXPECT_STATUS_OK(pair0);
490   EXPECT_EQ(std::get<0>(tup1), std::get<0>(*pair0).second);
491   EXPECT_EQ(std::get<1>(tup1), std::get<1>(*pair0));
492   auto const pair1 = v1.get<tuple<bool, pair<string, int64_t>>>();
493   EXPECT_STATUS_OK(pair1);
494   EXPECT_EQ(std::get<0>(tup1), std::get<0>(*pair1));
495   EXPECT_EQ(std::get<1>(tup1), std::get<1>(*pair1).second);
496   auto const pair01 =
497       v1.get<tuple<pair<string, bool>, pair<string, int64_t>>>();
498   EXPECT_STATUS_OK(pair01);
499   EXPECT_EQ(std::get<0>(tup1), std::get<0>(*pair01).second);
500   EXPECT_EQ(std::get<1>(tup1), std::get<1>(*pair01).second);
501 
502   auto tup2 = make_tuple(false, make_pair(string("f2"), int64_t{123}));
503   using T2 = decltype(tup2);
504   Value v2(tup2);
505   EXPECT_STATUS_OK(v2.get<T2>());
506   EXPECT_EQ(tup2, *v2.get<T2>());
507   EXPECT_EQ(v2, v2);
508   EXPECT_NE(v2, v1);
509 
510   // T1 is lacking field names, but otherwise the same as T2.
511   EXPECT_EQ(tup1, *v2.get<T1>());
512   EXPECT_NE(tup2, *v1.get<T2>());
513 
514   auto tup3 = make_tuple(false, make_pair(string("Other"), int64_t{123}));
515   using T3 = decltype(tup3);
516   Value v3(tup3);
517   EXPECT_STATUS_OK(v3.get<T3>());
518   EXPECT_EQ(tup3, *v3.get<T3>());
519   EXPECT_EQ(v3, v3);
520   EXPECT_NE(v3, v2);
521   EXPECT_NE(v3, v1);
522 
523   static_assert(std::is_same<T2, T3>::value, "Only diff is field name");
524 
525   // v1 != v2, yet T2 works with v1 and vice versa
526   EXPECT_NE(v1, v2);
527   EXPECT_STATUS_OK(v1.get<T2>());
528   EXPECT_STATUS_OK(v2.get<T1>());
529 
530   Value v_null(absl::optional<T1>{});
531   EXPECT_FALSE(v_null.get<absl::optional<T1>>()->has_value());
532   EXPECT_FALSE(v_null.get<absl::optional<T2>>()->has_value());
533 
534   EXPECT_NE(v1, v_null);
535   EXPECT_NE(v2, v_null);
536 
537   auto array_struct = std::vector<T3>{
538       T3{false, {"age", 1}},
539       T3{true, {"age", 2}},
540       T3{false, {"age", 3}},
541   };
542   using T4 = decltype(array_struct);
543   Value v4(array_struct);
544   EXPECT_STATUS_OK(v4.get<T4>());
545   EXPECT_FALSE(v4.get<T3>().ok());
546   EXPECT_FALSE(v4.get<T2>().ok());
547   EXPECT_FALSE(v4.get<T1>().ok());
548 
549   EXPECT_STATUS_OK(v4.get<T4>());
550   EXPECT_EQ(array_struct, *v4.get<T4>());
551 
552   auto empty = tuple<>{};
553   using T5 = decltype(empty);
554   Value v5(empty);
555   EXPECT_STATUS_OK(v5.get<T5>());
556   EXPECT_FALSE(v5.get<T4>().ok());
557   EXPECT_EQ(v5, v5);
558   EXPECT_NE(v5, v4);
559 
560   EXPECT_STATUS_OK(v5.get<T5>());
561   EXPECT_EQ(empty, *v5.get<T5>());
562 
563   auto crazy = tuple<tuple<std::vector<absl::optional<bool>>>>{};
564   using T6 = decltype(crazy);
565   Value v6(crazy);
566   EXPECT_STATUS_OK(v6.get<T6>());
567   EXPECT_FALSE(v6.get<T5>().ok());
568   EXPECT_EQ(v6, v6);
569   EXPECT_NE(v6, v5);
570 
571   EXPECT_STATUS_OK(v6.get<T6>());
572   EXPECT_EQ(crazy, *v6.get<T6>());
573 }
574 
TEST(Value,SpannerStructWithNull)575 TEST(Value, SpannerStructWithNull) {
576   auto v1 = Value(std::make_tuple(123, true));
577   auto v2 = Value(std::make_tuple(123, absl::optional<bool>{}));
578 
579   auto protos1 = internal::ToProto(v1);
580   auto protos2 = internal::ToProto(v2);
581 
582   // The type protos match for both values, but the value protos DO NOT match.
583   EXPECT_THAT(protos1.first, IsProtoEqual(protos2.first));
584   EXPECT_THAT(protos1.second, Not(IsProtoEqual(protos2.second)));
585 
586   // Now verify that the second value has two fields and the second field
587   // contains a NULL value.
588   ASSERT_EQ(protos2.second.list_value().values_size(), 2);
589   ASSERT_EQ(protos2.second.list_value().values(1).null_value(),
590             google::protobuf::NullValue::NULL_VALUE);
591 }
592 
TEST(Value,ProtoConversionBool)593 TEST(Value, ProtoConversionBool) {
594   for (auto b : {true, false}) {
595     Value const v(b);
596     auto const p = internal::ToProto(Value(b));
597     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
598     EXPECT_EQ(google::spanner::v1::TypeCode::BOOL, p.first.code());
599     EXPECT_EQ(b, p.second.bool_value());
600   }
601 }
602 
TEST(Value,ProtoConversionInt64)603 TEST(Value, ProtoConversionInt64) {
604   auto const min64 = std::numeric_limits<std::int64_t>::min();
605   auto const max64 = std::numeric_limits<std::int64_t>::max();
606   for (auto x : std::vector<std::int64_t>{min64, -1, 0, 1, 42, max64}) {
607     Value const v(x);
608     auto const p = internal::ToProto(v);
609     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
610     EXPECT_EQ(google::spanner::v1::TypeCode::INT64, p.first.code());
611     EXPECT_EQ(std::to_string(x), p.second.string_value());
612   }
613 }
614 
TEST(Value,ProtoConversionFloat64)615 TEST(Value, ProtoConversionFloat64) {
616   for (auto x : {-1.0, -0.5, 0.0, 0.5, 1.0}) {
617     Value const v(x);
618     auto const p = internal::ToProto(v);
619     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
620     EXPECT_EQ(google::spanner::v1::TypeCode::FLOAT64, p.first.code());
621     EXPECT_EQ(x, p.second.number_value());
622   }
623 
624   // Tests special cases
625   auto const inf = std::numeric_limits<double>::infinity();
626   Value v(inf);
627   auto p = internal::ToProto(v);
628   EXPECT_EQ(v, internal::FromProto(p.first, p.second));
629   EXPECT_EQ(google::spanner::v1::TypeCode::FLOAT64, p.first.code());
630   EXPECT_EQ("Infinity", p.second.string_value());
631 
632   v = Value(-inf);
633   p = internal::ToProto(v);
634   EXPECT_EQ(v, internal::FromProto(p.first, p.second));
635   EXPECT_EQ(google::spanner::v1::TypeCode::FLOAT64, p.first.code());
636   EXPECT_EQ("-Infinity", p.second.string_value());
637 
638   auto const nan = std::nan("NaN");
639   v = Value(nan);
640   p = internal::ToProto(v);
641   // Note: NaN is defined to be not equal to everything, including itself, so
642   // we instead ensure that it is not equal with EXPECT_NE.
643   EXPECT_NE(v, internal::FromProto(p.first, p.second));
644   EXPECT_EQ(google::spanner::v1::TypeCode::FLOAT64, p.first.code());
645   EXPECT_EQ("NaN", p.second.string_value());
646 }
647 
TEST(Value,ProtoConversionString)648 TEST(Value, ProtoConversionString) {
649   for (auto const& x :
650        std::vector<std::string>{"", "f", "foo", "12345678901234567890"}) {
651     Value const v(x);
652     auto const p = internal::ToProto(v);
653     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
654     EXPECT_EQ(google::spanner::v1::TypeCode::STRING, p.first.code());
655     EXPECT_EQ(x, p.second.string_value());
656   }
657 }
658 
TEST(Value,ProtoConversionBytes)659 TEST(Value, ProtoConversionBytes) {
660   for (auto const& x : std::vector<Bytes>{Bytes(""), Bytes("f"), Bytes("foo"),
661                                           Bytes("12345678901234567890")}) {
662     Value const v(x);
663     auto const p = internal::ToProto(v);
664     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
665     EXPECT_EQ(google::spanner::v1::TypeCode::BYTES, p.first.code());
666     EXPECT_EQ(internal::BytesToBase64(x), p.second.string_value());
667   }
668 }
669 
TEST(Value,ProtoConversionNumeric)670 TEST(Value, ProtoConversionNumeric) {
671   for (auto const& x : std::vector<Numeric>{
672            MakeNumeric(-0.9e29).value(),
673            MakeNumeric(-1).value(),
674            MakeNumeric(-1.0e-9).value(),
675            Numeric(),
676            MakeNumeric(1.0e-9).value(),
677            MakeNumeric(1U).value(),
678            MakeNumeric(0.9e29).value(),
679        }) {
680     Value const v(x);
681     auto const p = internal::ToProto(v);
682     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
683     EXPECT_EQ(google::spanner::v1::TypeCode::NUMERIC, p.first.code());
684     EXPECT_EQ(x.ToString(), p.second.string_value());
685   }
686 }
687 
TEST(Value,ProtoConversionTimestamp)688 TEST(Value, ProtoConversionTimestamp) {
689   for (time_t t : {
690            -9223372035LL,   // near the limit of 64-bit/ns system_clock
691            -2147483649LL,   // below min 32-bit int
692            -2147483648LL,   // min 32-bit int
693            -1LL, 0LL, 1LL,  // around the unix epoch
694            1561147549LL,    // contemporary
695            2147483647LL,    // max 32-bit int
696            2147483648LL,    // above max 32-bit int
697            9223372036LL     // near the limit of 64-bit/ns system_clock
698        }) {
699     for (auto nanos : {-1, 0, 1}) {
700       auto ts = MakeTimestamp(MakeTimePoint(t, nanos)).value();
701       Value const v(ts);
702       auto const p = internal::ToProto(v);
703       EXPECT_EQ(v, internal::FromProto(p.first, p.second));
704       EXPECT_EQ(google::spanner::v1::TypeCode::TIMESTAMP, p.first.code());
705       EXPECT_EQ(internal::TimestampToRFC3339(ts), p.second.string_value());
706     }
707   }
708 }
709 
TEST(Value,ProtoConversionDate)710 TEST(Value, ProtoConversionDate) {
711   struct {
712     absl::CivilDay day;
713     std::string expected;
714   } test_cases[] = {
715       {absl::CivilDay(-9999, 1, 2), "-9999-01-02"},
716       {absl::CivilDay(-999, 1, 2), "-999-01-02"},
717       {absl::CivilDay(-1, 1, 2), "-001-01-02"},
718       {absl::CivilDay(0, 1, 2), "0000-01-02"},
719       {absl::CivilDay(1, 1, 2), "0001-01-02"},
720       {absl::CivilDay(999, 1, 2), "0999-01-02"},
721       {absl::CivilDay(1582, 10, 15), "1582-10-15"},
722       {absl::CivilDay(1677, 9, 21), "1677-09-21"},
723       {absl::CivilDay(1901, 12, 13), "1901-12-13"},
724       {absl::CivilDay(1970, 1, 1), "1970-01-01"},
725       {absl::CivilDay(2019, 6, 21), "2019-06-21"},
726       {absl::CivilDay(2038, 1, 19), "2038-01-19"},
727       {absl::CivilDay(2262, 4, 12), "2262-04-12"},
728   };
729 
730   for (auto const& tc : test_cases) {
731     SCOPED_TRACE("CivilDay: " + absl::FormatCivilTime(tc.day));
732     Value const v(tc.day);
733     auto const p = internal::ToProto(v);
734     EXPECT_EQ(v, internal::FromProto(p.first, p.second));
735     EXPECT_EQ(google::spanner::v1::TypeCode::DATE, p.first.code());
736     EXPECT_EQ(tc.expected, p.second.string_value());
737   }
738 }
739 
TEST(Value,ProtoConversionArray)740 TEST(Value, ProtoConversionArray) {
741   std::vector<std::int64_t> data{1, 2, 3};
742   Value const v(data);
743   auto const p = internal::ToProto(v);
744   EXPECT_EQ(v, internal::FromProto(p.first, p.second));
745   EXPECT_EQ(google::spanner::v1::TypeCode::ARRAY, p.first.code());
746   EXPECT_EQ(google::spanner::v1::TypeCode::INT64,
747             p.first.array_element_type().code());
748   EXPECT_EQ("1", p.second.list_value().values(0).string_value());
749   EXPECT_EQ("2", p.second.list_value().values(1).string_value());
750   EXPECT_EQ("3", p.second.list_value().values(2).string_value());
751 }
752 
TEST(Value,ProtoConversionStruct)753 TEST(Value, ProtoConversionStruct) {
754   auto data = std::make_tuple(3.14, std::make_pair("foo", 42));
755   Value const v(data);
756   auto const p = internal::ToProto(v);
757   EXPECT_EQ(v, internal::FromProto(p.first, p.second));
758   EXPECT_EQ(google::spanner::v1::TypeCode::STRUCT, p.first.code());
759 
760   Value const null_struct_value(
761       MakeNullValue<std::tuple<bool, std::int64_t>>());
762   auto const null_struct_proto = internal::ToProto(null_struct_value);
763   EXPECT_EQ(google::spanner::v1::TypeCode::STRUCT,
764             null_struct_proto.first.code());
765 
766   auto const& field0 = p.first.struct_type().fields(0);
767   EXPECT_EQ("", field0.name());
768   EXPECT_EQ(google::spanner::v1::TypeCode::FLOAT64, field0.type().code());
769   EXPECT_EQ(3.14, p.second.list_value().values(0).number_value());
770 
771   auto const& field1 = p.first.struct_type().fields(1);
772   EXPECT_EQ("foo", field1.name());
773   EXPECT_EQ(google::spanner::v1::TypeCode::INT64, field1.type().code());
774   EXPECT_EQ("42", p.second.list_value().values(1).string_value());
775 }
776 
SetProtoKind(Value & v,google::protobuf::NullValue x)777 void SetProtoKind(Value& v, google::protobuf::NullValue x) {
778   auto p = internal::ToProto(v);
779   p.second.set_null_value(x);
780   v = internal::FromProto(p.first, p.second);
781 }
782 
SetProtoKind(Value & v,double x)783 void SetProtoKind(Value& v, double x) {
784   auto p = internal::ToProto(v);
785   p.second.set_number_value(x);
786   v = internal::FromProto(p.first, p.second);
787 }
788 
SetProtoKind(Value & v,char const * x)789 void SetProtoKind(Value& v, char const* x) {
790   auto p = internal::ToProto(v);
791   p.second.set_string_value(x);
792   v = internal::FromProto(p.first, p.second);
793 }
794 
SetProtoKind(Value & v,bool x)795 void SetProtoKind(Value& v, bool x) {
796   auto p = internal::ToProto(v);
797   p.second.set_bool_value(x);
798   v = internal::FromProto(p.first, p.second);
799 }
800 
ClearProtoKind(Value & v)801 void ClearProtoKind(Value& v) {
802   auto p = internal::ToProto(v);
803   p.second.clear_kind();
804   v = internal::FromProto(p.first, p.second);
805 }
806 
TEST(Value,GetBadBool)807 TEST(Value, GetBadBool) {
808   Value v(true);
809   ClearProtoKind(v);
810   EXPECT_FALSE(v.get<bool>().ok());
811 
812   SetProtoKind(v, google::protobuf::NULL_VALUE);
813   EXPECT_FALSE(v.get<bool>().ok());
814 
815   SetProtoKind(v, 0.0);
816   EXPECT_FALSE(v.get<bool>().ok());
817 
818   SetProtoKind(v, "hello");
819   EXPECT_FALSE(v.get<bool>().ok());
820 }
821 
TEST(Value,GetBadDouble)822 TEST(Value, GetBadDouble) {
823   Value v(0.0);
824   ClearProtoKind(v);
825   EXPECT_FALSE(v.get<double>().ok());
826 
827   SetProtoKind(v, google::protobuf::NULL_VALUE);
828   EXPECT_FALSE(v.get<double>().ok());
829 
830   SetProtoKind(v, true);
831   EXPECT_FALSE(v.get<double>().ok());
832 
833   SetProtoKind(v, "bad string");
834   EXPECT_FALSE(v.get<double>().ok());
835 }
836 
TEST(Value,GetBadString)837 TEST(Value, GetBadString) {
838   Value v("hello");
839   ClearProtoKind(v);
840   EXPECT_FALSE(v.get<std::string>().ok());
841 
842   SetProtoKind(v, google::protobuf::NULL_VALUE);
843   EXPECT_FALSE(v.get<std::string>().ok());
844 
845   SetProtoKind(v, true);
846   EXPECT_FALSE(v.get<std::string>().ok());
847 
848   SetProtoKind(v, 0.0);
849   EXPECT_FALSE(v.get<std::string>().ok());
850 }
851 
TEST(Value,GetBadBytes)852 TEST(Value, GetBadBytes) {
853   Value v(Bytes("hello"));
854   ClearProtoKind(v);
855   EXPECT_FALSE(v.get<Bytes>().ok());
856 
857   SetProtoKind(v, google::protobuf::NULL_VALUE);
858   EXPECT_FALSE(v.get<Bytes>().ok());
859 
860   SetProtoKind(v, true);
861   EXPECT_FALSE(v.get<Bytes>().ok());
862 
863   SetProtoKind(v, 0.0);
864   EXPECT_FALSE(v.get<Bytes>().ok());
865 }
866 
TEST(Value,GetBadNumeric)867 TEST(Value, GetBadNumeric) {
868   Value v(MakeNumeric(0).value());
869   ClearProtoKind(v);
870   EXPECT_FALSE(v.get<std::string>().ok());
871 
872   SetProtoKind(v, google::protobuf::NULL_VALUE);
873   EXPECT_FALSE(v.get<std::string>().ok());
874 
875   SetProtoKind(v, true);
876   EXPECT_FALSE(v.get<std::string>().ok());
877 
878   SetProtoKind(v, 0.0);
879   EXPECT_FALSE(v.get<std::string>().ok());
880 
881   SetProtoKind(v, "");
882   EXPECT_FALSE(v.get<std::int64_t>().ok());
883 
884   SetProtoKind(v, "blah");
885   EXPECT_FALSE(v.get<std::int64_t>().ok());
886 
887   SetProtoKind(v, "123blah");
888   EXPECT_FALSE(v.get<std::int64_t>().ok());
889 }
890 
TEST(Value,GetBadInt)891 TEST(Value, GetBadInt) {
892   Value v(42);
893   ClearProtoKind(v);
894   EXPECT_FALSE(v.get<std::int64_t>().ok());
895 
896   SetProtoKind(v, google::protobuf::NULL_VALUE);
897   EXPECT_FALSE(v.get<std::int64_t>().ok());
898 
899   SetProtoKind(v, true);
900   EXPECT_FALSE(v.get<std::int64_t>().ok());
901 
902   SetProtoKind(v, 0.0);
903   EXPECT_FALSE(v.get<std::int64_t>().ok());
904 
905   SetProtoKind(v, "");
906   EXPECT_FALSE(v.get<std::int64_t>().ok());
907 
908   SetProtoKind(v, "blah");
909   EXPECT_FALSE(v.get<std::int64_t>().ok());
910 
911   SetProtoKind(v, "123blah");
912   EXPECT_FALSE(v.get<std::int64_t>().ok());
913 }
914 
TEST(Value,GetBadTimestamp)915 TEST(Value, GetBadTimestamp) {
916   Value v(Timestamp{});
917   ClearProtoKind(v);
918   EXPECT_FALSE(v.get<Timestamp>().ok());
919 
920   SetProtoKind(v, google::protobuf::NULL_VALUE);
921   EXPECT_FALSE(v.get<Timestamp>().ok());
922 
923   SetProtoKind(v, true);
924   EXPECT_FALSE(v.get<Timestamp>().ok());
925 
926   SetProtoKind(v, 0.0);
927   EXPECT_FALSE(v.get<Timestamp>().ok());
928 
929   SetProtoKind(v, "blah");
930   EXPECT_FALSE(v.get<Timestamp>().ok());
931 }
932 
TEST(Value,GetBadCommitTimestamp)933 TEST(Value, GetBadCommitTimestamp) {
934   Value v(CommitTimestamp{});
935   ClearProtoKind(v);
936   EXPECT_FALSE(v.get<CommitTimestamp>().ok());
937 
938   SetProtoKind(v, google::protobuf::NULL_VALUE);
939   EXPECT_FALSE(v.get<CommitTimestamp>().ok());
940 
941   SetProtoKind(v, true);
942   EXPECT_FALSE(v.get<CommitTimestamp>().ok());
943 
944   SetProtoKind(v, 0.0);
945   EXPECT_FALSE(v.get<CommitTimestamp>().ok());
946 
947   SetProtoKind(v, "blah");
948   EXPECT_FALSE(v.get<CommitTimestamp>().ok());
949 }
950 
TEST(Value,GetBadDate)951 TEST(Value, GetBadDate) {
952   Value v(absl::CivilDay{});
953   ClearProtoKind(v);
954   EXPECT_FALSE(v.get<absl::CivilDay>().ok());
955 
956   SetProtoKind(v, google::protobuf::NULL_VALUE);
957   EXPECT_FALSE(v.get<absl::CivilDay>().ok());
958 
959   SetProtoKind(v, true);
960   EXPECT_FALSE(v.get<absl::CivilDay>().ok());
961 
962   SetProtoKind(v, 0.0);
963   EXPECT_FALSE(v.get<absl::CivilDay>().ok());
964 
965   SetProtoKind(v, "blah");
966   EXPECT_FALSE(v.get<absl::CivilDay>().ok());
967 }
968 
TEST(Value,GetBadOptional)969 TEST(Value, GetBadOptional) {
970   Value v(absl::optional<double>{});
971   ClearProtoKind(v);
972   EXPECT_FALSE(v.get<absl::optional<double>>().ok());
973 
974   SetProtoKind(v, true);
975   EXPECT_FALSE(v.get<absl::optional<double>>().ok());
976 
977   SetProtoKind(v, "blah");
978   EXPECT_FALSE(v.get<absl::optional<double>>().ok());
979 }
980 
TEST(Value,GetBadArray)981 TEST(Value, GetBadArray) {
982   Value v(std::vector<double>{});
983   ClearProtoKind(v);
984   EXPECT_FALSE(v.get<std::vector<double>>().ok());
985 
986   SetProtoKind(v, google::protobuf::NULL_VALUE);
987   EXPECT_FALSE(v.get<std::vector<double>>().ok());
988 
989   SetProtoKind(v, true);
990   EXPECT_FALSE(v.get<std::vector<double>>().ok());
991 
992   SetProtoKind(v, 0.0);
993   EXPECT_FALSE(v.get<std::vector<double>>().ok());
994 
995   SetProtoKind(v, "blah");
996   EXPECT_FALSE(v.get<std::vector<double>>().ok());
997 }
998 
TEST(Value,GetBadStruct)999 TEST(Value, GetBadStruct) {
1000   Value v(std::tuple<bool>{});
1001   ClearProtoKind(v);
1002   EXPECT_FALSE(v.get<std::tuple<bool>>().ok());
1003 
1004   SetProtoKind(v, google::protobuf::NULL_VALUE);
1005   EXPECT_FALSE(v.get<std::tuple<bool>>().ok());
1006 
1007   SetProtoKind(v, true);
1008   EXPECT_FALSE(v.get<std::tuple<bool>>().ok());
1009 
1010   SetProtoKind(v, 0.0);
1011   EXPECT_FALSE(v.get<std::tuple<bool>>().ok());
1012 
1013   SetProtoKind(v, "blah");
1014   EXPECT_FALSE(v.get<std::tuple<bool>>().ok());
1015 }
1016 
TEST(Value,CommitTimestamp)1017 TEST(Value, CommitTimestamp) {
1018   auto const v = Value(CommitTimestamp{});
1019   auto tv = internal::ToProto(v);
1020   EXPECT_EQ(google::spanner::v1::TypeCode::TIMESTAMP, tv.first.code());
1021 
1022   auto constexpr kText = R"pb(
1023     string_value: "spanner.commit_timestamp()"
1024   )pb";
1025   google::protobuf::Value pv;
1026   ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(kText, &pv));
1027   EXPECT_THAT(tv.second, IsProtoEqual(pv));
1028 
1029   auto good = v.get<CommitTimestamp>();
1030   EXPECT_STATUS_OK(good);
1031   EXPECT_EQ(CommitTimestamp{}, *good);
1032 
1033   auto bad = v.get<Timestamp>();
1034   EXPECT_FALSE(bad.ok());
1035 }
1036 
TEST(Value,OutputStream)1037 TEST(Value, OutputStream) {
1038   auto const normal = [](std::ostream& os) -> std::ostream& { return os; };
1039   auto const hex = [](std::ostream& os) -> std::ostream& {
1040     return os << std::hex;
1041   };
1042   auto const boolalpha = [](std::ostream& os) -> std::ostream& {
1043     return os << std::boolalpha;
1044   };
1045   auto const float4 = [](std::ostream& os) -> std::ostream& {
1046     return os << std::showpoint << std::setprecision(4);
1047   };
1048   auto const alphahex = [](std::ostream& os) -> std::ostream& {
1049     return os << std::boolalpha << std::hex;
1050   };
1051 
1052   auto const inf = std::numeric_limits<double>::infinity();
1053   auto const nan = std::nan("NaN");
1054 
1055   struct TestCase {
1056     Value value;
1057     std::string expected;
1058     std::function<std::ostream&(std::ostream&)> manip;
1059   };
1060 
1061   std::vector<TestCase> test_case = {
1062       {Value(false), "0", normal},
1063       {Value(true), "1", normal},
1064       {Value(false), "false", boolalpha},
1065       {Value(true), "true", boolalpha},
1066       {Value(42), "42", normal},
1067       {Value(42), "2a", hex},
1068       {Value(42.0), "42", normal},
1069       {Value(42.0), "42.00", float4},
1070       {Value(inf), "inf", normal},
1071       {Value(-inf), "-inf", normal},
1072       {Value(nan), "nan", normal},
1073       {Value(""), "", normal},
1074       {Value("foo"), "foo", normal},
1075       {Value("NULL"), "NULL", normal},
1076       {Value(Bytes(std::string("DEADBEEF"))), R"(B"DEADBEEF")", normal},
1077       {Value(MakeNumeric(1234567890).value()), "1234567890", normal},
1078       {Value(absl::CivilDay()), "1970-01-01", normal},
1079       {Value(Timestamp()), "1970-01-01T00:00:00Z", normal},
1080 
1081       // Tests string quoting: No quotes for scalars; quotes within aggregates
1082       {Value(""), "", normal},
1083       {Value("foo"), "foo", normal},
1084       {Value(std::vector<std::string>{"a", "b"}), R"(["a", "b"])", normal},
1085       {Value(std::make_tuple("foo")), R"(("foo"))", normal},
1086       {Value(std::make_tuple(std::make_pair("foo", "bar"))),
1087        R"(("foo": "bar"))", normal},
1088       {Value(std::vector<std::string>{"\"a\"", "\"b\""}),
1089        R"(["\"a\"", "\"b\""])", normal},
1090       {Value(std::make_tuple("\"foo\"")), R"(("\"foo\""))", normal},
1091       {Value(std::make_tuple(std::make_pair("\"foo\"", "\"bar\""))),
1092        R"(("\"foo\"": "\"bar\""))", normal},
1093 
1094       // Tests null values
1095       {MakeNullValue<bool>(), "NULL", normal},
1096       {MakeNullValue<std::int64_t>(), "NULL", normal},
1097       {MakeNullValue<double>(), "NULL", normal},
1098       {MakeNullValue<std::string>(), "NULL", normal},
1099       {MakeNullValue<Bytes>(), "NULL", normal},
1100       {MakeNullValue<Numeric>(), "NULL", normal},
1101       {MakeNullValue<absl::CivilDay>(), "NULL", normal},
1102       {MakeNullValue<Timestamp>(), "NULL", normal},
1103 
1104       // Tests arrays
1105       {Value(std::vector<bool>{false, true}), "[0, 1]", normal},
1106       {Value(std::vector<bool>{false, true}), "[false, true]", boolalpha},
1107       {Value(std::vector<std::int64_t>{10, 11}), "[10, 11]", normal},
1108       {Value(std::vector<std::int64_t>{10, 11}), "[a, b]", hex},
1109       {Value(std::vector<double>{1.0, 2.0}), "[1, 2]", normal},
1110       {Value(std::vector<double>{1.0, 2.0}), "[1.000, 2.000]", float4},
1111       {Value(std::vector<std::string>{"a", "b"}), R"(["a", "b"])", normal},
1112       {Value(std::vector<Bytes>{2}), R"([B"", B""])", normal},
1113       {Value(std::vector<Numeric>{2}), "[0, 0]", normal},
1114       {Value(std::vector<absl::CivilDay>{2}), "[1970-01-01, 1970-01-01]",
1115        normal},
1116       {Value(std::vector<Timestamp>{1}), "[1970-01-01T00:00:00Z]", normal},
1117       {Value(std::vector<absl::optional<double>>{1, {}, 2}), "[1, NULL, 2]",
1118        normal},
1119 
1120       // Tests null arrays
1121       {MakeNullValue<std::vector<bool>>(), "NULL", normal},
1122       {MakeNullValue<std::vector<std::int64_t>>(), "NULL", normal},
1123       {MakeNullValue<std::vector<double>>(), "NULL", normal},
1124       {MakeNullValue<std::vector<std::string>>(), "NULL", normal},
1125       {MakeNullValue<std::vector<Bytes>>(), "NULL", normal},
1126       {MakeNullValue<std::vector<Numeric>>(), "NULL", normal},
1127       {MakeNullValue<std::vector<absl::CivilDay>>(), "NULL", normal},
1128       {MakeNullValue<std::vector<Timestamp>>(), "NULL", normal},
1129 
1130       // Tests structs
1131       {Value(std::make_tuple(true, 123)), "(1, 123)", normal},
1132       {Value(std::make_tuple(true, 123)), "(true, 7b)", alphahex},
1133       {Value(std::make_tuple(std::make_pair("A", true),
1134                              std::make_pair("B", 123))),
1135        R"(("A": 1, "B": 123))", normal},
1136       {Value(std::make_tuple(std::make_pair("A", true),
1137                              std::make_pair("B", 123))),
1138        R"(("A": true, "B": 7b))", alphahex},
1139       {Value(std::make_tuple(
1140            std::vector<std::int64_t>{10, 11, 12},
1141            std::make_pair("B", std::vector<std::int64_t>{13, 14, 15}))),
1142        R"(([10, 11, 12], "B": [13, 14, 15]))", normal},
1143       {Value(std::make_tuple(
1144            std::vector<std::int64_t>{10, 11, 12},
1145            std::make_pair("B", std::vector<std::int64_t>{13, 14, 15}))),
1146        R"(([a, b, c], "B": [d, e, f]))", hex},
1147       {Value(std::make_tuple(std::make_tuple(
1148            std::make_tuple(std::vector<std::int64_t>{10, 11, 12})))),
1149        "((([10, 11, 12])))", normal},
1150       {Value(std::make_tuple(std::make_tuple(
1151            std::make_tuple(std::vector<std::int64_t>{10, 11, 12})))),
1152        "((([a, b, c])))", hex},
1153 
1154       // Tests struct with null members
1155       {Value(std::make_tuple(absl::optional<bool>{})), "(NULL)", normal},
1156       {Value(std::make_tuple(absl::optional<bool>{}, 123)), "(NULL, 123)",
1157        normal},
1158       {Value(std::make_tuple(absl::optional<bool>{}, 123)), "(NULL, 7b)", hex},
1159       {Value(std::make_tuple(absl::optional<bool>{},
1160                              absl::optional<std::int64_t>{})),
1161        "(NULL, NULL)", normal},
1162 
1163       // Tests null structs
1164       {MakeNullValue<std::tuple<bool>>(), "NULL", normal},
1165       {MakeNullValue<std::tuple<bool, std::int64_t>>(), "NULL", normal},
1166       {MakeNullValue<std::tuple<bool, std::string>>(), "NULL", normal},
1167       {MakeNullValue<std::tuple<double, Bytes, Timestamp>>(), "NULL", normal},
1168       {MakeNullValue<std::tuple<Numeric, absl::CivilDay>>(), "NULL", normal},
1169       {MakeNullValue<std::tuple<std::vector<bool>>>(), "NULL", normal},
1170   };
1171 
1172   for (auto const& tc : test_case) {
1173     std::stringstream ss;
1174     tc.manip(ss) << tc.value;
1175     EXPECT_EQ(ss.str(), tc.expected);
1176   }
1177 }
1178 
1179 // Ensures that the following expressions produce the same output.
1180 //
1181 // `os << t`
1182 // `os << Value(t)`
1183 //
1184 template <typename T>
1185 void StreamMatchesValueStream(T t) {
1186   std::ostringstream ss1;
1187   ss1 << t;
1188   std::ostringstream ss2;
1189   ss2 << Value(std::move(t));
1190   EXPECT_EQ(ss1.str(), ss2.str());
1191 }
1192 
1193 TEST(Value, OutputStreamMatchesT) {
1194   // bool
1195   StreamMatchesValueStream(false);
1196   StreamMatchesValueStream(true);
1197 
1198   // std::int64_t
1199   StreamMatchesValueStream(-1);
1200   StreamMatchesValueStream(0);
1201   StreamMatchesValueStream(1);
1202 
1203   // double
1204   StreamMatchesValueStream(0.0);
1205   StreamMatchesValueStream(3.14);
1206   StreamMatchesValueStream(std::nan("NaN"));
1207   StreamMatchesValueStream(std::numeric_limits<double>::infinity());
1208   StreamMatchesValueStream(-std::numeric_limits<double>::infinity());
1209 
1210   // std::string
1211   StreamMatchesValueStream("");
1212   StreamMatchesValueStream("foo");
1213   StreamMatchesValueStream("\"foo\"");
1214 
1215   // Bytes
1216   StreamMatchesValueStream(Bytes());
1217   StreamMatchesValueStream(Bytes("foo"));
1218 
1219   // Numeric
1220   StreamMatchesValueStream(MakeNumeric("999").value());
1221   StreamMatchesValueStream(MakeNumeric(3.14159).value());
1222   StreamMatchesValueStream(MakeNumeric(42).value());
1223 
1224   // Date
1225   StreamMatchesValueStream(absl::CivilDay(1, 1, 1));
1226   StreamMatchesValueStream(absl::CivilDay());
1227   StreamMatchesValueStream(absl::CivilDay(9999, 12, 31));
1228 
1229   // Timestamp
1230   StreamMatchesValueStream(Timestamp());
1231   StreamMatchesValueStream(MakeTimestamp(MakeTimePoint(1, 1)).value());
1232 
1233   // std::vector<T>
1234   // Not included, because a raw vector cannot be streamed.
1235 
1236   // std::tuple<...>
1237   // Not included, because a raw tuple cannot be streamed.
1238 }
1239 
1240 }  // namespace
1241 }  // namespace SPANNER_CLIENT_NS
1242 }  // namespace spanner
1243 }  // namespace cloud
1244 }  // namespace google
1245