天天看點

Unity(純C語言單元測試架構!不是那個Unity3d)斷言參考文檔Unity斷言參考手冊以下是中英對照版Unity Assertions Reference

譯者注:譯者部落格(http://blog.csdn.net/lin_strong),轉載請保留這條。此為Unity手冊的翻譯,僅供學習交流使用,請勿用于商業用途。

翻譯的資料是公開的,在docs/UnityAssertionsReference.md,我想應該不會有什麼版權問題,如涉及版權問題,請聯系我删除文章。

文章目錄

  • Unity斷言參考手冊
    • 背景和綜述
      • 超濃縮版
      • Unity的成分很多,但它主要是斷言
      • 斷言是什麼
      • Unity的斷言:有用的資訊 _和_ 免費的源碼文檔
    • 斷言的約定及配置
      • 命名和參數約定
        • TEST_ASSERT_X_MESSAGE 變體
        • TEST_ASSERT_X_ARRAY 變體
      • TEST_ASSERT_EACH_EQUAL_X 變體
      • 配置
        • 浮點數支援是可選的
        • 最大資料寬度是可配置的。
    • The Assertions in All Their Blessed Glory
      • 基礎Fail和Ignore
          • `TEST_FAIL()`
          • `TEST_IGNORE()`
      • 布爾值
          • `TEST_ASSERT (condition)`
          • `TEST_ASSERT_TRUE (condition)`
          • `TEST_ASSERT_FALSE (condition)`
          • `TEST_ASSERT_UNLESS (condition)`
          • `TEST_ASSERT_NULL (pointer)`
          • `TEST_ASSERT_NOT_NULL (pointer)`
      • (所有大小的)有符号和無符号整型
          • `TEST_ASSERT_EQUAL_INT (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT64 (expected, actual)`
          • `TEST_ASSERT_EQUAL (expected, actual)`
          • `TEST_ASSERT_NOT_EQUAL (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT64 (expected, actual)`
      • (所有大小的)十六進制無符号整型
          • `TEST_ASSERT_EQUAL_HEX (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX64 (expected, actual)`
      • 掩碼及位等級的斷言
          • `TEST_ASSERT_BITS (mask, expected, actual)`
          • `TEST_ASSERT_BITS_HIGH (mask, actual)`
          • `TEST_ASSERT_BITS_LOW (mask, actual)`
          • `TEST_ASSERT_BIT_HIGH (bit, actual)`
          • `TEST_ASSERT_BIT_LOW (bit, actual)`
      • 整數小于/大于
          • `TEST_ASSERT_GREATER_THAN (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT32 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT32 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX32 (threshold, actual)`
      • (所有大小的)整型範圍
          • `TEST_ASSERT_INT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT64_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)`
      • 結構體和字元串
          • `TEST_ASSERT_EQUAL_PTR (expected, actual)`
          • `TEST_ASSERT_EQUAL_STRING (expected, actual)`
          • `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`
      • 數組
          • `TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)`
      • (數組與單個值)分别相等
        • `TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)`
      • 單精度浮點數(如果啟用)
          • `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
          • `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_FLOAT_IS_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NEG_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NAN (actual)`
          • `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)`
      • 雙精度浮點數(如果啟用)
          • `TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
          • `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_DOUBLE_IS_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NAN (actual)`
          • `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)`
    • 進階斷言:那些巧妙的斷言中的細節
      • EQUAL斷言是怎麼處理單精度和雙精度浮點數的?
      • 怎麼處理整型大小非标準的目标呢?
  • 以下是中英對照版
  • Unity Assertions Reference
    • Background and Overview
      • Super Condensed Version
      • Unity Is Several Things But Mainly It's Assertions
      • What's an Assertion?
      • Unity's Assertions: Helpful Messages _and_ Free Source Code Documentation
    • Assertion Conventions and Configurations
      • Naming and Parameter Conventions
        • TEST_ASSERT_X_MESSAGE Variants
        • TEST_ASSERT_X_ARRAY Variants
      • TEST_ASSERT_EACH_EQUAL_X Variants
      • Configuration
        • Floating Point Support Is Optional
        • Maximum Data Type Width Is Configurable
    • The Assertions in All Their Blessed Glory
      • Basic Fail and Ignore
          • `TEST_FAIL()`
          • `TEST_IGNORE()`
      • Boolean
          • `TEST_ASSERT (condition)`
          • `TEST_ASSERT_TRUE (condition)`
          • `TEST_ASSERT_FALSE (condition)`
          • `TEST_ASSERT_UNLESS (condition)`
          • `TEST_ASSERT_NULL (pointer)`
          • `TEST_ASSERT_NOT_NULL (pointer)`
      • Signed and Unsigned Integers (of all sizes)
          • `TEST_ASSERT_EQUAL_INT (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_INT64 (expected, actual)`
          • `TEST_ASSERT_EQUAL (expected, actual)`
          • `TEST_ASSERT_NOT_EQUAL (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_UINT64 (expected, actual)`
      • Unsigned Integers (of all sizes) in Hexadecimal
          • `TEST_ASSERT_EQUAL_HEX (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX8 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX16 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX32 (expected, actual)`
          • `TEST_ASSERT_EQUAL_HEX64 (expected, actual)`
      • Masked and Bit-level Assertions
          • `TEST_ASSERT_BITS (mask, expected, actual)`
          • `TEST_ASSERT_BITS_HIGH (mask, actual)`
          • `TEST_ASSERT_BITS_LOW (mask, actual)`
          • `TEST_ASSERT_BIT_HIGH (bit, actual)`
          • `TEST_ASSERT_BIT_LOW (bit, actual)`
      • Integer Less Than / Greater Than
          • `TEST_ASSERT_GREATER_THAN (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_INT32 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_UINT32 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX8 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX16 (threshold, actual)`
          • `TEST_ASSERT_GREATER_THAN_HEX32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_UINT32 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX8 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX16 (threshold, actual)`
          • `TEST_ASSERT_LESS_THAN_HEX32 (threshold, actual)`
      • Integer Ranges (of all sizes)
          • `TEST_ASSERT_INT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_INT64_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)`
      • Structs and Strings
          • `TEST_ASSERT_EQUAL_PTR (expected, actual)`
          • `TEST_ASSERT_EQUAL_STRING (expected, actual)`
          • `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`
      • Arrays
          • `TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)`
      • Each Equal (Arrays to Single Value)
        • `TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)`
        • `TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)`
      • Floating Point (If enabled)
          • `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
          • `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_FLOAT_IS_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NEG_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NAN (actual)`
          • `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)`
          • `TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)`
      • Double (If enabled)
          • `TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)`
          • `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
          • `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
          • `TEST_ASSERT_DOUBLE_IS_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NAN (actual)`
          • `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)`
          • `TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)`
    • Advanced Asserting: Details On Tricky Assertions
      • How do the EQUAL assertions work for FLOAT and DOUBLE?
      • How do we deal with targets with non-standard int sizes?

Unity斷言參考手冊

背景和綜述

超濃縮版

  • 一條斷言為單個條件确立真理(即邏輯真)
  • Unity主要是一個豐富的斷言集合加上對收集并簡單地執行這些斷言的支援。
  • Unity的結構允許你簡單地從源代碼中把斷言分離進測試代碼中。
  • Unity的斷言:
  • 加了許多許多香料以處理不同的C類型和斷言用例
  • 使用上下文來提供詳細并有用的故障消息。
  • 提供文檔類型、期望值以及源碼中的基礎行為。

Unity的成分很多,但它主要是斷言

一個思考Unity的簡單方式是把它想成一個豐富的斷言集合,你可以用這個集合來限制你的

源碼按照你想的方式工作。Unity提供了一個架構,你可以使用其以簡單地組織和執行那些

在測試代碼中的斷言,斷言與源碼是分離的。

斷言是什麼

在它們的核心,斷言是真理的一個确立 - 邏輯真理。這個東西和那個相等嗎?這段精妙的代碼

有這樣的特性麼?你明白了。斷言是可執行的代碼(看看這個大圖以閱讀差别)。一個失敗的斷言

會停止執行并通過一些合适的I/O口報告一個錯誤(例如:stdout、GUI、檔案、閃燈)

基本上,想要動态驗證的話,你唯一需要的就是單個斷言機制。事實上,那就是C标準庫中assert()宏

做的事。是以,為什麼不幹脆就用它算了?好吧,我們在報告上可以做的好的多。C的

assert()

原本就很啞巴,并且特别處理不來普通資料類型如數組、資料結構等。并且,如果沒有一些其他

支援,往C垃圾源碼中扔一堆

assert()

實在太誘人了。按照Unity的方式把測試和源碼分開通常

會更加幹淨、更易于管理以及更有用。

Unity的斷言:有用的資訊 和 免費的源碼文檔

斷言一個簡單的真相條件是有價值的,但是使用斷言的上下文更有價值。比如,如果你知道

你在比較标志位而不是整數,那麼為什麼不使用那上下文以在一個斷言失敗時給予精準、可

讀、位等級的回報呢?

那就是Unity的斷言集合做的事 - 捕獲上下文以給你有用且有意義的斷言失敗資訊。事實上,

斷言本身也是可執行的文檔,關于你源碼中類型和值的文檔。隻要你的測試和你的源碼保持

同步,并且所有的測試都能通過,你就有了你源碼的一個詳細且最新的目的與機制視圖。并且

由于一個奇妙的秘密,良好測試的代碼通常趨于良好的設計。

斷言的約定及配置

命名和參數約定

斷言的參數約定通常遵循這個順序:

TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
           

很簡單的斷言可能隻使用單個"actual"參數(比如一個簡單的null檢查)。

"Actual"是被測試的值,不像斷言結構中的其他參數,它是唯一會出現在所有斷言變體中的參數。

"Modifiers"是掩碼、範圍、标志位說明符、浮點位。

"Expected"是你期望的(emmm)與"actual"值進行比較的值;它被标記為可選參數是因為一些斷言

僅僅需要單個"actual"參數(比如null檢查)。

"Size/count"指的是如字元串長度、數組長度。

Unity的許多斷言有明顯的重複,同個資料類型許多斷言都能處理。差別是失敗資訊的樣子。

比如,一條斷言的

_HEX

變體會将這條斷言的期望值與真實值列印為十六進制格式。

TEST_ASSERT_X_MESSAGE 變體

所有 斷言都有一個把簡單字元串資訊作為最終參數的變體。你指定的字元串會在Unity輸出

中被附加到斷言失敗資訊中。

為了簡明起見,帶有一個消息參數的斷言變體不會列在下面。隻需要附加

_MESSAGE

到下面參考

清單中任意斷言名的後面并添加一個字元串作為最終參數就行。

示例:

TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
           

消息化後就像這樣…

TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
           

注意:

  • _MESSAGE

    變體故意不支援

    printf

    風格的格式化,因為許多嵌入式項目因多種原因不支援或

    避免使用

    printf

    。如果需要的話,可以在斷言前使用sprintf來組裝一個複雜的失敗消息。
  • 如果你想要在一個斷言失敗消息中輸出一個計數器值(比如,在一個循環中),搭建一個結果的

    數組,然後使用某個

    _ARRAY

    斷言(見以下)可能就能有效避免使用

    sprintf

TEST_ASSERT_X_ARRAY 變體

Unity為多種類型的數組提供了一個斷言集合。下面是數組部分的文檔。它和

_MESSAGE

變體很像

,對于Unity中很多類型的斷言你同樣隻要在名字後面加上

_ARRAY

就能在整塊記憶體上運作斷言了。

TEST_ASSERT_EQUAL_TYPEX_ARRAY( expected, actual, {size/count} )
           

"Expected"是一個數組本身。

"Size/count"是一或兩個指定數組元素個數的必要參數也可能是數組中元素的長度。

注意:

  • _MESSAGE

    變體的約定仍能作用于array斷言上。

    _ARRAY

    斷言的

    _MESSAGE

    變體的名字

    是以

    _ARRAY_MESSAGE

    結尾的。
  • 處理浮點值的斷言分類進float和double斷言(見後面章節)。

TEST_ASSERT_EACH_EQUAL_X 變體

Unity為多種類型的數組提供了一個斷言集合,同樣可以隻比較單個值。下面是Each Equal部分的

文檔。它和

_MESSAGE

變體很像,對于Unity中很多類型的斷言你隻要在名字中注入_EACH_EQUAL

就能在整塊記憶體上運作斷言了。

TEST_ASSERT_EACH_EQUAL_TYPEX( expected, actual, {size/count} )
           

"Expected"是要比較的單個值

"Actual"是一個數組,其中的每個元素都會與期望值比較。

"Size/count"是一或兩個指定數組元素個數的必要參數也可能是數組中元素的長度。

注意:

  • _MESSAGE

    變體的約定仍能作用于Each Equal斷言上。
  • 處理浮點值的斷言分類進float和double斷言(見後面章節)。

配置

浮點數支援是可選的

對浮點數類型的支援是可配置的。這是說,通過定義合适的預處理器符号,單精度浮點數和

雙精度浮點數可以在Unity代碼中獨立地啟用或禁用。這對于沒有浮點數運算支援的嵌入式

目标十分有用(即Unity在僅支援點定數的平台上不會編譯出錯)。詳見Unity文檔。

最大資料寬度是可配置的。

不是所有目标都支援64位寬度類型,甚至有些32位都不支援。定義合适的預處理器符合,然後

Unity就會在編譯中隐藏所有超過了你目标平台最大寬度的操作。詳見Unity文檔。

The Assertions in All Their Blessed Glory

基礎Fail和Ignore

TEST_FAIL()

這家夥最常用于一種特殊狀況,即你的測試代碼在一個簡單的斷言上執行邏輯。就是說,

實踐中,

TEST_FAIL()

總是會出現在一個條件代碼塊中。

示例:

  • 多次執行一個遞增一個計數器的狀态機,然後你的測試代碼驗證其為最後一步
  • 觸發一個異常并驗證它(就像在Try / Catch / Throw中,見CException項目)。

TEST_IGNORE()

标記一個測試用例(即應該包含測試斷言的函數)為忽視。通常這被用于标記未來要實作的一個

測試用例。如果其他斷言在一個封裝的測試用例中,一個被忽視的測試用例是有用的(詳見Unity

文檔)。

布爾值

TEST_ASSERT (condition)

TEST_ASSERT_TRUE (condition)

TEST_ASSERT_FALSE (condition)

TEST_ASSERT_UNLESS (condition)

隻是

TEST_ASSERT_FALSE

的措辭的一些變化。

TEST_ASSERT_UNLESS

在特定的條件語句或

測試結構中有助于增加語義上的可讀性。

TEST_ASSERT_NULL (pointer)

TEST_ASSERT_NOT_NULL (pointer)

(所有大小的)有符号和無符号整型

對于不支援大整型的建構目标,可以禁用大整型。比如,如果你的目标最多支援16位類型,

通過定義适當的符号,Unity可以配置為隐藏32和64位操作,這樣就可以編譯通過了(詳見

Unity文檔)。參考這篇文檔後面的進階斷言以擷取怎麼處理其他大小的建議。

TEST_ASSERT_EQUAL_INT (expected, actual)

TEST_ASSERT_EQUAL_INT8 (expected, actual)

TEST_ASSERT_EQUAL_INT16 (expected, actual)

TEST_ASSERT_EQUAL_INT32 (expected, actual)

TEST_ASSERT_EQUAL_INT64 (expected, actual)

TEST_ASSERT_EQUAL (expected, actual)

TEST_ASSERT_NOT_EQUAL (expected, actual)

TEST_ASSERT_EQUAL_UINT (expected, actual)

TEST_ASSERT_EQUAL_UINT8 (expected, actual)

TEST_ASSERT_EQUAL_UINT16 (expected, actual)

TEST_ASSERT_EQUAL_UINT32 (expected, actual)

TEST_ASSERT_EQUAL_UINT64 (expected, actual)

(所有大小的)十六進制無符号整型

所有函數名中帶有的

_HEX

的斷言都是無符号整型斷言,它們産生的

expected

actual

值都是十六進制格式的。Unity輸出是大端的。

TEST_ASSERT_EQUAL_HEX (expected, actual)

TEST_ASSERT_EQUAL_HEX8 (expected, actual)

TEST_ASSERT_EQUAL_HEX16 (expected, actual)

TEST_ASSERT_EQUAL_HEX32 (expected, actual)

TEST_ASSERT_EQUAL_HEX64 (expected, actual)

掩碼及位等級的斷言

掩碼及位等級的斷言産生十六進制格式的輸出。Unity輸出是大端的。

TEST_ASSERT_BITS (mask, expected, actual)

僅比較

expected

actual

參數的掩碼(即1)位。

TEST_ASSERT_BITS_HIGH (mask, actual)

斷言

actual

參數的掩碼位為1。

TEST_ASSERT_BITS_LOW (mask, actual)

斷言

actual

參數的掩碼為為0。

TEST_ASSERT_BIT_HIGH (bit, actual)

斷言

actual

參數的指定位為1。

TEST_ASSERT_BIT_LOW (bit, actual)

斷言

actual

參數的指定位為0。

整數小于/大于

這些斷言驗證

actual

參數比

threshold

(不含)小或大。比如,如果門檻值為0,對于大于

的那個,斷言在它為0或更小值時會失敗。

TEST_ASSERT_GREATER_THAN (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT32 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT32 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX32 (threshold, actual)

TEST_ASSERT_LESS_THAN (threshold, actual)

TEST_ASSERT_LESS_THAN_INT (threshold, actual)

TEST_ASSERT_LESS_THAN_INT8 (threshold, actual)

TEST_ASSERT_LESS_THAN_INT16 (threshold, actual)

TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT8 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT16 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT32 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX8 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX16 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX32 (threshold, actual)

(所有大小的)整型範圍

這些斷言驗證

expected

參數處于

actual

參數的+/-

delta

(含)之間。比如,如果期望值

是10,delta值是3,那如果給定值在7 - 13之外的話斷言就會失敗。

TEST_ASSERT_INT_WITHIN (delta, expected, actual)

TEST_ASSERT_INT8_WITHIN (delta, expected, actual)

TEST_ASSERT_INT16_WITHIN (delta, expected, actual)

TEST_ASSERT_INT32_WITHIN (delta, expected, actual)

TEST_ASSERT_INT64_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)

結構體和字元串

TEST_ASSERT_EQUAL_PTR (expected, actual)

斷言指針指向同個記憶體位置。

TEST_ASSERT_EQUAL_STRING (expected, actual)

斷言以null結尾(

'\0'

)的字元串是一緻的。如果字元串的長度不一樣,或者在結束符前的

任何部分字元串不一樣,斷言就是失敗。兩個NULL字元串(即長度為0)被認為相等。

TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)

斷言由

expected

指針和

actual

指針所指向的記憶體中的内容是一緻的。記憶體塊的位元組大小

len

參數指定。

數組

expected

actual

參數都是數組。

num_elements

指定了數組中要比較的元素數量。

_HEX

斷言産生的失敗資訊會以十六進制列印expected和actual數組中的内容。

至于字元串數組的比較行為,見前面章節中對

TEST_ASSERT_EQUAL_STRING

的注釋。

要比較的數組中第一個不比對的元素會導緻斷言失敗。失敗資訊會說明比較失敗的索引。

TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)

len

是每個數組元素的位元組大小。

(數組與單個值)分别相等

expected

是單個值,

actual

是數組。

num_elements

指定數組中要比較的元素的個數。

_HEX

斷言産生的失敗資訊會以十六進制列印expected和actual數組中的内容。

要比較的數組中第一個不比對的元素會導緻斷言失敗。失敗資訊會說明比較失敗的數組索引。

TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)

len

是每個數組元素的位元組大小。

單精度浮點數(如果啟用)

TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)

這些斷言驗證

actual

參數處于

expected

參數的+/-

delta

之間。浮點數表示法的特性

導緻沒法保證準确的比較。

TEST_ASSERT_EQUAL_FLOAT (expected, actual)

斷言

actual

值與

expected

值"接近到足以認為相等"。如果你對細節很好奇,詳見進階斷言

章節。在浮點數斷言中隐藏使用者指定的內插補點一方面是友善使用,另一方面也是CMock代碼生成

約定的要求。

TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)

詳見 數組斷言 章節。注意,數組元素的單個單精度浮點數比較使用的是

TEST_ASSERT_EQUAL_FLOAT

就是說,使用者指定的內插補點比較值需要一個定制實作的單精度浮點數數組斷言。

TEST_ASSERT_FLOAT_IS_INF (actual)

斷言

actual

參數等于正無窮的浮點數表示。

TEST_ASSERT_FLOAT_IS_NEG_INF (actual)

斷言

actual

參數等于負無窮的浮點數表示。

TEST_ASSERT_FLOAT_IS_NAN (actual)

斷言

actual

參數是一個非數字浮點數表示。

TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)

斷言

actual

參數是一個可以用于數學運算的浮點數。就是說,

actual

參數既不是正無窮,

又不是負無窮,還不是非數字浮點數。

TEST_ASSERT_FLOAT_IS_NOT_INF (actual)

斷言

actual

參數是一個值,而不是正無窮。

TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)

斷言

actual

參數是一個值,而不是負無窮。

TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)

斷言

actual

參數是一個值,而不是非數字浮點數表示。

TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)

斷言

actual

參數不可用于數學運算。就是說,

actual

參數不是正無窮,就是負無窮,

或是非數字浮點數。

雙精度浮點數(如果啟用)

TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)

這些斷言驗證

actual

參數處于

expected

參數的+/-

delta

之間。浮點數表示法的特性

導緻沒法保證準确的比較。

TEST_ASSERT_EQUAL_DOUBLE (expected, actual)

斷言

actual

值與

expected

值"接近到足以認為相等"。如果你對細節很好奇,詳見進階斷言

章節。在浮點數斷言中隐藏使用者指定的內插補點一方面是友善使用,另一方面也是CMock代碼生成

約定的要求。

TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)

詳見 數組斷言 章節。注意,數組元素的單個雙精度浮點數比較使用的是

TEST_ASSERT_EQUAL_DOUBLE

就是說,使用者指定的內插補點比較值需要一個定制實作的雙精度浮點數數組斷言。

TEST_ASSERT_DOUBLE_IS_INF (actual)

斷言

actual

參數等于正無窮的浮點數表示。

TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)

斷言

actual

參數等于負無窮的浮點數表示。

TEST_ASSERT_DOUBLE_IS_NAN (actual)

斷言

actual

參數是一個非數字浮點數表示。

TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)

斷言

actual

參數是一個可以用于數學運算的浮點數。就是說,

actual

參數既不是正無窮,

又不是負無窮,還不是非數字浮點數。

TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)

斷言

actual

參數是一個值,而不是正無窮。

TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)

斷言

actual

參數是一個值,而不是負無窮。

TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)

斷言

actual

參數是一個值,而不是非數字浮點數表示。

TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)

斷言

actual

參數不可用于數學運算。就是說,

actual

參數不是正無窮,就是負無窮,

或是非數字浮點數。

進階斷言:那些巧妙的斷言中的細節

這個章節幫助你了解怎麼處理一些你可能會遇到的更有技巧性的斷言。帶你瞥一眼Unity的

斷言機制引擎下的一些細節。如果你是那種喜歡刨根知底的人,那這章節很适合你。如果不

是的話,當你需要的時候再來讀這個章節吧。

EQUAL斷言是怎麼處理單精度和雙精度浮點數的?

如你也許知道的,直接檢查一對浮點數相等實在太扯淡了,簡直是zz。浮點數值常能以多種

方式表示,特别是對一個值進行一系列運算後。初始化一個變量為2.0很可能導緻浮點數表示

法記為 2 x 20,但是一系列數學運算後可能導緻記為 8 x 2-2,這也表示2這個值。有時候

重複的運算會導緻相等運算失敗。

是以Unity不直接比較浮點數相等。它會檢查兩個浮點數是否"十分接近"。如果你讓Unity使

用預設配置,“十分接近"等價于"在一個或兩個有效bit内”。在底層,

TEST_ASSERT_EQUAL_FLOAT

實際上是

delta

參數在運作時計算的

TEST_ASSERT_FLOAT_WITHIN

。對于單精度,delta是

期望值乘以0.00001,則會産生在期望值周圍一個很小比例的範圍。

如果你期望的值為20,000.0,delta則算出來是0.2。是以任何19,999.8與20,000.2之間的值

都能通過相等檢查。實踐證明這大緻是單精度浮點數單個bit的範圍,

是以當是0的時候怎麼辦呢? 0 - 可以用許多種方式表示 - 甚至比其他浮點值還多的方式。

不管是0 x 20 還是 0 x 263,它都是0,對吧?幸運地是,如果你用任意兩個0相減,內插補點

都還是0,它仍然會落在0加減一個內插補點的範圍内。是以這個方法仍然有用!

雙精度浮點數使用一個小得多的乘數,同樣大約一個bit的誤差。

如果你不喜歡這個範圍,想要讓你的浮點數相等斷言不那麼嚴格,你可以修改這些乘數,定義

UNITY_FLOAT_PRECISION和UNITY_DOUBLE_PRECISION的值就行。詳見Unity文檔。

怎麼處理整型大小非标準的目标呢?

"搞笑"的是,在C這個标準中,如整型這麼基礎的東西居然會根據目标平台而變化。根據C的标準,

一個

int

的大小是目标的自然寄存器大小,并且至少16位且為位元組的整數倍。它還保證

以下大小排序:

char <= short <= int <= long <= long long
           

int

最常見的是32位。在嵌入式中,大部分情況下

int

是16位。極少微控制器是24位

整型,但它仍然完美滿足标準C。

讓事情更有趣的是,有一些編譯器和目标遇到了更困難的選擇。如果自然寄存器的大小是

10位或12位的話怎麼辦?顯然它們無法同時滿足至少16位以及比對自然寄存器大小這 兩個 條件。

在這些情況下,它們通常選擇自然寄存器大小,然後就變成了這樣:

char (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit)
           

啊…哈。這明顯違背了一兩個規則…但是它們不得不違背一些規則,是以它們做了個選擇。

當C99标準出現時,它引入了多種标準大小類型。它還引入了用于拉取整型最大/最小值的宏。

這太贊了!不幸的是,許多嵌入式編譯器并沒有使用C99類型(有時是因為它們遇到了如上描述

的奇怪的寄存器大小。有時是因為它們不喜歡它?)。

Unity的目标從一開始就是要支援每個微控制器或微處理器與C編譯器的組合。随着時間的流逝,

我們真的很接近這個目标呢。但是如果你想要在一些更古怪的目标上高效地使用Unity的話,

有一些你需要意識到的竅門。

首先,當将Unity配置到一個新目标上時,你應該特别關注下用于自動檢測類型的(可用)宏,

或手動配置它們。你可以在Unity的文檔中獲得相關資訊。

當你突然需要處理一些怪異的東西,如24位

int

時怎麼辦呢?最簡單的解決方案是使用大一号

的尺寸。如果你有24位的

int

,将Unity配置為使用32位的整型。如果你有一個12位的

int

配置Unity使用16位的。注意以下兩個事情:

  1. 當Unity提示失敗資訊時,它會往高位未使用bit中填充0。
  2. 你要當心執行有符号的運算的斷言,特别是

    TEST_ASSERT_INT_WITHIN

    。這樣的斷言可能會

    錯用你的

    int

    ,可能你會遇到錯誤的失敗。你總可以回歸最簡單的

    TEST_ASSERT

    并自己做運算。

Find The Latest of This And More at ThrowTheSwitch.org

以下是中英對照版

Unity Assertions Reference

Unity斷言參考手冊

Background and Overview

背景和綜述

Super Condensed Version

超濃縮版

  • An assertion establishes truth (i.e. boolean True) for a single condition.

    Upon boolean False, an assertion stops execution and reports the failure.

  • Unity is mainly a rich collection of assertions and the support to gather up

    and easily execute those assertions.

  • The structure of Unity allows you to easily separate test assertions from

    source code in, well, test code.

  • Unity’s assertions:
  • Come in many, many flavors to handle different C types and assertion cases.
  • Use context to provide detailed and helpful failure messages.
  • Document types, expected values, and basic behavior in your source code for

    free.

  • 一條斷言為單個條件确立真理(即邏輯真)
  • Unity主要是一個豐富的斷言集合加上對收集并簡單地執行這些斷言的支援。
  • Unity的結構允許你簡單地從源代碼中把斷言分離進測試代碼中。
  • Unity的斷言:
  • 加了許多許多香料以處理不同的C類型和斷言用例
  • 使用上下文來提供詳細并有用的故障消息。
  • 提供文檔類型、期望值以及源碼中的基礎行為。

Unity Is Several Things But Mainly It’s Assertions

Unity的成分很多,但它主要是斷言

One way to think of Unity is simply as a rich collection of assertions you can

use to establish whether your source code behaves the way you think it does.

Unity provides a framework to easily organize and execute those assertions in

test code separate from your source code.

一個思考Unity的簡單方式是把它想成一個豐富的斷言集合,你可以用這個集合來限制你的

源碼按照你想的方式工作。Unity提供了一個架構,你可以使用其以簡單地組織和執行那些

在測試代碼中的斷言,斷言與源碼是分離的。

What’s an Assertion?

斷言是什麼

At their core, assertions are an establishment of truth - boolean truth. Was this

thing equal to that thing? Does that code doohickey have such-and-such property

or not? You get the idea. Assertions are executable code (to appreciate the big

picture on this read up on the difference between

[link:Dynamic Verification and Static Analysis]). A failing assertion stops

execution and reports an error through some appropriate I/O channel (e.g.

stdout, GUI, file, blinky light).

在它們的核心,斷言是真理的一個确立 - 邏輯真理。這個東西和那個相等嗎?這段精妙的代碼

有這樣的特性麼?你明白了。斷言是可執行的代碼(看看這個大圖以閱讀差别)。一個失敗的斷言

會停止執行并通過一些合适的I/O口報告一個錯誤(例如:stdout、GUI、檔案、閃燈)

Fundamentally, for dynamic verification all you need is a single assertion

mechanism. In fact, that’s what the assert() macro in C’s standard library

is for. So why not just use it? Well, we can do far better in the reporting

department. C’s

assert()

is pretty dumb as-is and is particularly poor for

handling common data types like arrays, structs, etc. And, without some other

support, it’s far too tempting to litter source code with C’s

assert()

's. It’s

generally much cleaner, manageable, and more useful to separate test and source

code in the way Unity facilitates.

基本上,想要動态驗證的話,你唯一需要的就是單個斷言機制。事實上,那就是C标準庫中assert()宏

做的事。是以,為什麼不幹脆就用它算了?好吧,我們在報告上可以做的好的多。C的

assert()

原本就很啞巴,并且特别處理不來普通資料類型如數組、資料結構等。并且,如果沒有一些其他

支援,往C垃圾源碼中扔一堆

assert()

實在太誘人了。按照Unity的方式把測試和源碼分開通常

會更加幹淨、更易于管理以及更有用。

Unity’s Assertions: Helpful Messages and Free Source Code Documentation

Unity的斷言:有用的資訊 和 免費的源碼文檔

Asserting a simple truth condition is valuable, but using the context of the

assertion is even more valuable. For instance, if you know you’re comparing bit

flags and not just integers, then why not use that context to give explicit,

readable, bit-level feedback when an assertion fails?

斷言一個簡單的真相條件是有價值的,但是使用斷言的上下文更有價值。比如,如果你知道

你在比較标志位而不是整數,那麼為什麼不使用那上下文以在一個斷言失敗時給予精準、可

讀、位等級的回報呢?

That’s what Unity’s collection of assertions do - capture context to give you

helpful, meaningful assertion failure messages. In fact, the assertions

themselves also serve as executable documentation about types and values in your

source code. So long as your tests remain current with your source and all those

tests pass, you have a detailed, up-to-date view of the intent and mechanisms in

your source code. And due to a wondrous mystery, well-tested code usually tends

to be well designed code.

那就是Unity的斷言集合做的事 - 捕獲上下文以給你有用且有意義的斷言失敗資訊。事實上,

斷言本身也是可執行的文檔,關于你源碼中類型和值的文檔。隻要你的測試和你的源碼保持

同步,并且所有的測試都能通過,你就有了你源碼的一個詳細且最新的目的與機制視圖。并且

由于一個奇妙的秘密,良好測試的代碼通常趨于良好的設計。

Assertion Conventions and Configurations

斷言的約定及配置

Naming and Parameter Conventions

命名和參數約定

The convention of assertion parameters generally follows this order:

斷言的參數約定通常遵循這個順序:

TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
           

The very simplest assertion possible uses only a single “actual” parameter (e.g.

a simple null check).

很簡單的斷言可能隻使用單個"actual"參數(比如一個簡單的null檢查)。

“Actual” is the value being tested and unlike the other parameters in an

assertion construction is the only parameter present in all assertion variants.

“Modifiers” are masks, ranges, bit flag specifiers, floating point deltas.

“Expected” is your expected value (duh) to compare to an “actual” value; it’s

marked as an optional parameter because some assertions only need a single

“actual” parameter (e.g. null check).

“Size/count” refers to string lengths, number of array elements, etc.

"Actual"是被測試的值,不像斷言結構中的其他參數,它是唯一會出現在所有斷言變體中的參數。

"Modifiers"是掩碼、範圍、标志位說明符、浮點位。

"Expected"是你期望的(emmm)與"actual"值進行比較的值;它被标記為可選參數是因為一些斷言

僅僅需要單個"actual"參數(比如null檢查)。

"Size/count"指的是如字元串長度、數組長度。

Many of Unity’s assertions are clear duplications in that the same data type

is handled by several assertions. The differences among these are in how failure

messages are presented. For instance, a

_HEX

variant of an assertion prints

the expected and actual values of that assertion formatted as hexadecimal.

Unity的許多斷言有明顯的重複,同個資料類型許多斷言都能處理。差別是失敗資訊的樣子。

比如,一條斷言的

_HEX

變體會将這條斷言的期望值與真實值列印為十六進制格式。

TEST_ASSERT_X_MESSAGE Variants

TEST_ASSERT_X_MESSAGE變體

All assertions are complemented with a variant that includes a simple string

message as a final parameter. The string you specify is appended to an assertion

failure message in Unity output.

所有 斷言都有一個把簡單字元串資訊作為最終參數的變體。你指定的字元串會在Unity輸出

中被附加到斷言失敗資訊中。

For brevity, the assertion variants with a message parameter are not listed

below. Just tack on

_MESSAGE

as the final component to any assertion name in

the reference list below and add a string as the final parameter.

為了簡明起見,帶有一個消息參數的斷言變體不會列在下面。隻需要附加

_MESSAGE

到下面參考

清單中任意斷言名的後面并添加一個字元串作為最終參數就行。

Example:

示例:

TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
           

becomes messageified like thus…

消息化後就像這樣…

TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
           

Notes:

  • The

    _MESSAGE

    variants intentionally do not support

    printf

    style formatting

    since many embedded projects don’t support or avoid

    printf

    for various reasons.

    It is possible to use

    sprintf

    before the assertion to assemble a complex fail

    message, if necessary.

  • If you want to output a counter value within an assertion fail message (e.g. from

    a loop) , building up an array of results and then using one of the

    _ARRAY

    assertions (see below) might be a handy alternative to

    sprintf

    .

注意:

  • _MESSAGE

    變體故意不支援

    printf

    風格的格式化,因為許多嵌入式項目因多種原因不支援或

    避免使用

    printf

    。如果需要的話,可以在斷言前使用sprintf來組裝一個複雜的失敗消息。
  • 如果你想要在一個斷言失敗消息中輸出一個計數器值(比如,在一個循環中),搭建一個結果的

    數組,然後使用某個

    _ARRAY

    斷言(見以下)可能就能有效避免使用

    sprintf

TEST_ASSERT_X_ARRAY Variants

TEST_ASSERT_X_ARRAY 變體

Unity provides a collection of assertions for arrays containing a variety of

types. These are documented in the Array section below. These are almost on par

with the

_MESSAGE

variants of Unity’s Asserts in that for pretty much any Unity

type assertion you can tack on

_ARRAY

and run assertions on an entire block of

memory.

Unity為多種類型的數組提供了一個斷言集合。下面是數組部分的文檔。它和

_MESSAGE

變體很像

,對于Unity中很多類型的斷言你同樣隻要在名字後面加上

_ARRAY

就能在整塊記憶體上運作斷言了。

TEST_ASSERT_EQUAL_TYPEX_ARRAY( expected, actual, {size/count} )
           

“Expected” is an array itself.

“Size/count” is one or two parameters necessary to establish the number of array

elements and perhaps the length of elements within the array.

"Expected"是一個數組本身。

"Size/count"是一或兩個指定數組元素個數的必要參數也可能是數組中元素的長度。

Notes:

  • The

    _MESSAGE

    variant convention still applies here to array assertions. The

    _MESSAGE

    variants of the

    _ARRAY

    assertions have names ending with

    _ARRAY_MESSAGE

    .
  • Assertions for handling arrays of floating point values are grouped with float

    and double assertions (see immediately following section).

注意:

  • _MESSAGE

    變體的約定仍能作用于array斷言上。

    _ARRAY

    斷言的

    _MESSAGE

    變體的名字

    是以

    _ARRAY_MESSAGE

    結尾的。
  • 處理浮點值的斷言分類進float和double斷言(見後面章節)。

TEST_ASSERT_EACH_EQUAL_X Variants

TEST_ASSERT_EACH_EQUAL_X變體

Unity provides a collection of assertions for arrays containing a variety of

types which can be compared to a single value as well. These are documented in

the Each Equal section below. these are almost on par with the

_MESSAGE

variants of Unity’s Asserts in that for pretty much any Unity type assertion you

can inject _EACH_EQUAL and run assertions on an entire block of memory.

Unity為多種類型的數組提供了一個斷言集合,同樣可以隻比較單個值。下面是Each Equal部分的

文檔。它和

_MESSAGE

變體很像,對于Unity中很多類型的斷言你隻要在名字中注入_EACH_EQUAL

就能在整塊記憶體上運作斷言了。

TEST_ASSERT_EACH_EQUAL_TYPEX( expected, actual, {size/count} )
           

“Expected” is a single value to compare to.

“Actual” is an array where each element will be compared to the expected value.

“Size/count” is one of two parameters necessary to establish the number of array

elements and perhaps the length of elements within the array.

"Expected"是要比較的單個值

"Actual"是一個數組,其中的每個元素都會與期望值比較。

"Size/count"是一或兩個指定數組元素個數的必要參數也可能是數組中元素的長度。

Notes:

  • The

    _MESSAGE

    variant convention still applies here to Each Equal assertions.
  • Assertions for handling Each Equal of floating point values are grouped with

    float and double assertions (see immediately following section).

注意:

  • _MESSAGE

    變體的約定仍能作用于Each Equal斷言上。
  • 處理浮點值的斷言分類進float和double斷言(見後面章節)。

Configuration

配置

Floating Point Support Is Optional

浮點數支援是可選的

Support for floating point types is configurable. That is, by defining the

appropriate preprocessor symbols, floats and doubles can be individually enabled

or disabled in Unity code. This is useful for embedded targets with no floating

point math support (i.e. Unity compiles free of errors for fixed point only

platforms). See Unity documentation for specifics.

對浮點數類型的支援是可配置的。這是說,通過定義合适的預處理器符号,單精度浮點數和

雙精度浮點數可以在Unity代碼中獨立地啟用或禁用。這對于沒有浮點數運算支援的嵌入式

目标十分有用(即Unity在僅支援點定數的平台上不會編譯出錯)。詳見Unity文檔。

Maximum Data Type Width Is Configurable

最大資料寬度是可配置的。

Not all targets support 64 bit wide types or even 32 bit wide types. Define the

appropriate preprocessor symbols and Unity will omit all operations from

compilation that exceed the maximum width of your target. See Unity

documentation for specifics.

不是所有目标都支援64位寬度類型,甚至有些32位都不支援。定義合适的預處理器符合,然後

Unity就會在編譯中隐藏所有超過了你目标平台最大寬度的操作。詳見Unity文檔。

The Assertions in All Their Blessed Glory

Basic Fail and Ignore

基礎Fail和Ignore

TEST_FAIL()

This fella is most often used in special conditions where your test code is

performing logic beyond a simple assertion. That is, in practice,

TEST_FAIL()

will always be found inside a conditional code block.

這家夥最常用于一種特殊狀況,即你的測試代碼在一個簡單的斷言上執行邏輯。就是說,

實踐中,

TEST_FAIL()

總是會出現在一個條件代碼塊中。

Examples:

  • Executing a state machine multiple times that increments a counter your test

    code then verifies as a final step.

  • Triggering an exception and verifying it (as in Try / Catch / Throw - see the

    CException project).

示例:

  • 多次執行一個遞增一個計數器的狀态機,然後你的測試代碼驗證其為最後一步
  • 觸發一個異常并驗證它(就像在Try / Catch / Throw中,見CException項目)。

TEST_IGNORE()

Marks a test case (i.e. function meant to contain test assertions) as ignored.

Usually this is employed as a breadcrumb to come back and implement a test case.

An ignored test case has effects if other assertions are in the enclosing test

case (see Unity documentation for more).

标記一個測試用例(即應該包含測試斷言的函數)為忽視。通常這被用于标記未來要實作的一個

測試用例。如果其他斷言在一個封裝的測試用例中,一個被忽視的測試用例是有用的(詳見Unity

文檔)。

Boolean

布爾值

TEST_ASSERT (condition)

TEST_ASSERT_TRUE (condition)

TEST_ASSERT_FALSE (condition)

TEST_ASSERT_UNLESS (condition)

A simple wording variation on

TEST_ASSERT_FALSE

.The semantics of

TEST_ASSERT_UNLESS

aid readability in certain test constructions or

conditional statements.

隻是

TEST_ASSERT_FALSE

的措辭的一些變化。

TEST_ASSERT_UNLESS

在特定的條件語句或

測試結構中有助于增加語義上的可讀性。

TEST_ASSERT_NULL (pointer)

TEST_ASSERT_NOT_NULL (pointer)

Signed and Unsigned Integers (of all sizes)

(所有大小的)有符号和無符号整型

Large integer sizes can be disabled for build targets that do not support them.

For example, if your target only supports up to 16 bit types, by defining the

appropriate symbols Unity can be configured to omit 32 and 64 bit operations

that would break compilation (see Unity documentation for more). Refer to

Advanced Asserting later in this document for advice on dealing with other word

sizes.

對于不支援大整型的建構目标,可以禁用大整型。比如,如果你的目标最多支援16位類型,

通過定義适當的符号,Unity可以配置為隐藏32和64位操作,這樣就可以編譯通過了(詳見

Unity文檔)。參考這篇文檔後面的進階斷言以擷取怎麼處理其他大小的建議。

TEST_ASSERT_EQUAL_INT (expected, actual)

TEST_ASSERT_EQUAL_INT8 (expected, actual)

TEST_ASSERT_EQUAL_INT16 (expected, actual)

TEST_ASSERT_EQUAL_INT32 (expected, actual)

TEST_ASSERT_EQUAL_INT64 (expected, actual)

TEST_ASSERT_EQUAL (expected, actual)

TEST_ASSERT_NOT_EQUAL (expected, actual)

TEST_ASSERT_EQUAL_UINT (expected, actual)

TEST_ASSERT_EQUAL_UINT8 (expected, actual)

TEST_ASSERT_EQUAL_UINT16 (expected, actual)

TEST_ASSERT_EQUAL_UINT32 (expected, actual)

TEST_ASSERT_EQUAL_UINT64 (expected, actual)

Unsigned Integers (of all sizes) in Hexadecimal

(所有大小的)十六進制無符号整型

All

_HEX

assertions are identical in function to unsigned integer assertions

but produce failure messages with the

expected

and

actual

values formatted

in hexadecimal. Unity output is big endian.

所有函數名中帶有的

_HEX

的斷言都是無符号整型斷言,它們産生的

expected

actual

值都是十六進制格式的。Unity輸出是大端的。

TEST_ASSERT_EQUAL_HEX (expected, actual)

TEST_ASSERT_EQUAL_HEX8 (expected, actual)

TEST_ASSERT_EQUAL_HEX16 (expected, actual)

TEST_ASSERT_EQUAL_HEX32 (expected, actual)

TEST_ASSERT_EQUAL_HEX64 (expected, actual)

Masked and Bit-level Assertions

掩碼及位等級的斷言

Masked and bit-level assertions produce output formatted in hexadecimal. Unity

output is big endian.

掩碼及位等級的斷言産生十六進制格式的輸出。Unity輸出是大端的。

TEST_ASSERT_BITS (mask, expected, actual)

Only compares the masked (i.e. high) bits of

expected

and

actual

parameters.

僅比較

expected

actual

參數的掩碼(即1)位。

TEST_ASSERT_BITS_HIGH (mask, actual)

Asserts the masked bits of the

actual

parameter are high.

斷言

actual

參數的掩碼位為1。

TEST_ASSERT_BITS_LOW (mask, actual)

Asserts the masked bits of the

actual

parameter are low.

斷言

actual

參數的掩碼為為0。

TEST_ASSERT_BIT_HIGH (bit, actual)

Asserts the specified bit of the

actual

parameter is high.

斷言

actual

參數的指定位為1。

TEST_ASSERT_BIT_LOW (bit, actual)

Asserts the specified bit of the

actual

parameter is low.

斷言

actual

參數的指定位為0。

Integer Less Than / Greater Than

整數小于/大于

These assertions verify that the

actual

parameter is less than or greater

than

threshold

(exclusive). For example, if the threshold value is 0 for the

greater than assertion will fail if it is 0 or less.

這些斷言驗證

actual

參數比

threshold

(不含)小或大。比如,如果門檻值為0,對于大于

的那個,斷言在它為0或更小值時會失敗。

TEST_ASSERT_GREATER_THAN (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_INT32 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_UINT32 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX8 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX16 (threshold, actual)

TEST_ASSERT_GREATER_THAN_HEX32 (threshold, actual)

TEST_ASSERT_LESS_THAN (threshold, actual)

TEST_ASSERT_LESS_THAN_INT (threshold, actual)

TEST_ASSERT_LESS_THAN_INT8 (threshold, actual)

TEST_ASSERT_LESS_THAN_INT16 (threshold, actual)

TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT8 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT16 (threshold, actual)

TEST_ASSERT_LESS_THAN_UINT32 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX8 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX16 (threshold, actual)

TEST_ASSERT_LESS_THAN_HEX32 (threshold, actual)

Integer Ranges (of all sizes)

(所有大小的)整型範圍

These assertions verify that the

expected

parameter is within +/-

delta

(inclusive) of the

actual

parameter. For example, if the expected value is 10

and the delta is 3 then the assertion will fail for any value outside the range

of 7 - 13.

這些斷言驗證

expected

參數處于

actual

參數的+/-

delta

(含)之間。比如,如果期望值

是10,delta值是3,那如果給定值在7 - 13之外的話斷言就會失敗。

TEST_ASSERT_INT_WITHIN (delta, expected, actual)

TEST_ASSERT_INT8_WITHIN (delta, expected, actual)

TEST_ASSERT_INT16_WITHIN (delta, expected, actual)

TEST_ASSERT_INT32_WITHIN (delta, expected, actual)

TEST_ASSERT_INT64_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)

TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)

TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)

Structs and Strings

結構體和字元串

TEST_ASSERT_EQUAL_PTR (expected, actual)

Asserts that the pointers point to the same memory location.

斷言指針指向同個記憶體位置。

TEST_ASSERT_EQUAL_STRING (expected, actual)

Asserts that the null terminated (

'\0'

)strings are identical. If strings are

of different lengths or any portion of the strings before their terminators

differ, the assertion fails. Two NULL strings (i.e. zero length) are considered

equivalent.

斷言以null結尾(

'\0'

)的字元串是一緻的。如果字元串的長度不一樣,或者在結束符前的

任何部分字元串不一樣,斷言就是失敗。兩個NULL字元串(即長度為0)被認為相等。

TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)

Asserts that the contents of the memory specified by the

expected

and

actual

pointers is identical. The size of the memory blocks in bytes is specified by

the

len

parameter.

斷言由

expected

指針和

actual

指針所指向的記憶體中的内容是一緻的。記憶體塊的位元組大小

len

參數指定。

Arrays

數組

expected

and

actual

parameters are both arrays.

num_elements

specifies the

number of elements in the arrays to compare.

expected

actual

參數都是數組。

num_elements

指定了數組中要比較的元素數量。

_HEX

assertions produce failure messages with expected and actual array

contents formatted in hexadecimal.

_HEX

斷言産生的失敗資訊會以十六進制列印expected和actual數組中的内容。

For array of strings comparison behavior, see comments for

TEST_ASSERT_EQUAL_STRING

in the preceding section.

至于字元串數組的比較行為,見前面章節中對

TEST_ASSERT_EQUAL_STRING

的注釋。

Assertions fail upon the first element in the compared arrays found not to

match. Failure messages specify the array index of the failed comparison.

要比較的數組中第一個不比對的元素會導緻斷言失敗。失敗資訊會說明比較失敗的索引。

TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)

TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)

len

is the memory in bytes to be compared at each array element.

len

是每個數組元素的位元組大小。

Each Equal (Arrays to Single Value)

(數組與單個值)分别相等

expected

are single values and

actual

are arrays.

num_elements

specifies

the number of elements in the arrays to compare.

expected

是單個值,

actual

是數組。

num_elements

指定數組中要比較的元素的個數。

_HEX

assertions produce failure messages with expected and actual array

contents formatted in hexadecimal.

_HEX

斷言産生的失敗資訊會以十六進制列印expected和actual數組中的内容。

Assertions fail upon the first element in the compared arrays found not to

match. Failure messages specify the array index of the failed comparison.

要比較的數組中第一個不比對的元素會導緻斷言失敗。失敗資訊會說明比較失敗的數組索引。

TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)

TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)

len

is the memory in bytes to be compared at each array element.

len

是每個數組元素的位元組大小。

Floating Point (If enabled)

單精度浮點數(如果啟用)

TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)

Asserts that the

actual

value is within +/-

delta

of the

expected

value.

The nature of floating point representation is such that exact evaluations of

equality are not guaranteed.

這些斷言驗證

actual

參數處于

expected

參數的+/-

delta

之間。浮點數表示法的特性

導緻沒法保證準确的比較。

TEST_ASSERT_EQUAL_FLOAT (expected, actual)

Asserts that the ?actual?value is “close enough to be considered equal” to the

expected

value. If you are curious about the details, refer to the Advanced

Asserting section for more details on this. Omitting a user-specified delta in a

floating point assertion is both a shorthand convenience and a requirement of

code generation conventions for CMock.

斷言

actual

值與

expected

值"接近到足以認為相等"。如果你對細節很好奇,詳見進階斷言

章節。在浮點數斷言中隐藏使用者指定的內插補點一方面是友善使用,另一方面也是CMock代碼生成

約定的要求。

TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)

See Array assertion section for details. Note that individual array element

float comparisons are executed using T?EST_ASSERT_EQUAL_FLOAT?.That is, user

specified delta comparison values requires a custom-implemented floating point

array assertion.

詳見 數組斷言 章節。注意,數組元素的單個單精度浮點數比較使用的是

TEST_ASSERT_EQUAL_FLOAT

就是說,使用者指定的內插補點比較值需要一個定制實作的單精度浮點數數組斷言。

TEST_ASSERT_FLOAT_IS_INF (actual)

Asserts that

actual

parameter is equivalent to positive infinity floating

point representation.

斷言

actual

參數等于正無窮的浮點數表示。

TEST_ASSERT_FLOAT_IS_NEG_INF (actual)

Asserts that

actual

parameter is equivalent to negative infinity floating

point representation.

斷言

actual

參數等于負無窮的浮點數表示。

TEST_ASSERT_FLOAT_IS_NAN (actual)

Asserts that

actual

parameter is a Not A Number floating point representation.

斷言

actual

參數是一個非數字浮點數表示。

TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)

Asserts that ?actual?parameter is a floating point representation usable for

mathematical operations. That is, the

actual

parameter is neither positive

infinity nor negative infinity nor Not A Number floating point representations.

斷言

actual

參數是一個可以用于數學運算的浮點數。就是說,

actual

參數既不是正無窮,

又不是負無窮,還不是非數字浮點數。

TEST_ASSERT_FLOAT_IS_NOT_INF (actual)

Asserts that

actual

parameter is a value other than positive infinity floating

point representation.

斷言

actual

參數是一個值,而不是正無窮。

TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)

Asserts that

actual

parameter is a value other than negative infinity floating

point representation.

斷言

actual

參數是一個值,而不是負無窮。

TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)

Asserts that

actual

parameter is a value other than Not A Number floating

point representation.

斷言

actual

參數是一個值,而不是非數字浮點數表示。

TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)

Asserts that

actual

parameter is not usable for mathematical operations. That

is, the

actual

parameter is either positive infinity or negative infinity or

Not A Number floating point representations.

斷言

actual

參數不可用于數學運算。就是說,

actual

參數不是正無窮,就是負無窮,

或是非數字浮點數。

Double (If enabled)

雙精度浮點數(如果啟用)

TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)

Asserts that the

actual

value is within +/-

delta

of the

expected

value.

The nature of floating point representation is such that exact evaluations of

equality are not guaranteed.

這些斷言驗證

actual

參數處于

expected

參數的+/-

delta

之間。浮點數表示法的特性

導緻沒法保證準确的比較。

TEST_ASSERT_EQUAL_DOUBLE (expected, actual)

Asserts that the

actual

value is “close enough to be considered equal” to the

expected

value. If you are curious about the details, refer to the Advanced

Asserting section for more details. Omitting a user-specified delta in a

floating point assertion is both a shorthand convenience and a requirement of

code generation conventions for CMock.

斷言

actual

值與

expected

值"接近到足以認為相等"。如果你對細節很好奇,詳見進階斷言

章節。在浮點數斷言中隐藏使用者指定的內插補點一方面是友善使用,另一方面也是CMock代碼生成

約定的要求。

TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)

See Array assertion section for details. Note that individual array element

double comparisons are executed using

TEST_ASSERT_EQUAL_DOUBLE

.That is, user

specified delta comparison values requires a custom implemented double array

assertion.

詳見 數組斷言 章節。注意,數組元素的單個雙精度浮點數比較使用的是

TEST_ASSERT_EQUAL_DOUBLE

就是說,使用者指定的內插補點比較值需要一個定制實作的雙精度浮點數數組斷言。

TEST_ASSERT_DOUBLE_IS_INF (actual)

Asserts that

actual

parameter is equivalent to positive infinity floating

point representation.

斷言

actual

參數等于正無窮的浮點數表示。

TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)

Asserts that

actual

parameter is equivalent to negative infinity floating point

representation.

斷言

actual

參數等于負無窮的浮點數表示。

TEST_ASSERT_DOUBLE_IS_NAN (actual)

Asserts that

actual

parameter is a Not A Number floating point representation.

斷言

actual

參數是一個非數字浮點數表示。

TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)

Asserts that

actual

parameter is a floating point representation usable for

mathematical operations. That is, the ?actual?parameter is neither positive

infinity nor negative infinity nor Not A Number floating point representations.

斷言

actual

參數是一個可以用于數學運算的浮點數。就是說,

actual

參數既不是正無窮,

又不是負無窮,還不是非數字浮點數。

TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)

Asserts that

actual

parameter is a value other than positive infinity floating

point representation.

斷言

actual

參數是一個值,而不是正無窮。

TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)

Asserts that

actual

parameter is a value other than negative infinity floating

point representation.

斷言

actual

參數是一個值,而不是負無窮。

TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)

Asserts that

actual

parameter is a value other than Not A Number floating

point representation.

斷言

actual

參數是一個值,而不是非數字浮點數表示。

TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)

Asserts that

actual

parameter is not usable for mathematical operations. That

is, the

actual

parameter is either positive infinity or negative infinity or

Not A Number floating point representations.

斷言

actual

參數不可用于數學運算。就是說,

actual

參數不是正無窮,就是負無窮,

或是非數字浮點數。

Advanced Asserting: Details On Tricky Assertions

進階斷言:那些巧妙的斷言中的細節

This section helps you understand how to deal with some of the trickier

assertion situations you may run into. It will give you a glimpse into some of

the under-the-hood details of Unity’s assertion mechanisms. If you’re one of

those people who likes to know what is going on in the background, read on. If

not, feel free to ignore the rest of this document until you need it.

這個章節幫助你了解怎麼處理一些你可能會遇到的更有技巧性的斷言。帶你瞥一眼Unity的

斷言機制引擎下的一些細節。如果你是那種喜歡刨根知底的人,那這章節很适合你。如果不

是的話,當你需要的時候再來讀這個章節吧。

How do the EQUAL assertions work for FLOAT and DOUBLE?

EQUAL斷言是怎麼處理單精度和雙精度浮點數的?

As you may know, directly checking for equality between a pair of floats or a

pair of doubles is sloppy at best and an outright no-no at worst. Floating point

values can often be represented in multiple ways, particularly after a series of

operations on a value. Initializing a variable to the value of 2.0 is likely to

result in a floating point representation of 2 x 20,but a series of

mathematical operations might result in a representation of 8 x 2-2

that also evaluates to a value of 2. At some point repeated operations cause

equality checks to fail.

如你也許知道的,直接檢查一對浮點數相等實在太扯淡了,簡直是zz。浮點數值常能以多種

方式表示,特别是對一個值進行一系列運算後。初始化一個變量為2.0很可能導緻浮點數表示

法記為 2 x 20,但是一系列數學運算後可能導緻記為 8 x 2-2,這也表示2這個值。有時候

重複的運算會導緻相等運算失敗。

So Unity doesn’t do direct floating point comparisons for equality. Instead, it

checks if two floating point values are “really close.” If you leave Unity

running with defaults, “really close” means “within a significant bit or two.”

Under the hood,

TEST_ASSERT_EQUAL_FLOAT

is really

TEST_ASSERT_FLOAT_WITHIN

with the

delta

parameter calculated on the fly. For single precision, delta is

the expected value multiplied by 0.00001, producing a very small proportional

range around the expected value.

是以Unity不直接比較浮點數相等。它會檢查兩個浮點數是否"十分接近"。如果你讓Unity使

用預設配置,“十分接近"等價于"在一個或兩個有效bit内”。在底層,

TEST_ASSERT_EQUAL_FLOAT

實際上是

delta

參數在運作時計算的

TEST_ASSERT_FLOAT_WITHIN

。對于單精度,delta是

期望值乘以0.00001,則會産生在期望值周圍一個很小比例的範圍。

If you are expecting a value of 20,000.0 the delta is calculated to be 0.2. So

any value between 19,999.8 and 20,000.2 will satisfy the equality check. This

works out to be roughly a single bit of range for a single-precision number, and

that’s just about as tight a tolerance as you can reasonably get from a floating

point value.

如果你期望的值為20,000.0,delta則算出來是0.2。是以任何19,999.8與20,000.2之間的值

都能通過相等檢查。實踐證明這大緻是單精度浮點數單個bit的範圍,

So what happens when it’s zero? Zero - even more than other floating point

values - can be represented many different ways. It doesn’t matter if you have

0 x 20 or 0 x 263.It’s still zero, right? Luckily, if you

subtract these values from each other, they will always produce a difference of

zero, which will still fall between 0 plus or minus a delta of 0. So it still

works!

是以當是0的時候怎麼辦呢? 0 - 可以用許多種方式表示 - 甚至比其他浮點值還多的方式。

不管是0 x 20 還是 0 x 263,它都是0,對吧?幸運地是,如果你用任意兩個0相減,內插補點

都還是0,它仍然會落在0加減一個內插補點的範圍内。是以這個方法仍然有用!

Double precision floating point numbers use a much smaller multiplier, again

approximating a single bit of error.

雙精度浮點數使用一個小得多的乘數,同樣大約一個bit的誤差。

If you don’t like these ranges and you want to make your floating point equality

assertions less strict, you can change these multipliers to whatever you like by

defining UNITY_FLOAT_PRECISION and UNITY_DOUBLE_PRECISION. See Unity

documentation for more.

如果你不喜歡這個範圍,想要讓你的浮點數相等斷言不那麼嚴格,你可以修改這些乘數,定義

UNITY_FLOAT_PRECISION和UNITY_DOUBLE_PRECISION的值就行。詳見Unity文檔。

How do we deal with targets with non-standard int sizes?

怎麼處理整型大小非标準的目标呢?

It’s “fun” that C is a standard where something as fundamental as an integer

varies by target. According to the C standard, an

int

is to be the target’s

natural register size, and it should be at least 16-bits and a multiple of a

byte. It also guarantees an order of sizes:

"搞笑"的是,在C這個标準中,如整型這麼基礎的東西居然會根據目标平台而變化。根據C的标準,

一個

int

的大小是目标的自然寄存器大小,并且至少16位且為位元組的整數倍。它還保證

以下大小排序:

char <= short <= int <= long <= long long
           

Most often,

int

is 32-bits. In many cases in the embedded world,

int

is

16-bits. There are rare microcontrollers out there that have 24-bit integers,

and this remains perfectly standard C.

int

最常見的是32位。在嵌入式中,大部分情況下

int

是16位。極少微控制器是24位

整型,但它仍然完美滿足标準C。

To make things even more interesting, there are compilers and targets out there

that have a hard choice to make. What if their natural register size is 10-bits

or 12-bits? Clearly they can’t fulfill both the requirement to be at least

16-bits AND the requirement to match the natural register size. In these

situations, they often choose the natural register size, leaving us with

something like this:

讓事情更有趣的是,有一些編譯器和目标遇到了更困難的選擇。如果自然寄存器的大小是

10位或12位的話怎麼辦?顯然它們無法同時滿足至少16位以及比對自然寄存器大小這 兩個 條件。

在這些情況下,它們通常選擇自然寄存器大小,然後就變成了這樣:

char (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit)
           

Um… yikes. It’s obviously breaking a rule or two… but they had to break SOME

rules, so they made a choice.

啊…哈。這明顯違背了一兩個規則…但是它們不得不違背一些規則,是以它們做了個選擇。

When the C99 standard rolled around, it introduced alternate standard-size types.

It also introduced macros for pulling in MIN/MAX values for your integer types.

It’s glorious! Unfortunately, many embedded compilers can’t be relied upon to

use the C99 types (Sometimes because they have weird register sizes as described

above. Sometimes because they don’t feel like it?).

當C99标準出現時,它引入了多種标準大小類型。它還引入了用于拉取整型最大/最小值的宏。

這太贊了!不幸的是,許多嵌入式編譯器并沒有使用C99類型(有時是因為它們遇到了如上描述

的奇怪的寄存器大小。有時是因為它們不喜歡它?)。

A goal of Unity from the beginning was to support every combination of

microcontroller or microprocessor and C compiler. Over time, we’ve gotten really

close to this. There are a few tricks that you should be aware of, though, if

you’re going to do this effectively on some of these more idiosyncratic targets.

Unity的目标從一開始就是要支援每個微控制器或微處理器與C編譯器的組合。随着時間的流逝,

我們真的很接近這個目标呢。但是如果你想要在一些更古怪的目标上高效地使用Unity的話,

有一些你需要意識到的竅門。

First, when setting up Unity for a new target, you’re going to want to pay

special attention to the macros for automatically detecting types

(where available) or manually configuring them yourself. You can get information

on both of these in Unity’s documentation.

首先,當将Unity配置到一個新目标上時,你應該特别關注下用于自動檢測類型的(可用)宏,

或手動配置它們。你可以在Unity的文檔中獲得相關資訊。

What about the times where you suddenly need to deal with something odd, like a

24-bit

int

? The simplest solution is to use the next size up. If you have a

24-bit

int

, configure Unity to use 32-bit integers. If you have a 12-bit

int

, configure Unity to use 16 bits. There are two ways this is going to

affect you:

當你突然需要處理一些怪異的東西,如24位

int

時怎麼辦呢?最簡單的解決方案是使用大一号

的尺寸。如果你有24位的

int

,将Unity配置為使用32位的整型。如果你有一個12位的

int

配置Unity使用16位的。注意以下兩個事情:

  1. When Unity displays errors for you, it’s going to pad the upper unused bits

    with zeros.

  2. You’re going to have to be careful of assertions that perform signed

    operations, particularly

    TEST_ASSERT_INT_WITHIN

    .Such assertions might wrap

    your

    int

    in the wrong place, and you could experience false failures. You can

    always back down to a simple

    TEST_ASSERT

    and do the operations yourself.
  3. 當Unity提示失敗資訊時,它會往高位未使用bit中填充0。
  4. 你要當心執行有符号的運算的斷言,特别是

    TEST_ASSERT_INT_WITHIN

    。這樣的斷言可能會

    錯用你的

    int

    ,可能你會遇到錯誤的失敗。你總可以回歸最簡單的

    TEST_ASSERT

    并自己做運算。

Find The Latest of This And More at ThrowTheSwitch.org