RangeTest.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2016-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. */
  9. #include "utils/Range.h"
  10. #include <gtest/gtest.h>
  11. #include <string>
  12. using namespace pzstd;
  13. // Range is directly copied from folly.
  14. // Just some sanity tests to make sure everything seems to work.
  15. TEST(Range, Constructors) {
  16. StringPiece empty;
  17. EXPECT_TRUE(empty.empty());
  18. EXPECT_EQ(0, empty.size());
  19. std::string str = "hello";
  20. {
  21. Range<std::string::const_iterator> piece(str.begin(), str.end());
  22. EXPECT_EQ(5, piece.size());
  23. EXPECT_EQ('h', *piece.data());
  24. EXPECT_EQ('o', *(piece.end() - 1));
  25. }
  26. {
  27. StringPiece piece(str.data(), str.size());
  28. EXPECT_EQ(5, piece.size());
  29. EXPECT_EQ('h', *piece.data());
  30. EXPECT_EQ('o', *(piece.end() - 1));
  31. }
  32. {
  33. StringPiece piece(str);
  34. EXPECT_EQ(5, piece.size());
  35. EXPECT_EQ('h', *piece.data());
  36. EXPECT_EQ('o', *(piece.end() - 1));
  37. }
  38. {
  39. StringPiece piece(str.c_str());
  40. EXPECT_EQ(5, piece.size());
  41. EXPECT_EQ('h', *piece.data());
  42. EXPECT_EQ('o', *(piece.end() - 1));
  43. }
  44. }
  45. TEST(Range, Modifiers) {
  46. StringPiece range("hello world");
  47. ASSERT_EQ(11, range.size());
  48. {
  49. auto hello = range.subpiece(0, 5);
  50. EXPECT_EQ(5, hello.size());
  51. EXPECT_EQ('h', *hello.data());
  52. EXPECT_EQ('o', *(hello.end() - 1));
  53. }
  54. {
  55. auto hello = range;
  56. hello.subtract(6);
  57. EXPECT_EQ(5, hello.size());
  58. EXPECT_EQ('h', *hello.data());
  59. EXPECT_EQ('o', *(hello.end() - 1));
  60. }
  61. {
  62. auto world = range;
  63. world.advance(6);
  64. EXPECT_EQ(5, world.size());
  65. EXPECT_EQ('w', *world.data());
  66. EXPECT_EQ('d', *(world.end() - 1));
  67. }
  68. std::string expected = "hello world";
  69. EXPECT_EQ(expected, std::string(range.begin(), range.end()));
  70. EXPECT_EQ(expected, std::string(range.data(), range.size()));
  71. }