小组成员:姚凯文(kevinyao0901),姜嘉琪
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.

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