Commit e9a0cb87 by Yang Gu Committed by Commit Bot

Fix assertion when running tests without suffix on Windows

On Windows, we may omit suffix ".exe" when running an executable. However, this will trigger an assertion failure in current code and this CL is to fix this issue. Bug: angleproject:4640 Change-Id: I7edfdc0b4a7c590c874817530b19a812018b9288 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2206425 Commit-Queue: Jamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarShahbaz Youssefi <syoussefi@chromium.org>
parent 3e1a57b8
......@@ -18,6 +18,19 @@
#include "common/platform.h"
#include "common/system_utils.h"
namespace
{
bool EndsWithSuffix(const char *str,
const size_t strLen,
const char *suffix,
const size_t suffixLen)
{
return suffixLen <= strLen && strncmp(str + strLen - suffixLen, suffix, suffixLen) == 0;
}
} // anonymous namespace
namespace angle
{
......@@ -178,15 +191,19 @@ bool BeginsWith(const std::string &str, const std::string &prefix, const size_t
return strncmp(str.c_str(), prefix.c_str(), prefixLength) == 0;
}
bool EndsWith(const std::string &str, const char *suffix)
bool EndsWith(const std::string &str, const std::string &suffix)
{
const auto len = strlen(suffix);
if (len > str.size())
return false;
return EndsWithSuffix(str.c_str(), str.length(), suffix.c_str(), suffix.length());
}
const char *end = str.c_str() + str.size() - len;
bool EndsWith(const std::string &str, const char *suffix)
{
return EndsWithSuffix(str.c_str(), str.length(), suffix, strlen(suffix));
}
return memcmp(end, suffix, len) == 0;
bool EndsWith(const char *str, const char *suffix)
{
return EndsWithSuffix(str, strlen(str), suffix, strlen(suffix));
}
void ToLower(std::string *str)
......
......@@ -69,10 +69,19 @@ bool BeginsWith(const char *str, const char *prefix);
bool BeginsWith(const std::string &str, const std::string &prefix, const size_t prefixLength);
// Check if the string str ends with the given suffix.
// Suffix may not be NUL and needs to be NULL terminated.
// The comparison is case sensitive.
bool EndsWith(const std::string &str, const std::string &suffix);
// Check if the string str ends with the given suffix.
// Suffix may not be NULL and needs to be NULL terminated.
// The comparison is case sensitive.
bool EndsWith(const std::string &str, const char *suffix);
// Check if the string str ends with the given suffix.
// str and suffix may not be NULL and need to be NULL terminated.
// The comparison is case sensitive.
bool EndsWith(const char *str, const char *suffix);
// Convert to lower-case.
void ToLower(std::string *str);
......
......@@ -202,17 +202,68 @@ TEST_F(BeginsWithTest, Strings)
runTest();
}
// Test that EndsWith works correctly.
TEST(EndsWithTest, EndsWith)
class EndsWithTest : public testing::Test
{
ASSERT_FALSE(EndsWith("foo", "bar"));
ASSERT_FALSE(EndsWith("", "bar"));
ASSERT_FALSE(EndsWith("foo", "foobar"));
ASSERT_TRUE(EndsWith("foobar", "bar"));
ASSERT_TRUE(EndsWith("foobar", ""));
ASSERT_TRUE(EndsWith("bar", "bar"));
ASSERT_TRUE(EndsWith("", ""));
public:
EndsWithTest() : mMode(TestMode::CHAR_ARRAY) {}
enum class TestMode
{
CHAR_ARRAY,
STRING_AND_CHAR_ARRAY,
STRING
};
void setMode(TestMode mode) { mMode = mode; }
bool runEndsWith(const char *str, const char *suffix)
{
if (mMode == TestMode::CHAR_ARRAY)
{
return EndsWith(str, suffix);
}
if (mMode == TestMode::STRING_AND_CHAR_ARRAY)
{
return EndsWith(std::string(str), suffix);
}
return EndsWith(std::string(str), std::string(suffix));
}
void runTest()
{
ASSERT_FALSE(EndsWith("foo", "bar"));
ASSERT_FALSE(EndsWith("", "bar"));
ASSERT_FALSE(EndsWith("foo", "foobar"));
ASSERT_TRUE(EndsWith("foobar", "bar"));
ASSERT_TRUE(EndsWith("foobar", ""));
ASSERT_TRUE(EndsWith("bar", "bar"));
ASSERT_TRUE(EndsWith("", ""));
}
private:
TestMode mMode;
};
// Test that EndsWith works correctly for const char * arguments.
TEST_F(EndsWithTest, CharArrays)
{
setMode(TestMode::CHAR_ARRAY);
runTest();
}
// Test that EndsWith works correctly for std::string and const char * arguments.
TEST_F(EndsWithTest, StringAndCharArray)
{
setMode(TestMode::STRING_AND_CHAR_ARRAY);
runTest();
}
// Test that EndsWith works correctly for std::string arguments.
TEST_F(EndsWithTest, Strings)
{
setMode(TestMode::STRING);
runTest();
}
} // anonymous namespace
......@@ -10,6 +10,7 @@
#include "common/debug.h"
#include "common/platform.h"
#include "common/string_utils.h"
#include "common/system_utils.h"
#include "util/Timer.h"
......@@ -421,9 +422,12 @@ std::string ParseTestSuiteName(const char *executable)
return baseNameStart;
}
const char *baseNameSuffix = strstr(baseNameStart, suffix);
ASSERT(baseNameSuffix == (baseNameStart + strlen(baseNameStart) - suffixLen));
return std::string(baseNameStart, baseNameSuffix);
if (!EndsWith(baseNameStart, suffix))
{
return baseNameStart;
}
return std::string(baseNameStart, baseNameStart + strlen(baseNameStart) - suffixLen);
}
bool GetTestResultsFromJSON(const js::Document &document, TestResults *resultsOut)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment