19750e409Sopenharmony_ci# Unity Assertions Reference
29750e409Sopenharmony_ci
39750e409Sopenharmony_ci## Background and Overview
49750e409Sopenharmony_ci
59750e409Sopenharmony_ci### Super Condensed Version
69750e409Sopenharmony_ci
79750e409Sopenharmony_ci- An assertion establishes truth (i.e. boolean True) for a single condition.
89750e409Sopenharmony_ciUpon boolean False, an assertion stops execution and reports the failure.
99750e409Sopenharmony_ci- Unity is mainly a rich collection of assertions and the support to gather up
109750e409Sopenharmony_ciand easily execute those assertions.
119750e409Sopenharmony_ci- The structure of Unity allows you to easily separate test assertions from
129750e409Sopenharmony_cisource code in, well, test code.
139750e409Sopenharmony_ci- Unity's assertions:
149750e409Sopenharmony_ci- Come in many, many flavors to handle different C types and assertion cases.
159750e409Sopenharmony_ci- Use context to provide detailed and helpful failure messages.
169750e409Sopenharmony_ci- Document types, expected values, and basic behavior in your source code for
179750e409Sopenharmony_cifree.
189750e409Sopenharmony_ci
199750e409Sopenharmony_ci
209750e409Sopenharmony_ci### Unity Is Several Things But Mainly It's Assertions
219750e409Sopenharmony_ci
229750e409Sopenharmony_ciOne way to think of Unity is simply as a rich collection of assertions you can
239750e409Sopenharmony_ciuse to establish whether your source code behaves the way you think it does.
249750e409Sopenharmony_ciUnity provides a framework to easily organize and execute those assertions in
259750e409Sopenharmony_citest code separate from your source code.
269750e409Sopenharmony_ci
279750e409Sopenharmony_ci
289750e409Sopenharmony_ci### What's an Assertion?
299750e409Sopenharmony_ci
309750e409Sopenharmony_ciAt their core, assertions are an establishment of truth - boolean truth. Was this
319750e409Sopenharmony_cithing equal to that thing? Does that code doohickey have such-and-such property
329750e409Sopenharmony_cior not? You get the idea. Assertions are executable code (to appreciate the big
339750e409Sopenharmony_cipicture on this read up on the difference between
349750e409Sopenharmony_ci[link:Dynamic Verification and Static Analysis]). A failing assertion stops
359750e409Sopenharmony_ciexecution and reports an error through some appropriate I/O channel (e.g.
369750e409Sopenharmony_cistdout, GUI, file, blinky light).
379750e409Sopenharmony_ci
389750e409Sopenharmony_ciFundamentally, for dynamic verification all you need is a single assertion
399750e409Sopenharmony_cimechanism. In fact, that's what the [assert() macro in C's standard library](http://en.wikipedia.org/en/wiki/Assert.h)
409750e409Sopenharmony_ciis for. So why not just use it? Well, we can do far better in the reporting
419750e409Sopenharmony_cidepartment. C's `assert()` is pretty dumb as-is and is particularly poor for
429750e409Sopenharmony_cihandling common data types like arrays, structs, etc. And, without some other
439750e409Sopenharmony_cisupport, it's far too tempting to litter source code with C's `assert()`'s. It's
449750e409Sopenharmony_cigenerally much cleaner, manageable, and more useful to separate test and source
459750e409Sopenharmony_cicode in the way Unity facilitates.
469750e409Sopenharmony_ci
479750e409Sopenharmony_ci
489750e409Sopenharmony_ci### Unity's Assertions: Helpful Messages _and_ Free Source Code Documentation
499750e409Sopenharmony_ci
509750e409Sopenharmony_ciAsserting a simple truth condition is valuable, but using the context of the
519750e409Sopenharmony_ciassertion is even more valuable. For instance, if you know you're comparing bit
529750e409Sopenharmony_ciflags and not just integers, then why not use that context to give explicit,
539750e409Sopenharmony_cireadable, bit-level feedback when an assertion fails?
549750e409Sopenharmony_ci
559750e409Sopenharmony_ciThat's what Unity's collection of assertions do - capture context to give you
569750e409Sopenharmony_cihelpful, meaningful assertion failure messages. In fact, the assertions
579750e409Sopenharmony_cithemselves also serve as executable documentation about types and values in your
589750e409Sopenharmony_cisource code. So long as your tests remain current with your source and all those
599750e409Sopenharmony_citests pass, you have a detailed, up-to-date view of the intent and mechanisms in
609750e409Sopenharmony_ciyour source code. And due to a wondrous mystery, well-tested code usually tends
619750e409Sopenharmony_cito be well designed code.
629750e409Sopenharmony_ci
639750e409Sopenharmony_ci
649750e409Sopenharmony_ci## Assertion Conventions and Configurations
659750e409Sopenharmony_ci
669750e409Sopenharmony_ci### Naming and Parameter Conventions
679750e409Sopenharmony_ci
689750e409Sopenharmony_ciThe convention of assertion parameters generally follows this order:
699750e409Sopenharmony_ci
709750e409Sopenharmony_ci    TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
719750e409Sopenharmony_ci
729750e409Sopenharmony_ciThe very simplest assertion possible uses only a single "actual" parameter (e.g.
739750e409Sopenharmony_cia simple null check).
749750e409Sopenharmony_ci
759750e409Sopenharmony_ci"Actual" is the value being tested and unlike the other parameters in an
769750e409Sopenharmony_ciassertion construction is the only parameter present in all assertion variants.
779750e409Sopenharmony_ci"Modifiers" are masks, ranges, bit flag specifiers, floating point deltas.
789750e409Sopenharmony_ci"Expected" is your expected value (duh) to compare to an "actual" value; it's
799750e409Sopenharmony_cimarked as an optional parameter because some assertions only need a single
809750e409Sopenharmony_ci"actual" parameter (e.g. null check).
819750e409Sopenharmony_ci"Size/count" refers to string lengths, number of array elements, etc.
829750e409Sopenharmony_ci
839750e409Sopenharmony_ciMany of Unity's assertions are apparent duplications in that the same data type
849750e409Sopenharmony_ciis handled by several assertions. The differences among these are in how failure
859750e409Sopenharmony_cimessages are presented. For instance, a `_HEX` variant of an assertion prints
869750e409Sopenharmony_cithe expected and actual values of that assertion formatted as hexadecimal.
879750e409Sopenharmony_ci
889750e409Sopenharmony_ci
899750e409Sopenharmony_ci#### TEST_ASSERT_X_MESSAGE Variants
909750e409Sopenharmony_ci
919750e409Sopenharmony_ci_All_ assertions are complemented with a variant that includes a simple string
929750e409Sopenharmony_cimessage as a final parameter. The string you specify is appended to an assertion
939750e409Sopenharmony_cifailure message in Unity output.
949750e409Sopenharmony_ci
959750e409Sopenharmony_ciFor brevity, the assertion variants with a message parameter are not listed
969750e409Sopenharmony_cibelow. Just tack on `_MESSAGE` as the final component to any assertion name in
979750e409Sopenharmony_cithe reference list below and add a string as the final parameter.
989750e409Sopenharmony_ci
999750e409Sopenharmony_ci_Example:_
1009750e409Sopenharmony_ci
1019750e409Sopenharmony_ci    TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
1029750e409Sopenharmony_ci
1039750e409Sopenharmony_cibecomes messageified like thus...
1049750e409Sopenharmony_ci
1059750e409Sopenharmony_ci    TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
1069750e409Sopenharmony_ci
1079750e409Sopenharmony_ci
1089750e409Sopenharmony_ci#### TEST_ASSERT_X_ARRAY Variants
1099750e409Sopenharmony_ci
1109750e409Sopenharmony_ciUnity provides a collection of assertions for arrays containing a variety of
1119750e409Sopenharmony_citypes. These are documented in the Array section below. These are almost on par
1129750e409Sopenharmony_ciwith the `_MESSAGE`variants of Unity's Asserts in that for pretty much any Unity
1139750e409Sopenharmony_citype assertion you can tack on `_ARRAY` and run assertions on an entire block of
1149750e409Sopenharmony_cimemory.
1159750e409Sopenharmony_ci
1169750e409Sopenharmony_ci    TEST_ASSERT_EQUAL_TYPEX_ARRAY( expected, actual, {size/count} )
1179750e409Sopenharmony_ci
1189750e409Sopenharmony_ci"Expected" is an array itself.
1199750e409Sopenharmony_ci"Size/count" is one or two parameters necessary to establish the number of array
1209750e409Sopenharmony_cielements and perhaps the length of elements within the array.
1219750e409Sopenharmony_ci
1229750e409Sopenharmony_ciNotes:
1239750e409Sopenharmony_ci- The `_MESSAGE` variant convention still applies here to array assertions. The
1249750e409Sopenharmony_ci`_MESSAGE` variants of the `_ARRAY` assertions have names ending with
1259750e409Sopenharmony_ci`_ARRAY_MESSAGE`.
1269750e409Sopenharmony_ci- Assertions for handling arrays of floating point values are grouped with float
1279750e409Sopenharmony_ciand double assertions (see immediately following section).
1289750e409Sopenharmony_ci
1299750e409Sopenharmony_ci
1309750e409Sopenharmony_ci### TEST_ASSERT_EACH_EQUAL_X Variants
1319750e409Sopenharmony_ci
1329750e409Sopenharmony_ciUnity provides a collection of assertions for arrays containing a variety of
1339750e409Sopenharmony_citypes which can be compared to a single value as well. These are documented in
1349750e409Sopenharmony_cithe Each Equal section below. these are almost on par with the `_MESSAGE`
1359750e409Sopenharmony_civariants of Unity's Asserts in that for pretty much any Unity type assertion you
1369750e409Sopenharmony_cican inject _EACH_EQUAL and run assertions on an entire block of memory.
1379750e409Sopenharmony_ci
1389750e409Sopenharmony_ci    TEST_ASSERT_EACH_EQUAL_TYPEX( expected, actual, {size/count} )
1399750e409Sopenharmony_ci
1409750e409Sopenharmony_ci"Expected" is a single value to compare to.
1419750e409Sopenharmony_ci"Actual" is an array where each element will be compared to the expected value.
1429750e409Sopenharmony_ci"Size/count" is one of two parameters necessary to establish the number of array
1439750e409Sopenharmony_cielements and perhaps the length of elements within the array.
1449750e409Sopenharmony_ci
1459750e409Sopenharmony_ciNotes:
1469750e409Sopenharmony_ci- The `_MESSAGE` variant convention still applies here to Each Equal assertions.
1479750e409Sopenharmony_ci- Assertions for handling Each Equal of floating point values are grouped with
1489750e409Sopenharmony_cifloat and double assertions (see immediately following section).
1499750e409Sopenharmony_ci
1509750e409Sopenharmony_ci
1519750e409Sopenharmony_ci### Configuration
1529750e409Sopenharmony_ci
1539750e409Sopenharmony_ci#### Floating Point Support Is Optional
1549750e409Sopenharmony_ci
1559750e409Sopenharmony_ciSupport for floating point types is configurable. That is, by defining the
1569750e409Sopenharmony_ciappropriate preprocessor symbols, floats and doubles can be individually enabled
1579750e409Sopenharmony_cior disabled in Unity code. This is useful for embedded targets with no floating
1589750e409Sopenharmony_cipoint math support (i.e. Unity compiles free of errors for fixed point only
1599750e409Sopenharmony_ciplatforms). See Unity documentation for specifics.
1609750e409Sopenharmony_ci
1619750e409Sopenharmony_ci
1629750e409Sopenharmony_ci#### Maximum Data Type Width Is Configurable
1639750e409Sopenharmony_ci
1649750e409Sopenharmony_ciNot all targets support 64 bit wide types or even 32 bit wide types. Define the
1659750e409Sopenharmony_ciappropriate preprocessor symbols and Unity will omit all operations from
1669750e409Sopenharmony_cicompilation that exceed the maximum width of your target. See Unity
1679750e409Sopenharmony_cidocumentation for specifics.
1689750e409Sopenharmony_ci
1699750e409Sopenharmony_ci
1709750e409Sopenharmony_ci## The Assertions in All Their Blessed Glory
1719750e409Sopenharmony_ci
1729750e409Sopenharmony_ci### Basic Fail and Ignore
1739750e409Sopenharmony_ci
1749750e409Sopenharmony_ci##### `TEST_FAIL()`
1759750e409Sopenharmony_ci
1769750e409Sopenharmony_ciThis fella is most often used in special conditions where your test code is
1779750e409Sopenharmony_ciperforming logic beyond a simple assertion. That is, in practice, `TEST_FAIL()`
1789750e409Sopenharmony_ciwill always be found inside a conditional code block.
1799750e409Sopenharmony_ci
1809750e409Sopenharmony_ci_Examples:_
1819750e409Sopenharmony_ci- Executing a state machine multiple times that increments a counter your test
1829750e409Sopenharmony_cicode then verifies as a final step.
1839750e409Sopenharmony_ci- Triggering an exception and verifying it (as in Try / Catch / Throw - see the
1849750e409Sopenharmony_ci[CException](https://github.com/ThrowTheSwitch/CException) project).
1859750e409Sopenharmony_ci
1869750e409Sopenharmony_ci##### `TEST_IGNORE()`
1879750e409Sopenharmony_ci
1889750e409Sopenharmony_ciMarks a test case (i.e. function meant to contain test assertions) as ignored.
1899750e409Sopenharmony_ciUsually this is employed as a breadcrumb to come back and implement a test case.
1909750e409Sopenharmony_ciAn ignored test case has effects if other assertions are in the enclosing test
1919750e409Sopenharmony_cicase (see Unity documentation for more).
1929750e409Sopenharmony_ci
1939750e409Sopenharmony_ci### Boolean
1949750e409Sopenharmony_ci
1959750e409Sopenharmony_ci##### `TEST_ASSERT (condition)`
1969750e409Sopenharmony_ci
1979750e409Sopenharmony_ci##### `TEST_ASSERT_TRUE (condition)`
1989750e409Sopenharmony_ci
1999750e409Sopenharmony_ci##### `TEST_ASSERT_FALSE (condition)`
2009750e409Sopenharmony_ci
2019750e409Sopenharmony_ci##### `TEST_ASSERT_UNLESS (condition)`
2029750e409Sopenharmony_ci
2039750e409Sopenharmony_ciA simple wording variation on `TEST_ASSERT_FALSE`.The semantics of
2049750e409Sopenharmony_ci`TEST_ASSERT_UNLESS` aid readability in certain test constructions or
2059750e409Sopenharmony_ciconditional statements.
2069750e409Sopenharmony_ci
2079750e409Sopenharmony_ci##### `TEST_ASSERT_NULL (pointer)`
2089750e409Sopenharmony_ci
2099750e409Sopenharmony_ci##### `TEST_ASSERT_NOT_NULL (pointer)`
2109750e409Sopenharmony_ci
2119750e409Sopenharmony_ci
2129750e409Sopenharmony_ci### Signed and Unsigned Integers (of all sizes)
2139750e409Sopenharmony_ci
2149750e409Sopenharmony_ciLarge integer sizes can be disabled for build targets that do not support them.
2159750e409Sopenharmony_ciFor example, if your target only supports up to 16 bit types, by defining the
2169750e409Sopenharmony_ciappropriate symbols Unity can be configured to omit 32 and 64 bit operations
2179750e409Sopenharmony_cithat would break compilation (see Unity documentation for more). Refer to
2189750e409Sopenharmony_ciAdvanced Asserting later in this document for advice on dealing with other word
2199750e409Sopenharmony_cisizes.
2209750e409Sopenharmony_ci
2219750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT (expected, actual)`
2229750e409Sopenharmony_ci
2239750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT8 (expected, actual)`
2249750e409Sopenharmony_ci
2259750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT16 (expected, actual)`
2269750e409Sopenharmony_ci
2279750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT32 (expected, actual)`
2289750e409Sopenharmony_ci
2299750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT64 (expected, actual)`
2309750e409Sopenharmony_ci
2319750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL (expected, actual)`
2329750e409Sopenharmony_ci
2339750e409Sopenharmony_ci##### `TEST_ASSERT_NOT_EQUAL (expected, actual)`
2349750e409Sopenharmony_ci
2359750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT (expected, actual)`
2369750e409Sopenharmony_ci
2379750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT8 (expected, actual)`
2389750e409Sopenharmony_ci
2399750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT16 (expected, actual)`
2409750e409Sopenharmony_ci
2419750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT32 (expected, actual)`
2429750e409Sopenharmony_ci
2439750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT64 (expected, actual)`
2449750e409Sopenharmony_ci
2459750e409Sopenharmony_ci
2469750e409Sopenharmony_ci### Unsigned Integers (of all sizes) in Hexadecimal
2479750e409Sopenharmony_ci
2489750e409Sopenharmony_ciAll `_HEX` assertions are identical in function to unsigned integer assertions
2499750e409Sopenharmony_cibut produce failure messages with the `expected` and `actual` values formatted
2509750e409Sopenharmony_ciin hexadecimal. Unity output is big endian.
2519750e409Sopenharmony_ci
2529750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX (expected, actual)`
2539750e409Sopenharmony_ci
2549750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX8 (expected, actual)`
2559750e409Sopenharmony_ci
2569750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX16 (expected, actual)`
2579750e409Sopenharmony_ci
2589750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX32 (expected, actual)`
2599750e409Sopenharmony_ci
2609750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX64 (expected, actual)`
2619750e409Sopenharmony_ci
2629750e409Sopenharmony_ci
2639750e409Sopenharmony_ci### Masked and Bit-level Assertions
2649750e409Sopenharmony_ci
2659750e409Sopenharmony_ciMasked and bit-level assertions produce output formatted in hexadecimal. Unity
2669750e409Sopenharmony_cioutput is big endian.
2679750e409Sopenharmony_ci
2689750e409Sopenharmony_ci
2699750e409Sopenharmony_ci##### `TEST_ASSERT_BITS (mask, expected, actual)`
2709750e409Sopenharmony_ci
2719750e409Sopenharmony_ciOnly compares the masked (i.e. high) bits of `expected` and `actual` parameters.
2729750e409Sopenharmony_ci
2739750e409Sopenharmony_ci
2749750e409Sopenharmony_ci##### `TEST_ASSERT_BITS_HIGH (mask, actual)`
2759750e409Sopenharmony_ci
2769750e409Sopenharmony_ciAsserts the masked bits of the `actual` parameter are high.
2779750e409Sopenharmony_ci
2789750e409Sopenharmony_ci
2799750e409Sopenharmony_ci##### `TEST_ASSERT_BITS_LOW (mask, actual)`
2809750e409Sopenharmony_ci
2819750e409Sopenharmony_ciAsserts the masked bits of the `actual` parameter are low.
2829750e409Sopenharmony_ci
2839750e409Sopenharmony_ci
2849750e409Sopenharmony_ci##### `TEST_ASSERT_BIT_HIGH (bit, actual)`
2859750e409Sopenharmony_ci
2869750e409Sopenharmony_ciAsserts the specified bit of the `actual` parameter is high.
2879750e409Sopenharmony_ci
2889750e409Sopenharmony_ci
2899750e409Sopenharmony_ci##### `TEST_ASSERT_BIT_LOW (bit, actual)`
2909750e409Sopenharmony_ci
2919750e409Sopenharmony_ciAsserts the specified bit of the `actual` parameter is low.
2929750e409Sopenharmony_ci
2939750e409Sopenharmony_ci### Integer Less Than / Greater Than
2949750e409Sopenharmony_ci
2959750e409Sopenharmony_ciThese assertions verify that the `actual` parameter is less than or greater
2969750e409Sopenharmony_cithan `threshold` (exclusive). For example, if the threshold value is 0 for the
2979750e409Sopenharmony_cigreater than assertion will fail if it is 0 or less.
2989750e409Sopenharmony_ci
2999750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN (threshold, actual)`
3009750e409Sopenharmony_ci
3019750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_INT (threshold, actual)`
3029750e409Sopenharmony_ci
3039750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)`
3049750e409Sopenharmony_ci
3059750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_INT16 (threshold, actual)`
3069750e409Sopenharmony_ci
3079750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_INT32 (threshold, actual)`
3089750e409Sopenharmony_ci
3099750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_UINT (threshold, actual)`
3109750e409Sopenharmony_ci
3119750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_UINT8 (threshold, actual)`
3129750e409Sopenharmony_ci
3139750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_UINT16 (threshold, actual)`
3149750e409Sopenharmony_ci
3159750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_UINT32 (threshold, actual)`
3169750e409Sopenharmony_ci
3179750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_HEX8 (threshold, actual)`
3189750e409Sopenharmony_ci
3199750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_HEX16 (threshold, actual)`
3209750e409Sopenharmony_ci
3219750e409Sopenharmony_ci##### `TEST_ASSERT_GREATER_THAN_HEX32 (threshold, actual)`
3229750e409Sopenharmony_ci
3239750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN (threshold, actual)`
3249750e409Sopenharmony_ci
3259750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_INT (threshold, actual)`
3269750e409Sopenharmony_ci
3279750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_INT8 (threshold, actual)`
3289750e409Sopenharmony_ci
3299750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_INT16 (threshold, actual)`
3309750e409Sopenharmony_ci
3319750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)`
3329750e409Sopenharmony_ci
3339750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_UINT (threshold, actual)`
3349750e409Sopenharmony_ci
3359750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_UINT8 (threshold, actual)`
3369750e409Sopenharmony_ci
3379750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_UINT16 (threshold, actual)`
3389750e409Sopenharmony_ci
3399750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_UINT32 (threshold, actual)`
3409750e409Sopenharmony_ci
3419750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_HEX8 (threshold, actual)`
3429750e409Sopenharmony_ci
3439750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_HEX16 (threshold, actual)`
3449750e409Sopenharmony_ci
3459750e409Sopenharmony_ci##### `TEST_ASSERT_LESS_THAN_HEX32 (threshold, actual)`
3469750e409Sopenharmony_ci
3479750e409Sopenharmony_ci
3489750e409Sopenharmony_ci### Integer Ranges (of all sizes)
3499750e409Sopenharmony_ci
3509750e409Sopenharmony_ciThese assertions verify that the `expected` parameter is within +/- `delta`
3519750e409Sopenharmony_ci(inclusive) of the `actual` parameter. For example, if the expected value is 10
3529750e409Sopenharmony_ciand the delta is 3 then the assertion will fail for any value outside the range
3539750e409Sopenharmony_ciof 7 - 13.
3549750e409Sopenharmony_ci
3559750e409Sopenharmony_ci##### `TEST_ASSERT_INT_WITHIN (delta, expected, actual)`
3569750e409Sopenharmony_ci
3579750e409Sopenharmony_ci##### `TEST_ASSERT_INT8_WITHIN (delta, expected, actual)`
3589750e409Sopenharmony_ci
3599750e409Sopenharmony_ci##### `TEST_ASSERT_INT16_WITHIN (delta, expected, actual)`
3609750e409Sopenharmony_ci
3619750e409Sopenharmony_ci##### `TEST_ASSERT_INT32_WITHIN (delta, expected, actual)`
3629750e409Sopenharmony_ci
3639750e409Sopenharmony_ci##### `TEST_ASSERT_INT64_WITHIN (delta, expected, actual)`
3649750e409Sopenharmony_ci
3659750e409Sopenharmony_ci##### `TEST_ASSERT_UINT_WITHIN (delta, expected, actual)`
3669750e409Sopenharmony_ci
3679750e409Sopenharmony_ci##### `TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)`
3689750e409Sopenharmony_ci
3699750e409Sopenharmony_ci##### `TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)`
3709750e409Sopenharmony_ci
3719750e409Sopenharmony_ci##### `TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)`
3729750e409Sopenharmony_ci
3739750e409Sopenharmony_ci##### `TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)`
3749750e409Sopenharmony_ci
3759750e409Sopenharmony_ci##### `TEST_ASSERT_HEX_WITHIN (delta, expected, actual)`
3769750e409Sopenharmony_ci
3779750e409Sopenharmony_ci##### `TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)`
3789750e409Sopenharmony_ci
3799750e409Sopenharmony_ci##### `TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)`
3809750e409Sopenharmony_ci
3819750e409Sopenharmony_ci##### `TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)`
3829750e409Sopenharmony_ci
3839750e409Sopenharmony_ci##### `TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)`
3849750e409Sopenharmony_ci
3859750e409Sopenharmony_ci
3869750e409Sopenharmony_ci### Structs and Strings
3879750e409Sopenharmony_ci
3889750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_PTR (expected, actual)`
3899750e409Sopenharmony_ci
3909750e409Sopenharmony_ciAsserts that the pointers point to the same memory location.
3919750e409Sopenharmony_ci
3929750e409Sopenharmony_ci
3939750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_STRING (expected, actual)`
3949750e409Sopenharmony_ci
3959750e409Sopenharmony_ciAsserts that the null terminated (`'\0'`)strings are identical. If strings are
3969750e409Sopenharmony_ciof different lengths or any portion of the strings before their terminators
3979750e409Sopenharmony_cidiffer, the assertion fails. Two NULL strings (i.e. zero length) are considered
3989750e409Sopenharmony_ciequivalent.
3999750e409Sopenharmony_ci
4009750e409Sopenharmony_ci
4019750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`
4029750e409Sopenharmony_ci
4039750e409Sopenharmony_ciAsserts that the contents of the memory specified by the `expected` and `actual`
4049750e409Sopenharmony_cipointers is identical. The size of the memory blocks in bytes is specified by
4059750e409Sopenharmony_cithe `len` parameter.
4069750e409Sopenharmony_ci
4079750e409Sopenharmony_ci
4089750e409Sopenharmony_ci### Arrays
4099750e409Sopenharmony_ci
4109750e409Sopenharmony_ci`expected` and `actual` parameters are both arrays. `num_elements` specifies the
4119750e409Sopenharmony_cinumber of elements in the arrays to compare.
4129750e409Sopenharmony_ci
4139750e409Sopenharmony_ci`_HEX` assertions produce failure messages with expected and actual array
4149750e409Sopenharmony_cicontents formatted in hexadecimal.
4159750e409Sopenharmony_ci
4169750e409Sopenharmony_ciFor array of strings comparison behavior, see comments for
4179750e409Sopenharmony_ci`TEST_ASSERT_EQUAL_STRING` in the preceding section.
4189750e409Sopenharmony_ci
4199750e409Sopenharmony_ciAssertions fail upon the first element in the compared arrays found not to
4209750e409Sopenharmony_cimatch. Failure messages specify the array index of the failed comparison.
4219750e409Sopenharmony_ci
4229750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)`
4239750e409Sopenharmony_ci
4249750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)`
4259750e409Sopenharmony_ci
4269750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)`
4279750e409Sopenharmony_ci
4289750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)`
4299750e409Sopenharmony_ci
4309750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)`
4319750e409Sopenharmony_ci
4329750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)`
4339750e409Sopenharmony_ci
4349750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)`
4359750e409Sopenharmony_ci
4369750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)`
4379750e409Sopenharmony_ci
4389750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)`
4399750e409Sopenharmony_ci
4409750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)`
4419750e409Sopenharmony_ci
4429750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)`
4439750e409Sopenharmony_ci
4449750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)`
4459750e409Sopenharmony_ci
4469750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)`
4479750e409Sopenharmony_ci
4489750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)`
4499750e409Sopenharmony_ci
4509750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)`
4519750e409Sopenharmony_ci
4529750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)`
4539750e409Sopenharmony_ci
4549750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)`
4559750e409Sopenharmony_ci
4569750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)`
4579750e409Sopenharmony_ci
4589750e409Sopenharmony_ci`len` is the memory in bytes to be compared at each array element.
4599750e409Sopenharmony_ci
4609750e409Sopenharmony_ci
4619750e409Sopenharmony_ci### Each Equal (Arrays to Single Value)
4629750e409Sopenharmony_ci
4639750e409Sopenharmony_ci`expected` are single values and `actual` are arrays. `num_elements` specifies
4649750e409Sopenharmony_cithe number of elements in the arrays to compare.
4659750e409Sopenharmony_ci
4669750e409Sopenharmony_ci`_HEX` assertions produce failure messages with expected and actual array
4679750e409Sopenharmony_cicontents formatted in hexadecimal.
4689750e409Sopenharmony_ci
4699750e409Sopenharmony_ciAssertions fail upon the first element in the compared arrays found not to
4709750e409Sopenharmony_cimatch. Failure messages specify the array index of the failed comparison.
4719750e409Sopenharmony_ci
4729750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_INT (expected, actual, num_elements)`
4739750e409Sopenharmony_ci
4749750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_INT8 (expected, actual, num_elements)`
4759750e409Sopenharmony_ci
4769750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_INT16 (expected, actual, num_elements)`
4779750e409Sopenharmony_ci
4789750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_INT32 (expected, actual, num_elements)`
4799750e409Sopenharmony_ci
4809750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_INT64 (expected, actual, num_elements)`
4819750e409Sopenharmony_ci
4829750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_UINT (expected, actual, num_elements)`
4839750e409Sopenharmony_ci
4849750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_UINT8 (expected, actual, num_elements)`
4859750e409Sopenharmony_ci
4869750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_UINT16 (expected, actual, num_elements)`
4879750e409Sopenharmony_ci
4889750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_UINT32 (expected, actual, num_elements)`
4899750e409Sopenharmony_ci
4909750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_UINT64 (expected, actual, num_elements)`
4919750e409Sopenharmony_ci
4929750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_HEX (expected, actual, num_elements)`
4939750e409Sopenharmony_ci
4949750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_HEX8 (expected, actual, num_elements)`
4959750e409Sopenharmony_ci
4969750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_HEX16 (expected, actual, num_elements)`
4979750e409Sopenharmony_ci
4989750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_HEX32 (expected, actual, num_elements)`
4999750e409Sopenharmony_ci
5009750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_HEX64 (expected, actual, num_elements)`
5019750e409Sopenharmony_ci
5029750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_PTR (expected, actual, num_elements)`
5039750e409Sopenharmony_ci
5049750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_STRING (expected, actual, num_elements)`
5059750e409Sopenharmony_ci
5069750e409Sopenharmony_ci#### `TEST_ASSERT_EACH_EQUAL_MEMORY (expected, actual, len, num_elements)`
5079750e409Sopenharmony_ci
5089750e409Sopenharmony_ci`len` is the memory in bytes to be compared at each array element.
5099750e409Sopenharmony_ci
5109750e409Sopenharmony_ci
5119750e409Sopenharmony_ci### Floating Point (If enabled)
5129750e409Sopenharmony_ci
5139750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`
5149750e409Sopenharmony_ci
5159750e409Sopenharmony_ciAsserts that the `actual` value is within +/- `delta` of the `expected` value.
5169750e409Sopenharmony_ciThe nature of floating point representation is such that exact evaluations of
5179750e409Sopenharmony_ciequality are not guaranteed.
5189750e409Sopenharmony_ci
5199750e409Sopenharmony_ci
5209750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
5219750e409Sopenharmony_ci
5229750e409Sopenharmony_ciAsserts that the ?actual?value is "close enough to be considered equal" to the
5239750e409Sopenharmony_ci`expected` value. If you are curious about the details, refer to the Advanced
5249750e409Sopenharmony_ciAsserting section for more details on this. Omitting a user-specified delta in a
5259750e409Sopenharmony_cifloating point assertion is both a shorthand convenience and a requirement of
5269750e409Sopenharmony_cicode generation conventions for CMock.
5279750e409Sopenharmony_ci
5289750e409Sopenharmony_ci
5299750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
5309750e409Sopenharmony_ci
5319750e409Sopenharmony_ciSee Array assertion section for details. Note that individual array element
5329750e409Sopenharmony_cifloat comparisons are executed using T?EST_ASSERT_EQUAL_FLOAT?.That is, user
5339750e409Sopenharmony_cispecified delta comparison values requires a custom-implemented floating point
5349750e409Sopenharmony_ciarray assertion.
5359750e409Sopenharmony_ci
5369750e409Sopenharmony_ci
5379750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_INF (actual)`
5389750e409Sopenharmony_ci
5399750e409Sopenharmony_ciAsserts that `actual` parameter is equivalent to positive infinity floating
5409750e409Sopenharmony_cipoint representation.
5419750e409Sopenharmony_ci
5429750e409Sopenharmony_ci
5439750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NEG_INF (actual)`
5449750e409Sopenharmony_ci
5459750e409Sopenharmony_ciAsserts that `actual` parameter is equivalent to negative infinity floating
5469750e409Sopenharmony_cipoint representation.
5479750e409Sopenharmony_ci
5489750e409Sopenharmony_ci
5499750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NAN (actual)`
5509750e409Sopenharmony_ci
5519750e409Sopenharmony_ciAsserts that `actual` parameter is a Not A Number floating point representation.
5529750e409Sopenharmony_ci
5539750e409Sopenharmony_ci
5549750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`
5559750e409Sopenharmony_ci
5569750e409Sopenharmony_ciAsserts that ?actual?parameter is a floating point representation usable for
5579750e409Sopenharmony_cimathematical operations. That is, the `actual` parameter is neither positive
5589750e409Sopenharmony_ciinfinity nor negative infinity nor Not A Number floating point representations.
5599750e409Sopenharmony_ci
5609750e409Sopenharmony_ci
5619750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NOT_INF (actual)`
5629750e409Sopenharmony_ci
5639750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than positive infinity floating
5649750e409Sopenharmony_cipoint representation.
5659750e409Sopenharmony_ci
5669750e409Sopenharmony_ci
5679750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)`
5689750e409Sopenharmony_ci
5699750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than negative infinity floating
5709750e409Sopenharmony_cipoint representation.
5719750e409Sopenharmony_ci
5729750e409Sopenharmony_ci
5739750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)`
5749750e409Sopenharmony_ci
5759750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than Not A Number floating
5769750e409Sopenharmony_cipoint representation.
5779750e409Sopenharmony_ci
5789750e409Sopenharmony_ci
5799750e409Sopenharmony_ci##### `TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)`
5809750e409Sopenharmony_ci
5819750e409Sopenharmony_ciAsserts that `actual` parameter is not usable for mathematical operations. That
5829750e409Sopenharmony_ciis, the `actual` parameter is either positive infinity or negative infinity or
5839750e409Sopenharmony_ciNot A Number floating point representations.
5849750e409Sopenharmony_ci
5859750e409Sopenharmony_ci
5869750e409Sopenharmony_ci### Double (If enabled)
5879750e409Sopenharmony_ci
5889750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)`
5899750e409Sopenharmony_ci
5909750e409Sopenharmony_ciAsserts that the `actual` value is within +/- `delta` of the `expected` value.
5919750e409Sopenharmony_ciThe nature of floating point representation is such that exact evaluations of
5929750e409Sopenharmony_ciequality are not guaranteed.
5939750e409Sopenharmony_ci
5949750e409Sopenharmony_ci
5959750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
5969750e409Sopenharmony_ci
5979750e409Sopenharmony_ciAsserts that the `actual` value is "close enough to be considered equal" to the
5989750e409Sopenharmony_ci`expected` value. If you are curious about the details, refer to the Advanced
5999750e409Sopenharmony_ciAsserting section for more details. Omitting a user-specified delta in a
6009750e409Sopenharmony_cifloating point assertion is both a shorthand convenience and a requirement of
6019750e409Sopenharmony_cicode generation conventions for CMock.
6029750e409Sopenharmony_ci
6039750e409Sopenharmony_ci
6049750e409Sopenharmony_ci##### `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
6059750e409Sopenharmony_ci
6069750e409Sopenharmony_ciSee Array assertion section for details. Note that individual array element
6079750e409Sopenharmony_cidouble comparisons are executed using `TEST_ASSERT_EQUAL_DOUBLE`.That is, user
6089750e409Sopenharmony_cispecified delta comparison values requires a custom implemented double array
6099750e409Sopenharmony_ciassertion.
6109750e409Sopenharmony_ci
6119750e409Sopenharmony_ci
6129750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_INF (actual)`
6139750e409Sopenharmony_ci
6149750e409Sopenharmony_ciAsserts that `actual` parameter is equivalent to positive infinity floating
6159750e409Sopenharmony_cipoint representation.
6169750e409Sopenharmony_ci
6179750e409Sopenharmony_ci
6189750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)`
6199750e409Sopenharmony_ci
6209750e409Sopenharmony_ciAsserts that `actual` parameter is equivalent to negative infinity floating point
6219750e409Sopenharmony_cirepresentation.
6229750e409Sopenharmony_ci
6239750e409Sopenharmony_ci
6249750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NAN (actual)`
6259750e409Sopenharmony_ci
6269750e409Sopenharmony_ciAsserts that `actual` parameter is a Not A Number floating point representation.
6279750e409Sopenharmony_ci
6289750e409Sopenharmony_ci
6299750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`
6309750e409Sopenharmony_ci
6319750e409Sopenharmony_ciAsserts that `actual` parameter is a floating point representation usable for
6329750e409Sopenharmony_cimathematical operations. That is, the ?actual?parameter is neither positive
6339750e409Sopenharmony_ciinfinity nor negative infinity nor Not A Number floating point representations.
6349750e409Sopenharmony_ci
6359750e409Sopenharmony_ci
6369750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
6379750e409Sopenharmony_ci
6389750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than positive infinity floating
6399750e409Sopenharmony_cipoint representation.
6409750e409Sopenharmony_ci
6419750e409Sopenharmony_ci
6429750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)`
6439750e409Sopenharmony_ci
6449750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than negative infinity floating
6459750e409Sopenharmony_cipoint representation.
6469750e409Sopenharmony_ci
6479750e409Sopenharmony_ci
6489750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)`
6499750e409Sopenharmony_ci
6509750e409Sopenharmony_ciAsserts that `actual` parameter is a value other than Not A Number floating
6519750e409Sopenharmony_cipoint representation.
6529750e409Sopenharmony_ci
6539750e409Sopenharmony_ci
6549750e409Sopenharmony_ci##### `TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)`
6559750e409Sopenharmony_ci
6569750e409Sopenharmony_ciAsserts that `actual` parameter is not usable for mathematical operations. That
6579750e409Sopenharmony_ciis, the `actual` parameter is either positive infinity or negative infinity or
6589750e409Sopenharmony_ciNot A Number floating point representations.
6599750e409Sopenharmony_ci
6609750e409Sopenharmony_ci
6619750e409Sopenharmony_ci## Advanced Asserting: Details On Tricky Assertions
6629750e409Sopenharmony_ci
6639750e409Sopenharmony_ciThis section helps you understand how to deal with some of the trickier
6649750e409Sopenharmony_ciassertion situations you may run into. It will give you a glimpse into some of
6659750e409Sopenharmony_cithe under-the-hood details of Unity's assertion mechanisms. If you're one of
6669750e409Sopenharmony_cithose people who likes to know what is going on in the background, read on. If
6679750e409Sopenharmony_cinot, feel free to ignore the rest of this document until you need it.
6689750e409Sopenharmony_ci
6699750e409Sopenharmony_ci
6709750e409Sopenharmony_ci### How do the EQUAL assertions work for FLOAT and DOUBLE?
6719750e409Sopenharmony_ci
6729750e409Sopenharmony_ciAs you may know, directly checking for equality between a pair of floats or a
6739750e409Sopenharmony_cipair of doubles is sloppy at best and an outright no-no at worst. Floating point
6749750e409Sopenharmony_civalues can often be represented in multiple ways, particularly after a series of
6759750e409Sopenharmony_cioperations on a value. Initializing a variable to the value of 2.0 is likely to
6769750e409Sopenharmony_ciresult in a floating point representation of 2 x 20,but a series of
6779750e409Sopenharmony_cimathematical operations might result in a representation of 8 x 2-2
6789750e409Sopenharmony_cithat also evaluates to a value of 2. At some point repeated operations cause
6799750e409Sopenharmony_ciequality checks to fail.
6809750e409Sopenharmony_ci
6819750e409Sopenharmony_ciSo Unity doesn't do direct floating point comparisons for equality. Instead, it
6829750e409Sopenharmony_cichecks if two floating point values are "really close." If you leave Unity
6839750e409Sopenharmony_cirunning with defaults, "really close" means "within a significant bit or two."
6849750e409Sopenharmony_ciUnder the hood, `TEST_ASSERT_EQUAL_FLOAT` is really `TEST_ASSERT_FLOAT_WITHIN`
6859750e409Sopenharmony_ciwith the `delta` parameter calculated on the fly. For single precision, delta is
6869750e409Sopenharmony_cithe expected value multiplied by 0.00001, producing a very small proportional
6879750e409Sopenharmony_cirange around the expected value.
6889750e409Sopenharmony_ci
6899750e409Sopenharmony_ciIf you are expecting a value of 20,000.0 the delta is calculated to be 0.2. So
6909750e409Sopenharmony_ciany value between 19,999.8 and 20,000.2 will satisfy the equality check. This
6919750e409Sopenharmony_ciworks out to be roughly a single bit of range for a single-precision number, and
6929750e409Sopenharmony_cithat's just about as tight a tolerance as you can reasonably get from a floating
6939750e409Sopenharmony_cipoint value.
6949750e409Sopenharmony_ci
6959750e409Sopenharmony_ciSo what happens when it's zero? Zero - even more than other floating point
6969750e409Sopenharmony_civalues - can be represented many different ways. It doesn't matter if you have
6979750e409Sopenharmony_ci0 x 20or 0 x 263.It's still zero, right? Luckily, if you
6989750e409Sopenharmony_cisubtract these values from each other, they will always produce a difference of
6999750e409Sopenharmony_cizero, which will still fall between 0 plus or minus a delta of 0. So it still
7009750e409Sopenharmony_ciworks!
7019750e409Sopenharmony_ci
7029750e409Sopenharmony_ciDouble precision floating point numbers use a much smaller multiplier, again
7039750e409Sopenharmony_ciapproximating a single bit of error.
7049750e409Sopenharmony_ci
7059750e409Sopenharmony_ciIf you don't like these ranges and you want to make your floating point equality
7069750e409Sopenharmony_ciassertions less strict, you can change these multipliers to whatever you like by
7079750e409Sopenharmony_cidefining UNITY_FLOAT_PRECISION and UNITY_DOUBLE_PRECISION. See Unity
7089750e409Sopenharmony_cidocumentation for more.
7099750e409Sopenharmony_ci
7109750e409Sopenharmony_ci
7119750e409Sopenharmony_ci### How do we deal with targets with non-standard int sizes?
7129750e409Sopenharmony_ci
7139750e409Sopenharmony_ciIt's "fun" that C is a standard where something as fundamental as an integer
7149750e409Sopenharmony_civaries by target. According to the C standard, an `int` is to be the target's
7159750e409Sopenharmony_cinatural register size, and it should be at least 16-bits and a multiple of a
7169750e409Sopenharmony_cibyte. It also guarantees an order of sizes:
7179750e409Sopenharmony_ci
7189750e409Sopenharmony_ci```C
7199750e409Sopenharmony_cichar <= short <= int <= long <= long long
7209750e409Sopenharmony_ci```
7219750e409Sopenharmony_ci
7229750e409Sopenharmony_ciMost often, `int` is 32-bits. In many cases in the embedded world, `int` is
7239750e409Sopenharmony_ci16-bits. There are rare microcontrollers out there that have 24-bit integers,
7249750e409Sopenharmony_ciand this remains perfectly standard C.
7259750e409Sopenharmony_ci
7269750e409Sopenharmony_ciTo make things even more interesting, there are compilers and targets out there
7279750e409Sopenharmony_cithat have a hard choice to make. What if their natural register size is 10-bits
7289750e409Sopenharmony_cior 12-bits? Clearly they can't fulfill _both_ the requirement to be at least
7299750e409Sopenharmony_ci16-bits AND the requirement to match the natural register size. In these
7309750e409Sopenharmony_cisituations, they often choose the natural register size, leaving us with
7319750e409Sopenharmony_cisomething like this:
7329750e409Sopenharmony_ci
7339750e409Sopenharmony_ci```C
7349750e409Sopenharmony_cichar (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit)
7359750e409Sopenharmony_ci```
7369750e409Sopenharmony_ci
7379750e409Sopenharmony_ciUm... yikes. It's obviously breaking a rule or two... but they had to break SOME
7389750e409Sopenharmony_cirules, so they made a choice.
7399750e409Sopenharmony_ci
7409750e409Sopenharmony_ciWhen the C99 standard rolled around, it introduced alternate standard-size types.
7419750e409Sopenharmony_ciIt also introduced macros for pulling in MIN/MAX values for your integer types.
7429750e409Sopenharmony_ciIt's glorious! Unfortunately, many embedded compilers can't be relied upon to
7439750e409Sopenharmony_ciuse the C99 types (Sometimes because they have weird register sizes as described
7449750e409Sopenharmony_ciabove. Sometimes because they don't feel like it?).
7459750e409Sopenharmony_ci
7469750e409Sopenharmony_ciA goal of Unity from the beginning was to support every combination of
7479750e409Sopenharmony_cimicrocontroller or microprocessor and C compiler. Over time, we've gotten really
7489750e409Sopenharmony_ciclose to this. There are a few tricks that you should be aware of, though, if
7499750e409Sopenharmony_ciyou're going to do this effectively on some of these more idiosyncratic targets.
7509750e409Sopenharmony_ci
7519750e409Sopenharmony_ciFirst, when setting up Unity for a new target, you're going to want to pay
7529750e409Sopenharmony_cispecial attention to the macros for automatically detecting types
7539750e409Sopenharmony_ci(where available) or manually configuring them yourself. You can get information
7549750e409Sopenharmony_cion both of these in Unity's documentation.
7559750e409Sopenharmony_ci
7569750e409Sopenharmony_ciWhat about the times where you suddenly need to deal with something odd, like a
7579750e409Sopenharmony_ci24-bit `int`? The simplest solution is to use the next size up. If you have a
7589750e409Sopenharmony_ci24-bit `int`, configure Unity to use 32-bit integers. If you have a 12-bit
7599750e409Sopenharmony_ci`int`, configure Unity to use 16 bits. There are two ways this is going to
7609750e409Sopenharmony_ciaffect you:
7619750e409Sopenharmony_ci
7629750e409Sopenharmony_ci1. When Unity displays errors for you, it's going to pad the upper unused bits
7639750e409Sopenharmony_ciwith zeros.
7649750e409Sopenharmony_ci2. You're going to have to be careful of assertions that perform signed
7659750e409Sopenharmony_cioperations, particularly `TEST_ASSERT_INT_WITHIN`.Such assertions might wrap
7669750e409Sopenharmony_ciyour `int` in the wrong place, and you could experience false failures. You can
7679750e409Sopenharmony_cialways back down to a simple `TEST_ASSERT` and do the operations yourself.
7689750e409Sopenharmony_ci
7699750e409Sopenharmony_ci
7709750e409Sopenharmony_ci*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*
771