软件开发_1505错误与测试.pdfVIP

  • 7
  • 0
  • 约1.68万字
  • 约 37页
  • 2017-05-27 发布于河南
  • 举报
软件开发_1505错误与测试

第五章 错误与测试 5.1程序错误 5.2 运行时错误 5.2.1 错误的处理方式 5.2.2 异常 5.2.3 错误的检测 5.3 逻辑错误 5.4 软件测试 5.4.1 基本概念 5.4.2 白盒测试技术 5.4.3 黑盒测试技术 5.5 单元测试 1 5.1程序错误 ※错误的类型 ⊙编译/连接时错误 • 语法错误 • 类型错误 • 函数重定义/不存在 ⊙运行时错误 • 由计算机检测出的错误 • 由库检测出的错误 • 由用户程序检测出的错误 ⊙逻辑错误 运行结果不正确 2 5.1程序错误 ※程序的正确性与健壮性 ⊙对于所有合法输入应输出正确的结果 ⊙对于所有非法输入应输出错误信息 ※程序错误的来源 ⊙设计不完备 没有考虑到所有可能的情况 ⊙意外的参数 ⊙意外的输入 ⊙意外的状态 ⊙逻辑错误 程序没有按照预期的方式运行 3 5.2 运行时错误 程序除了完成正常的计算任务外,还应包含错误处理代码,在运 行时检测和处理各类错误。在产品软件中,错误处理代码的规模往 往会超过正常流程的代码。 // 计算长方形面积 int area(int length, int width) { if (length =0 || width = 0) ERROR(); return = length * height; } 4 5.2.1 错误的处理方式 程序发现错误后,可以有四种处理方式: Abort, Retry, Ignore, Fail 5 ※ 终止程序运行(Abort ) 在程序遇到不能处理的意外情况时,应终止程序运行。 // 输出错误信息并终止程序运行 void error (const char* p, const char* p2 = ) { cerr p ′ ′ p2 ′\n′; exit(1); } int main(int argc, char* argv []) { if (argc != 3) error (wrong number of arguments); ifstream from(argv [1]); if (!from) error (can not open input file, argv [1]); ofstream to(argv [2]); if(!to) error (can not open output file, argv [2]); char ch; while (from.get(ch)) to.put(ch); return 0; } 6 assert宏 C标准库的assert.h定义了assert宏,用于检测错误,输出错误 信息并终止程序运行。 #include fstream #include cassert // assert using namespace std; int main(int argc, char* argv []) { assert(argc == 3); ifstream from(argv [1]); assert((can not open input file, from)); ofstre

文档评论(0)

1亿VIP精品文档

相关文档