| OLD | NEW |
| (Empty) | |
| 1 /* -*- c++ -*- */ |
| 2 /* |
| 3 * Copyright (c) 2013 The Native Client Authors. All rights reserved. |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "native_client/src/shared/serialization/serialization.h" |
| 9 |
| 10 #include <math.h> |
| 11 #include <stdio.h> |
| 12 #include <string.h> |
| 13 |
| 14 #include "native_client/src/shared/platform/nacl_check.h" |
| 15 |
| 16 int main(void) { |
| 17 nacl::SerializationBuffer buf; |
| 18 |
| 19 int8_t i8 = -128; |
| 20 uint8_t u8 = 127; |
| 21 int16_t i16 = -32768; |
| 22 uint16_t u16 = 65535; |
| 23 int32_t i32 = -2147483647 - 1; |
| 24 uint32_t u32 = 4294967295U; |
| 25 int64_t i64 = -2147483649LL; |
| 26 uint64_t u64 = 18446744073709551615ULL; |
| 27 |
| 28 // Check basic serialization/deserialization -- we get back what we |
| 29 // put in -- with various basic types and vectors. Test with some |
| 30 // extreme numerical values. |
| 31 CHECK(buf.Serialize<int16_t>(10)); |
| 32 CHECK(buf.Serialize(i8)); |
| 33 CHECK(buf.Serialize(u8)); |
| 34 CHECK(buf.Serialize(i16)); |
| 35 CHECK(buf.Serialize(u16)); |
| 36 CHECK(buf.Serialize(i32)); |
| 37 CHECK(buf.Serialize(u32)); |
| 38 CHECK(buf.Serialize(i64)); |
| 39 CHECK(buf.Serialize(u64)); |
| 40 |
| 41 int8_t ci8 = 0; |
| 42 uint8_t cu8 = 0; |
| 43 int16_t ci16 = 0; |
| 44 uint16_t cu16 = 0; |
| 45 int32_t ci32 = 0; |
| 46 uint32_t cu32 = 0; |
| 47 int64_t ci64 = 0; |
| 48 uint64_t cu64 = 0; |
| 49 |
| 50 CHECK(buf.Deserialize(&ci16)); |
| 51 CHECK(ci16 == 10); |
| 52 #define D(v) do { \ |
| 53 CHECK(buf.Deserialize(&c ## v)); \ |
| 54 CHECK(c ## v == v); \ |
| 55 } while (0) |
| 56 D(i8); |
| 57 D(u8); |
| 58 D(i16); |
| 59 D(u16); |
| 60 D(i32); |
| 61 D(u32); |
| 62 D(i64); |
| 63 D(u64); |
| 64 |
| 65 CHECK(!buf.Deserialize(&ci8)); |
| 66 buf.rewind(); |
| 67 |
| 68 std::vector<int32_t> v; |
| 69 v.push_back(i32); |
| 70 v.push_back(3); |
| 71 v.push_back(1); |
| 72 v.push_back(4); |
| 73 v.push_back(1); |
| 74 v.push_back(5); |
| 75 v.push_back(9); |
| 76 |
| 77 CHECK(buf.Serialize(v)); |
| 78 |
| 79 #if defined(NACL_HAS_IEEE_754) |
| 80 float e = M_E; |
| 81 double pi = M_PI; |
| 82 CHECK(buf.Serialize(e)); |
| 83 CHECK(buf.Serialize(pi)); |
| 84 |
| 85 long double e_exp_pi; |
| 86 e_exp_pi = powl(static_cast<long double>(e), static_cast<long double>(pi)); |
| 87 CHECK(buf.Serialize(e_exp_pi)); |
| 88 #endif |
| 89 |
| 90 CHECK(buf.Serialize("Hello world")); |
| 91 CHECK(buf.Serialize( |
| 92 "When in the Course of human events, it becomes necessary for" |
| 93 " one people to dissolve the political bands which have" |
| 94 " connected them with another, and to assume among the powers" |
| 95 " of the earth, the separate and equal station to which the" |
| 96 " Laws of Nature and of Nature's God entitle them, a decent" |
| 97 " respect to the opinions of mankind requires that they should" |
| 98 " declare the causes which impel them to the separation.")); |
| 99 |
| 100 std::string msg("Goodbye cruel world"); |
| 101 |
| 102 CHECK(buf.Serialize(msg)); |
| 103 |
| 104 std::vector<std::string> vs; |
| 105 |
| 106 vs.push_back("When in the Course of human events, it becomes necessary for"); |
| 107 vs.push_back(" one people to dissolve the political bands which have"); |
| 108 vs.push_back(" connected them with another, and to assume among the powers"); |
| 109 vs.push_back(" of the earth, the separate and equal station to which the"); |
| 110 vs.push_back(" Laws of Nature and of Nature's God entitle them, a decent"); |
| 111 vs.push_back(" respect to the opinions of mankind requires that they should"); |
| 112 vs.push_back(" declare the causes which impel them to the separation."); |
| 113 |
| 114 CHECK(buf.Serialize(vs)); |
| 115 |
| 116 buf.rewind(); |
| 117 |
| 118 CHECK(buf.Deserialize(&ci16)); |
| 119 CHECK(ci16 == 10); |
| 120 D(i8); |
| 121 D(u8); |
| 122 D(i16); |
| 123 D(u16); |
| 124 D(i32); |
| 125 D(u32); |
| 126 D(i64); |
| 127 D(u64); |
| 128 |
| 129 std::vector<int32_t> v2; |
| 130 CHECK(buf.Deserialize(&v2)); |
| 131 CHECK(v.size() == v2.size()); |
| 132 for (size_t ix = 0; ix < v.size(); ++ix) { |
| 133 CHECK(v[ix] == v2[ix]); |
| 134 } |
| 135 #if defined(NACL_HAS_IEEE_754) |
| 136 float f; |
| 137 CHECK(buf.Deserialize(&f)); |
| 138 CHECK(f == e); |
| 139 double d; |
| 140 CHECK(buf.Deserialize(&d)); |
| 141 CHECK(d == pi); |
| 142 long double ld; |
| 143 CHECK(buf.Deserialize(&ld)); |
| 144 CHECK(ld == e_exp_pi); |
| 145 #endif |
| 146 char buffer[64]; |
| 147 size_t nbytes = sizeof buffer; |
| 148 CHECK(buf.Deserialize(buffer, &nbytes)); |
| 149 CHECK(nbytes == strlen("Hello world") + 1); |
| 150 CHECK(!strcmp(buffer, "Hello world")); |
| 151 char *obuf; |
| 152 CHECK(buf.Deserialize(&obuf)); |
| 153 CHECK(!strcmp(obuf, |
| 154 "When in the Course of human events, it becomes necessary for" |
| 155 " one people to dissolve the political bands which have" |
| 156 " connected them with another, and to assume among the powers" |
| 157 " of the earth, the separate and equal station to which the" |
| 158 " Laws of Nature and of Nature's God entitle them, a decent" |
| 159 " respect to the opinions of mankind requires that they should" |
| 160 " declare the causes which impel them to the separation.")); |
| 161 |
| 162 delete[] obuf; |
| 163 |
| 164 std::string msg2; |
| 165 CHECK(buf.Deserialize(&msg2)); |
| 166 CHECK(msg2 == "Goodbye cruel world"); |
| 167 |
| 168 std::vector<std::string> vs2; |
| 169 |
| 170 CHECK(buf.Deserialize(&vs2)); |
| 171 |
| 172 CHECK(vs.size() == vs2.size()); |
| 173 for (size_t ix = 0; ix < vs.size(); ++ix) { |
| 174 CHECK(vs[ix] == vs2[ix]); |
| 175 } |
| 176 |
| 177 // Check the ability to construct a SerializationBuffer from |
| 178 // "received data". |
| 179 |
| 180 buf.reset(); |
| 181 CHECK(buf.Serialize(i8)); |
| 182 CHECK(buf.Serialize(u8)); |
| 183 CHECK(buf.Serialize(i16)); |
| 184 CHECK(buf.Serialize(u16)); |
| 185 CHECK(buf.Serialize(i32)); |
| 186 CHECK(buf.Serialize(u32)); |
| 187 CHECK(buf.Serialize(i64)); |
| 188 CHECK(buf.Serialize(u64)); |
| 189 CHECK(buf.Serialize(vs)); |
| 190 |
| 191 nacl::SerializationBuffer buf2(buf.data(), buf.num_bytes()); |
| 192 #define D2(v) do { \ |
| 193 CHECK(buf2.Deserialize(&c ## v)); \ |
| 194 CHECK(c ## v == v); \ |
| 195 } while (0) |
| 196 D2(i8); |
| 197 D2(u8); |
| 198 D2(i16); |
| 199 D2(u16); |
| 200 D2(i32); |
| 201 D2(u32); |
| 202 D2(i64); |
| 203 D2(u64); |
| 204 |
| 205 vs2.clear(); |
| 206 CHECK(buf2.Deserialize(&vs2)); |
| 207 |
| 208 CHECK(vs.size() == vs2.size()); |
| 209 for (size_t ix = 0; ix < vs.size(); ++ix) { |
| 210 CHECK(vs[ix] == vs2[ix]); |
| 211 } |
| 212 |
| 213 // Tests that use exposed implementation details. |
| 214 |
| 215 // Verify that the space needed to serialize vectors of a basic type |
| 216 // grows at the size of the basic type. |
| 217 |
| 218 buf.reset(); |
| 219 CHECK(buf.Serialize(v)); |
| 220 size_t v_size = buf.num_bytes(); |
| 221 buf.reset(); |
| 222 v.push_back(100); |
| 223 CHECK(buf.Serialize(v)); |
| 224 v.pop_back(); |
| 225 size_t v_prime_size = buf.num_bytes(); |
| 226 CHECK(v_size + sizeof(int32_t) == v_prime_size); |
| 227 |
| 228 // Check typetag is offset based |
| 229 CHECK(buf.data()[0] >= nacl::kVectorOffset); |
| 230 |
| 231 CHECK(nacl::kRecursiveVector < nacl::kVectorOffset); |
| 232 |
| 233 // Check typetag is fixed, using recursive serialization |
| 234 buf.reset(); |
| 235 std::vector<std::vector<int32_t> > vv; |
| 236 vv.push_back(v); |
| 237 vv.push_back(v); |
| 238 CHECK(buf.Serialize(vv)); |
| 239 CHECK(buf.data()[0] == nacl::kRecursiveVector); |
| 240 |
| 241 // Check that the encoding space usage grows as expected, with the |
| 242 // nested vector also tagged etc. TODO(bsy): omit this test? this |
| 243 // may be too much implementation detail. |
| 244 size_t vv_size = buf.num_bytes(); |
| 245 std::vector<int32_t> v_singleton; |
| 246 v_singleton.push_back(42); |
| 247 vv.push_back(v_singleton); |
| 248 buf.reset(); |
| 249 CHECK(buf.Serialize(vv)); |
| 250 size_t vv_plus_1_size = buf.num_bytes(); |
| 251 CHECK(vv_size + nacl::SerializationBuffer::kTagBytes + 2 * sizeof(int32_t) |
| 252 == vv_plus_1_size); |
| 253 |
| 254 return 0; |
| 255 } |
| OLD | NEW |