小组成员:10215300402-朱维清 & 10222140408 谷杰
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 line
5.1 KiB

  1. // Copyright (c) 2018 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 <limits>
  5. #include <string>
  6. #include "leveldb/slice.h"
  7. #include "util/logging.h"
  8. #include "util/testharness.h"
  9. namespace leveldb {
  10. class Logging {};
  11. TEST(Logging, NumberToString) {
  12. ASSERT_EQ("0", NumberToString(0));
  13. ASSERT_EQ("1", NumberToString(1));
  14. ASSERT_EQ("9", NumberToString(9));
  15. ASSERT_EQ("10", NumberToString(10));
  16. ASSERT_EQ("11", NumberToString(11));
  17. ASSERT_EQ("19", NumberToString(19));
  18. ASSERT_EQ("99", NumberToString(99));
  19. ASSERT_EQ("100", NumberToString(100));
  20. ASSERT_EQ("109", NumberToString(109));
  21. ASSERT_EQ("190", NumberToString(190));
  22. ASSERT_EQ("123", NumberToString(123));
  23. ASSERT_EQ("12345678", NumberToString(12345678));
  24. static_assert(std::numeric_limits<uint64_t>::max() == 18446744073709551615U,
  25. "Test consistency check");
  26. ASSERT_EQ("18446744073709551000", NumberToString(18446744073709551000U));
  27. ASSERT_EQ("18446744073709551600", NumberToString(18446744073709551600U));
  28. ASSERT_EQ("18446744073709551610", NumberToString(18446744073709551610U));
  29. ASSERT_EQ("18446744073709551614", NumberToString(18446744073709551614U));
  30. ASSERT_EQ("18446744073709551615", NumberToString(18446744073709551615U));
  31. }
  32. void ConsumeDecimalNumberRoundtripTest(uint64_t number,
  33. const std::string& padding = "") {
  34. std::string decimal_number = NumberToString(number);
  35. std::string input_string = decimal_number + padding;
  36. Slice input(input_string);
  37. Slice output = input;
  38. uint64_t result;
  39. ASSERT_TRUE(ConsumeDecimalNumber(&output, &result));
  40. ASSERT_EQ(number, result);
  41. ASSERT_EQ(decimal_number.size(), output.data() - input.data());
  42. ASSERT_EQ(padding.size(), output.size());
  43. }
  44. TEST(Logging, ConsumeDecimalNumberRoundtrip) {
  45. ConsumeDecimalNumberRoundtripTest(0);
  46. ConsumeDecimalNumberRoundtripTest(1);
  47. ConsumeDecimalNumberRoundtripTest(9);
  48. ConsumeDecimalNumberRoundtripTest(10);
  49. ConsumeDecimalNumberRoundtripTest(11);
  50. ConsumeDecimalNumberRoundtripTest(19);
  51. ConsumeDecimalNumberRoundtripTest(99);
  52. ConsumeDecimalNumberRoundtripTest(100);
  53. ConsumeDecimalNumberRoundtripTest(109);
  54. ConsumeDecimalNumberRoundtripTest(190);
  55. ConsumeDecimalNumberRoundtripTest(123);
  56. ASSERT_EQ("12345678", NumberToString(12345678));
  57. for (uint64_t i = 0; i < 100; ++i) {
  58. uint64_t large_number = std::numeric_limits<uint64_t>::max() - i;
  59. ConsumeDecimalNumberRoundtripTest(large_number);
  60. }
  61. }
  62. TEST(Logging, ConsumeDecimalNumberRoundtripWithPadding) {
  63. ConsumeDecimalNumberRoundtripTest(0, " ");
  64. ConsumeDecimalNumberRoundtripTest(1, "abc");
  65. ConsumeDecimalNumberRoundtripTest(9, "x");
  66. ConsumeDecimalNumberRoundtripTest(10, "_");
  67. ConsumeDecimalNumberRoundtripTest(11, std::string("\0\0\0", 3));
  68. ConsumeDecimalNumberRoundtripTest(19, "abc");
  69. ConsumeDecimalNumberRoundtripTest(99, "padding");
  70. ConsumeDecimalNumberRoundtripTest(100, " ");
  71. for (uint64_t i = 0; i < 100; ++i) {
  72. uint64_t large_number = std::numeric_limits<uint64_t>::max() - i;
  73. ConsumeDecimalNumberRoundtripTest(large_number, "pad");
  74. }
  75. }
  76. void ConsumeDecimalNumberOverflowTest(const std::string& input_string) {
  77. Slice input(input_string);
  78. Slice output = input;
  79. uint64_t result;
  80. ASSERT_EQ(false, ConsumeDecimalNumber(&output, &result));
  81. }
  82. TEST(Logging, ConsumeDecimalNumberOverflow) {
  83. static_assert(std::numeric_limits<uint64_t>::max() == 18446744073709551615U,
  84. "Test consistency check");
  85. ConsumeDecimalNumberOverflowTest("18446744073709551616");
  86. ConsumeDecimalNumberOverflowTest("18446744073709551617");
  87. ConsumeDecimalNumberOverflowTest("18446744073709551618");
  88. ConsumeDecimalNumberOverflowTest("18446744073709551619");
  89. ConsumeDecimalNumberOverflowTest("18446744073709551620");
  90. ConsumeDecimalNumberOverflowTest("18446744073709551621");
  91. ConsumeDecimalNumberOverflowTest("18446744073709551622");
  92. ConsumeDecimalNumberOverflowTest("18446744073709551623");
  93. ConsumeDecimalNumberOverflowTest("18446744073709551624");
  94. ConsumeDecimalNumberOverflowTest("18446744073709551625");
  95. ConsumeDecimalNumberOverflowTest("18446744073709551626");
  96. ConsumeDecimalNumberOverflowTest("18446744073709551700");
  97. ConsumeDecimalNumberOverflowTest("99999999999999999999");
  98. }
  99. void ConsumeDecimalNumberNoDigitsTest(const std::string& input_string) {
  100. Slice input(input_string);
  101. Slice output = input;
  102. uint64_t result;
  103. ASSERT_EQ(false, ConsumeDecimalNumber(&output, &result));
  104. ASSERT_EQ(input.data(), output.data());
  105. ASSERT_EQ(input.size(), output.size());
  106. }
  107. TEST(Logging, ConsumeDecimalNumberNoDigits) {
  108. ConsumeDecimalNumberNoDigitsTest("");
  109. ConsumeDecimalNumberNoDigitsTest(" ");
  110. ConsumeDecimalNumberNoDigitsTest("a");
  111. ConsumeDecimalNumberNoDigitsTest(" 123");
  112. ConsumeDecimalNumberNoDigitsTest("a123");
  113. ConsumeDecimalNumberNoDigitsTest(std::string("\000123", 4));
  114. ConsumeDecimalNumberNoDigitsTest(std::string("\177123", 4));
  115. ConsumeDecimalNumberNoDigitsTest(std::string("\377123", 4));
  116. }
  117. } // namespace leveldb
  118. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }