Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

196 linhas
5.5 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include <vector>
  5. #include "util/coding.h"
  6. #include "util/testharness.h"
  7. namespace leveldb {
  8. class Coding {};
  9. TEST(Coding, Fixed32) {
  10. std::string s;
  11. for (uint32_t v = 0; v < 100000; v++) {
  12. PutFixed32(&s, v);
  13. }
  14. const char* p = s.data();
  15. for (uint32_t v = 0; v < 100000; v++) {
  16. uint32_t actual = DecodeFixed32(p);
  17. ASSERT_EQ(v, actual);
  18. p += sizeof(uint32_t);
  19. }
  20. }
  21. TEST(Coding, Fixed64) {
  22. std::string s;
  23. for (int power = 0; power <= 63; power++) {
  24. uint64_t v = static_cast<uint64_t>(1) << power;
  25. PutFixed64(&s, v - 1);
  26. PutFixed64(&s, v + 0);
  27. PutFixed64(&s, v + 1);
  28. }
  29. const char* p = s.data();
  30. for (int power = 0; power <= 63; power++) {
  31. uint64_t v = static_cast<uint64_t>(1) << power;
  32. uint64_t actual;
  33. actual = DecodeFixed64(p);
  34. ASSERT_EQ(v - 1, actual);
  35. p += sizeof(uint64_t);
  36. actual = DecodeFixed64(p);
  37. ASSERT_EQ(v + 0, actual);
  38. p += sizeof(uint64_t);
  39. actual = DecodeFixed64(p);
  40. ASSERT_EQ(v + 1, actual);
  41. p += sizeof(uint64_t);
  42. }
  43. }
  44. // Test that encoding routines generate little-endian encodings
  45. TEST(Coding, EncodingOutput) {
  46. std::string dst;
  47. PutFixed32(&dst, 0x04030201);
  48. ASSERT_EQ(4, dst.size());
  49. ASSERT_EQ(0x01, static_cast<int>(dst[0]));
  50. ASSERT_EQ(0x02, static_cast<int>(dst[1]));
  51. ASSERT_EQ(0x03, static_cast<int>(dst[2]));
  52. ASSERT_EQ(0x04, static_cast<int>(dst[3]));
  53. dst.clear();
  54. PutFixed64(&dst, 0x0807060504030201ull);
  55. ASSERT_EQ(8, dst.size());
  56. ASSERT_EQ(0x01, static_cast<int>(dst[0]));
  57. ASSERT_EQ(0x02, static_cast<int>(dst[1]));
  58. ASSERT_EQ(0x03, static_cast<int>(dst[2]));
  59. ASSERT_EQ(0x04, static_cast<int>(dst[3]));
  60. ASSERT_EQ(0x05, static_cast<int>(dst[4]));
  61. ASSERT_EQ(0x06, static_cast<int>(dst[5]));
  62. ASSERT_EQ(0x07, static_cast<int>(dst[6]));
  63. ASSERT_EQ(0x08, static_cast<int>(dst[7]));
  64. }
  65. TEST(Coding, Varint32) {
  66. std::string s;
  67. for (uint32_t i = 0; i < (32 * 32); i++) {
  68. uint32_t v = (i / 32) << (i % 32);
  69. PutVarint32(&s, v);
  70. }
  71. const char* p = s.data();
  72. const char* limit = p + s.size();
  73. for (uint32_t i = 0; i < (32 * 32); i++) {
  74. uint32_t expected = (i / 32) << (i % 32);
  75. uint32_t actual;
  76. const char* start = p;
  77. p = GetVarint32Ptr(p, limit, &actual);
  78. ASSERT_TRUE(p != nullptr);
  79. ASSERT_EQ(expected, actual);
  80. ASSERT_EQ(VarintLength(actual), p - start);
  81. }
  82. ASSERT_EQ(p, s.data() + s.size());
  83. }
  84. TEST(Coding, Varint64) {
  85. // Construct the list of values to check
  86. std::vector<uint64_t> values;
  87. // Some special values
  88. values.push_back(0);
  89. values.push_back(100);
  90. values.push_back(~static_cast<uint64_t>(0));
  91. values.push_back(~static_cast<uint64_t>(0) - 1);
  92. for (uint32_t k = 0; k < 64; k++) {
  93. // Test values near powers of two
  94. const uint64_t power = 1ull << k;
  95. values.push_back(power);
  96. values.push_back(power - 1);
  97. values.push_back(power + 1);
  98. }
  99. std::string s;
  100. for (size_t i = 0; i < values.size(); i++) {
  101. PutVarint64(&s, values[i]);
  102. }
  103. const char* p = s.data();
  104. const char* limit = p + s.size();
  105. for (size_t i = 0; i < values.size(); i++) {
  106. ASSERT_TRUE(p < limit);
  107. uint64_t actual;
  108. const char* start = p;
  109. p = GetVarint64Ptr(p, limit, &actual);
  110. ASSERT_TRUE(p != nullptr);
  111. ASSERT_EQ(values[i], actual);
  112. ASSERT_EQ(VarintLength(actual), p - start);
  113. }
  114. ASSERT_EQ(p, limit);
  115. }
  116. TEST(Coding, Varint32Overflow) {
  117. uint32_t result;
  118. std::string input("\x81\x82\x83\x84\x85\x11");
  119. ASSERT_TRUE(GetVarint32Ptr(input.data(), input.data() + input.size(),
  120. &result) == nullptr);
  121. }
  122. TEST(Coding, Varint32Truncation) {
  123. uint32_t large_value = (1u << 31) + 100;
  124. std::string s;
  125. PutVarint32(&s, large_value);
  126. uint32_t result;
  127. for (size_t len = 0; len < s.size() - 1; len++) {
  128. ASSERT_TRUE(GetVarint32Ptr(s.data(), s.data() + len, &result) == nullptr);
  129. }
  130. ASSERT_TRUE(GetVarint32Ptr(s.data(), s.data() + s.size(), &result) !=
  131. nullptr);
  132. ASSERT_EQ(large_value, result);
  133. }
  134. TEST(Coding, Varint64Overflow) {
  135. uint64_t result;
  136. std::string input("\x81\x82\x83\x84\x85\x81\x82\x83\x84\x85\x11");
  137. ASSERT_TRUE(GetVarint64Ptr(input.data(), input.data() + input.size(),
  138. &result) == nullptr);
  139. }
  140. TEST(Coding, Varint64Truncation) {
  141. uint64_t large_value = (1ull << 63) + 100ull;
  142. std::string s;
  143. PutVarint64(&s, large_value);
  144. uint64_t result;
  145. for (size_t len = 0; len < s.size() - 1; len++) {
  146. ASSERT_TRUE(GetVarint64Ptr(s.data(), s.data() + len, &result) == nullptr);
  147. }
  148. ASSERT_TRUE(GetVarint64Ptr(s.data(), s.data() + s.size(), &result) !=
  149. nullptr);
  150. ASSERT_EQ(large_value, result);
  151. }
  152. TEST(Coding, Strings) {
  153. std::string s;
  154. PutLengthPrefixedSlice(&s, Slice(""));
  155. PutLengthPrefixedSlice(&s, Slice("foo"));
  156. PutLengthPrefixedSlice(&s, Slice("bar"));
  157. PutLengthPrefixedSlice(&s, Slice(std::string(200, 'x')));
  158. Slice input(s);
  159. Slice v;
  160. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  161. ASSERT_EQ("", v.ToString());
  162. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  163. ASSERT_EQ("foo", v.ToString());
  164. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  165. ASSERT_EQ("bar", v.ToString());
  166. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  167. ASSERT_EQ(std::string(200, 'x'), v.ToString());
  168. ASSERT_EQ("", input.ToString());
  169. }
  170. } // namespace leveldb
  171. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }