zoukankan      html  css  js  c++  java
  • Google Test(Primer)(三)——断言

    Assertions


    断言


    Google Test assertions are macros that resemble function calls. You test a class or function by making assertions about its behavior. When an assertion fails, Google Test prints the assertion's source file and line number location, along with a failure message. You may also supply a custom failure message which will be appended to Google Test's message.

    Google Test断言通过类似于函数调用的宏来实现,通过断言行为来测试一个类或函数。当断言失败时,Google Test打印出断言源文件名和定位行号,并打印失败信息。可以提供自定义的失败信息,这些信息将添加到Google Test信息的后面。(如果你看过Google TestFAQ,你就会知道,这些自定义的输出内容是输出到stdout中的,而Google Test的断言信息是输出到stderr中的,可以通过重定向到文件中将stdoutstderr分离,具体查看FAQ


    The assertions come in pairs that test the same thing but have different effects on the current function.
    ASSERT_* versions generate fatal failures when they fail, and abort the current function. EXPECT_* versions generate nonfatal failures, which don't abort the current function. Usually EXPECT_* are preferred, as they allow more than one failures to be reported in a test. However, you should use ASSERT_* if it doesn't make sense to continue when the assertion in question fails.

    对同一个测试,断言是成对出现的,但是对当前函数有不同的影响。当测试失败时ASSERT_*版本产生一个致命失败,并终止当前函数EXPECT_*版本产生一个非致命失败,但并不终止当前函数。通常使用EXPECT_*更好一些,EXPECT_*允许在一次测试中报告更多的失败。但是,如果当断言查询失败时,继续运行变得无意义时,应该使用ASSERT_*


    Since a failed
    ASSERT_* returns from the current function immediately, possibly skipping clean-up code that comes after it, it may cause a space leak. Depending on the nature of the leak, it may or may not be worth fixing - so keep this in mind if you get a heap checker error in addition to assertion errors.

    由于ASSERT_*失败立即从当前函数返回,可能跳过了之后的清除代码,这可能导致资源泄漏。依赖泄漏的类型,因此记住如果在添加断言错误时获取一个堆检测错误,可能或可能不值得修复


    To provide a custom failure message, simply stream it into the macro using the
    << operator, or a sequence of such operators. An example:

    为提供自定义失败信息,通过使用<<操作符或一系列该操作符,简单地将自定义失败信息流向宏。例如:

    ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length";

     

    for (int i = 0; i < x.size(); ++i) {

      EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;

    }


    Anything that can be streamed to an
    ostream can be streamed to an assertion macro--in particular, C strings and string objects. If a wide string (wchar_t*, TCHAR* in UNICODE mode on Windows, or std::wstring) is streamed to an assertion, it will be translated to UTF-8 when printed.

    任何可以流到ostream中的流均可以流向断言宏中——尤其是C字符串和string对象。如果宽字符串(在Windows中的UNICODE模式下wchar_t*, TCHAR*或是std::wstring)流向一个断言,打印(输出)时被转换为UTF-8


    Basic Assertions

    基础断言

    These assertions do basic true/false condition testing.

    下面断言是做基本的true/false条件测试

    Fatal assertion

    致命断言

    Nonfatal assertion

    非致命断言

    Verifies

    举例说明

    ASSERT_TRUE(condition);

    EXPECT_TRUE(condition);

    condition is true

    ASSERT_FALSE(condition);

    EXPECT_FALSE(condition);

    condition is false


    Remember, when they fail,
    ASSERT_* yields a fatal failure and returns from the current function, while EXPECT_* yields a nonfatal failure, allowing the function to continue running. In either case, an assertion failure means its containing test fails.

    记住,当断言失败时,ASSERT_*产生一个致命失败,从当前函数返回,而EXPECT_*产生一个非致命失败,允许函数继续运行。在两个测试中,一个断言失败意味着包含的测试条件失败。

    Availability: Linux, Windows, Mac.
     

    Binary Comparison

    二进制比较

    This section describes assertions that compare two values.

    下面描述的断言为比较两个值

    Fatal assertion

    致命断言

    Nonfatal assertion

    非致命断言

    Verifies

    举例说明

    ASSERT_EQ(expected, actual);

    EXPECT_EQ(expected, actual);

    expected == actual

    ASSERT_NE(val1, val2);

    EXPECT_NE(val1, val2);

    val1 != val2

    ASSERT_LT(val1, val2);

    EXPECT_LT(val1, val2);

    val1 < val2

    ASSERT_LE(val1, val2);

    EXPECT_LE(val1, val2);

    val1 <= val2

    ASSERT_GT(val1, val2);

    EXPECT_GT(val1, val2);

    val1 > val2

    ASSERT_GE(val1, val2);

    EXPECT_GE(val1, val2);

    val1 >= val2


    In the event of a failure, Google Test prints both val1 and val2 . In
    ASSERT_EQ* and EXPECT_EQ* (and all other equality assertions we'll introduce later), you should put the expression you want to test in the position of actual, and put its expected value in expected, as Google Test's failure messages are optimized for this convention.

    在失败事件中,Google Test打印val1val2。在ASSERT_EQ*EXPECT_EQ*(与我们之后介绍的所有其他等同的断言)中,应该将测试表达式放置在actual位置,将期待的测试值放置在expected中,这是由于Google Test将优化这个约定的失败信息。


    Value arguments must be comparable by the assertion's comparison operator or you'll get a compiler error. Values must also support the
    << operator for streaming to an ostream. All built-in types support this.

    值参数必须通过断言比较操作符来比较,否则将获取编译器错误,值必须也支持流到ostream类的<<操作符,所有C++语言内建类型支持这个特性。


    These assertions can work with a user-defined type, but only if you define the corresponding comparison operator (e.g.
    ==, <, etc). If the corresponding operator is defined, prefer using the ASSERT_*() macros because they will print out not only the result of the comparison, but the two operands as well.

    仅当用户定义了比较操作符(如==,<等)时,上述表格中的断言可以在用户定义类型上运行,如相关操作符已经定义,最好使用ASSERT_*()宏,因为断言不仅打印比较结果,也打印两个操作符。


    Arguments are always evaluated exactly once. Therefore, it's OK for the arguments to have side effects. However, as with any ordinary C/C++ function, the arguments' evaluation order is undefined (i.e. the compiler is free to choose any order) and your code should not depend on any particular argument evaluation order.

    参数通常是一次精确赋值。因此可以确定参数是存在副作用的。但是由于任何普通的C/C++函数,参数赋值顺序是不确定的(如,编译器可以自由选择任何顺序的参数赋值),因此代码不应该依赖于特殊的参数赋值顺序。


    ASSERT_EQ()
    does pointer equality on pointers. If used on two C strings, it tests if they are in the same memory location, not if they have the same value. Therefore, if you want to compare C strings (e.g. const char*) by value, use ASSERT_STREQ() , which will be described later on. In particular, to assert that a C string is NULL, use ASSERT_STREQ(NULL, c_string) . However, to compare two string objects, you should use ASSERT_EQ.


    ASSERT_EQ()
    使得指针上是相同的。如果使用两个C字符串,ASSERT_EQ()测试它们是否为相同的内存地址,而不是它们拥有相同的值。因此,如果希望通过值比较两个C字符串(如 const char*),使用后面介绍的ASSERT_STREQ()。特别地,判断C字符串为NULL,使用ASSERT_STREQ(NULL,c_string)。但是,如果比较两个string对象,应该使用ASSERT_EQ


    Macros in this section work with both narrow and wide string objects (
    string and wstring).

    本节的宏支持窄字符和宽字符对象(stringwstring

    Availability: Linux, Windows, Mac.


    String Comparison

    字符串比较

    The assertions in this group compare two C strings. If you want to compare two string objects, use EXPECT_EQ, EXPECT_NE, and etc instead.

    本组的断言是两个C字符串比较。如果希望比较两个string对象,使用EXPECT_EQEXPECT_NE以及其他宏进行替代。

    Fatal assertion

    致命断言

    Nonfatal assertion

    非致命断言

    Verifies

    举例说明

    ASSERT_STREQ(expected_str, actual_str);

    EXPECT_STREQ(expected_str, actual_str);

    the two C strings have the same content 两个C字符串有相同的内容

    ASSERT_STRNE(str1, str2);

    EXPECT_STRNE(str1, str2);

    the two C strings have different content 两个字符串有不同的内容

    ASSERT_STRCASEEQ(expected_str, actual_str);

    EXPECT_STRCASEEQ(expected_str, actual_str);

    the two C strings have the same content, ignoring case

    ASSERT_STRCASENE(str1, str2);

    EXPECT_STRCASENE(str1, str2);

    the two C strings have different content, ignoring case


    Note that "CASE" in an assertion name means that case is ignored.

    注意:在断言名称中的CASE表示是大小写忽略的。


    *STREQ*
    and *STRNE* also accept wide C strings (wchar_t*). If a comparison of two wide strings fails, their values will be printed as UTF-8 narrow strings.

    *STREQ**STRNE*也接受两个宽字节C字符串(wchar_t*),如果比较两个宽字符串失败,使用UTF-8窄字符串打印他们的值。


    A
    NULL pointer and an empty string are considered different.

    NULL指针和空字符串是不同的。


    Availability
    : Linux, Windows, Mac.


    See also: For more string comparison tricks (substring, prefix, suffix, and regular expression matching, for example), see the [AdvancedGuide Advanced Google Test Guide].

    查看:更多的字符串比较技巧(例如,子串,前缀,后缀,正则表达式匹配),查看[AdvancedGuide Advanced Google Test Guide].


    转帖请注明来自ubunoon[http:www.cnblogs.com/ubunoon]

  • 相关阅读:
    二进制求和
    删除排序数组中的重复项--leetcode算法题
    vue render
    数字实现千分位分隔符
    用nodejs实现向文件的固定位置插入内容
    工作中用到的正则表达式
    组件toast(类似于element-ui的message组件)的实现
    用svg实现一个环形进度条
    批量删除当前文件夹下面的.svn文件夹
    windows下的包管理器scoop
  • 原文地址:https://www.cnblogs.com/ubunoon/p/GoogleTestPrimerAssertion.html
Copyright © 2011-2022 走看看