gpt4 book ai didi

c++ - UnitTest++ 问题 : Trying to use a Predicate that has state

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:37 25 4
gpt4 key购买 nike

相关代码,这个来自UnitTest++/TestRunner.h:

class TestRunner
{
public:
explicit TestRunner(TestReporter& reporter);
~TestRunner();

template <class Predicate>
int RunTestsIf(TestList const& list, char const* suiteName,
const Predicate& predicate, int maxTestTimeInMs) const
{
Test* curTest = list.GetHead();

while (curTest != 0)
{
if (IsTestInSuite(curTest,suiteName) && predicate(curTest))
{
RunTest(m_result, curTest, maxTestTimeInMs);
}

curTest = curTest->next;
}

return Finish();
}

private:
TestReporter* m_reporter;
TestResults* m_result;
Timer* m_timer;

int Finish() const;
bool IsTestInSuite(const Test* const curTest, char const* suiteName) const;
void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const;
};

这是我试图修改的谓词类,让它做我想做的事:

class ListFilterRemember {
char **list;
int n;
Test **testsAlreadyRun;
int index_tar;
int max_allocd;
public:
ListFilterRemember(char **list_, int count) {
int testCount = 0;
Test* curTest = Test::GetTestList().GetHead();
while (curTest != 0) {
testCount++;
}
list = list_; n = count; max_allocd = testCount;
testsAlreadyRun = new Test *[max_allocd];
index_tar = 0;
}
bool operator()(const Test* const t) const {
for (int i=0;i<index_tar;++i) {
if (testsAlreadyRun[i] == t) { return false; }
}
for (int i=0;i<n;++i) {
std::string dot_cpp_appended = std::string(list[i]) + ".cpp";
if (!strcasecmp(t->m_details.testName, list[i]) ||
!strcasecmp(t->m_details.suiteName, list[i]) ||
!strcasecmp(t->m_details.filename, list[i]) ||
!strcasecmp(t->m_details.filename, dot_cpp_appended.c_str()) || (
filename_dir_prefix_len < (int)strlen(t->m_details.filename) && ( // ensure the ptr arith in next 2 lines doesn't go out of bounds
!strcasecmp(t->m_details.filename+filename_dir_prefix_len, list[i]) ||
!strcasecmp(t->m_details.filename+filename_dir_prefix_len, dot_cpp_appended.c_str())
)
) || (
std::string::npos != findCaseInsensitive(t->m_details.testName,list[i])
)
) {
// erring on the side of matching more tests
//printf(" running\n");
if (index_tar >= max_allocd) throw std::runtime_error("Did not allocate! Segfault here.");
testsAlreadyRun[index_tar] = (Test *)t;
index_tar += 1;
return true;
}
}
//printf(" not running\n");
return false;
}
~ListFilterRemember() {
delete[] testsAlreadyRun;
}
};

您会看到它在 TestRunner.h 中的定义方式附加了一个 const 限定符,这使得我的 operator () 函数无法更改成员变量。我需要进行这些更改,这样我才能记住我已经运行过哪些测试,这样我就不会再次运行它们。之所以会有再次运行的风险,是因为我打算多次运行RunTestsIf()

我在命令行输入测试列表,以根据名称指定要运行的测试。这就是所有字符串匹配代码的用途。我仍然想使用它们,但这次我想改进它,以便我指定的测试将按照我指定的顺序运行。为了做到这一点,我必须移动测试运行器,以便循环我的指定测试列表并根据它们进行匹配。

想法?我会在 UnitTest++ 代码中对 const 进行核对,但如果不需要的话,我不想破坏它。

最佳答案

您可以使用 mutable 关键字,但测试的结构方式表明测试运行器可能不会重用谓词的相同实例,因此您的更改可能会在测试之间丢失。当然,如果发布的代码是整个测试运行器,那么它看起来确实每次都调用相同的 Predicate 实例。

关于c++ - UnitTest++ 问题 : Trying to use a Predicate that has state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7437575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com