From fcaab5d5e3c0746bf40d2dd1396af9081ee25a18 Mon Sep 17 00:00:00 2001 From: Ravindra Kumar Bundela Date: Sun, 4 Oct 2026 23:23:46 +0530 Subject: [PATCH] [core] Add comprehensive unit tests for TString This PR adds several standard tests for the foundational TString class to improve core test coverage. Methods covered include Contains, ReplaceAll, ToUpper, ToLower, Form, operator(), and Tokenize. --- core/base/test/TStringTest.cxx | 60 +++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/core/base/test/TStringTest.cxx b/core/base/test/TStringTest.cxx index 8ed2c22720206..9ae5849c38653 100644 --- a/core/base/test/TStringTest.cxx +++ b/core/base/test/TStringTest.cxx @@ -1,8 +1,8 @@ #include "gtest/gtest.h" - #include "ROOT/TestSupport.hxx" - #include "TString.h" +#include "TObjArray.h" +#include "TObjString.h" TEST(TString, Basics) { @@ -10,8 +10,60 @@ TEST(TString, Basics) ROOT_EXPECT_ERROR(s = new TString("Test", -5), "TString::TString", "Negative length!"); delete s; TString p("Test", 1); - EXPECT_STREQ("T", p); + EXPECT_STREQ("T", p.Data()); TString a = "test"; ROOT_EXPECT_ERROR(a.Append("s", -5), "TString::Replace", "Negative number of replacement characters!"); - EXPECT_STREQ("test", a); + EXPECT_STREQ("test", a.Data()); +} + +TEST(TString, Contains) +{ + TString s("HelloWorld"); + EXPECT_TRUE(s.Contains("World")); + EXPECT_TRUE(s.Contains("hello", TString::kIgnoreCase)); + EXPECT_FALSE(s.Contains("hello", TString::kExact)); +} + +TEST(TString, ReplaceAll) +{ + TString s("The quick brown fox"); + s.ReplaceAll("quick", "slow"); + EXPECT_STREQ("The slow brown fox", s.Data()); + + s.ReplaceAll("fox", "dog"); + EXPECT_STREQ("The slow brown dog", s.Data()); +} + +TEST(TString, CaseConversion) +{ + TString s("ROOT Framework"); + s.ToUpper(); + EXPECT_STREQ("ROOT FRAMEWORK", s.Data()); + s.ToLower(); + EXPECT_STREQ("root framework", s.Data()); +} + +TEST(TString, Formatting) +{ + TString s; + s.Form("Number %d and string %s", 42, "test"); + EXPECT_STREQ("Number 42 and string test", s.Data()); } + +TEST(TString, Substring) +{ + TString s("abcdef"); + TString sub = s(1, 3); + EXPECT_STREQ("bcd", sub.Data()); +} + +TEST(TString, Tokenize) +{ + TString s("apple,banana,orange"); + std::unique_ptr tokens(s.Tokenize(",")); + ASSERT_EQ(3, tokens->GetEntries()); + EXPECT_STREQ("apple", ((TObjString*)tokens->At(0))->GetString().Data()); + EXPECT_STREQ("banana", ((TObjString*)tokens->At(1))->GetString().Data()); + EXPECT_STREQ("orange", ((TObjString*)tokens->At(2))->GetString().Data()); +} +