Repository: xbmc/xbmc
File: xbmc/utils/test/TestStringUtils.cpp (C++)

Code before:
  EXPECT_EQ(1, StringUtils::FindNumber("aabcaadeaa", "b"));
}

TEST(TestStringUtils, AlphaNumericCompare)
{
  // less than
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"abc123"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"124abc"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"123bbc"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"abc123", L"abc124"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"abc123", L"bbc123"), 0);

  // equals
  EXPECT_EQ(StringUtils::AlphaNumericCompare(L"123abc", L"123abc"), 0);

  // greater than (same as less than but reversed arguments)
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"abc123", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"124abc", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"123bbc", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"abc124", L"abc123"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"bbc123", L"abc123"), 0);

  // test that long numbers are treated correctly
  EXPECT_EQ(StringUtils::AlphaNumericCompare(L"12345678901234567890", L"12345678901234567890"), 0);
}

TEST(TestStringUtils, TimeStringToSeconds)
{
  EXPECT_EQ(77455, StringUtils::TimeStringToSeconds("21:30:55"));
  EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min"));
  EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min\t"));
  EXPECT_EQ(154*60, StringUtils::TimeStringToSeconds("   154 min"));
  EXPECT_EQ(1*60+1, StringUtils::TimeStringToSeconds("1:01"));
  EXPECT_EQ(4*60+3, StringUtils::TimeStringToSeconds("4:03"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("2:04:03"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("   2:4:3"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("  \t\t 02:04:03 \n "));
  EXPECT_EQ(1*3600+5*60+2, StringUtils::TimeStringToSeconds("01:05:02:04:03 \n "));
  EXPECT_EQ(0, StringUtils::TimeStringToSeconds("blah"));
  EXPECT_EQ(0, StringUtils::TimeStringToSeconds("ля-ля"));
}

TEST(TestStringUtils, RemoveCRLF)
{
  std::string refstr, varstr;

  refstr = "test\r\nstring\nblah blah";
  varstr = "test\r\nstring\nblah blah\n";
  StringUtils::RemoveCRLF(varstr);
  EXPECT_STREQ(refstr.c_str(), varstr.c_str());
}

Review comment (bug):
Something like `EXPECT_LT(StringUtils::AlphaNumericCompare(L"2", L"12"), 0);`
would be good as a test of the numeric comparison.
The 15 characters limit is weird. It works to avoid overflowing the int64_t used internally, but behavior after 15 digits doesn't make a lot of sense. It's not a practical case though so doesn't matter.

Code after:
  EXPECT_EQ(1, StringUtils::FindNumber("aabcaadeaa", "b"));
}

TEST(TestStringUtils, AlphaNumericCompare)
{
  // less than
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"abc123"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"124abc"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"123abc", L"123bbc"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"abc123", L"abc124"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"abc123", L"bbc123"), 0);
  EXPECT_LT(StringUtils::AlphaNumericCompare(L"2", L"12"), 0);

  // equals
  EXPECT_EQ(StringUtils::AlphaNumericCompare(L"123abc", L"123abc"), 0);

  // greater than (same as less than but reversed arguments)
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"abc123", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"124abc", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"123bbc", L"123abc"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"abc124", L"abc123"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"bbc123", L"abc123"), 0);
  EXPECT_GT(StringUtils::AlphaNumericCompare(L"12", L"2"), 0);

  // test that long numbers are treated correctly
  EXPECT_EQ(StringUtils::AlphaNumericCompare(L"12345678901234567890", L"12345678901234567890"), 0);
}

TEST(TestStringUtils, TimeStringToSeconds)
{
  EXPECT_EQ(77455, StringUtils::TimeStringToSeconds("21:30:55"));
  EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min"));
  EXPECT_EQ(7*60, StringUtils::TimeStringToSeconds("7 min\t"));
  EXPECT_EQ(154*60, StringUtils::TimeStringToSeconds("   154 min"));
  EXPECT_EQ(1*60+1, StringUtils::TimeStringToSeconds("1:01"));
  EXPECT_EQ(4*60+3, StringUtils::TimeStringToSeconds("4:03"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("2:04:03"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("   2:4:3"));
  EXPECT_EQ(2*3600+4*60+3, StringUtils::TimeStringToSeconds("  \t\t 02:04:03 \n "));
  EXPECT_EQ(1*3600+5*60+2, StringUtils::TimeStringToSeconds("01:05:02:04:03 \n "));
  EXPECT_EQ(0, StringUtils::TimeStringToSeconds("blah"));
  EXPECT_EQ(0, StringUtils::TimeStringToSeconds("ля-ля"));
}

TEST(TestStringUtils, RemoveCRLF)
{
  std::string refstr, varstr;

  refstr = "test\r\nstring\nblah blah";
  varstr = "test\r\nstring\nblah blah\n";
  StringUtils::RemoveCRLF(varstr);