小组成员:10215300402-朱维清 & 10222140408 谷杰
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

173 lignes
4.6 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 "util/coding.h"
  5. #include "util/testharness.h"
  6. namespace leveldb {
  7. class Coding { };
  8. TEST(Coding, Fixed32) {
  9. std::string s;
  10. for (uint32_t v = 0; v < 100000; v++) {
  11. PutFixed32(&s, v);
  12. }
  13. const char* p = s.data();
  14. for (uint32_t v = 0; v < 100000; v++) {
  15. uint32_t actual = DecodeFixed32(p);
  16. ASSERT_EQ(v, actual);
  17. p += sizeof(uint32_t);
  18. }
  19. }
  20. TEST(Coding, Fixed64) {
  21. std::string s;
  22. for (int power = 0; power <= 63; power++) {
  23. uint64_t v = static_cast<uint64_t>(1) << power;
  24. PutFixed64(&s, v - 1);
  25. PutFixed64(&s, v + 0);
  26. PutFixed64(&s, v + 1);
  27. }
  28. const char* p = s.data();
  29. for (int power = 0; power <= 63; power++) {
  30. uint64_t v = static_cast<uint64_t>(1) << power;
  31. uint64_t actual;
  32. actual = DecodeFixed64(p);
  33. ASSERT_EQ(v-1, actual);
  34. p += sizeof(uint64_t);
  35. actual = DecodeFixed64(p);
  36. ASSERT_EQ(v+0, actual);
  37. p += sizeof(uint64_t);
  38. actual = DecodeFixed64(p);
  39. ASSERT_EQ(v+1, actual);
  40. p += sizeof(uint64_t);
  41. }
  42. }
  43. TEST(Coding, Varint32) {
  44. std::string s;
  45. for (uint32_t i = 0; i < (32 * 32); i++) {
  46. uint32_t v = (i / 32) << (i % 32);
  47. PutVarint32(&s, v);
  48. }
  49. const char* p = s.data();
  50. const char* limit = p + s.size();
  51. for (uint32_t i = 0; i < (32 * 32); i++) {
  52. uint32_t expected = (i / 32) << (i % 32);
  53. uint32_t actual;
  54. const char* start = p;
  55. p = GetVarint32Ptr(p, limit, &actual);
  56. ASSERT_TRUE(p != NULL);
  57. ASSERT_EQ(expected, actual);
  58. ASSERT_EQ(VarintLength(actual), p - start);
  59. }
  60. ASSERT_EQ(p, s.data() + s.size());
  61. }
  62. TEST(Coding, Varint64) {
  63. // Construct the list of values to check
  64. std::vector<uint64_t> values;
  65. // Some special values
  66. values.push_back(0);
  67. values.push_back(100);
  68. values.push_back(~static_cast<uint64_t>(0));
  69. values.push_back(~static_cast<uint64_t>(0) - 1);
  70. for (uint32_t k = 0; k < 64; k++) {
  71. // Test values near powers of two
  72. const uint64_t power = 1ull << k;
  73. values.push_back(power);
  74. values.push_back(power-1);
  75. values.push_back(power+1);
  76. };
  77. std::string s;
  78. for (int i = 0; i < values.size(); i++) {
  79. PutVarint64(&s, values[i]);
  80. }
  81. const char* p = s.data();
  82. const char* limit = p + s.size();
  83. for (int i = 0; i < values.size(); i++) {
  84. ASSERT_TRUE(p < limit);
  85. uint64_t actual;
  86. const char* start = p;
  87. p = GetVarint64Ptr(p, limit, &actual);
  88. ASSERT_TRUE(p != NULL);
  89. ASSERT_EQ(values[i], actual);
  90. ASSERT_EQ(VarintLength(actual), p - start);
  91. }
  92. ASSERT_EQ(p, limit);
  93. }
  94. TEST(Coding, Varint32Overflow) {
  95. uint32_t result;
  96. std::string input("\x81\x82\x83\x84\x85\x11");
  97. ASSERT_TRUE(GetVarint32Ptr(input.data(), input.data() + input.size(), &result)
  98. == NULL);
  99. }
  100. TEST(Coding, Varint32Truncation) {
  101. uint32_t large_value = (1u << 31) + 100;
  102. std::string s;
  103. PutVarint32(&s, large_value);
  104. uint32_t result;
  105. for (int len = 0; len < s.size() - 1; len++) {
  106. ASSERT_TRUE(GetVarint32Ptr(s.data(), s.data() + len, &result) == NULL);
  107. }
  108. ASSERT_TRUE(GetVarint32Ptr(s.data(), s.data() + s.size(), &result) != NULL);
  109. ASSERT_EQ(large_value, result);
  110. }
  111. TEST(Coding, Varint64Overflow) {
  112. uint64_t result;
  113. std::string input("\x81\x82\x83\x84\x85\x81\x82\x83\x84\x85\x11");
  114. ASSERT_TRUE(GetVarint64Ptr(input.data(), input.data() + input.size(), &result)
  115. == NULL);
  116. }
  117. TEST(Coding, Varint64Truncation) {
  118. uint64_t large_value = (1ull << 63) + 100ull;
  119. std::string s;
  120. PutVarint64(&s, large_value);
  121. uint64_t result;
  122. for (int len = 0; len < s.size() - 1; len++) {
  123. ASSERT_TRUE(GetVarint64Ptr(s.data(), s.data() + len, &result) == NULL);
  124. }
  125. ASSERT_TRUE(GetVarint64Ptr(s.data(), s.data() + s.size(), &result) != NULL);
  126. ASSERT_EQ(large_value, result);
  127. }
  128. TEST(Coding, Strings) {
  129. std::string s;
  130. PutLengthPrefixedSlice(&s, Slice(""));
  131. PutLengthPrefixedSlice(&s, Slice("foo"));
  132. PutLengthPrefixedSlice(&s, Slice("bar"));
  133. PutLengthPrefixedSlice(&s, Slice(std::string(200, 'x')));
  134. Slice input(s);
  135. Slice v;
  136. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  137. ASSERT_EQ("", v.ToString());
  138. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  139. ASSERT_EQ("foo", v.ToString());
  140. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  141. ASSERT_EQ("bar", v.ToString());
  142. ASSERT_TRUE(GetLengthPrefixedSlice(&input, &v));
  143. ASSERT_EQ(std::string(200, 'x'), v.ToString());
  144. ASSERT_EQ("", input.ToString());
  145. }
  146. }
  147. int main(int argc, char** argv) {
  148. return leveldb::test::RunAllTests();
  149. }