Home | History | Annotate | Line # | Download | only in test
RangeTest.cpp revision 1.1.1.1
      1 /*
      2  * Copyright (c) Meta Platforms, Inc. and affiliates.
      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 
     11 #include <gtest/gtest.h>
     12 #include <string>
     13 
     14 using namespace pzstd;
     15 
     16 // Range is directly copied from folly.
     17 // Just some sanity tests to make sure everything seems to work.
     18 
     19 TEST(Range, Constructors) {
     20   StringPiece empty;
     21   EXPECT_TRUE(empty.empty());
     22   EXPECT_EQ(0, empty.size());
     23 
     24   std::string str = "hello";
     25   {
     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));
     30   }
     31 
     32   {
     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));
     37   }
     38 
     39   {
     40     StringPiece piece(str);
     41     EXPECT_EQ(5, piece.size());
     42     EXPECT_EQ('h', *piece.data());
     43     EXPECT_EQ('o', *(piece.end() - 1));
     44   }
     45 
     46   {
     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));
     51   }
     52 }
     53 
     54 TEST(Range, Modifiers) {
     55   StringPiece range("hello world");
     56   ASSERT_EQ(11, range.size());
     57 
     58   {
     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));
     63   }
     64   {
     65     auto hello = range;
     66     hello.subtract(6);
     67     EXPECT_EQ(5, hello.size());
     68     EXPECT_EQ('h', *hello.data());
     69     EXPECT_EQ('o', *(hello.end() - 1));
     70   }
     71   {
     72     auto world = range;
     73     world.advance(6);
     74     EXPECT_EQ(5, world.size());
     75     EXPECT_EQ('w', *world.data());
     76     EXPECT_EQ('d', *(world.end() - 1));
     77   }
     78 
     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()));
     82 }
     83