2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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).
9 #include "utils/Range.h"
11 #include <gtest/gtest.h>
14 using namespace pzstd;
16 // Range is directly copied from folly.
17 // Just some sanity tests to make sure everything seems to work.
19 TEST(Range, Constructors) {
21 EXPECT_TRUE(empty.empty());
22 EXPECT_EQ(0, empty.size());
24 std::string str = "hello";
26 Range<std::string::const_iterator> piece(str.begin(), str.end());
27 EXPECT_EQ(5, piece.size());
28 EXPECT_EQ('h', *piece.data());
29 EXPECT_EQ('o', *(piece.end() - 1));
33 StringPiece piece(str.data(), str.size());
34 EXPECT_EQ(5, piece.size());
35 EXPECT_EQ('h', *piece.data());
36 EXPECT_EQ('o', *(piece.end() - 1));
40 StringPiece piece(str);
41 EXPECT_EQ(5, piece.size());
42 EXPECT_EQ('h', *piece.data());
43 EXPECT_EQ('o', *(piece.end() - 1));
47 StringPiece piece(str.c_str());
48 EXPECT_EQ(5, piece.size());
49 EXPECT_EQ('h', *piece.data());
50 EXPECT_EQ('o', *(piece.end() - 1));
54 TEST(Range, Modifiers) {
55 StringPiece range("hello world");
56 ASSERT_EQ(11, range.size());
59 auto hello = range.subpiece(0, 5);
60 EXPECT_EQ(5, hello.size());
61 EXPECT_EQ('h', *hello.data());
62 EXPECT_EQ('o', *(hello.end() - 1));
67 EXPECT_EQ(5, hello.size());
68 EXPECT_EQ('h', *hello.data());
69 EXPECT_EQ('o', *(hello.end() - 1));
74 EXPECT_EQ(5, world.size());
75 EXPECT_EQ('w', *world.data());
76 EXPECT_EQ('d', *(world.end() - 1));
79 std::string expected = "hello world";
80 EXPECT_EQ(expected, std::string(range.begin(), range.end()));
81 EXPECT_EQ(expected, std::string(range.data(), range.size()));