Home | History | Annotate | Download | only in libpdx
      1 #include <errno.h>
      2 #include <fcntl.h>
      3 #include <unistd.h>
      4 
      5 #include <memory>
      6 #include <string>
      7 #include <thread>
      8 #include <utility>
      9 
     10 #include <gtest/gtest.h>
     11 #include <pdx/rpc/argument_encoder.h>
     12 #include <pdx/rpc/array_wrapper.h>
     13 #include <pdx/rpc/default_initialization_allocator.h>
     14 #include <pdx/rpc/payload.h>
     15 #include <pdx/rpc/serializable.h>
     16 #include <pdx/rpc/serialization.h>
     17 #include <pdx/rpc/string_wrapper.h>
     18 #include <pdx/utility.h>
     19 
     20 using namespace android::pdx;
     21 using namespace android::pdx::rpc;
     22 
     23 // Tests the serialization/deserialization of all supported types, verifying all
     24 // reasonable boundary conditions for types with multiple encodings.
     25 //
     26 // NOTE: Sometimes this file uses the construct "var = decltype(var)({...})"
     27 // instead of the equivalent "var = {...}" to construct vectors. This is to
     28 // prevent clang-format from producing annoyingly vertical code from long
     29 // initializers.
     30 
     31 // TODO(eieio): Automatically generate some of these tests?
     32 
     33 namespace {
     34 
     35 // Test data for serialization/deserialization of floats.
     36 const float kZeroFloat = 0.0f;
     37 const float kOneFloat = 1.0f;
     38 const auto kZeroFloatBytes = reinterpret_cast<const std::uint8_t*>(&kZeroFloat);
     39 const auto kOneFloatBytes = reinterpret_cast<const std::uint8_t*>(&kOneFloat);
     40 const double kZeroDouble = 0.0;
     41 const double kOneDouble = 1.0;
     42 const auto kZeroDoubleBytes =
     43     reinterpret_cast<const std::uint8_t*>(&kZeroDouble);
     44 const auto kOneDoubleBytes = reinterpret_cast<const std::uint8_t*>(&kOneDouble);
     45 
     46 struct TestType {
     47   enum class Foo { kFoo, kBar, kBaz };
     48 
     49   int a;
     50   float b;
     51   std::string c;
     52   Foo d;
     53 
     54   TestType() {}
     55   TestType(int a, float b, const std::string& c, Foo d)
     56       : a(a), b(b), c(c), d(d) {}
     57 
     58   // Make gtest expressions simpler by defining equality operator. This is not
     59   // needed for serialization.
     60   bool operator==(const TestType& other) const {
     61     return a == other.a && b == other.b && c == other.c && d == other.d;
     62   }
     63 
     64  private:
     65   PDX_SERIALIZABLE_MEMBERS(TestType, a, b, c, d);
     66 };
     67 
     68 template <typename FileHandleType>
     69 struct TestTemplateType {
     70   FileHandleType fd;
     71 
     72   TestTemplateType() {}
     73   TestTemplateType(FileHandleType fd) : fd(std::move(fd)) {}
     74 
     75   bool operator==(const TestTemplateType& other) const {
     76     return fd.Get() == other.fd.Get();
     77   }
     78 
     79  private:
     80   PDX_SERIALIZABLE_MEMBERS(TestTemplateType<FileHandleType>, fd);
     81 };
     82 
     83 // Utilities to generate test maps and payloads.
     84 template <typename MapType>
     85 MapType MakeMap(std::size_t size) {
     86   MapType result;
     87   for (std::size_t i = 0; i < size; i++) {
     88     result.emplace(i, i);
     89   }
     90   return result;
     91 }
     92 
     93 template <typename MapType>
     94 void InsertKeyValue(MessageWriter* writer, std::size_t size) {
     95   MapType map;
     96   for (std::size_t i = 0; i < size; i++) {
     97     map.emplace(i, i);
     98   }
     99   for (const auto& element : map) {
    100     Serialize(element.first, writer);
    101     Serialize(element.second, writer);
    102   }
    103 }
    104 
    105 }  // anonymous namespace
    106 
    107 TEST(SerializableTypes, Constructor) {
    108   TestType tt(1, 2.0, "three", TestType::Foo::kBar);
    109   EXPECT_EQ(1, tt.a);
    110   EXPECT_EQ(2.0, tt.b);
    111   EXPECT_EQ("three", tt.c);
    112   EXPECT_EQ(TestType::Foo::kBar, tt.d);
    113 }
    114 
    115 TEST(SerializationTest, bool) {
    116   Payload result;
    117   Payload expected;
    118   bool value;
    119 
    120   // True.
    121   value = true;
    122   Serialize(value, &result);
    123   expected = {ENCODING_TYPE_TRUE};
    124   EXPECT_EQ(expected, result);
    125   result.Clear();
    126 
    127   // False.
    128   value = false;
    129   Serialize(value, &result);
    130   expected = {ENCODING_TYPE_FALSE};
    131   EXPECT_EQ(expected, result);
    132   result.Clear();
    133 }
    134 
    135 TEST(SerializationTest, uint8_t) {
    136   Payload result;
    137   Payload expected;
    138   uint8_t value;
    139 
    140   // Min FIXINT.
    141   value = 0;
    142   Serialize(value, &result);
    143   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    144   EXPECT_EQ(expected, result);
    145   result.Clear();
    146 
    147   // Max FIXINT.
    148   value = (1 << 7) - 1;
    149   Serialize(value, &result);
    150   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    151   EXPECT_EQ(expected, result);
    152   result.Clear();
    153 
    154   // Min UINT8.
    155   value = (1 << 7);
    156   Serialize(value, &result);
    157   expected = {ENCODING_TYPE_UINT8, (1 << 7)};
    158   EXPECT_EQ(expected, result);
    159   result.Clear();
    160 
    161   // Max UINT8.
    162   value = 0xff;
    163   Serialize(value, &result);
    164   expected = {ENCODING_TYPE_UINT8, 0xff};
    165   EXPECT_EQ(expected, result);
    166   result.Clear();
    167 }
    168 
    169 TEST(SerializationTest, uint16_t) {
    170   Payload result;
    171   Payload expected;
    172   uint16_t value;
    173 
    174   // Min FIXINT.
    175   value = 0;
    176   Serialize(value, &result);
    177   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    178   EXPECT_EQ(expected, result);
    179   result.Clear();
    180 
    181   // Max FIXINT.
    182   value = (1 << 7) - 1;
    183   Serialize(value, &result);
    184   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    185   EXPECT_EQ(expected, result);
    186   result.Clear();
    187 
    188   // Min UINT8.
    189   value = (1 << 7);
    190   Serialize(value, &result);
    191   expected = {ENCODING_TYPE_UINT8, (1 << 7)};
    192   EXPECT_EQ(expected, result);
    193   result.Clear();
    194 
    195   // Max UINT8.
    196   value = 0xff;
    197   Serialize(value, &result);
    198   expected = {ENCODING_TYPE_UINT8, 0xff};
    199   EXPECT_EQ(expected, result);
    200   result.Clear();
    201 
    202   // Min UINT16.
    203   value = (1 << 8);
    204   Serialize(value, &result);
    205   expected = {ENCODING_TYPE_UINT16, 0, 1};
    206   EXPECT_EQ(expected, result);
    207   result.Clear();
    208 
    209   // Max UINT16.
    210   value = 0xffff;
    211   Serialize(value, &result);
    212   expected = {ENCODING_TYPE_UINT16, 0xff, 0xff};
    213   EXPECT_EQ(expected, result);
    214   result.Clear();
    215 }
    216 
    217 TEST(SerializationTest, uint32_t) {
    218   Payload result;
    219   Payload expected;
    220   uint32_t value;
    221 
    222   // Min FIXINT.
    223   value = 0;
    224   Serialize(value, &result);
    225   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    226   EXPECT_EQ(expected, result);
    227   result.Clear();
    228 
    229   // Max FIXINT.
    230   value = (1 << 7) - 1;
    231   Serialize(value, &result);
    232   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    233   EXPECT_EQ(expected, result);
    234   result.Clear();
    235 
    236   // Min UINT8.
    237   value = (1 << 7);
    238   Serialize(value, &result);
    239   expected = {ENCODING_TYPE_UINT8, (1 << 7)};
    240   EXPECT_EQ(expected, result);
    241   result.Clear();
    242 
    243   // Max UINT8.
    244   value = 0xff;
    245   Serialize(value, &result);
    246   expected = {ENCODING_TYPE_UINT8, 0xff};
    247   EXPECT_EQ(expected, result);
    248   result.Clear();
    249 
    250   // Min UINT16.
    251   value = (1 << 8);
    252   Serialize(value, &result);
    253   expected = {ENCODING_TYPE_UINT16, 0, 1};
    254   EXPECT_EQ(expected, result);
    255   result.Clear();
    256 
    257   // Max UINT16.
    258   value = 0xffff;
    259   Serialize(value, &result);
    260   expected = {ENCODING_TYPE_UINT16, 0xff, 0xff};
    261   EXPECT_EQ(expected, result);
    262   result.Clear();
    263 
    264   // Min UINT32.
    265   value = (1 << 16);
    266   Serialize(value, &result);
    267   expected = {ENCODING_TYPE_UINT32, 0, 0, 1, 0};
    268   EXPECT_EQ(expected, result);
    269   result.Clear();
    270 
    271   // Max UINT32.
    272   value = 0xffffffff;
    273   Serialize(value, &result);
    274   expected = {ENCODING_TYPE_UINT32, 0xff, 0xff, 0xff, 0xff};
    275   EXPECT_EQ(expected, result);
    276   result.Clear();
    277 }
    278 
    279 TEST(SerializationTest, uint64_t) {
    280   Payload result;
    281   Payload expected;
    282   uint64_t value;
    283 
    284   // Min FIXINT.
    285   value = 0;
    286   Serialize(value, &result);
    287   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    288   EXPECT_EQ(expected, result);
    289   result.Clear();
    290 
    291   // Max FIXINT.
    292   value = (1 << 7) - 1;
    293   Serialize(value, &result);
    294   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    295   EXPECT_EQ(expected, result);
    296   result.Clear();
    297 
    298   // Min UINT8.
    299   value = (1 << 7);
    300   Serialize(value, &result);
    301   expected = {ENCODING_TYPE_UINT8, (1 << 7)};
    302   EXPECT_EQ(expected, result);
    303   result.Clear();
    304 
    305   // Max UINT8.
    306   value = 0xff;
    307   Serialize(value, &result);
    308   expected = {ENCODING_TYPE_UINT8, 0xff};
    309   EXPECT_EQ(expected, result);
    310   result.Clear();
    311 
    312   // Min UINT16.
    313   value = (1 << 8);
    314   Serialize(value, &result);
    315   expected = {ENCODING_TYPE_UINT16, 0, 1};
    316   EXPECT_EQ(expected, result);
    317   result.Clear();
    318 
    319   // Max UINT16.
    320   value = 0xffff;
    321   Serialize(value, &result);
    322   expected = {ENCODING_TYPE_UINT16, 0xff, 0xff};
    323   EXPECT_EQ(expected, result);
    324   result.Clear();
    325 
    326   // Min UINT32.
    327   value = (1 << 16);
    328   Serialize(value, &result);
    329   expected = {ENCODING_TYPE_UINT32, 0, 0, 1, 0};
    330   EXPECT_EQ(expected, result);
    331   result.Clear();
    332 
    333   // Max UINT32.
    334   value = 0xffffffff;
    335   Serialize(value, &result);
    336   expected = {ENCODING_TYPE_UINT32, 0xff, 0xff, 0xff, 0xff};
    337   EXPECT_EQ(expected, result);
    338   result.Clear();
    339 
    340   // Min UINT64.
    341   value = (1ULL << 32);
    342   Serialize(value, &result);
    343   expected = {ENCODING_TYPE_UINT64, 0, 0, 0, 0, 1, 0, 0, 0};
    344   EXPECT_EQ(expected, result);
    345   result.Clear();
    346 
    347   // Max UINT64.
    348   value = 0xffffffffffffffffULL;
    349   Serialize(value, &result);
    350   expected = {
    351       ENCODING_TYPE_UINT64, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
    352   EXPECT_EQ(expected, result);
    353   result.Clear();
    354 }
    355 
    356 TEST(SerializationTest, int8_t) {
    357   Payload result;
    358   Payload expected;
    359   int8_t value;
    360 
    361   // Min NEGATIVE FIXINT.
    362   value = -32;
    363   Serialize(value, &result);
    364   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
    365   EXPECT_EQ(expected, result);
    366   result.Clear();
    367 
    368   // Max NEGATIVE FIXINT.
    369   value = -1;
    370   Serialize(value, &result);
    371   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
    372   EXPECT_EQ(expected, result);
    373   result.Clear();
    374 
    375   // Min FIXINT.
    376   value = 0;
    377   Serialize(value, &result);
    378   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    379   EXPECT_EQ(expected, result);
    380   result.Clear();
    381 
    382   // Max FIXINT.
    383   value = 127;
    384   Serialize(value, &result);
    385   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    386   EXPECT_EQ(expected, result);
    387   result.Clear();
    388 
    389   // Min INT8.
    390   value = -128;
    391   Serialize(value, &result);
    392   expected = {ENCODING_TYPE_INT8, 0x80};
    393   EXPECT_EQ(expected, result);
    394   result.Clear();
    395 
    396   // Max INT8.
    397   value = -33;
    398   Serialize(value, &result);
    399   expected = {ENCODING_TYPE_INT8, 0xdf};
    400   EXPECT_EQ(expected, result);
    401   result.Clear();
    402 }
    403 
    404 TEST(SerializationTest, int16_t) {
    405   Payload result;
    406   Payload expected;
    407   int16_t value;
    408 
    409   // Min NEGATIVE FIXINT.
    410   value = -32;
    411   Serialize(value, &result);
    412   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
    413   EXPECT_EQ(expected, result);
    414   result.Clear();
    415 
    416   // Max NEGATIVE FIXINT.
    417   value = -1;
    418   Serialize(value, &result);
    419   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
    420   EXPECT_EQ(expected, result);
    421   result.Clear();
    422 
    423   // Min FIXINT.
    424   value = 0;
    425   Serialize(value, &result);
    426   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    427   EXPECT_EQ(expected, result);
    428   result.Clear();
    429 
    430   // Max FIXINT.
    431   value = 127;
    432   Serialize(value, &result);
    433   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    434   EXPECT_EQ(expected, result);
    435   result.Clear();
    436 
    437   // Min INT8.
    438   value = -128;
    439   Serialize(value, &result);
    440   expected = {ENCODING_TYPE_INT8, 0x80};
    441   EXPECT_EQ(expected, result);
    442   result.Clear();
    443 
    444   // Max INT8.
    445   value = -33;
    446   Serialize(value, &result);
    447   expected = {ENCODING_TYPE_INT8, 0xdf};
    448   EXPECT_EQ(expected, result);
    449   result.Clear();
    450 
    451   // Min INT16.
    452   value = -32768;
    453   Serialize(value, &result);
    454   expected = {ENCODING_TYPE_INT16, 0x00, 0x80};
    455   EXPECT_EQ(expected, result);
    456   result.Clear();
    457 
    458   // Max INT16.
    459   value = 32767;
    460   Serialize(value, &result);
    461   expected = {ENCODING_TYPE_INT16, 0xff, 0x7f};
    462   EXPECT_EQ(expected, result);
    463   result.Clear();
    464 }
    465 
    466 TEST(SerializationTest, int32_t) {
    467   Payload result;
    468   Payload expected;
    469   int32_t value;
    470 
    471   // Min NEGATIVE FIXINT.
    472   value = -32;
    473   Serialize(value, &result);
    474   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
    475   EXPECT_EQ(expected, result);
    476   result.Clear();
    477 
    478   // Max NEGATIVE FIXINT.
    479   value = -1;
    480   Serialize(value, &result);
    481   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
    482   EXPECT_EQ(expected, result);
    483   result.Clear();
    484 
    485   // Min FIXINT.
    486   value = 0;
    487   Serialize(value, &result);
    488   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    489   EXPECT_EQ(expected, result);
    490   result.Clear();
    491 
    492   // Max FIXINT.
    493   value = 127;
    494   Serialize(value, &result);
    495   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    496   EXPECT_EQ(expected, result);
    497   result.Clear();
    498 
    499   // Min INT8.
    500   value = -128;
    501   Serialize(value, &result);
    502   expected = {ENCODING_TYPE_INT8, 0x80};
    503   EXPECT_EQ(expected, result);
    504   result.Clear();
    505 
    506   // Max INT8.
    507   value = -33;
    508   Serialize(value, &result);
    509   expected = {ENCODING_TYPE_INT8, 0xdf};
    510   EXPECT_EQ(expected, result);
    511   result.Clear();
    512 
    513   // Min INT16.
    514   value = -32768;
    515   Serialize(value, &result);
    516   expected = {ENCODING_TYPE_INT16, 0x00, 0x80};
    517   EXPECT_EQ(expected, result);
    518   result.Clear();
    519 
    520   // Max INT16.
    521   value = 32767;
    522   Serialize(value, &result);
    523   expected = {ENCODING_TYPE_INT16, 0xff, 0x7f};
    524   EXPECT_EQ(expected, result);
    525   result.Clear();
    526 
    527   // Min INT32.
    528   value = -2147483648;
    529   Serialize(value, &result);
    530   expected = {ENCODING_TYPE_INT32, 0x00, 0x00, 0x00, 0x80};
    531   EXPECT_EQ(expected, result);
    532   result.Clear();
    533 
    534   // Max INT32.
    535   value = 2147483647;
    536   Serialize(value, &result);
    537   expected = {ENCODING_TYPE_INT32, 0xff, 0xff, 0xff, 0x7f};
    538   EXPECT_EQ(expected, result);
    539   result.Clear();
    540 }
    541 
    542 TEST(SerializationTest, int64_t) {
    543   Payload result;
    544   Payload expected;
    545   int64_t value;
    546 
    547   // Min NEGATIVE FIXINT.
    548   value = -32;
    549   Serialize(value, &result);
    550   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
    551   EXPECT_EQ(expected, result);
    552   result.Clear();
    553 
    554   // Max NEGATIVE FIXINT.
    555   value = -1;
    556   Serialize(value, &result);
    557   expected = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
    558   EXPECT_EQ(expected, result);
    559   result.Clear();
    560 
    561   // Min FIXINT.
    562   value = 0;
    563   Serialize(value, &result);
    564   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
    565   EXPECT_EQ(expected, result);
    566   result.Clear();
    567 
    568   // Max FIXINT.
    569   value = 127;
    570   Serialize(value, &result);
    571   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
    572   EXPECT_EQ(expected, result);
    573   result.Clear();
    574 
    575   // Min INT8.
    576   value = -128;
    577   Serialize(value, &result);
    578   expected = {ENCODING_TYPE_INT8, 0x80};
    579   EXPECT_EQ(expected, result);
    580   result.Clear();
    581 
    582   // Max INT8.
    583   value = -33;
    584   Serialize(value, &result);
    585   expected = {ENCODING_TYPE_INT8, 0xdf};
    586   EXPECT_EQ(expected, result);
    587   result.Clear();
    588 
    589   // Min INT16.
    590   value = -32768;
    591   Serialize(value, &result);
    592   expected = {ENCODING_TYPE_INT16, 0x00, 0x80};
    593   EXPECT_EQ(expected, result);
    594   result.Clear();
    595 
    596   // Max INT16.
    597   value = 32767;
    598   Serialize(value, &result);
    599   expected = {ENCODING_TYPE_INT16, 0xff, 0x7f};
    600   EXPECT_EQ(expected, result);
    601   result.Clear();
    602 
    603   // Min INT32.
    604   value = -2147483648;
    605   Serialize(value, &result);
    606   expected = {ENCODING_TYPE_INT32, 0x00, 0x00, 0x00, 0x80};
    607   EXPECT_EQ(expected, result);
    608   result.Clear();
    609 
    610   // Max INT32.
    611   value = 2147483647;
    612   Serialize(value, &result);
    613   expected = {ENCODING_TYPE_INT32, 0xff, 0xff, 0xff, 0x7f};
    614   EXPECT_EQ(expected, result);
    615   result.Clear();
    616 
    617   // Min INT64.
    618   value = -9223372036854775808ULL;
    619   Serialize(value, &result);
    620   expected = {
    621       ENCODING_TYPE_INT64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80};
    622   EXPECT_EQ(expected, result);
    623   result.Clear();
    624 
    625   // Max INT64.
    626   value = 9223372036854775807ULL;
    627   Serialize(value, &result);
    628   expected = {
    629       ENCODING_TYPE_INT64, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f};
    630   EXPECT_EQ(expected, result);
    631   result.Clear();
    632 }
    633 
    634 TEST(SerializationTest, float) {
    635   Payload result;
    636   Payload expected;
    637   float value;
    638 
    639   value = 0.0f;
    640   Serialize(value, &result);
    641   expected = {ENCODING_TYPE_FLOAT32, kZeroFloatBytes[0], kZeroFloatBytes[1],
    642               kZeroFloatBytes[2], kZeroFloatBytes[3]};
    643   EXPECT_EQ(expected, result);
    644   result.Clear();
    645 
    646   value = 1.0f;
    647   Serialize(value, &result);
    648   expected = {ENCODING_TYPE_FLOAT32, kOneFloatBytes[0], kOneFloatBytes[1],
    649               kOneFloatBytes[2], kOneFloatBytes[3]};
    650   EXPECT_EQ(expected, result);
    651   result.Clear();
    652 }
    653 
    654 TEST(SerializationTest, double) {
    655   Payload result;
    656   Payload expected;
    657   double value;
    658 
    659   value = 0.0f;
    660   Serialize(value, &result);
    661   expected = {ENCODING_TYPE_FLOAT64, kZeroDoubleBytes[0], kZeroDoubleBytes[1],
    662               kZeroDoubleBytes[2],   kZeroDoubleBytes[3], kZeroDoubleBytes[4],
    663               kZeroDoubleBytes[5],   kZeroDoubleBytes[6], kZeroDoubleBytes[7]};
    664   EXPECT_EQ(expected, result);
    665   result.Clear();
    666 
    667   value = 1.0f;
    668   Serialize(value, &result);
    669   expected = {ENCODING_TYPE_FLOAT64, kOneDoubleBytes[0], kOneDoubleBytes[1],
    670               kOneDoubleBytes[2],    kOneDoubleBytes[3], kOneDoubleBytes[4],
    671               kOneDoubleBytes[5],    kOneDoubleBytes[6], kOneDoubleBytes[7]};
    672   EXPECT_EQ(expected, result);
    673   result.Clear();
    674 }
    675 
    676 TEST(SerializationTest, Enum) {
    677   Payload result;
    678   Payload expected;
    679 
    680   enum Foo { kFoo, kBar, kBaz };
    681   Foo value = kBar;
    682   Serialize(value, &result);
    683   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1};
    684   EXPECT_EQ(expected, result);
    685   result.Clear();
    686 }
    687 
    688 TEST(SerializationTest, EnumClass) {
    689   Payload result;
    690   Payload expected;
    691 
    692   enum class Foo { kFoo, kBar, kBaz };
    693   Foo value = Foo::kBaz;
    694   Serialize(value, &result);
    695   expected = {ENCODING_TYPE_POSITIVE_FIXINT_MIN + 2};
    696   EXPECT_EQ(expected, result);
    697   result.Clear();
    698 }
    699 
    700 TEST(SerializationTest, LocalHandle) {
    701   Payload result;
    702   Payload expected;
    703   LocalHandle fd1;
    704   LocalHandle fd2;
    705 
    706   fd1 = LocalHandle(100);
    707   Serialize(fd1, &result);
    708   expected = {ENCODING_TYPE_FIXEXT2, ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0, 0};
    709   EXPECT_EQ(expected, result);
    710   EXPECT_EQ(1u, result.FdCount());
    711   EXPECT_EQ(100, result.FdArray()[0]);
    712   result.Clear();
    713 
    714   fd2 = LocalHandle(200);
    715   Serialize(std::forward_as_tuple(fd1, fd2), &result);
    716   expected = decltype(expected)(
    717       {ENCODING_TYPE_FIXARRAY_MIN + 2, ENCODING_TYPE_FIXEXT2,
    718        ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0, 0, ENCODING_TYPE_FIXEXT2,
    719        ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 1, 0});
    720   EXPECT_EQ(expected, result);
    721   EXPECT_EQ(2u, result.FdCount());
    722   EXPECT_EQ(100, result.FdArray()[0]);
    723   EXPECT_EQ(200, result.FdArray()[1]);
    724   result.Clear();
    725 
    726   fd1.Release();  // Don't try to close fd 100.
    727   fd2.Release();  // Don't try to close fd 200.
    728 
    729   fd1 = LocalHandle(-2);
    730   Serialize(fd1, &result);
    731   expected = {ENCODING_TYPE_FIXEXT2, ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0xfe,
    732               0xff};
    733   EXPECT_EQ(expected, result);
    734   EXPECT_EQ(0u, result.FdCount());
    735   result.Clear();
    736 }
    737 
    738 TEST(SerializationTest, string) {
    739   Payload result;
    740   Payload expected;
    741   std::string value;
    742 
    743   // Min FIXSTR.
    744   value = "";
    745   Serialize(value, &result);
    746   expected = {ENCODING_TYPE_FIXSTR_MIN};
    747   EXPECT_EQ(expected, result);
    748   result.Clear();
    749 
    750   // Max FIXSTR.
    751   value = std::string((1 << 5) - 1, 'x');
    752   Serialize(value, &result);
    753   expected = {ENCODING_TYPE_FIXSTR_MAX};
    754   expected.Append((1 << 5) - 1, 'x');
    755   EXPECT_EQ(expected, result);
    756   result.Clear();
    757 
    758   // Min STR8.
    759   value = std::string((1 << 5), 'x');
    760   Serialize(value, &result);
    761   expected = {ENCODING_TYPE_STR8, (1 << 5)};
    762   expected.Append((1 << 5), 'x');
    763   EXPECT_EQ(expected, result);
    764   result.Clear();
    765 
    766   // Max STR8.
    767   value = std::string((1 << 8) - 1, 'x');
    768   Serialize(value, &result);
    769   expected = {ENCODING_TYPE_STR8, (1 << 8) - 1};
    770   expected.Append((1 << 8) - 1, 'x');
    771   EXPECT_EQ(expected, result);
    772   result.Clear();
    773 
    774   // Min STR16.
    775   value = std::string((1 << 8), 'x');
    776   Serialize(value, &result);
    777   expected = {ENCODING_TYPE_STR16, 0x00, 0x01};
    778   expected.Append((1 << 8), 'x');
    779   EXPECT_EQ(expected, result);
    780   result.Clear();
    781 
    782   // Max STR16.
    783   value = std::string((1 << 16) - 1, 'x');
    784   Serialize(value, &result);
    785   expected = {ENCODING_TYPE_STR16, 0xff, 0xff};
    786   expected.Append((1 << 16) - 1, 'x');
    787   EXPECT_EQ(expected, result);
    788   result.Clear();
    789 
    790   // Min STR32.
    791   value = std::string((1 << 16), 'x');
    792   Serialize(value, &result);
    793   expected = {ENCODING_TYPE_STR32, 0x00, 0x00, 0x01, 0x00};
    794   expected.Append((1 << 16), 'x');
    795   EXPECT_EQ(expected, result);
    796   result.Clear();
    797 }
    798 
    799 TEST(SerializationTest, StringWrapper) {
    800   Payload result;
    801   Payload expected;
    802   std::string value;
    803 
    804   // Min FIXSTR.
    805   value = "";
    806   Serialize(WrapString(value), &result);
    807   expected = {ENCODING_TYPE_FIXSTR_MIN};
    808   EXPECT_EQ(expected, result);
    809   result.Clear();
    810 
    811   // Max FIXSTR.
    812   value = std::string((1 << 5) - 1, 'x');
    813   Serialize(WrapString(value), &result);
    814   expected = {ENCODING_TYPE_FIXSTR_MAX};
    815   expected.Append((1 << 5) - 1, 'x');
    816   EXPECT_EQ(expected, result);
    817   result.Clear();
    818 
    819   // Min STR8.
    820   value = std::string((1 << 5), 'x');
    821   Serialize(WrapString(value), &result);
    822   expected = {ENCODING_TYPE_STR8, (1 << 5)};
    823   expected.Append((1 << 5), 'x');
    824   EXPECT_EQ(expected, result);
    825   result.Clear();
    826 
    827   // Max STR8.
    828   value = std::string((1 << 8) - 1, 'x');
    829   Serialize(WrapString(value), &result);
    830   expected = {ENCODING_TYPE_STR8, (1 << 8) - 1};
    831   expected.Append((1 << 8) - 1, 'x');
    832   EXPECT_EQ(expected, result);
    833   result.Clear();
    834 
    835   // Min STR16.
    836   value = std::string((1 << 8), 'x');
    837   Serialize(WrapString(value), &result);
    838   expected = {ENCODING_TYPE_STR16, 0x00, 0x01};
    839   expected.Append((1 << 8), 'x');
    840   EXPECT_EQ(expected, result);
    841   result.Clear();
    842 
    843   // Max STR16.
    844   value = std::string((1 << 16) - 1, 'x');
    845   Serialize(WrapString(value), &result);
    846   expected = {ENCODING_TYPE_STR16, 0xff, 0xff};
    847   expected.Append((1 << 16) - 1, 'x');
    848   EXPECT_EQ(expected, result);
    849   result.Clear();
    850 
    851   // Min STR32.
    852   value = std::string((1 << 16), 'x');
    853   Serialize(WrapString(value), &result);
    854   expected = {ENCODING_TYPE_STR32, 0x00, 0x00, 0x01, 0x00};
    855   expected.Append((1 << 16), 'x');
    856   EXPECT_EQ(expected, result);
    857   result.Clear();
    858 }
    859 
    860 TEST(SerializationTest, vector) {
    861   Payload result;
    862   Payload expected;
    863   std::vector<uint8_t> value;
    864 
    865   // Min FIXARRAY.
    866   value = {};
    867   Serialize(value, &result);
    868   expected = {ENCODING_TYPE_FIXARRAY_MIN};
    869   EXPECT_EQ(expected, result);
    870   result.Clear();
    871 
    872   // Max FIXARRAY.
    873   value = decltype(value)((1 << 4) - 1, 'x');
    874   Serialize(value, &result);
    875   expected = {ENCODING_TYPE_FIXARRAY_MAX};
    876   expected.Append((1 << 4) - 1, 'x');
    877   EXPECT_EQ(expected, result);
    878   result.Clear();
    879 
    880   // Min ARRAY16.
    881   value = decltype(value)((1 << 4), 'x');
    882   Serialize(value, &result);
    883   expected = {ENCODING_TYPE_ARRAY16, 0x10, 0x00};
    884   expected.Append((1 << 4), 'x');
    885   EXPECT_EQ(expected, result);
    886   result.Clear();
    887 
    888   // Max ARRAY16.
    889   value = decltype(value)((1 << 16) - 1, 'x');
    890   Serialize(value, &result);
    891   expected = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
    892   expected.Append((1 << 16) - 1, 'x');
    893   EXPECT_EQ(expected, result);
    894   result.Clear();
    895 
    896   // Min ARRAY32.
    897   value = decltype(value)((1 << 16), 'x');
    898   Serialize(value, &result);
    899   expected = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
    900   expected.Append((1 << 16), 'x');
    901   EXPECT_EQ(expected, result);
    902   result.Clear();
    903 }
    904 
    905 TEST(SerializationTest, map) {
    906   Payload result;
    907   Payload expected;
    908   std::map<std::uint32_t, std::uint32_t> value;
    909 
    910   // Min FIXMAP.
    911   value = {};
    912   Serialize(value, &result);
    913   expected = {ENCODING_TYPE_FIXMAP_MIN};
    914   EXPECT_EQ(expected, result);
    915   result.Clear();
    916 
    917   // Max FIXMAP.
    918   value = MakeMap<decltype(value)>((1 << 4) - 1);
    919   Serialize(value, &result);
    920   expected = {ENCODING_TYPE_FIXMAP_MAX};
    921   InsertKeyValue<decltype(value)>(&expected, (1 << 4) - 1);
    922   EXPECT_EQ(expected, result);
    923   result.Clear();
    924 
    925   // Min MAP16.
    926   value = MakeMap<decltype(value)>((1 << 4));
    927   Serialize(value, &result);
    928   expected = {ENCODING_TYPE_MAP16, 0x10, 0x00};
    929   InsertKeyValue<decltype(value)>(&expected, (1 << 4));
    930   EXPECT_EQ(expected, result);
    931   result.Clear();
    932 
    933   // Max MAP16.
    934   value = MakeMap<decltype(value)>((1 << 16) - 1);
    935   Serialize(value, &result);
    936   expected = {ENCODING_TYPE_MAP16, 0xff, 0xff};
    937   InsertKeyValue<decltype(value)>(&expected, (1 << 16) - 1);
    938   EXPECT_EQ(expected, result);
    939   result.Clear();
    940 
    941   // Min MAP32.
    942   value = MakeMap<decltype(value)>((1 << 16));
    943   Serialize(value, &result);
    944   expected = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x01, 0x00};
    945   InsertKeyValue<decltype(value)>(&expected, (1 << 16));
    946   EXPECT_EQ(expected, result);
    947   result.Clear();
    948 }
    949 
    950 TEST(SerializationTest, unordered_map) {
    951   Payload result;
    952   Payload expected;
    953   std::unordered_map<std::uint32_t, std::uint32_t> value;
    954 
    955   // Min FIXMAP.
    956   value = {};
    957   Serialize(value, &result);
    958   expected = {ENCODING_TYPE_FIXMAP_MIN};
    959   EXPECT_EQ(expected, result);
    960   result.Clear();
    961 
    962   // Max FIXMAP.
    963   value = MakeMap<decltype(value)>((1 << 4) - 1);
    964   Serialize(value, &result);
    965   expected = {ENCODING_TYPE_FIXMAP_MAX};
    966   InsertKeyValue<decltype(value)>(&expected, (1 << 4) - 1);
    967   EXPECT_EQ(expected, result);
    968   result.Clear();
    969 
    970   // Min MAP16.
    971   value = MakeMap<decltype(value)>((1 << 4));
    972   Serialize(value, &result);
    973   expected = {ENCODING_TYPE_MAP16, 0x10, 0x00};
    974   InsertKeyValue<decltype(value)>(&expected, (1 << 4));
    975   EXPECT_EQ(expected, result);
    976   result.Clear();
    977 
    978   // Max MAP16.
    979   value = MakeMap<decltype(value)>((1 << 16) - 1);
    980   Serialize(value, &result);
    981   expected = {ENCODING_TYPE_MAP16, 0xff, 0xff};
    982   InsertKeyValue<decltype(value)>(&expected, (1 << 16) - 1);
    983   EXPECT_EQ(expected, result);
    984   result.Clear();
    985 
    986   // Min MAP32.
    987   value = MakeMap<decltype(value)>((1 << 16));
    988   Serialize(value, &result);
    989   expected = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x01, 0x00};
    990   InsertKeyValue<decltype(value)>(&expected, (1 << 16));
    991   EXPECT_EQ(expected, result);
    992   result.Clear();
    993 }
    994 
    995 TEST(SerializationTest, array) {
    996   Payload result;
    997   Payload expected;
    998 
    999   // Min FIXARRAY.
   1000   std::array<std::uint8_t, 0> a0;
   1001   Serialize(a0, &result);
   1002   expected = {ENCODING_TYPE_FIXARRAY_MIN};
   1003   EXPECT_EQ(expected, result);
   1004   result.Clear();
   1005 
   1006   // Max FIXARRAY.
   1007   std::array<std::uint8_t, (1 << 4) - 1> a1;
   1008   for (auto& element : a1)
   1009     element = 'x';
   1010   Serialize(a1, &result);
   1011   expected = {ENCODING_TYPE_FIXARRAY_MAX};
   1012   expected.Append((1 << 4) - 1, 'x');
   1013   EXPECT_EQ(expected, result);
   1014   result.Clear();
   1015 
   1016   // Min ARRAY16.
   1017   std::array<std::uint8_t, (1 << 4)> a2;
   1018   for (auto& element : a2)
   1019     element = 'x';
   1020   Serialize(a2, &result);
   1021   expected = {ENCODING_TYPE_ARRAY16, 0x10, 0x00};
   1022   expected.Append((1 << 4), 'x');
   1023   EXPECT_EQ(expected, result);
   1024   result.Clear();
   1025 
   1026   // Max ARRAY16.
   1027   std::array<std::uint8_t, (1 << 16) - 1> a3;
   1028   for (auto& element : a3)
   1029     element = 'x';
   1030   Serialize(a3, &result);
   1031   expected = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   1032   expected.Append((1 << 16) - 1, 'x');
   1033   EXPECT_EQ(expected, result);
   1034   result.Clear();
   1035 
   1036   // Min ARRAY32.
   1037   std::array<std::uint8_t, (1 << 16)> a4;
   1038   for (auto& element : a4)
   1039     element = 'x';
   1040   Serialize(a4, &result);
   1041   expected = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   1042   expected.Append((1 << 16), 'x');
   1043   EXPECT_EQ(expected, result);
   1044   result.Clear();
   1045 }
   1046 
   1047 TEST(SerializationTest, ArrayWrapper) {
   1048   Payload result;
   1049   Payload expected;
   1050   std::vector<std::uint8_t, DefaultInitializationAllocator<std::uint8_t>> value;
   1051   ArrayWrapper<std::uint8_t> wrapper;
   1052 
   1053   // Min FIXARRAY.
   1054   value = {};
   1055   Serialize(wrapper, &result);
   1056   expected = {ENCODING_TYPE_FIXARRAY_MIN};
   1057   EXPECT_EQ(expected, result);
   1058   result.Clear();
   1059 
   1060   // Max FIXARRAY.
   1061   value = decltype(value)((1 << 4) - 1, 'x');
   1062   wrapper = decltype(wrapper)(value.data(), value.capacity(), value.size());
   1063   Serialize(wrapper, &result);
   1064   expected = {ENCODING_TYPE_FIXARRAY_MAX};
   1065   expected.Append((1 << 4) - 1, 'x');
   1066   EXPECT_EQ(expected, result);
   1067   result.Clear();
   1068 
   1069   // Min ARRAY16.
   1070   value = decltype(value)((1 << 4), 'x');
   1071   wrapper = decltype(wrapper)(value.data(), value.capacity(), value.size());
   1072   Serialize(wrapper, &result);
   1073   expected = {ENCODING_TYPE_ARRAY16, 0x10, 0x00};
   1074   expected.Append((1 << 4), 'x');
   1075   EXPECT_EQ(expected, result);
   1076   result.Clear();
   1077 
   1078   // Max ARRAY16.
   1079   value = decltype(value)((1 << 16) - 1, 'x');
   1080   wrapper = decltype(wrapper)(value.data(), value.capacity(), value.size());
   1081   Serialize(wrapper, &result);
   1082   expected = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   1083   expected.Append((1 << 16) - 1, 'x');
   1084   EXPECT_EQ(expected, result);
   1085   result.Clear();
   1086 
   1087   // Min ARRAY32.
   1088   value = decltype(value)((1 << 16), 'x');
   1089   wrapper = decltype(wrapper)(value.data(), value.capacity(), value.size());
   1090   Serialize(wrapper, &result);
   1091   expected = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   1092   expected.Append((1 << 16), 'x');
   1093   EXPECT_EQ(expected, result);
   1094   result.Clear();
   1095 }
   1096 
   1097 TEST(SerializationTest, pair) {
   1098   Payload result;
   1099   Payload expected;
   1100 
   1101   auto p1 = std::make_pair(1, 2);
   1102   Serialize(p1, &result);
   1103   expected = {ENCODING_TYPE_FIXARRAY_MIN + 2, 1, 2};
   1104   EXPECT_EQ(expected, result);
   1105   result.Clear();
   1106 
   1107   auto p2 = std::make_pair('x', std::string("12345"));
   1108   Serialize(p2, &result);
   1109   expected = decltype(expected)({ENCODING_TYPE_FIXARRAY_MIN + 2, 'x',
   1110                                  ENCODING_TYPE_FIXSTR_MIN + 5, '1', '2', '3',
   1111                                  '4', '5'});
   1112   EXPECT_EQ(expected, result);
   1113   result.Clear();
   1114 }
   1115 
   1116 TEST(SerializationTest, tuple) {
   1117   Payload result;
   1118   Payload expected;
   1119 
   1120   // Min FIXARRAY.
   1121   auto t1 = std::make_tuple();
   1122   Serialize(t1, &result);
   1123   expected = {ENCODING_TYPE_FIXARRAY_MIN};
   1124   EXPECT_EQ(expected, result);
   1125   result.Clear();
   1126 
   1127   // Max FIXARRAY.
   1128   auto t2 = GetNTuple<15>('x');
   1129   Serialize(t2, &result);
   1130   expected = {ENCODING_TYPE_FIXARRAY_MAX};
   1131   expected.Append((1 << 4) - 1, 'x');
   1132   EXPECT_EQ(expected, result);
   1133   result.Clear();
   1134 
   1135   // Min ARRAY16.
   1136   auto t3 = GetNTuple<(1 << 4)>('x');
   1137   Serialize(t3, &result);
   1138   expected = {ENCODING_TYPE_ARRAY16, 0x10, 0x00};
   1139   expected.Append((1 << 4), 'x');
   1140   EXPECT_EQ(expected, result);
   1141   result.Clear();
   1142 
   1143 // Template instantiation depth is an issue for these tests. They are commented
   1144 // out to document the expected behavior, even though tuples of this order are
   1145 // not expected in practice.
   1146 #if 0
   1147   // Max ARRAY16.
   1148   auto t4 = GetNTuple<(1 << 16)-1>('x');
   1149   Serialize(t4, &result);
   1150   expected = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   1151   expected.Append((1 << 16)-1, 'x');
   1152   EXPECT_EQ(expected, result);
   1153   result.Clear();
   1154 
   1155   // Min ARRAY32.
   1156   auto t5 = GetNTuple<(1 << 16)>('x');
   1157   Serialize(t5, &result);
   1158   expected = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   1159   expected.Append((1 << 16), 'x');
   1160   EXPECT_EQ(expected, result);
   1161   result.Clear();
   1162 #endif
   1163 }
   1164 
   1165 // TODO(eieio): More exhaustive testing of type nesting.
   1166 TEST(SerializationTest, NestedTuple) {
   1167   Payload result;
   1168   Payload expected;
   1169 
   1170   auto t1 = std::make_tuple('x', std::make_tuple<int, int>(1, 2));
   1171   Serialize(t1, &result);
   1172   expected = decltype(expected)({ENCODING_TYPE_FIXARRAY_MIN + 2, 'x',
   1173                                  ENCODING_TYPE_FIXARRAY_MIN + 2, 1, 2});
   1174   EXPECT_EQ(expected, result);
   1175   result.Clear();
   1176 
   1177   auto t2 = std::make_tuple('x', std::make_tuple<int, int>(1, 2),
   1178                             std::string("0123456789"));
   1179   Serialize(t2, &result);
   1180   expected = decltype(expected)({ENCODING_TYPE_FIXARRAY_MIN + 3, 'x',
   1181                                  ENCODING_TYPE_FIXARRAY_MIN + 2, 1, 2,
   1182                                  ENCODING_TYPE_FIXSTR | 10, '0', '1', '2', '3',
   1183                                  '4', '5', '6', '7', '8', '9'});
   1184   EXPECT_EQ(expected, result);
   1185   result.Clear();
   1186 
   1187   auto t3 = std::make_tuple(0.0f, std::uint64_t(10ULL),
   1188                             std::vector<char>{'a', 'b', 'c'});
   1189   Serialize(t3, &result);
   1190   expected = decltype(expected)(
   1191       {ENCODING_TYPE_FIXARRAY_MIN + 3, ENCODING_TYPE_FLOAT32,
   1192        kZeroFloatBytes[0], kZeroFloatBytes[1], kZeroFloatBytes[2],
   1193        kZeroFloatBytes[3], ENCODING_TYPE_POSITIVE_FIXINT_MIN + 10,
   1194        ENCODING_TYPE_FIXARRAY_MIN + 3, 'a', 'b', 'c'});
   1195   EXPECT_EQ(expected, result);
   1196   result.Clear();
   1197 }
   1198 
   1199 TEST(SerializationTest, NestedMap) {
   1200   Payload result;
   1201   Payload expected;
   1202 
   1203   std::map<int, std::pair<std::string, int>> m1 = {{0, {"a", 2}},
   1204                                                    {1, {"b", 10}}};
   1205   Serialize(m1, &result);
   1206   expected = decltype(expected)(
   1207       {ENCODING_TYPE_FIXMAP_MIN + 2, 0, ENCODING_TYPE_FIXARRAY_MIN + 2,
   1208        ENCODING_TYPE_FIXSTR_MIN + 1, 'a', 2, 1, ENCODING_TYPE_FIXARRAY_MIN + 2,
   1209        ENCODING_TYPE_FIXSTR_MIN + 1, 'b', 10});
   1210   EXPECT_EQ(expected, result);
   1211   result.Clear();
   1212 }
   1213 
   1214 TEST(SerializationTest, Serializable) {
   1215   Payload result;
   1216   Payload expected;
   1217 
   1218   TestType t1{10, 0.0, "12345", TestType::Foo::kBaz};
   1219   Serialize(t1, &result);
   1220   expected = decltype(expected)(
   1221       {ENCODING_TYPE_FIXARRAY_MIN + 4, 10, ENCODING_TYPE_FLOAT32,
   1222        kZeroFloatBytes[0], kZeroFloatBytes[1], kZeroFloatBytes[2],
   1223        kZeroFloatBytes[3], ENCODING_TYPE_FIXSTR_MIN + 5, '1', '2', '3', '4',
   1224        '5', ENCODING_TYPE_POSITIVE_FIXINT_MIN + 2});
   1225   EXPECT_EQ(expected, result);
   1226   result.Clear();
   1227 
   1228   TestTemplateType<LocalHandle> tt{LocalHandle(-1)};
   1229   Serialize(tt, &result);
   1230   expected =
   1231       decltype(expected)({ENCODING_TYPE_FIXARRAY_MIN + 1, ENCODING_TYPE_FIXEXT2,
   1232                           ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0xff, 0xff});
   1233   EXPECT_EQ(expected, result);
   1234 }
   1235 
   1236 TEST(SerializationTest, Variant) {
   1237   Payload result;
   1238   Payload expected;
   1239 
   1240   Variant<int, bool, float> v;
   1241 
   1242   // Empty variant.
   1243   Serialize(v, &result);
   1244   expected = {ENCODING_TYPE_FIXMAP_MIN + 1, ENCODING_TYPE_NEGATIVE_FIXINT_MAX,
   1245               ENCODING_TYPE_NIL};
   1246   EXPECT_EQ(expected, result);
   1247   result.Clear();
   1248 
   1249   v = 10;
   1250   Serialize(v, &result);
   1251   expected = {ENCODING_TYPE_FIXMAP_MIN + 1,
   1252               ENCODING_TYPE_POSITIVE_FIXINT_MIN + 0,
   1253               ENCODING_TYPE_POSITIVE_FIXINT_MIN + 10};
   1254   EXPECT_EQ(expected, result);
   1255   result.Clear();
   1256 
   1257   v = true;
   1258   Serialize(v, &result);
   1259   expected = {ENCODING_TYPE_FIXMAP_MIN + 1,
   1260               ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1, ENCODING_TYPE_TRUE};
   1261   EXPECT_EQ(expected, result);
   1262   result.Clear();
   1263 
   1264   v = false;
   1265   Serialize(v, &result);
   1266   expected = {ENCODING_TYPE_FIXMAP_MIN + 1,
   1267               ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1, ENCODING_TYPE_FALSE};
   1268   EXPECT_EQ(expected, result);
   1269   result.Clear();
   1270 
   1271   v = 1.0f;
   1272   Serialize(v, &result);
   1273   expected = {ENCODING_TYPE_FIXMAP_MIN + 1,
   1274               ENCODING_TYPE_POSITIVE_FIXINT_MIN + 2,
   1275               ENCODING_TYPE_FLOAT32,
   1276               kOneFloatBytes[0],
   1277               kOneFloatBytes[1],
   1278               kOneFloatBytes[2],
   1279               kOneFloatBytes[3]};
   1280   EXPECT_EQ(expected, result);
   1281   result.Clear();
   1282 
   1283   // TODO(eieio): Add more serialization tests for Variant.
   1284 }
   1285 
   1286 TEST(DeserializationTest, bool) {
   1287   Payload buffer;
   1288   bool result = false;
   1289   ErrorType error;
   1290 
   1291   // True.
   1292   buffer = {ENCODING_TYPE_TRUE};
   1293   error = Deserialize(&result, &buffer);
   1294   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1295   EXPECT_EQ(1, result);  // Gtest generates warning from bool literals.
   1296 
   1297   // False.
   1298   buffer = {ENCODING_TYPE_FALSE};
   1299   error = Deserialize(&result, &buffer);
   1300   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1301   EXPECT_EQ(0, result);  // Gtest generates warning from bool literals.
   1302 }
   1303 
   1304 TEST(DeserializationTest, uint8_t) {
   1305   Payload buffer;
   1306   std::uint8_t result = 0;
   1307   ErrorType error;
   1308 
   1309   // Min FIXINT.
   1310   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1311   error = Deserialize(&result, &buffer);
   1312   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1313   EXPECT_EQ(0U, result);
   1314 
   1315   // Max FIXINT.
   1316   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1317   error = Deserialize(&result, &buffer);
   1318   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1319   EXPECT_EQ(127U, result);
   1320 
   1321   // Min UINT8.
   1322   buffer = {ENCODING_TYPE_UINT8, 0x00};
   1323   error = Deserialize(&result, &buffer);
   1324   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1325   EXPECT_EQ(0U, result);
   1326 
   1327   // Max UINT8.
   1328   buffer = {ENCODING_TYPE_UINT8, 0xff};
   1329   error = Deserialize(&result, &buffer);
   1330   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1331   EXPECT_EQ(0xffU, result);
   1332 
   1333   // UINT16 out of range.
   1334   buffer = {ENCODING_TYPE_UINT16};
   1335   error = Deserialize(&result, &buffer);
   1336   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1337   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1338   EXPECT_EQ(ENCODING_TYPE_UINT16, error.encoding_type());
   1339 
   1340   // UINT32 out of range.
   1341   buffer = {ENCODING_TYPE_UINT32};
   1342   error = Deserialize(&result, &buffer);
   1343   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1344   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1345   EXPECT_EQ(ENCODING_TYPE_UINT32, error.encoding_type());
   1346 
   1347   // UINT64 out of range.
   1348   buffer = {ENCODING_TYPE_UINT64};
   1349   error = Deserialize(&result, &buffer);
   1350   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1351   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1352   EXPECT_EQ(ENCODING_TYPE_UINT64, error.encoding_type());
   1353 }
   1354 
   1355 TEST(DeserializationTest, uint16_t) {
   1356   Payload buffer;
   1357   std::uint16_t result = 0;
   1358   ErrorType error;
   1359 
   1360   // Min FIXINT.
   1361   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1362   error = Deserialize(&result, &buffer);
   1363   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1364   EXPECT_EQ(0U, result);
   1365 
   1366   // Max FIXINT.
   1367   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1368   error = Deserialize(&result, &buffer);
   1369   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1370   EXPECT_EQ(127U, result);
   1371 
   1372   // Min UINT8.
   1373   buffer = {ENCODING_TYPE_UINT8, 0x00};
   1374   error = Deserialize(&result, &buffer);
   1375   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1376   EXPECT_EQ(0U, result);
   1377 
   1378   // Max UINT8.
   1379   buffer = {ENCODING_TYPE_UINT8, 0xff};
   1380   error = Deserialize(&result, &buffer);
   1381   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1382   EXPECT_EQ(0xffU, result);
   1383 
   1384   // Min UINT16.
   1385   buffer = {ENCODING_TYPE_UINT16, 0x00, 0x00};
   1386   error = Deserialize(&result, &buffer);
   1387   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1388   EXPECT_EQ(0U, result);
   1389 
   1390   // Max UINT16.
   1391   buffer = {ENCODING_TYPE_UINT16, 0xff, 0xff};
   1392   error = Deserialize(&result, &buffer);
   1393   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1394   EXPECT_EQ(0xffffU, result);
   1395 
   1396   // UINT32 out of range.
   1397   buffer = {ENCODING_TYPE_UINT32};
   1398   error = Deserialize(&result, &buffer);
   1399   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1400   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1401   EXPECT_EQ(ENCODING_TYPE_UINT32, error.encoding_type());
   1402 
   1403   // UINT64 out of range.
   1404   buffer = {ENCODING_TYPE_UINT64};
   1405   error = Deserialize(&result, &buffer);
   1406   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1407   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1408   EXPECT_EQ(ENCODING_TYPE_UINT64, error.encoding_type());
   1409 }
   1410 
   1411 TEST(DeserializationTest, uint32_t) {
   1412   Payload buffer;
   1413   std::uint32_t result = 0;
   1414   ErrorType error;
   1415 
   1416   // Min FIXINT.
   1417   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1418   error = Deserialize(&result, &buffer);
   1419   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1420   EXPECT_EQ(0U, result);
   1421 
   1422   // Max FIXINT.
   1423   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1424   error = Deserialize(&result, &buffer);
   1425   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1426   EXPECT_EQ(127U, result);
   1427 
   1428   // Min UINT8.
   1429   buffer = {ENCODING_TYPE_UINT8, 0x00};
   1430   error = Deserialize(&result, &buffer);
   1431   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1432   EXPECT_EQ(0U, result);
   1433 
   1434   // Max UINT8.
   1435   buffer = {ENCODING_TYPE_UINT8, 0xff};
   1436   error = Deserialize(&result, &buffer);
   1437   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1438   EXPECT_EQ(0xffU, result);
   1439 
   1440   // Min UINT16.
   1441   buffer = {ENCODING_TYPE_UINT16, 0x00, 0x00};
   1442   error = Deserialize(&result, &buffer);
   1443   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1444   EXPECT_EQ(0U, result);
   1445 
   1446   // Max UINT16.
   1447   buffer = {ENCODING_TYPE_UINT16, 0xff, 0xff};
   1448   error = Deserialize(&result, &buffer);
   1449   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1450   EXPECT_EQ(0xffffU, result);
   1451 
   1452   // Min UINT32.
   1453   buffer = {ENCODING_TYPE_UINT32, 0x00, 0x00, 0x00, 0x00};
   1454   error = Deserialize(&result, &buffer);
   1455   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1456   EXPECT_EQ(0U, result);
   1457 
   1458   // Max UINT32.
   1459   buffer = {ENCODING_TYPE_UINT32, 0xff, 0xff, 0xff, 0xff};
   1460   error = Deserialize(&result, &buffer);
   1461   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1462   EXPECT_EQ(0xffffffffU, result);
   1463 
   1464   // UINT64 out of range.
   1465   buffer = {ENCODING_TYPE_UINT64};
   1466   error = Deserialize(&result, &buffer);
   1467   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1468   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   1469   EXPECT_EQ(ENCODING_TYPE_UINT64, error.encoding_type());
   1470 }
   1471 
   1472 TEST(DeserializationTest, uint64_t) {
   1473   Payload buffer;
   1474   std::uint64_t result = 0;
   1475   ErrorType error;
   1476 
   1477   // Min FIXINT.
   1478   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1479   error = Deserialize(&result, &buffer);
   1480   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1481   EXPECT_EQ(0U, result);
   1482 
   1483   // Max FIXINT.
   1484   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1485   error = Deserialize(&result, &buffer);
   1486   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1487   EXPECT_EQ(127U, result);
   1488 
   1489   // Min UINT8.
   1490   buffer = {ENCODING_TYPE_UINT8, 0x00};
   1491   error = Deserialize(&result, &buffer);
   1492   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1493   EXPECT_EQ(0U, result);
   1494 
   1495   // Max UINT8.
   1496   buffer = {ENCODING_TYPE_UINT8, 0xff};
   1497   error = Deserialize(&result, &buffer);
   1498   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1499   EXPECT_EQ(0xffU, result);
   1500 
   1501   // Min UINT16.
   1502   buffer = {ENCODING_TYPE_UINT16, 0x00, 0x00};
   1503   error = Deserialize(&result, &buffer);
   1504   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1505   EXPECT_EQ(0U, result);
   1506 
   1507   // Max UINT16.
   1508   buffer = {ENCODING_TYPE_UINT16, 0xff, 0xff};
   1509   error = Deserialize(&result, &buffer);
   1510   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1511   EXPECT_EQ(0xffffU, result);
   1512 
   1513   // Min UINT32.
   1514   buffer = {ENCODING_TYPE_UINT32, 0x00, 0x00, 0x00, 0x00};
   1515   error = Deserialize(&result, &buffer);
   1516   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1517   EXPECT_EQ(0U, result);
   1518 
   1519   // Max UINT32.
   1520   buffer = {ENCODING_TYPE_UINT32, 0xff, 0xff, 0xff, 0xff};
   1521   error = Deserialize(&result, &buffer);
   1522   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1523   EXPECT_EQ(0xffffffffU, result);
   1524 
   1525   // Min UINT64.
   1526   buffer = {
   1527       ENCODING_TYPE_UINT64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
   1528   error = Deserialize(&result, &buffer);
   1529   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1530   EXPECT_EQ(0U, result);
   1531 
   1532   // Max UINT64.
   1533   buffer = {
   1534       ENCODING_TYPE_UINT64, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
   1535   error = Deserialize(&result, &buffer);
   1536   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1537   EXPECT_EQ(0xffffffffffffffffUL, result);
   1538 }
   1539 
   1540 TEST(DeserializationTest, int8_t) {
   1541   Payload buffer;
   1542   std::int8_t result = 0;
   1543   ErrorType error;
   1544 
   1545   // Min NEGATIVE FIXINT.
   1546   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
   1547   error = Deserialize(&result, &buffer);
   1548   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1549   EXPECT_EQ(-32, result);
   1550 
   1551   // Max NEGATIVE FIXINT.
   1552   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
   1553   error = Deserialize(&result, &buffer);
   1554   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1555   EXPECT_EQ(-1, result);
   1556 
   1557   // Min FIXINT.
   1558   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1559   error = Deserialize(&result, &buffer);
   1560   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1561   EXPECT_EQ(0, result);
   1562 
   1563   // Max FIXINT.
   1564   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1565   error = Deserialize(&result, &buffer);
   1566   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1567   EXPECT_EQ(127, result);
   1568 
   1569   // Min INT8.
   1570   buffer = {ENCODING_TYPE_INT8, 0x80};
   1571   error = Deserialize(&result, &buffer);
   1572   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1573   EXPECT_EQ(-128, result);
   1574 
   1575   // Max INT8.
   1576   buffer = {ENCODING_TYPE_INT8, 0x7f};
   1577   error = Deserialize(&result, &buffer);
   1578   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1579   EXPECT_EQ(127, result);
   1580 
   1581   // INT16 out of range.
   1582   buffer = {ENCODING_TYPE_INT16};
   1583   error = Deserialize(&result, &buffer);
   1584   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1585   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1586   EXPECT_EQ(ENCODING_TYPE_INT16, error.encoding_type());
   1587 
   1588   // INT32 out of range.
   1589   buffer = {ENCODING_TYPE_INT32};
   1590   error = Deserialize(&result, &buffer);
   1591   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1592   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1593   EXPECT_EQ(ENCODING_TYPE_INT32, error.encoding_type());
   1594 
   1595   // INT64 out of range.
   1596   buffer = {ENCODING_TYPE_INT64};
   1597   error = Deserialize(&result, &buffer);
   1598   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1599   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1600   EXPECT_EQ(ENCODING_TYPE_INT64, error.encoding_type());
   1601 }
   1602 
   1603 TEST(DeserializationTest, int16_t) {
   1604   Payload buffer;
   1605   std::int16_t result = 0;
   1606   ErrorType error;
   1607 
   1608   // Min NEGATIVE FIXINT.
   1609   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
   1610   error = Deserialize(&result, &buffer);
   1611   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1612   EXPECT_EQ(-32, result);
   1613 
   1614   // Max NEGATIVE FIXINT.
   1615   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
   1616   error = Deserialize(&result, &buffer);
   1617   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1618   EXPECT_EQ(-1, result);
   1619 
   1620   // Min FIXINT.
   1621   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1622   error = Deserialize(&result, &buffer);
   1623   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1624   EXPECT_EQ(0, result);
   1625 
   1626   // Max FIXINT.
   1627   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1628   error = Deserialize(&result, &buffer);
   1629   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1630   EXPECT_EQ(127, result);
   1631 
   1632   // Min INT8.
   1633   buffer = {ENCODING_TYPE_INT8, 0x80};
   1634   error = Deserialize(&result, &buffer);
   1635   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1636   EXPECT_EQ(-128, result);
   1637 
   1638   // Max INT8.
   1639   buffer = {ENCODING_TYPE_INT8, 0x7f};
   1640   error = Deserialize(&result, &buffer);
   1641   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1642   EXPECT_EQ(127, result);
   1643 
   1644   // Min INT16.
   1645   buffer = {ENCODING_TYPE_INT16, 0x00, 0x80};
   1646   error = Deserialize(&result, &buffer);
   1647   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1648   EXPECT_EQ(-32768, result);
   1649 
   1650   // Max INT16.
   1651   buffer = {ENCODING_TYPE_INT16, 0xff, 0x7f};
   1652   error = Deserialize(&result, &buffer);
   1653   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1654   EXPECT_EQ(32767, result);
   1655 
   1656   // INT32 out of range.
   1657   buffer = {ENCODING_TYPE_INT32};
   1658   error = Deserialize(&result, &buffer);
   1659   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1660   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1661   EXPECT_EQ(ENCODING_TYPE_INT32, error.encoding_type());
   1662 
   1663   // INT64 out of range.
   1664   buffer = {ENCODING_TYPE_INT64};
   1665   error = Deserialize(&result, &buffer);
   1666   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1667   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1668   EXPECT_EQ(ENCODING_TYPE_INT64, error.encoding_type());
   1669 }
   1670 
   1671 TEST(DeserializationTest, int32_t) {
   1672   Payload buffer;
   1673   std::int32_t result = 0;
   1674   ErrorType error;
   1675 
   1676   // Min NEGATIVE FIXINT.
   1677   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
   1678   error = Deserialize(&result, &buffer);
   1679   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1680   EXPECT_EQ(-32, result);
   1681 
   1682   // Max NEGATIVE FIXINT.
   1683   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
   1684   error = Deserialize(&result, &buffer);
   1685   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1686   EXPECT_EQ(-1, result);
   1687 
   1688   // Min FIXINT.
   1689   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1690   error = Deserialize(&result, &buffer);
   1691   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1692   EXPECT_EQ(0, result);
   1693 
   1694   // Max FIXINT.
   1695   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1696   error = Deserialize(&result, &buffer);
   1697   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1698   EXPECT_EQ(127, result);
   1699 
   1700   // Min INT8.
   1701   buffer = {ENCODING_TYPE_INT8, 0x80};
   1702   error = Deserialize(&result, &buffer);
   1703   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1704   EXPECT_EQ(-128, result);
   1705 
   1706   // Max INT8.
   1707   buffer = {ENCODING_TYPE_INT8, 0x7f};
   1708   error = Deserialize(&result, &buffer);
   1709   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1710   EXPECT_EQ(127, result);
   1711 
   1712   // Min INT16.
   1713   buffer = {ENCODING_TYPE_INT16, 0x00, 0x80};
   1714   error = Deserialize(&result, &buffer);
   1715   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1716   EXPECT_EQ(-32768, result);
   1717 
   1718   // Max INT16.
   1719   buffer = {ENCODING_TYPE_INT16, 0xff, 0x7f};
   1720   error = Deserialize(&result, &buffer);
   1721   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1722   EXPECT_EQ(32767, result);
   1723 
   1724   // Min INT32.
   1725   buffer = {ENCODING_TYPE_INT32, 0x00, 0x00, 0x00, 0x80};
   1726   error = Deserialize(&result, &buffer);
   1727   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1728   EXPECT_EQ(-2147483648, result);
   1729 
   1730   // Max INT32.
   1731   buffer = {ENCODING_TYPE_INT32, 0xff, 0xff, 0xff, 0x7f};
   1732   error = Deserialize(&result, &buffer);
   1733   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1734   EXPECT_EQ(2147483647, result);
   1735 
   1736   // INT64 out of range.
   1737   buffer = {ENCODING_TYPE_INT64};
   1738   error = Deserialize(&result, &buffer);
   1739   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   1740   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   1741   EXPECT_EQ(ENCODING_TYPE_INT64, error.encoding_type());
   1742 }
   1743 
   1744 TEST(DeserializationTest, int64_t) {
   1745   Payload buffer;
   1746   std::int64_t result = 0;
   1747   ErrorType error;
   1748 
   1749   // Min NEGATIVE FIXINT.
   1750   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MIN};
   1751   error = Deserialize(&result, &buffer);
   1752   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1753   EXPECT_EQ(-32, result);
   1754 
   1755   // Max NEGATIVE FIXINT.
   1756   buffer = {ENCODING_TYPE_NEGATIVE_FIXINT_MAX};
   1757   error = Deserialize(&result, &buffer);
   1758   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1759   EXPECT_EQ(-1, result);
   1760 
   1761   // Min FIXINT.
   1762   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN};
   1763   error = Deserialize(&result, &buffer);
   1764   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1765   EXPECT_EQ(0, result);
   1766 
   1767   // Max FIXINT.
   1768   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MAX};
   1769   error = Deserialize(&result, &buffer);
   1770   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1771   EXPECT_EQ(127, result);
   1772 
   1773   // Min INT8.
   1774   buffer = {ENCODING_TYPE_INT8, 0x80};
   1775   error = Deserialize(&result, &buffer);
   1776   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1777   EXPECT_EQ(-128, result);
   1778 
   1779   // Max INT8.
   1780   buffer = {ENCODING_TYPE_INT8, 0x7f};
   1781   error = Deserialize(&result, &buffer);
   1782   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1783   EXPECT_EQ(127, result);
   1784 
   1785   // Min INT16.
   1786   buffer = {ENCODING_TYPE_INT16, 0x00, 0x80};
   1787   error = Deserialize(&result, &buffer);
   1788   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1789   EXPECT_EQ(-32768, result);
   1790 
   1791   // Max INT16.
   1792   buffer = {ENCODING_TYPE_INT16, 0xff, 0x7f};
   1793   error = Deserialize(&result, &buffer);
   1794   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1795   EXPECT_EQ(32767, result);
   1796 
   1797   // Min INT32.
   1798   buffer = {ENCODING_TYPE_INT32, 0x00, 0x00, 0x00, 0x80};
   1799   error = Deserialize(&result, &buffer);
   1800   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1801   EXPECT_EQ(-2147483648, result);
   1802 
   1803   // Max INT32.
   1804   buffer = {ENCODING_TYPE_INT32, 0xff, 0xff, 0xff, 0x7f};
   1805   error = Deserialize(&result, &buffer);
   1806   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1807   EXPECT_EQ(2147483647, result);
   1808 
   1809   // Min INT64.
   1810   buffer = {
   1811       ENCODING_TYPE_INT64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80};
   1812   error = Deserialize(&result, &buffer);
   1813   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1814   // Believe it or not, this is actually the correct way to specify the most
   1815   // negative signed long long.
   1816   EXPECT_EQ(-9223372036854775807LL - 1, result);
   1817 
   1818   // Max INT64.
   1819   buffer = {
   1820       ENCODING_TYPE_INT64, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f};
   1821   error = Deserialize(&result, &buffer);
   1822   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1823   EXPECT_EQ(9223372036854775807LL, result);
   1824 }
   1825 
   1826 TEST(DeserializationTest, float) {
   1827   Payload buffer;
   1828   float result;
   1829   ErrorType error;
   1830 
   1831   // FLOAT32.
   1832   buffer = {ENCODING_TYPE_FLOAT32, kZeroFloatBytes[0], kZeroFloatBytes[1],
   1833             kZeroFloatBytes[2], kZeroFloatBytes[3]};
   1834   error = Deserialize(&result, &buffer);
   1835   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1836   EXPECT_EQ(kZeroFloat, result);
   1837 
   1838   // FLOAT32.
   1839   buffer = {ENCODING_TYPE_FLOAT32, kOneFloatBytes[0], kOneFloatBytes[1],
   1840             kOneFloatBytes[2], kOneFloatBytes[3]};
   1841   error = Deserialize(&result, &buffer);
   1842   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1843   EXPECT_EQ(kOneFloat, result);
   1844 }
   1845 
   1846 TEST(DeserializationTest, double) {
   1847   Payload buffer;
   1848   double result;
   1849   ErrorType error;
   1850 
   1851   // FLOAT32.
   1852   buffer = {ENCODING_TYPE_FLOAT32, kZeroFloatBytes[0], kZeroFloatBytes[1],
   1853             kZeroFloatBytes[2], kZeroFloatBytes[3]};
   1854   error = Deserialize(&result, &buffer);
   1855   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1856   EXPECT_EQ(kZeroDouble, result);
   1857 
   1858   // FLOAT64.
   1859   buffer = {ENCODING_TYPE_FLOAT64, kZeroDoubleBytes[0], kZeroDoubleBytes[1],
   1860             kZeroDoubleBytes[2],   kZeroDoubleBytes[3], kZeroDoubleBytes[4],
   1861             kZeroDoubleBytes[5],   kZeroDoubleBytes[6], kZeroDoubleBytes[7]};
   1862   error = Deserialize(&result, &buffer);
   1863   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1864   EXPECT_EQ(kZeroDouble, result);
   1865 
   1866   // FLOAT32.
   1867   buffer = {ENCODING_TYPE_FLOAT32, kOneFloatBytes[0], kOneFloatBytes[1],
   1868             kOneFloatBytes[2], kOneFloatBytes[3]};
   1869   error = Deserialize(&result, &buffer);
   1870   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1871   EXPECT_EQ(kOneDouble, result);
   1872 
   1873   // FLOAT64.
   1874   buffer = {ENCODING_TYPE_FLOAT64, kOneDoubleBytes[0], kOneDoubleBytes[1],
   1875             kOneDoubleBytes[2],    kOneDoubleBytes[3], kOneDoubleBytes[4],
   1876             kOneDoubleBytes[5],    kOneDoubleBytes[6], kOneDoubleBytes[7]};
   1877   error = Deserialize(&result, &buffer);
   1878   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1879   EXPECT_EQ(kOneDouble, result);
   1880 }
   1881 
   1882 TEST(DeserializationTest, Enum) {
   1883   Payload buffer;
   1884   enum Foo { kFoo, kBar, kBaz } result;
   1885   ErrorType error;
   1886 
   1887   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1};
   1888   error = Deserialize(&result, &buffer);
   1889   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1890   EXPECT_EQ(kBar, result);
   1891 }
   1892 
   1893 TEST(DeserializationTest, EnumClass) {
   1894   Payload buffer;
   1895   enum Foo { kFoo, kBar, kBaz } result;
   1896   ErrorType error;
   1897 
   1898   buffer = {ENCODING_TYPE_POSITIVE_FIXINT_MIN + 2};
   1899   error = Deserialize(&result, &buffer);
   1900   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1901   EXPECT_EQ(Foo::kBaz, result);
   1902 }
   1903 
   1904 TEST(DeserializationTest, LocalHandle) {
   1905   Payload buffer;
   1906   LocalHandle result1;
   1907   LocalHandle result2;
   1908   ErrorType error;
   1909 
   1910   buffer = {ENCODING_TYPE_FIXEXT2, ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0, 0};
   1911   error = Deserialize(&result1, &buffer);
   1912   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1913   EXPECT_EQ(0, result1.Get());
   1914   result1.Release();  // Don't close fd 0.
   1915 
   1916   std::tuple<LocalHandle&, LocalHandle&> t1(result1, result2);
   1917   buffer = decltype(buffer)(
   1918       {ENCODING_TYPE_FIXARRAY_MIN + 2, ENCODING_TYPE_FIXEXT2,
   1919        ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0, 0, ENCODING_TYPE_FIXEXT2,
   1920        ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 1, 0});
   1921   error = Deserialize(&t1, &buffer);
   1922   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1923   EXPECT_EQ(0, result1.Get());
   1924   EXPECT_EQ(1, result2.Get());
   1925   result1.Release();  // Don't close fd 0.
   1926   result2.Release();  // Don't close fd 1.
   1927 
   1928   buffer = {ENCODING_TYPE_FIXEXT2, ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0xfe,
   1929             0xff};
   1930   error = Deserialize(&result1, &buffer);
   1931   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1932   EXPECT_EQ(-2, result1.Get());
   1933 }
   1934 
   1935 TEST(DeserializationTest, string) {
   1936   Payload buffer;
   1937   std::string result = "";
   1938   ErrorType error;
   1939 
   1940   // Min FIXSTR.
   1941   buffer = {ENCODING_TYPE_FIXSTR_MIN};
   1942   error = Deserialize(&result, &buffer);
   1943   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1944   EXPECT_EQ("", result);
   1945 
   1946   // Max FIXSTR.
   1947   buffer = {ENCODING_TYPE_FIXSTR_MAX};
   1948   buffer.Append((1 << 5) - 1, 'x');
   1949   error = Deserialize(&result, &buffer);
   1950   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1951   EXPECT_EQ(std::string((1 << 5) - 1, 'x'), result);
   1952 
   1953   // Min STR8.
   1954   buffer = {ENCODING_TYPE_STR8, 0x00};
   1955   error = Deserialize(&result, &buffer);
   1956   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1957   EXPECT_EQ("", result);
   1958 
   1959   // Max STR8.
   1960   buffer = {ENCODING_TYPE_STR8, 0xff};
   1961   buffer.Append(0xff, 'x');
   1962   error = Deserialize(&result, &buffer);
   1963   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1964   EXPECT_EQ(std::string(0xff, 'x'), result);
   1965 
   1966   // Min STR16.
   1967   buffer = {ENCODING_TYPE_STR16, 0x00, 0x00};
   1968   error = Deserialize(&result, &buffer);
   1969   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1970   EXPECT_EQ("", result);
   1971 
   1972   // Max STR16.
   1973   buffer = {ENCODING_TYPE_STR16, 0xff, 0xff};
   1974   buffer.Append(0xffff, 'x');
   1975   error = Deserialize(&result, &buffer);
   1976   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1977   EXPECT_EQ(std::string(0xffff, 'x'), result);
   1978 
   1979   // Min STR32.
   1980   buffer = {ENCODING_TYPE_STR32, 0x00, 0x00, 0x00, 0x00};
   1981   error = Deserialize(&result, &buffer);
   1982   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1983   EXPECT_EQ("", result);
   1984 
   1985   // Test STR32 with max STR16 + 1 bytes. It's not practical to test max
   1986   // STR32.
   1987   buffer = {ENCODING_TYPE_STR32, 0x00, 0x00, 0x01, 0x00};
   1988   buffer.Append(0x10000, 'x');
   1989   error = Deserialize(&result, &buffer);
   1990   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   1991   EXPECT_EQ(std::string(0x10000, 'x'), result);
   1992 }
   1993 
   1994 TEST(DeserializationTest, vector) {
   1995   Payload buffer;
   1996   std::vector<std::uint8_t, DefaultInitializationAllocator<std::uint8_t>>
   1997       result;
   1998   Payload expected;
   1999   ErrorType error;
   2000 
   2001   // Min FIXARRAY.
   2002   buffer = {ENCODING_TYPE_FIXARRAY_MIN};
   2003   error = Deserialize(&result, &buffer);
   2004   expected = {};
   2005   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2006   EXPECT_EQ(expected, result);
   2007 
   2008   // Max FIXARRAY.
   2009   buffer = {ENCODING_TYPE_FIXARRAY_MAX};
   2010   buffer.Append((1 << 4) - 1, 1);
   2011   error = Deserialize(&result, &buffer);
   2012   expected = decltype(expected)((1 << 4) - 1, 1);
   2013   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2014   EXPECT_EQ(expected, result);
   2015 
   2016   // Min ARRAY16.
   2017   buffer = {ENCODING_TYPE_ARRAY16, 0x00, 0x00};
   2018   error = Deserialize(&result, &buffer);
   2019   expected = {};
   2020   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2021   EXPECT_EQ(expected, result);
   2022 
   2023   // Max ARRAY16.
   2024   buffer = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   2025   buffer.Append(0xffff, 1);
   2026   error = Deserialize(&result, &buffer);
   2027   expected = decltype(expected)(0xffff, 1);
   2028   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2029   EXPECT_EQ(expected, result);
   2030 
   2031   // Min ARRAY32.
   2032   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x00, 0x00};
   2033   error = Deserialize(&result, &buffer);
   2034   expected = {};
   2035   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2036   EXPECT_EQ(expected, result);
   2037 
   2038   // ARRAY32 with max ARRAY16 + 1. It's not practical to test max ARRAY32.
   2039   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   2040   buffer.Append(0x10000, 1);
   2041   error = Deserialize(&result, &buffer);
   2042   expected = decltype(expected)(0x10000, 1);
   2043   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2044   EXPECT_EQ(expected, result);
   2045 }
   2046 
   2047 TEST(DeserializationTest, map) {
   2048   Payload buffer;
   2049   std::map<std::uint32_t, std::uint32_t> result;
   2050   std::map<std::uint32_t, std::uint32_t> expected;
   2051   ErrorType error;
   2052 
   2053   // Min FIXMAP.
   2054   buffer = {ENCODING_TYPE_FIXMAP_MIN};
   2055   error = Deserialize(&result, &buffer);
   2056   expected = {};
   2057   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2058   EXPECT_EQ(expected, result);
   2059 
   2060   // Size mismatch.
   2061   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1};
   2062   error = Deserialize(&result, &buffer);
   2063   EXPECT_EQ(ErrorCode::INSUFFICIENT_BUFFER, error);
   2064 
   2065   // Max FIXMAP.
   2066   buffer = {ENCODING_TYPE_FIXMAP_MAX};
   2067   InsertKeyValue<decltype(result)>(&buffer, (1 << 4) - 1);
   2068   error = Deserialize(&result, &buffer);
   2069   expected = MakeMap<decltype(expected)>((1 << 4) - 1);
   2070   EXPECT_EQ(ErrorCode::NO_ERROR, error) << std::string(error);
   2071   EXPECT_EQ(expected, result);
   2072 
   2073   // Min MAP16.
   2074   buffer = {ENCODING_TYPE_MAP16, 0x00, 0x00};
   2075   error = Deserialize(&result, &buffer);
   2076   expected = {};
   2077   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2078   EXPECT_EQ(expected, result);
   2079 
   2080   // Max MAP16.
   2081   buffer = {ENCODING_TYPE_MAP16, 0xff, 0xff};
   2082   InsertKeyValue<decltype(result)>(&buffer, (1 << 16) - 1);
   2083   error = Deserialize(&result, &buffer);
   2084   expected = MakeMap<decltype(expected)>((1 << 16) - 1);
   2085   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2086   EXPECT_EQ(expected, result);
   2087 
   2088   // Min MAP32.
   2089   buffer = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x00, 0x00};
   2090   error = Deserialize(&result, &buffer);
   2091   expected = {};
   2092   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2093   EXPECT_EQ(expected, result);
   2094 
   2095   // MAP32 with max MAP16 + 1. It's not practical to test max MAP32.
   2096   buffer = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x01, 0x00};
   2097   InsertKeyValue<decltype(result)>(&buffer, (1 << 16));
   2098   error = Deserialize(&result, &buffer);
   2099   expected = MakeMap<decltype(expected)>((1 << 16));
   2100   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2101   EXPECT_EQ(expected, result);
   2102 }
   2103 
   2104 TEST(DeserializationTest, unordered_map) {
   2105   Payload buffer;
   2106   std::unordered_map<std::uint32_t, std::uint32_t> result;
   2107   std::unordered_map<std::uint32_t, std::uint32_t> expected;
   2108   ErrorType error;
   2109 
   2110   // Min FIXMAP.
   2111   buffer = {ENCODING_TYPE_FIXMAP_MIN};
   2112   error = Deserialize(&result, &buffer);
   2113   expected = {};
   2114   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2115   EXPECT_EQ(expected, result);
   2116 
   2117   // Size mismatch.
   2118   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1};
   2119   error = Deserialize(&result, &buffer);
   2120   EXPECT_EQ(ErrorCode::INSUFFICIENT_BUFFER, error);
   2121 
   2122   // Max FIXMAP.
   2123   buffer = {ENCODING_TYPE_FIXMAP_MAX};
   2124   InsertKeyValue<decltype(result)>(&buffer, (1 << 4) - 1);
   2125   error = Deserialize(&result, &buffer);
   2126   expected = MakeMap<decltype(expected)>((1 << 4) - 1);
   2127   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2128   EXPECT_EQ(expected, result);
   2129 
   2130   // Min MAP16.
   2131   buffer = {ENCODING_TYPE_MAP16, 0x00, 0x00};
   2132   error = Deserialize(&result, &buffer);
   2133   expected = {};
   2134   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2135   EXPECT_EQ(expected, result);
   2136 
   2137   // Max MAP16.
   2138   buffer = {ENCODING_TYPE_MAP16, 0xff, 0xff};
   2139   InsertKeyValue<decltype(result)>(&buffer, (1 << 16) - 1);
   2140   error = Deserialize(&result, &buffer);
   2141   expected = MakeMap<decltype(expected)>((1 << 16) - 1);
   2142   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2143   EXPECT_EQ(expected, result);
   2144 
   2145   // Min MAP32.
   2146   buffer = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x00, 0x00};
   2147   error = Deserialize(&result, &buffer);
   2148   expected = {};
   2149   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2150   EXPECT_EQ(expected, result);
   2151 
   2152   // MAP32 with max MAP16 + 1. It's not practical to test max MAP32.
   2153   buffer = {ENCODING_TYPE_MAP32, 0x00, 0x00, 0x01, 0x00};
   2154   InsertKeyValue<decltype(result)>(&buffer, (1 << 16));
   2155   error = Deserialize(&result, &buffer);
   2156   expected = MakeMap<decltype(expected)>((1 << 16));
   2157   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2158   EXPECT_EQ(expected, result);
   2159 }
   2160 
   2161 TEST(DeserializationTest, array) {
   2162   Payload buffer;
   2163   ErrorType error;
   2164 
   2165   // Min FIXARRAY.
   2166   buffer = {ENCODING_TYPE_FIXARRAY_MIN};
   2167   std::array<std::uint8_t, 0> a0;
   2168   error = Deserialize(&a0, &buffer);
   2169   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2170 
   2171   // Size mismatch.
   2172   buffer = {ENCODING_TYPE_FIXARRAY_MIN + 1};
   2173   error = Deserialize(&a0, &buffer);
   2174   EXPECT_EQ(ErrorCode::INSUFFICIENT_DESTINATION_SIZE, error);
   2175 
   2176   // Max FIXARRAY.
   2177   buffer = {ENCODING_TYPE_FIXARRAY_MAX};
   2178   buffer.Append((1 << 4) - 1, 'x');
   2179   std::array<std::uint8_t, (1 << 4) - 1> a1, expected1;
   2180   for (auto& element : expected1)
   2181     element = 'x';
   2182   error = Deserialize(&a1, &buffer);
   2183   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2184   EXPECT_EQ(expected1, a1);
   2185 
   2186   // Min ARRAY16.
   2187   buffer = {ENCODING_TYPE_ARRAY16, 0x00, 0x00};
   2188   error = Deserialize(&a0, &buffer);
   2189   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2190 
   2191   // Max ARRAY16.
   2192   buffer = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   2193   buffer.Append((1 << 16) - 1, 'x');
   2194   std::array<std::uint8_t, (1 << 16) - 1> a3, expected3;
   2195   for (auto& element : expected3)
   2196     element = 'x';
   2197   error = Deserialize(&a3, &buffer);
   2198   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2199   EXPECT_EQ(expected3, a3);
   2200 
   2201   // Min ARRAY32.
   2202   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x00, 0x00};
   2203   error = Deserialize(&a0, &buffer);
   2204   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2205 
   2206   // ARRAY32 with max ARRAY16 + 1. It's not practical to test max ARRAY32.
   2207   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   2208   buffer.Append((1 << 16), 'x');
   2209   std::array<std::uint8_t, (1 << 16)> a4, expected4;
   2210   for (auto& element : expected4)
   2211     element = 'x';
   2212   error = Deserialize(&a4, &buffer);
   2213   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2214   EXPECT_EQ(expected4, a4);
   2215 }
   2216 
   2217 TEST(DeserializationTest, ArrayWrapper) {
   2218   Payload buffer;
   2219   std::vector<std::uint8_t, DefaultInitializationAllocator<std::uint8_t>>
   2220       result;
   2221   std::vector<std::uint8_t, DefaultInitializationAllocator<std::uint8_t>>
   2222       expected;
   2223   ErrorType error;
   2224 
   2225   result.reserve(0x10000);
   2226   ArrayWrapper<std::uint8_t> wrapper(result.data(), result.capacity());
   2227 
   2228   // Min FIXARRAY.
   2229   buffer = {ENCODING_TYPE_FIXARRAY_MIN};
   2230   error = Deserialize(&wrapper, &buffer);
   2231   expected = {};
   2232   result.resize(wrapper.size());
   2233   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2234   EXPECT_EQ(expected, result);
   2235 
   2236   // Max FIXARRAY.
   2237   buffer = {ENCODING_TYPE_FIXARRAY_MAX};
   2238   buffer.Append((1 << 4) - 1, 1);
   2239   error = Deserialize(&wrapper, &buffer);
   2240   expected = decltype(expected)((1 << 4) - 1, 1);
   2241   result.resize(wrapper.size());
   2242   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2243   EXPECT_EQ(expected, result);
   2244 
   2245   // Min ARRAY16.
   2246   buffer = {ENCODING_TYPE_ARRAY16, 0x00, 0x00};
   2247   error = Deserialize(&wrapper, &buffer);
   2248   expected = {};
   2249   result.resize(wrapper.size());
   2250   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2251   EXPECT_EQ(expected, result);
   2252 
   2253   // Max ARRAY16.
   2254   buffer = {ENCODING_TYPE_ARRAY16, 0xff, 0xff};
   2255   buffer.Append(0xffff, 1);
   2256   error = Deserialize(&wrapper, &buffer);
   2257   expected = decltype(expected)(0xffff, 1);
   2258   result.resize(wrapper.size());
   2259   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2260   EXPECT_EQ(expected, result);
   2261 
   2262   // Min ARRAY32.
   2263   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x00, 0x00};
   2264   error = Deserialize(&wrapper, &buffer);
   2265   expected = {};
   2266   result.resize(wrapper.size());
   2267   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2268   EXPECT_EQ(expected, result);
   2269 
   2270   // ARRAY32 with max ARRAY16 + 1. It's not practical to test max ARRAY32.
   2271   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x01, 0x00};
   2272   buffer.Append(0x10000, 1);
   2273   error = Deserialize(&wrapper, &buffer);
   2274   expected = decltype(expected)(0x10000, 1);
   2275   result.resize(wrapper.size());
   2276   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2277   EXPECT_EQ(expected, result);
   2278 }
   2279 
   2280 TEST(DeserializationTest, pair) {
   2281   Payload buffer;
   2282   ErrorType error;
   2283 
   2284   std::pair<int, int> p1;
   2285   buffer = {ENCODING_TYPE_FIXARRAY_MIN + 2, 1, 2};
   2286   error = Deserialize(&p1, &buffer);
   2287   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2288   EXPECT_EQ(std::make_pair(1, 2), p1);
   2289 }
   2290 
   2291 TEST(DeserializationTest, tuple) {
   2292   Payload buffer;
   2293   ErrorType error;
   2294 
   2295   // Min FIXARRAY.
   2296   std::tuple<> t1;
   2297   buffer = {ENCODING_TYPE_FIXARRAY_MIN};
   2298   error = Deserialize(&t1, &buffer);
   2299   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2300   EXPECT_EQ(std::make_tuple(), t1);  // Superfluous.
   2301 
   2302   // Max FIXARRAY.
   2303   auto t2 = GetNTuple<15, int>(0);
   2304   buffer = {ENCODING_TYPE_FIXARRAY_MAX};
   2305   buffer.Append((1 << 4) - 1, 1);
   2306   error = Deserialize(&t2, &buffer);
   2307   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2308   EXPECT_EQ((GetNTuple<15, int>(1)), t2);
   2309 
   2310   // Min ARRAY16.
   2311   // Using t1 above.
   2312   buffer = {ENCODING_TYPE_ARRAY16, 0x00, 0x00};
   2313   error = Deserialize(&t1, &buffer);
   2314   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2315   EXPECT_EQ(std::make_tuple(), t1);
   2316 
   2317   // ARRAY16 at Max FIXARRAY + 1
   2318   auto t3 = GetNTuple<(1 << 4), int>(0);
   2319   buffer = {ENCODING_TYPE_ARRAY16, 0x10, 0x00};
   2320   buffer.Append((1 << 4), 1);
   2321   error = Deserialize(&t3, &buffer);
   2322   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2323   EXPECT_EQ((GetNTuple<(1 << 4), int>(1)), t3);
   2324 
   2325   // Min ARRAY32.
   2326   // Using t1 from above.
   2327   buffer = {ENCODING_TYPE_ARRAY32, 0x00, 0x00, 0x00, 0x00};
   2328   error = Deserialize(&t1, &buffer);
   2329   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2330   EXPECT_EQ(std::make_tuple(), t1);
   2331 
   2332   // ARRAY32 at Max FIXARRAY + 1
   2333   auto t4 = GetNTuple<(1 << 4), int>(0);
   2334   buffer = {ENCODING_TYPE_ARRAY32, 0x10, 0x00, 0x00, 0x00};
   2335   buffer.Append((1 << 4), 1);
   2336   error = Deserialize(&t4, &buffer);
   2337   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2338   EXPECT_EQ((GetNTuple<(1 << 4), int>(1)), t4);
   2339 
   2340   // Template instantiation depth is an issue for tuples with large numbers of
   2341   // elements. As these are not expected in practice, the limits of ARRAY16
   2342   // and ARRAY32 are not tested.
   2343 }
   2344 
   2345 TEST(DeserializationTest, Serializable) {
   2346   Payload buffer;
   2347   ErrorType error;
   2348 
   2349   buffer = decltype(buffer)(
   2350       {ENCODING_TYPE_FIXARRAY_MIN + 4, 10, ENCODING_TYPE_FLOAT32,
   2351        kZeroFloatBytes[0], kZeroFloatBytes[1], kZeroFloatBytes[2],
   2352        kZeroFloatBytes[3], ENCODING_TYPE_FIXSTR_MIN + 5, '1', '2', '3', '4',
   2353        '5', ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1});
   2354   TestType t1;
   2355   error = Deserialize(&t1, &buffer);
   2356   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2357   EXPECT_EQ(TestType(10, 0.f, "12345", TestType::Foo::kBar), t1);
   2358 
   2359   buffer =
   2360       decltype(buffer)({ENCODING_TYPE_FIXARRAY_MIN + 1, ENCODING_TYPE_FIXEXT2,
   2361                         ENCODING_EXT_TYPE_FILE_DESCRIPTOR, 0xff, 0xff});
   2362   TestTemplateType<LocalHandle> tt;
   2363   error = Deserialize(&tt, &buffer);
   2364   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2365   EXPECT_EQ(TestTemplateType<LocalHandle>(LocalHandle(-1)), tt);
   2366 }
   2367 
   2368 TEST(DeserializationTest, Variant) {
   2369   Payload buffer;
   2370   ErrorType error;
   2371 
   2372   Variant<int, bool, float> v;
   2373 
   2374   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1, ENCODING_TYPE_NEGATIVE_FIXINT_MAX,
   2375             ENCODING_TYPE_NIL};
   2376   error = Deserialize(&v, &buffer);
   2377   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2378   EXPECT_TRUE(v.empty());
   2379 
   2380   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1, ENCODING_TYPE_POSITIVE_FIXINT_MIN + 0,
   2381             ENCODING_TYPE_POSITIVE_FIXINT_MIN + 10};
   2382   error = Deserialize(&v, &buffer);
   2383   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2384   ASSERT_TRUE(v.is<int>());
   2385   EXPECT_EQ(10, std::get<int>(v));
   2386 
   2387   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1, ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1,
   2388             ENCODING_TYPE_TRUE};
   2389   error = Deserialize(&v, &buffer);
   2390   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2391   ASSERT_TRUE(v.is<bool>());
   2392   EXPECT_EQ(true, std::get<bool>(v));
   2393 
   2394   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1, ENCODING_TYPE_POSITIVE_FIXINT_MIN + 1,
   2395             ENCODING_TYPE_FALSE};
   2396   error = Deserialize(&v, &buffer);
   2397   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2398   ASSERT_TRUE(v.is<bool>());
   2399   EXPECT_EQ(false, std::get<bool>(v));
   2400 
   2401   buffer = {ENCODING_TYPE_FIXMAP_MIN + 1,
   2402             ENCODING_TYPE_POSITIVE_FIXINT_MIN + 2,
   2403             ENCODING_TYPE_FLOAT32,
   2404             kOneFloatBytes[0],
   2405             kOneFloatBytes[1],
   2406             kOneFloatBytes[2],
   2407             kOneFloatBytes[3]};
   2408   error = Deserialize(&v, &buffer);
   2409   EXPECT_EQ(ErrorCode::NO_ERROR, error);
   2410   ASSERT_TRUE(v.is<float>());
   2411   EXPECT_FLOAT_EQ(1.0, std::get<float>(v));
   2412 
   2413   // TODO(eieio): Add more deserialization tests for Variant.
   2414 }
   2415 
   2416 TEST(DeserializationTest, ErrorType) {
   2417   Payload buffer;
   2418   ErrorType error;
   2419 
   2420   std::uint8_t u8;
   2421   buffer = {ENCODING_TYPE_STR8};
   2422   error = Deserialize(&u8, &buffer);
   2423   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2424   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   2425   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2426 
   2427   std::uint16_t u16;
   2428   buffer = {ENCODING_TYPE_STR8};
   2429   error = Deserialize(&u16, &buffer);
   2430   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2431   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   2432   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2433 
   2434   std::uint32_t u32;
   2435   buffer = {ENCODING_TYPE_STR8};
   2436   error = Deserialize(&u32, &buffer);
   2437   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2438   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   2439   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2440 
   2441   std::uint64_t u64;
   2442   buffer = {ENCODING_TYPE_STR8};
   2443   error = Deserialize(&u64, &buffer);
   2444   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2445   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   2446   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2447 
   2448   std::int8_t i8;
   2449   buffer = {ENCODING_TYPE_STR8};
   2450   error = Deserialize(&i8, &buffer);
   2451   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2452   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   2453   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2454 
   2455   std::int16_t i16;
   2456   buffer = {ENCODING_TYPE_STR8};
   2457   error = Deserialize(&i16, &buffer);
   2458   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2459   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   2460   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2461 
   2462   std::int32_t i32;
   2463   buffer = {ENCODING_TYPE_STR8};
   2464   error = Deserialize(&i32, &buffer);
   2465   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2466   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   2467   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2468 
   2469   std::int64_t i64;
   2470   buffer = {ENCODING_TYPE_STR8};
   2471   error = Deserialize(&i64, &buffer);
   2472   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2473   EXPECT_EQ(ENCODING_CLASS_INT, error.encoding_class());
   2474   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2475 
   2476   std::string s;
   2477   buffer = {ENCODING_TYPE_POSITIVE_FIXINT};
   2478   error = Deserialize(&s, &buffer);
   2479   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2480   EXPECT_EQ(ENCODING_CLASS_STRING, error.encoding_class());
   2481   EXPECT_EQ(ENCODING_TYPE_POSITIVE_FIXINT, error.encoding_type());
   2482 
   2483   std::vector<std::uint8_t> v;
   2484   buffer = {ENCODING_TYPE_POSITIVE_FIXINT};
   2485   error = Deserialize(&v, &buffer);
   2486   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2487   EXPECT_EQ(ENCODING_CLASS_ARRAY, error.encoding_class());
   2488   EXPECT_EQ(ENCODING_TYPE_POSITIVE_FIXINT, error.encoding_type());
   2489 
   2490   buffer = {ENCODING_TYPE_FIXARRAY_MIN + 1, ENCODING_TYPE_STR8};
   2491   error = Deserialize(&v, &buffer);
   2492   EXPECT_EQ(ErrorCode::UNEXPECTED_ENCODING, error);
   2493   EXPECT_EQ(ENCODING_CLASS_UINT, error.encoding_class());
   2494   EXPECT_EQ(ENCODING_TYPE_STR8, error.encoding_type());
   2495 
   2496   buffer = {ENCODING_TYPE_FIXARRAY_MIN + 2, 0, 1};
   2497   std::tuple<int> t;
   2498   error = Deserialize(&t, &buffer);
   2499   EXPECT_EQ(ErrorCode::UNEXPECTED_TYPE_SIZE, error);
   2500 
   2501   buffer = {ENCODING_TYPE_FIXARRAY_MIN + 3, 0, 1, 2};
   2502   std::pair<int, int> p;
   2503   error = Deserialize(&p, &buffer);
   2504   EXPECT_EQ(ErrorCode::UNEXPECTED_TYPE_SIZE, error);
   2505 }
   2506