Exemple #1
0
TEST(OwnedStringTest, copy_constructor_original_copy) {
  // Initialize a mutable string.
  const char *original = "string";
  const size_t length = strlen(original);
  char *data = static_cast<char *>(malloc(length + 1));
  memcpy(data, original, length);
  data[length] = '\0';

  // Create an OwnedString.
  OwnedString ownedString(data, length);

  EXPECT_EQ(length, ownedString.size());
  EXPECT_FALSE(ownedString.empty());

  // Copy the string
  OwnedString copy = OwnedString(ownedString.copy());
  EXPECT_EQ(length, copy.size());
  EXPECT_FALSE(copy.empty());

  // Make sure we correctly copied the data and that it is null
  // terminated.
  StringRef str = copy.str();
  EXPECT_EQ("string", str);
  EXPECT_EQ(length, strlen(str.data()));

  // Make sure updating the original pointer doesn't affect the copy.
  data[0] = 'a';

  EXPECT_EQ("string", str);
}
TEST(OwnedStringTest, ref_counted_copies_buffer) {
  char *data = static_cast<char *>(malloc(6));
  memcpy(data, "hello", 6);
  size_t length = strlen(data);

  OwnedString ownedString =
      OwnedString::makeRefCounted(StringRef(data, length));

  EXPECT_EQ(ownedString.str(), "hello");
  EXPECT_NE(ownedString.str().data(), data);

  memcpy(data, "world", 6);

  // Even if the original buffer changes, the string should stay the same
  EXPECT_EQ(ownedString.str(), "hello");
}
TEST(OwnedStringTest, char_pointer_non_empty) {
  const char *data = "string";
  const size_t length = strlen(data);
  OwnedString ownedString = OwnedString::makeUnowned(data);

  EXPECT_EQ(length, ownedString.size());
  EXPECT_FALSE(ownedString.empty());
  EXPECT_EQ(data, ownedString.str().data());
}
Exemple #4
0
TEST(OwnedStringTest, char_pointer_non_empty) {
  const char *data = "string";
  const size_t length = strlen(data);
  OwnedString ownedString(data);

  EXPECT_EQ(length, ownedString.size());
  EXPECT_FALSE(ownedString.empty());

  OwnedString copy = ownedString.copy();
  EXPECT_EQ(length, copy.size());
  EXPECT_FALSE(copy.empty());

  StringRef str = copy.str();
  EXPECT_EQ("string", str);
  EXPECT_EQ(length, strlen(str.data()));
}
Exemple #5
0
TEST(OwnedStringTest, char_pointer_length_nonzero) {
  const char *data = "string";
  const size_t length = 1;
  OwnedString ownedString(data, length);

  EXPECT_EQ(length, ownedString.size());
  EXPECT_FALSE(ownedString.empty());

  OwnedString copy = ownedString.copy();
  EXPECT_EQ(length, copy.size());
  EXPECT_FALSE(copy.empty());

  // Make sure we correctly copied the data and that it is null
  // terminated.
  StringRef str = copy.str();
  EXPECT_EQ("s", str);
  EXPECT_EQ(1UL, strlen(str.data()));
}
Exemple #6
0
void RawSyntax::Profile(llvm::FoldingSetNodeID &ID, tok TokKind,
                        OwnedString Text, ArrayRef<TriviaPiece> LeadingTrivia,
                        ArrayRef<TriviaPiece> TrailingTrivia) {
  ID.AddInteger(unsigned(TokKind));
  switch (TokKind) {
#define TOKEN_DEFAULT(NAME) case tok::NAME:
#define PUNCTUATOR(NAME, X) TOKEN_DEFAULT(NAME)
#define KEYWORD(KW) TOKEN_DEFAULT(kw_##KW)
#define POUND_KEYWORD(KW) TOKEN_DEFAULT(pound_##KW)
#include "swift/Syntax/TokenKinds.def"
    break;
  default:
    ID.AddString(Text.str());
    break;
  }
  for (auto &Piece : LeadingTrivia)
    Piece.Profile(ID);
  for (auto &Piece : TrailingTrivia)
    Piece.Profile(ID);
}
RC<TokenSyntax>
syntax::findTokenSyntax(tok ExpectedKind,
                        OwnedString ExpectedText,
                        SourceManager &SourceMgr,
                        SourceLoc Loc,
                        unsigned BufferID,
                        const TokenPositionList &Tokens) {
  auto Offset = SourceMgr.getLocOffsetInBuffer(Loc, BufferID);

  size_t Start = 0;
  size_t End = Tokens.size() - 1;

  while (Start <= End) {
    auto Mid = (Start + End) / 2;
    auto TokAndPos = Tokens[Mid];
    auto Tok = TokAndPos.first;
    auto Pos = TokAndPos.second;

    auto TokStart = Pos.getOffset();
    auto TokEnd = TokStart + Tok->getText().size();

    if (Offset == TokStart) {
      if (Tok->getTokenKind() == ExpectedKind &&
          (ExpectedText.empty() || Tok->getText() == ExpectedText.str())) {
        return Tok;
      } else {
        return TokenSyntax::missingToken(ExpectedKind, ExpectedText);
      }
    }

    if (TokStart < Offset) {
      Start = Mid + 1;
    } else {
      End = Mid - 1;
    }
  }

  return TokenSyntax::missingToken(ExpectedKind, ExpectedText);
}
TEST(OwnedStringTest, ref_counted_assignment) {
  OwnedString str = OwnedString::makeRefCounted("hello");
  OwnedString copy = str;

  EXPECT_EQ(str.str().data(), copy.str().data());
}