You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

787 lines
30 KiB

11 months ago
  1. # Unity Assertions Reference
  2. ## Background and Overview
  3. ### Super Condensed Version
  4. - An assertion establishes truth (i.e. boolean True) for a single condition.
  5. Upon boolean False, an assertion stops execution and reports the failure.
  6. - Unity is mainly a rich collection of assertions and the support to gather up
  7. and easily execute those assertions.
  8. - The structure of Unity allows you to easily separate test assertions from
  9. source code in, well, test code.
  10. - Unity's assertions:
  11. - Come in many, many flavors to handle different C types and assertion cases.
  12. - Use context to provide detailed and helpful failure messages.
  13. - Document types, expected values, and basic behavior in your source code for
  14. free.
  15. ### Unity Is Several Things But Mainly It's Assertions
  16. One way to think of Unity is simply as a rich collection of assertions you can
  17. use to establish whether your source code behaves the way you think it does.
  18. Unity provides a framework to easily organize and execute those assertions in
  19. test code separate from your source code.
  20. ### What's an Assertion?
  21. At their core, assertions are an establishment of truth - boolean truth. Was this
  22. thing equal to that thing? Does that code doohickey have such-and-such property
  23. or not? You get the idea. Assertions are executable code (to appreciate the big
  24. picture on this read up on the difference between
  25. [link:Dynamic Verification and Static Analysis]). A failing assertion stops
  26. execution and reports an error through some appropriate I/O channel (e.g.
  27. stdout, GUI, file, blinky light).
  28. Fundamentally, for dynamic verification all you need is a single assertion
  29. mechanism. In fact, that's what the [assert() macro][] in C's standard library
  30. is for. So why not just use it? Well, we can do far better in the reporting
  31. department. C's `assert()` is pretty dumb as-is and is particularly poor for
  32. handling common data types like arrays, structs, etc. And, without some other
  33. support, it's far too tempting to litter source code with C's `assert()`'s. It's
  34. generally much cleaner, manageable, and more useful to separate test and source
  35. code in the way Unity facilitates.
  36. ### Unity's Assertions: Helpful Messages _and_ Free Source Code Documentation
  37. Asserting a simple truth condition is valuable, but using the context of the
  38. assertion is even more valuable. For instance, if you know you're comparing bit
  39. flags and not just integers, then why not use that context to give explicit,
  40. readable, bit-level feedback when an assertion fails?
  41. That's what Unity's collection of assertions do - capture context to give you
  42. helpful, meaningful assertion failure messages. In fact, the assertions
  43. themselves also serve as executable documentation about types and values in your
  44. source code. So long as your tests remain current with your source and all those
  45. tests pass, you have a detailed, up-to-date view of the intent and mechanisms in
  46. your source code. And due to a wondrous mystery, well-tested code usually tends
  47. to be well designed code.
  48. ## Assertion Conventions and Configurations
  49. ### Naming and Parameter Conventions
  50. The convention of assertion parameters generally follows this order:
  51. ```c
  52. TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
  53. ```
  54. The very simplest assertion possible uses only a single `actual` parameter (e.g.
  55. a simple null check).
  56. - `Actual` is the value being tested and unlike the other parameters in an
  57. assertion construction is the only parameter present in all assertion variants.
  58. - `Modifiers` are masks, ranges, bit flag specifiers, floating point deltas.
  59. - `Expected` is your expected value (duh) to compare to an `actual` value; it's
  60. marked as an optional parameter because some assertions only need a single
  61. `actual` parameter (e.g. null check).
  62. - `Size/count` refers to string lengths, number of array elements, etc.
  63. Many of Unity's assertions are clear duplications in that the same data type
  64. is handled by several assertions. The differences among these are in how failure
  65. messages are presented. For instance, a `_HEX` variant of an assertion prints
  66. the expected and actual values of that assertion formatted as hexadecimal.
  67. #### TEST_ASSERT_X_MESSAGE Variants
  68. _All_ assertions are complemented with a variant that includes a simple string
  69. message as a final parameter. The string you specify is appended to an assertion
  70. failure message in Unity output.
  71. For brevity, the assertion variants with a message parameter are not listed
  72. below. Just tack on `_MESSAGE` as the final component to any assertion name in
  73. the reference list below and add a string as the final parameter.
  74. _Example:_
  75. ```c
  76. TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
  77. ```
  78. becomes messageified like thus...
  79. ```c
  80. TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
  81. ```
  82. Notes:
  83. - The `_MESSAGE` variants intentionally do not support `printf` style formatting
  84. since many embedded projects don't support or avoid `printf` for various reasons.
  85. It is possible to use `sprintf` before the assertion to assemble a complex fail
  86. message, if necessary.
  87. - If you want to output a counter value within an assertion fail message (e.g. from
  88. a loop) , building up an array of results and then using one of the `_ARRAY`
  89. assertions (see below) might be a handy alternative to `sprintf`.
  90. #### TEST_ASSERT_X_ARRAY Variants
  91. Unity provides a collection of assertions for arrays containing a variety of
  92. types. These are documented in the Array section below. These are almost on par
  93. with the `_MESSAGE`variants of Unity's Asserts in that for pretty much any Unity
  94. type assertion you can tack on `_ARRAY` and run assertions on an entire block of
  95. memory.
  96. ```c
  97. TEST_ASSERT_EQUAL_TYPEX_ARRAY( expected, actual, {size/count} )
  98. ```
  99. - `Expected` is an array itself.
  100. - `Size/count` is one or two parameters necessary to establish the number of array
  101. elements and perhaps the length of elements within the array.
  102. Notes:
  103. - The `_MESSAGE` variant convention still applies here to array assertions. The
  104. `_MESSAGE` variants of the `_ARRAY` assertions have names ending with
  105. `_ARRAY_MESSAGE`.
  106. - Assertions for handling arrays of floating point values are grouped with float
  107. and double assertions (see immediately following section).
  108. ### TEST_ASSERT_EACH_EQUAL_X Variants
  109. Unity provides a collection of assertions for arrays containing a variety of
  110. types which can be compared to a single value as well. These are documented in
  111. the Each Equal section below. these are almost on par with the `_MESSAGE`
  112. variants of Unity's Asserts in that for pretty much any Unity type assertion you
  113. can inject `_EACH_EQUAL` and run assertions on an entire block of memory.
  114. ```c
  115. TEST_ASSERT_EACH_EQUAL_TYPEX( expected, actual, {size/count} )
  116. ```
  117. - `Expected` is a single value to compare to.
  118. - `Actual` is an array where each element will be compared to the expected value.
  119. - `Size/count` is one of two parameters necessary to establish the number of array
  120. elements and perhaps the length of elements within the array.
  121. Notes:
  122. - The `_MESSAGE` variant convention still applies here to Each Equal assertions.
  123. - Assertions for handling Each Equal of floating point values are grouped with
  124. float and double assertions (see immediately following section).
  125. ### Configuration
  126. #### Floating Point Support Is Optional
  127. Support for floating point types is configurable. That is, by defining the
  128. appropriate preprocessor symbols, floats and doubles can be individually enabled
  129. or disabled in Unity code. This is useful for embedded targets with no floating
  130. point math support (i.e. Unity compiles free of errors for fixed point only
  131. platforms). See Unity documentation for specifics.
  132. #### Maximum Data Type Width Is Configurable
  133. Not all targets support 64 bit wide types or even 32 bit wide types. Define the
  134. appropriate preprocessor symbols and Unity will omit all operations from
  135. compilation that exceed the maximum width of your target. See Unity
  136. documentation for specifics.
  137. ## The Assertions in All Their Blessed Glory
  138. ### Basic Fail, Pass and Ignore
  139. #### `TEST_FAIL()`
  140. #### `TEST_FAIL_MESSAGE("message")`
  141. This fella is most often used in special conditions where your test code is
  142. performing logic beyond a simple assertion. That is, in practice, `TEST_FAIL()`
  143. will always be found inside a conditional code block.
  144. _Examples:_
  145. - Executing a state machine multiple times that increments a counter your test
  146. code then verifies as a final step.
  147. - Triggering an exception and verifying it (as in Try / Catch / Throw - see the
  148. [CException](https://github.com/ThrowTheSwitch/CException) project).
  149. #### `TEST_PASS()`
  150. #### `TEST_PASS_MESSAGE("message")`
  151. This will abort the remainder of the test, but count the test as a pass. Under
  152. normal circumstances, it is not necessary to include this macro in your tests...
  153. a lack of failure will automatically be counted as a `PASS`. It is occasionally
  154. useful for tests with `#ifdef`s and such.
  155. #### `TEST_IGNORE()`
  156. #### `TEST_IGNORE_MESSAGE("message")`
  157. Marks a test case (i.e. function meant to contain test assertions) as ignored.
  158. Usually this is employed as a breadcrumb to come back and implement a test case.
  159. An ignored test case has effects if other assertions are in the enclosing test
  160. case (see Unity documentation for more).
  161. #### `TEST_MESSAGE(message)`
  162. This can be useful for outputting `INFO` messages into the Unity output stream
  163. without actually ending the test. Like pass and fail messages, it will be output
  164. with the filename and line number.
  165. ### Boolean
  166. #### `TEST_ASSERT (condition)`
  167. #### `TEST_ASSERT_TRUE (condition)`
  168. #### `TEST_ASSERT_FALSE (condition)`
  169. #### `TEST_ASSERT_UNLESS (condition)`
  170. A simple wording variation on `TEST_ASSERT_FALSE`.The semantics of
  171. `TEST_ASSERT_UNLESS` aid readability in certain test constructions or
  172. conditional statements.
  173. #### `TEST_ASSERT_NULL (pointer)`
  174. #### `TEST_ASSERT_NOT_NULL (pointer)`
  175. Verify if a pointer is or is not NULL.
  176. #### `TEST_ASSERT_EMPTY (pointer)`
  177. #### `TEST_ASSERT_NOT_EMPTY (pointer)`
  178. Verify if the first element dereferenced from a pointer is or is not zero. This
  179. is particularly useful for checking for empty (or non-empty) null-terminated
  180. C strings, but can be just as easily used for other null-terminated arrays.
  181. ### Signed and Unsigned Integers (of all sizes)
  182. Large integer sizes can be disabled for build targets that do not support them.
  183. For example, if your target only supports up to 16 bit types, by defining the
  184. appropriate symbols Unity can be configured to omit 32 and 64 bit operations
  185. that would break compilation (see Unity documentation for more). Refer to
  186. Advanced Asserting later in this document for advice on dealing with other word
  187. sizes.
  188. #### `TEST_ASSERT_EQUAL_INT (expected, actual)`
  189. #### `TEST_ASSERT_EQUAL_INT8 (expected, actual)`
  190. #### `TEST_ASSERT_EQUAL_INT16 (expected, actual)`
  191. #### `TEST_ASSERT_EQUAL_INT32 (expected, actual)`
  192. #### `TEST_ASSERT_EQUAL_INT64 (expected, actual)`
  193. #### `TEST_ASSERT_EQUAL_UINT (expected, actual)`
  194. #### `TEST_ASSERT_EQUAL_UINT8 (expected, actual)`
  195. #### `TEST_ASSERT_EQUAL_UINT16 (expected, actual)`
  196. #### `TEST_ASSERT_EQUAL_UINT32 (expected, actual)`
  197. #### `TEST_ASSERT_EQUAL_UINT64 (expected, actual)`
  198. ### Unsigned Integers (of all sizes) in Hexadecimal
  199. All `_HEX` assertions are identical in function to unsigned integer assertions
  200. but produce failure messages with the `expected` and `actual` values formatted
  201. in hexadecimal. Unity output is big endian.
  202. #### `TEST_ASSERT_EQUAL_HEX (expected, actual)`
  203. #### `TEST_ASSERT_EQUAL_HEX8 (expected, actual)`
  204. #### `TEST_ASSERT_EQUAL_HEX16 (expected, actual)`
  205. #### `TEST_ASSERT_EQUAL_HEX32 (expected, actual)`
  206. #### `TEST_ASSERT_EQUAL_HEX64 (expected, actual)`
  207. ### Characters
  208. While you can use the 8-bit integer assertions to compare `char`, another option is
  209. to use this specialized assertion which will show printable characters as printables,
  210. otherwise showing the HEX escape code for the characters.
  211. #### `TEST_ASSERT_EQUAL_CHAR (expected, actual)`
  212. ### Masked and Bit-level Assertions
  213. Masked and bit-level assertions produce output formatted in hexadecimal. Unity
  214. output is big endian.
  215. #### `TEST_ASSERT_BITS (mask, expected, actual)`
  216. Only compares the masked (i.e. high) bits of `expected` and `actual` parameters.
  217. #### `TEST_ASSERT_BITS_HIGH (mask, actual)`
  218. Asserts the masked bits of the `actual` parameter are high.
  219. #### `TEST_ASSERT_BITS_LOW (mask, actual)`
  220. Asserts the masked bits of the `actual` parameter are low.
  221. #### `TEST_ASSERT_BIT_HIGH (bit, actual)`
  222. Asserts the specified bit of the `actual` parameter is high.
  223. #### `TEST_ASSERT_BIT_LOW (bit, actual)`
  224. Asserts the specified bit of the `actual` parameter is low.
  225. ### Integer Less Than / Greater Than
  226. These assertions verify that the `actual` parameter is less than or greater
  227. than `threshold` (exclusive). For example, if the threshold value is 0 for the
  228. greater than assertion will fail if it is 0 or less. There are assertions for
  229. all the various sizes of ints, as for the equality assertions. Some examples:
  230. #### `TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)`
  231. #### `TEST_ASSERT_GREATER_OR_EQUAL_INT16 (threshold, actual)`
  232. #### `TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)`
  233. #### `TEST_ASSERT_LESS_OR_EQUAL_UINT (threshold, actual)`
  234. #### `TEST_ASSERT_NOT_EQUAL_UINT8 (threshold, actual)`
  235. ### Integer Ranges (of all sizes)
  236. These assertions verify that the `expected` parameter is within +/- `delta`
  237. (inclusive) of the `actual` parameter. For example, if the expected value is 10
  238. and the delta is 3 then the assertion will fail for any value outside the range
  239. of 7 - 13.
  240. #### `TEST_ASSERT_INT_WITHIN (delta, expected, actual)`
  241. #### `TEST_ASSERT_INT8_WITHIN (delta, expected, actual)`
  242. #### `TEST_ASSERT_INT16_WITHIN (delta, expected, actual)`
  243. #### `TEST_ASSERT_INT32_WITHIN (delta, expected, actual)`
  244. #### `TEST_ASSERT_INT64_WITHIN (delta, expected, actual)`
  245. #### `TEST_ASSERT_UINT_WITHIN (delta, expected, actual)`
  246. #### `TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)`
  247. #### `TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)`
  248. #### `TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)`
  249. #### `TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)`
  250. #### `TEST_ASSERT_HEX_WITHIN (delta, expected, actual)`
  251. #### `TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)`
  252. #### `TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)`
  253. #### `TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)`
  254. #### `TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)`
  255. #### `TEST_ASSERT_CHAR_WITHIN (delta, expected, actual)`
  256. ### Structs and Strings
  257. #### `TEST_ASSERT_EQUAL_PTR (expected, actual)`
  258. Asserts that the pointers point to the same memory location.
  259. #### `TEST_ASSERT_EQUAL_STRING (expected, actual)`
  260. Asserts that the null terminated (`'\0'`)strings are identical. If strings are
  261. of different lengths or any portion of the strings before their terminators
  262. differ, the assertion fails. Two NULL strings (i.e. zero length) are considered
  263. equivalent.
  264. #### `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`
  265. Asserts that the contents of the memory specified by the `expected` and `actual`
  266. pointers is identical. The size of the memory blocks in bytes is specified by
  267. the `len` parameter.
  268. ### Arrays
  269. `expected` and `actual` parameters are both arrays. `num_elements` specifies the
  270. number of elements in the arrays to compare.
  271. `_HEX` assertions produce failure messages with expected and actual array
  272. contents formatted in hexadecimal.
  273. For array of strings comparison behavior, see comments for
  274. `TEST_ASSERT_EQUAL_STRING` in the preceding section.
  275. Assertions fail upon the first element in the compared arrays found not to
  276. match. Failure messages specify the array index of the failed comparison.
  277. #### `TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)`
  278. #### `TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)`
  279. #### `TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)`
  280. #### `TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)`
  281. #### `TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)`
  282. #### `TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)`
  283. #### `TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)`
  284. #### `TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)`
  285. #### `TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)`
  286. #### `TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)`
  287. #### `TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)`
  288. #### `TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)`
  289. #### `TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)`
  290. #### `TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)`
  291. #### `TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)`
  292. #### `TEST_ASSERT_EQUAL_CHAR_ARRAY (expected, actual, num_elements)`
  293. #### `TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)`
  294. #### `TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)`
  295. #### `TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)`
  296. `len` is the memory in bytes to be compared at each array element.
  297. ### Integer Array Ranges (of all sizes)
  298. These assertions verify that the `expected` array parameter is within +/- `delta`
  299. (inclusive) of the `actual` array parameter. For example, if the expected value is
  300. \[10, 12\] and the delta is 3 then the assertion will fail for any value
  301. outside the range of \[7 - 13, 9 - 15\].
  302. #### `TEST_ASSERT_INT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  303. #### `TEST_ASSERT_INT8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  304. #### `TEST_ASSERT_INT16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  305. #### `TEST_ASSERT_INT32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  306. #### `TEST_ASSERT_INT64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  307. #### `TEST_ASSERT_UINT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  308. #### `TEST_ASSERT_UINT8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  309. #### `TEST_ASSERT_UINT16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  310. #### `TEST_ASSERT_UINT32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  311. #### `TEST_ASSERT_UINT64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  312. #### `TEST_ASSERT_HEX_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  313. #### `TEST_ASSERT_HEX8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  314. #### `TEST_ASSERT_HEX16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  315. #### `TEST_ASSERT_HEX32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  316. #### `TEST_ASSERT_HEX64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  317. #### `TEST_ASSERT_CHAR_ARRAY_WITHIN (delta, expected, actual, num_elements)`
  318. ### Each Equal (Arrays to Single Value)
  319. `expected` are single values and `actual` are arrays. `num_elements` specifies
  320. the number of elements in the arrays to compare.
  321. `_HEX` assertions produce failure messages with expected and actual array
  322. contents formatted in hexadecimal.
  323. Assertions fail upon the first element in the compared arrays found not to
  324. match. Failure messages specify the array index of the failed comparison.
  325. #### `TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)`
  326. #### `TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)`
  327. #### `TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)`
  328. #### `TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)`
  329. #### `TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)`
  330. #### `TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)`
  331. #### `TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)`
  332. #### `TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)`
  333. #### `TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)`
  334. #### `TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)`
  335. #### `TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)`
  336. #### `TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)`
  337. #### `TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)`
  338. #### `TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)`
  339. #### `TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)`
  340. #### `TEST_ASSERT_EACH_EQUAL_CHAR (expected, actual, num_elements)`
  341. #### `TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)`
  342. #### `TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)`
  343. #### `TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)`
  344. `len` is the memory in bytes to be compared at each array element.
  345. ### Floating Point (If enabled)
  346. #### `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`
  347. Asserts that the `actual` value is within +/- `delta` of the `expected` value.
  348. The nature of floating point representation is such that exact evaluations of
  349. equality are not guaranteed.
  350. #### `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
  351. Asserts that the ?actual?value is "close enough to be considered equal" to the
  352. `expected` value. If you are curious about the details, refer to the Advanced
  353. Asserting section for more details on this. Omitting a user-specified delta in a
  354. floating point assertion is both a shorthand convenience and a requirement of
  355. code generation conventions for CMock.
  356. #### `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
  357. See Array assertion section for details. Note that individual array element
  358. float comparisons are executed using T?EST_ASSERT_EQUAL_FLOAT?.That is, user
  359. specified delta comparison values requires a custom-implemented floating point
  360. array assertion.
  361. #### `TEST_ASSERT_FLOAT_IS_INF (actual)`
  362. Asserts that `actual` parameter is equivalent to positive infinity floating
  363. point representation.
  364. #### `TEST_ASSERT_FLOAT_IS_NEG_INF (actual)`
  365. Asserts that `actual` parameter is equivalent to negative infinity floating
  366. point representation.
  367. #### `TEST_ASSERT_FLOAT_IS_NAN (actual)`
  368. Asserts that `actual` parameter is a Not A Number floating point representation.
  369. #### `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`
  370. Asserts that ?actual?parameter is a floating point representation usable for
  371. mathematical operations. That is, the `actual` parameter is neither positive
  372. infinity nor negative infinity nor Not A Number floating point representations.
  373. #### `TEST_ASSERT_FLOAT_IS_NOT_INF (actual)`
  374. Asserts that `actual` parameter is a value other than positive infinity floating
  375. point representation.
  376. #### `TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)`
  377. Asserts that `actual` parameter is a value other than negative infinity floating
  378. point representation.
  379. #### `TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)`
  380. Asserts that `actual` parameter is a value other than Not A Number floating
  381. point representation.
  382. #### `TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)`
  383. Asserts that `actual` parameter is not usable for mathematical operations. That
  384. is, the `actual` parameter is either positive infinity or negative infinity or
  385. Not A Number floating point representations.
  386. ### Double (If enabled)
  387. #### `TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)`
  388. Asserts that the `actual` value is within +/- `delta` of the `expected` value.
  389. The nature of floating point representation is such that exact evaluations of
  390. equality are not guaranteed.
  391. #### `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
  392. Asserts that the `actual` value is "close enough to be considered equal" to the
  393. `expected` value. If you are curious about the details, refer to the Advanced
  394. Asserting section for more details. Omitting a user-specified delta in a
  395. floating point assertion is both a shorthand convenience and a requirement of
  396. code generation conventions for CMock.
  397. #### `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
  398. See Array assertion section for details. Note that individual array element
  399. double comparisons are executed using `TEST_ASSERT_EQUAL_DOUBLE`.That is, user
  400. specified delta comparison values requires a custom implemented double array
  401. assertion.
  402. #### `TEST_ASSERT_DOUBLE_IS_INF (actual)`
  403. Asserts that `actual` parameter is equivalent to positive infinity floating
  404. point representation.
  405. #### `TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)`
  406. Asserts that `actual` parameter is equivalent to negative infinity floating point
  407. representation.
  408. #### `TEST_ASSERT_DOUBLE_IS_NAN (actual)`
  409. Asserts that `actual` parameter is a Not A Number floating point representation.
  410. #### `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`
  411. Asserts that `actual` parameter is a floating point representation usable for
  412. mathematical operations. That is, the ?actual?parameter is neither positive
  413. infinity nor negative infinity nor Not A Number floating point representations.
  414. #### `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
  415. Asserts that `actual` parameter is a value other than positive infinity floating
  416. point representation.
  417. #### `TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)`
  418. Asserts that `actual` parameter is a value other than negative infinity floating
  419. point representation.
  420. #### `TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)`
  421. Asserts that `actual` parameter is a value other than Not A Number floating
  422. point representation.
  423. #### `TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)`
  424. Asserts that `actual` parameter is not usable for mathematical operations. That
  425. is, the `actual` parameter is either positive infinity or negative infinity or
  426. Not A Number floating point representations.
  427. ## Advanced Asserting: Details On Tricky Assertions
  428. This section helps you understand how to deal with some of the trickier
  429. assertion situations you may run into. It will give you a glimpse into some of
  430. the under-the-hood details of Unity's assertion mechanisms. If you're one of
  431. those people who likes to know what is going on in the background, read on. If
  432. not, feel free to ignore the rest of this document until you need it.
  433. ### How do the EQUAL assertions work for FLOAT and DOUBLE?
  434. As you may know, directly checking for equality between a pair of floats or a
  435. pair of doubles is sloppy at best and an outright no-no at worst. Floating point
  436. values can often be represented in multiple ways, particularly after a series of
  437. operations on a value. Initializing a variable to the value of 2.0 is likely to
  438. result in a floating point representation of 2 x 20,but a series of
  439. mathematical operations might result in a representation of 8 x 2-2
  440. that also evaluates to a value of 2. At some point repeated operations cause
  441. equality checks to fail.
  442. So Unity doesn't do direct floating point comparisons for equality. Instead, it
  443. checks if two floating point values are "really close." If you leave Unity
  444. running with defaults, "really close" means "within a significant bit or two."
  445. Under the hood, `TEST_ASSERT_EQUAL_FLOAT` is really `TEST_ASSERT_FLOAT_WITHIN`
  446. with the `delta` parameter calculated on the fly. For single precision, delta is
  447. the expected value multiplied by 0.00001, producing a very small proportional
  448. range around the expected value.
  449. If you are expecting a value of 20,000.0 the delta is calculated to be 0.2. So
  450. any value between 19,999.8 and 20,000.2 will satisfy the equality check. This
  451. works out to be roughly a single bit of range for a single-precision number, and
  452. that's just about as tight a tolerance as you can reasonably get from a floating
  453. point value.
  454. So what happens when it's zero? Zero - even more than other floating point
  455. values - can be represented many different ways. It doesn't matter if you have
  456. 0 x 20 or 0 x 263.It's still zero, right? Luckily, if you
  457. subtract these values from each other, they will always produce a difference of
  458. zero, which will still fall between 0 plus or minus a delta of 0. So it still
  459. works!
  460. Double precision floating point numbers use a much smaller multiplier, again
  461. approximating a single bit of error.
  462. If you don't like these ranges and you want to make your floating point equality
  463. assertions less strict, you can change these multipliers to whatever you like by
  464. defining UNITY_FLOAT_PRECISION and UNITY_DOUBLE_PRECISION. See Unity
  465. documentation for more.
  466. ### How do we deal with targets with non-standard int sizes?
  467. It's "fun" that C is a standard where something as fundamental as an integer
  468. varies by target. According to the C standard, an `int` is to be the target's
  469. natural register size, and it should be at least 16-bits and a multiple of a
  470. byte. It also guarantees an order of sizes:
  471. ```C
  472. char <= short <= int <= long <= long long
  473. ```
  474. Most often, `int` is 32-bits. In many cases in the embedded world, `int` is
  475. 16-bits. There are rare microcontrollers out there that have 24-bit integers,
  476. and this remains perfectly standard C.
  477. To make things even more interesting, there are compilers and targets out there
  478. that have a hard choice to make. What if their natural register size is 10-bits
  479. or 12-bits? Clearly they can't fulfill _both_ the requirement to be at least
  480. 16-bits AND the requirement to match the natural register size. In these
  481. situations, they often choose the natural register size, leaving us with
  482. something like this:
  483. ```C
  484. char (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit)
  485. ```
  486. Um... yikes. It's obviously breaking a rule or two... but they had to break SOME
  487. rules, so they made a choice.
  488. When the C99 standard rolled around, it introduced alternate standard-size types.
  489. It also introduced macros for pulling in MIN/MAX values for your integer types.
  490. It's glorious! Unfortunately, many embedded compilers can't be relied upon to
  491. use the C99 types (Sometimes because they have weird register sizes as described
  492. above. Sometimes because they don't feel like it?).
  493. A goal of Unity from the beginning was to support every combination of
  494. microcontroller or microprocessor and C compiler. Over time, we've gotten really
  495. close to this. There are a few tricks that you should be aware of, though, if
  496. you're going to do this effectively on some of these more idiosyncratic targets.
  497. First, when setting up Unity for a new target, you're going to want to pay
  498. special attention to the macros for automatically detecting types
  499. (where available) or manually configuring them yourself. You can get information
  500. on both of these in Unity's documentation.
  501. What about the times where you suddenly need to deal with something odd, like a
  502. 24-bit `int`? The simplest solution is to use the next size up. If you have a
  503. 24-bit `int`, configure Unity to use 32-bit integers. If you have a 12-bit
  504. `int`, configure Unity to use 16 bits. There are two ways this is going to
  505. affect you:
  506. 1. When Unity displays errors for you, it's going to pad the upper unused bits
  507. with zeros.
  508. 2. You're going to have to be careful of assertions that perform signed
  509. operations, particularly `TEST_ASSERT_INT_WITHIN`.Such assertions might wrap
  510. your `int` in the wrong place, and you could experience false failures. You can
  511. always back down to a simple `TEST_ASSERT` and do the operations yourself.
  512. *Find The Latest of This And More at [ThrowTheSwitch.org][]*
  513. [assert() macro]: http://en.wikipedia.org/en/wiki/Assert.h
  514. [ThrowTheSwitch.org]: https://throwtheswitch.org