c – 是否可以从std :: abort恢复?
发布时间:2020-12-16 10:00:38 所属栏目:百科 来源:网络整理
导读:我们的C代码库使用 assert来检查是否满足前置条件/??后置条件. #include cstdlib#include cassertvoid Aborting_Function(int n);int main(){ Aborting_Function(1); //good Aborting_Function(0); //calls std::abort() //Is it possible to recover someho
我们的C代码库使用
assert来检查是否满足前置条件/??后置条件.
#include <cstdlib> #include <cassert> void Aborting_Function(int n); int main(){ Aborting_Function(1); //good Aborting_Function(0); //calls std::abort() //Is it possible to recover somehow? //And continue on... } void Aborting_Function(int n){ assert(n > 0); //impl... } 在单元测试中,我想验证函数是否正确遵循其合同 是否有可能从std :: abort恢复? 我意识到单元测试检查断言应该检查完全相同的东西似乎有些重复,但这会有所帮助,因为我们可以自动检查不应该工作的特定用例. 解决方法
简短的回答,“不”.
您可能需要考虑使用Google测试框架,而不是颠覆abort(). 这有DEATH_TEST(文档:https://github.com/google/googletest/blob/master/googletest/docs/V1_7_AdvancedGuide.md) 基本上它的作用是fork一个子进程并检查该语句是否导致它退出(如果它中止它将会这样做). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |