UnityAssertionsReference.md 30.5 KB
Newer Older
T
toby 已提交
1
# Unity Assertions Reference
2

T
toby 已提交
3
## Background and Overview
4

T
toby 已提交
5
### Super Condensed Version
6 7

- An assertion establishes truth (i.e. boolean True) for a single condition.
T
toby 已提交
8
Upon boolean False, an assertion stops execution and reports the failure.
9
- Unity is mainly a rich collection of assertions and the support to gather up
T
toby 已提交
10
and easily execute those assertions.
11
- The structure of Unity allows you to easily separate test assertions from
T
toby 已提交
12 13
source code in, well, test code.
- Unity's assertions:
14
- Come in many, many flavors to handle different C types and assertion cases.
T
toby 已提交
15
- Use context to provide detailed and helpful failure messages.
16
- Document types, expected values, and basic behavior in your source code for
T
toby 已提交
17 18
free.

19

T
toby 已提交
20
### Unity Is Several Things But Mainly It's Assertions
21 22 23 24

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
T
toby 已提交
25 26
test code separate from your source code.

27

T
toby 已提交
28
### What's an Assertion?
29 30 31 32 33 34 35

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.
T
toby 已提交
36 37
stdout, GUI, file, blinky light).

38 39 40 41 42 43 44
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](http://en.wikipedia.org/en/wiki/Assert.h)
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
T
toby 已提交
45 46
code in the way Unity facilitates.

47

T
toby 已提交
48
### Unity's Assertions: Helpful Messages _and_ Free Source Code Documentation
49 50 51 52

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,
T
toby 已提交
53 54
readable, bit-level feedback when an assertion fails?

55 56 57 58 59 60
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
T
toby 已提交
61 62
to be well designed code.

63

T
toby 已提交
64
## Assertion Conventions and Configurations
65

T
toby 已提交
66
### Naming and Parameter Conventions
67

T
toby 已提交
68
The convention of assertion parameters generally follows this order:
69

70 71 72
```
TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
```
T
toby 已提交
73

74
The very simplest assertion possible uses only a single `actual` parameter (e.g.
T
toby 已提交
75 76
a simple null check).

77 78 79 80 81 82 83
 - `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.
T
toby 已提交
84

D
Deryew 已提交
85
Many of Unity's assertions are clear duplications in that the same data type
86 87
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
T
toby 已提交
88 89
the expected and actual values of that assertion formatted as hexadecimal.

90

T
toby 已提交
91
#### TEST_ASSERT_X_MESSAGE Variants
92 93 94

_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
T
toby 已提交
95 96
failure message in Unity output.

97 98
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
T
toby 已提交
99 100 101 102
the reference list below and add a string as the final parameter.

_Example:_

103 104 105
```
TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
```
106

T
toby 已提交
107 108
becomes messageified like thus...

109 110 111
```
TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
```
T
toby 已提交
112

113
Notes:
114
- The `_MESSAGE` variants intentionally do not support `printf` style formatting
115 116 117
  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.
118 119
- 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`
120 121
  assertions (see below) might be a handy alternative to `sprintf`.

122

T
toby 已提交
123
#### TEST_ASSERT_X_ARRAY Variants
124 125 126 127 128

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
T
toby 已提交
129 130
memory.

131
```
T
toby 已提交
132
    TEST_ASSERT_EQUAL_TYPEX_ARRAY( expected, actual, {size/count} )
133
```
T
toby 已提交
134

135 136 137
 - `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.
T
toby 已提交
138

139
Notes:
140 141 142 143 144 145

 - 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).
T
toby 已提交
146

147

T
toby 已提交
148 149
### TEST_ASSERT_EACH_EQUAL_X Variants

M
Mark VanderVoord 已提交
150 151 152
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`
T
toby 已提交
153
variants of Unity's Asserts in that for pretty much any Unity type assertion you
154
can inject `_EACH_EQUAL` and run assertions on an entire block of memory.
T
toby 已提交
155

156 157 158
```
TEST_ASSERT_EACH_EQUAL_TYPEX( expected, actual, {size/count} )
```
T
toby 已提交
159

160 161 162 163
 - `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.
T
toby 已提交
164 165

Notes:
166 167 168 169

 - 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).
T
toby 已提交
170 171


T
toby 已提交
172
### Configuration
173

T
toby 已提交
174
#### Floating Point Support Is Optional
175 176 177 178 179

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
T
toby 已提交
180 181
platforms). See Unity documentation for specifics.

182

T
toby 已提交
183
#### Maximum Data Type Width Is Configurable
184 185 186 187

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
T
toby 已提交
188 189
documentation for specifics.

190

T
toby 已提交
191
## The Assertions in All Their Blessed Glory
192

193
### Basic Fail, Pass and Ignore
T
toby 已提交
194 195

##### `TEST_FAIL()`
196

197 198
##### `TEST_FAIL_MESSAGE("message")`

199 200
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()`
T
toby 已提交
201 202 203
will always be found inside a conditional code block.

_Examples:_
204

205
- Executing a state machine multiple times that increments a counter your test
T
toby 已提交
206
code then verifies as a final step.
207
- Triggering an exception and verifying it (as in Try / Catch / Throw - see the
T
toby 已提交
208 209
[CException](https://github.com/ThrowTheSwitch/CException) project).

210 211 212 213 214 215 216 217 218
##### `TEST_PASS()`

##### `TEST_PASS_MESSAGE("message")`

This will abort the remainder of the test, but count the test as a pass. Under
normal circumstances, it is not necessary to include this macro in your tests...
a lack of failure will automatically be counted as a `PASS`. It is occasionally
useful for tests with `#ifdef`s and such.

T
toby 已提交
219
##### `TEST_IGNORE()`
220

221 222
##### `TEST_IGNORE_MESSAGE("message")`

223 224 225
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
T
toby 已提交
226 227
case (see Unity documentation for more).

228 229 230 231 232 233
##### `TEST_MESSAGE(message)`

This can be useful for outputting `INFO` messages into the Unity output stream
without actually ending the test. Like pass and fail messages, it will be output
with the filename and line number.

T
toby 已提交
234
### Boolean
235

T
toby 已提交
236
##### `TEST_ASSERT (condition)`
237

T
toby 已提交
238
##### `TEST_ASSERT_TRUE (condition)`
239

T
toby 已提交
240
##### `TEST_ASSERT_FALSE (condition)`
241

T
toby 已提交
242
##### `TEST_ASSERT_UNLESS (condition)`
243 244 245

A simple wording variation on `TEST_ASSERT_FALSE`.The semantics of
`TEST_ASSERT_UNLESS` aid readability in certain test constructions or
T
toby 已提交
246 247 248
conditional statements.

##### `TEST_ASSERT_NULL (pointer)`
249

T
toby 已提交
250 251
##### `TEST_ASSERT_NOT_NULL (pointer)`

252 253 254 255 256 257 258 259 260
Verify if a pointer is or is not NULL.

##### `TEST_ASSERT_EMPTY (pointer)`

##### `TEST_ASSERT_NOT_EMPTY (pointer)`

Verify if the first element dereferenced from a pointer is or is not zero. This
is particularly useful for checking for empty (or non-empty) null-terminated
C strings, but can be just as easily used for other null-terminated arrays.
T
toby 已提交
261 262

### Signed and Unsigned Integers (of all sizes)
263 264 265 266 267 268

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
T
toby 已提交
269 270 271
sizes.

##### `TEST_ASSERT_EQUAL_INT (expected, actual)`
272

T
toby 已提交
273
##### `TEST_ASSERT_EQUAL_INT8 (expected, actual)`
274

T
toby 已提交
275
##### `TEST_ASSERT_EQUAL_INT16 (expected, actual)`
276

T
toby 已提交
277
##### `TEST_ASSERT_EQUAL_INT32 (expected, actual)`
278

T
toby 已提交
279
##### `TEST_ASSERT_EQUAL_INT64 (expected, actual)`
280

T
toby 已提交
281
##### `TEST_ASSERT_EQUAL_UINT (expected, actual)`
282

T
toby 已提交
283
##### `TEST_ASSERT_EQUAL_UINT8 (expected, actual)`
284

T
toby 已提交
285
##### `TEST_ASSERT_EQUAL_UINT16 (expected, actual)`
286

T
toby 已提交
287
##### `TEST_ASSERT_EQUAL_UINT32 (expected, actual)`
288

T
toby 已提交
289 290
##### `TEST_ASSERT_EQUAL_UINT64 (expected, actual)`

291

T
toby 已提交
292
### Unsigned Integers (of all sizes) in Hexadecimal
293 294 295

All `_HEX` assertions are identical in function to unsigned integer assertions
but produce failure messages with the `expected` and `actual` values formatted
T
toby 已提交
296 297 298
in hexadecimal. Unity output is big endian.

##### `TEST_ASSERT_EQUAL_HEX (expected, actual)`
299

T
toby 已提交
300
##### `TEST_ASSERT_EQUAL_HEX8 (expected, actual)`
301

T
toby 已提交
302
##### `TEST_ASSERT_EQUAL_HEX16 (expected, actual)`
303

T
toby 已提交
304
##### `TEST_ASSERT_EQUAL_HEX32 (expected, actual)`
305

T
toby 已提交
306 307
##### `TEST_ASSERT_EQUAL_HEX64 (expected, actual)`

308

309 310 311 312 313 314 315 316 317
### Characters

While you can use the 8-bit integer assertions to compare `char`, another option is
to use this specialized assertion which will show printable characters as printables,
otherwise showing the HEX escape code for the characters.

##### `TEST_ASSERT_EQUAL_CHAR (expected, actual)`


T
toby 已提交
318
### Masked and Bit-level Assertions
319 320

Masked and bit-level assertions produce output formatted in hexadecimal. Unity
T
toby 已提交
321
output is big endian.
322 323


T
toby 已提交
324
##### `TEST_ASSERT_BITS (mask, expected, actual)`
325 326 327

Only compares the masked (i.e. high) bits of `expected` and `actual` parameters.

T
toby 已提交
328 329

##### `TEST_ASSERT_BITS_HIGH (mask, actual)`
330

T
toby 已提交
331 332
Asserts the masked bits of the `actual` parameter are high.

333

T
toby 已提交
334
##### `TEST_ASSERT_BITS_LOW (mask, actual)`
335

T
toby 已提交
336
Asserts the masked bits of the `actual` parameter are low.
337 338


T
toby 已提交
339
##### `TEST_ASSERT_BIT_HIGH (bit, actual)`
340

T
toby 已提交
341
Asserts the specified bit of the `actual` parameter is high.
342 343


T
toby 已提交
344
##### `TEST_ASSERT_BIT_LOW (bit, actual)`
345

T
toby 已提交
346 347
Asserts the specified bit of the `actual` parameter is low.

348 349 350 351
### 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
352 353
greater than assertion will fail if it is 0 or less. There are assertions for
all the various sizes of ints, as for the equality assertions. Some examples:
354 355 356

##### `TEST_ASSERT_GREATER_THAN_INT8 (threshold, actual)`

357
##### `TEST_ASSERT_GREATER_OR_EQUAL_INT16 (threshold, actual)`
358 359 360

##### `TEST_ASSERT_LESS_THAN_INT32 (threshold, actual)`

361
##### `TEST_ASSERT_LESS_OR_EQUAL_UINT (threshold, actual)`
362

363
##### `TEST_ASSERT_NOT_EQUAL_UINT8 (threshold, actual)`
364

365

T
toby 已提交
366
### Integer Ranges (of all sizes)
367 368 369 370

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
T
toby 已提交
371 372 373
of 7 - 13.

##### `TEST_ASSERT_INT_WITHIN (delta, expected, actual)`
374

T
toby 已提交
375
##### `TEST_ASSERT_INT8_WITHIN (delta, expected, actual)`
376

T
toby 已提交
377
##### `TEST_ASSERT_INT16_WITHIN (delta, expected, actual)`
378

T
toby 已提交
379
##### `TEST_ASSERT_INT32_WITHIN (delta, expected, actual)`
380

T
toby 已提交
381
##### `TEST_ASSERT_INT64_WITHIN (delta, expected, actual)`
382

T
toby 已提交
383
##### `TEST_ASSERT_UINT_WITHIN (delta, expected, actual)`
384

T
toby 已提交
385
##### `TEST_ASSERT_UINT8_WITHIN (delta, expected, actual)`
386

T
toby 已提交
387
##### `TEST_ASSERT_UINT16_WITHIN (delta, expected, actual)`
388

T
toby 已提交
389
##### `TEST_ASSERT_UINT32_WITHIN (delta, expected, actual)`
390

T
toby 已提交
391
##### `TEST_ASSERT_UINT64_WITHIN (delta, expected, actual)`
392

T
toby 已提交
393
##### `TEST_ASSERT_HEX_WITHIN (delta, expected, actual)`
394

T
toby 已提交
395
##### `TEST_ASSERT_HEX8_WITHIN (delta, expected, actual)`
396

T
toby 已提交
397
##### `TEST_ASSERT_HEX16_WITHIN (delta, expected, actual)`
398

T
toby 已提交
399
##### `TEST_ASSERT_HEX32_WITHIN (delta, expected, actual)`
400

T
toby 已提交
401 402
##### `TEST_ASSERT_HEX64_WITHIN (delta, expected, actual)`

403 404
##### `TEST_ASSERT_CHAR_WITHIN (delta, expected, actual)`

T
toby 已提交
405
### Structs and Strings
406

T
toby 已提交
407
##### `TEST_ASSERT_EQUAL_PTR (expected, actual)`
408

T
toby 已提交
409 410
Asserts that the pointers point to the same memory location.

411

T
toby 已提交
412
##### `TEST_ASSERT_EQUAL_STRING (expected, actual)`
413 414 415 416

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
T
toby 已提交
417 418
equivalent.

419

T
toby 已提交
420
##### `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`
421 422 423

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
T
toby 已提交
424 425
the `len` parameter.

426

T
toby 已提交
427
### Arrays
428 429

`expected` and `actual` parameters are both arrays. `num_elements` specifies the
T
toby 已提交
430 431
number of elements in the arrays to compare.

432
`_HEX` assertions produce failure messages with expected and actual array
T
toby 已提交
433 434
contents formatted in hexadecimal.

435
For array of strings comparison behavior, see comments for
T
toby 已提交
436 437
`TEST_ASSERT_EQUAL_STRING` in the preceding section.

438
Assertions fail upon the first element in the compared arrays found not to
T
toby 已提交
439 440 441
match. Failure messages specify the array index of the failed comparison.

##### `TEST_ASSERT_EQUAL_INT_ARRAY (expected, actual, num_elements)`
442

T
toby 已提交
443
##### `TEST_ASSERT_EQUAL_INT8_ARRAY (expected, actual, num_elements)`
444

T
toby 已提交
445
##### `TEST_ASSERT_EQUAL_INT16_ARRAY (expected, actual, num_elements)`
446

T
toby 已提交
447
##### `TEST_ASSERT_EQUAL_INT32_ARRAY (expected, actual, num_elements)`
448

T
toby 已提交
449
##### `TEST_ASSERT_EQUAL_INT64_ARRAY (expected, actual, num_elements)`
450

T
toby 已提交
451
##### `TEST_ASSERT_EQUAL_UINT_ARRAY (expected, actual, num_elements)`
452

T
toby 已提交
453
##### `TEST_ASSERT_EQUAL_UINT8_ARRAY (expected, actual, num_elements)`
454

T
toby 已提交
455
##### `TEST_ASSERT_EQUAL_UINT16_ARRAY (expected, actual, num_elements)`
456

T
toby 已提交
457
##### `TEST_ASSERT_EQUAL_UINT32_ARRAY (expected, actual, num_elements)`
458

T
toby 已提交
459
##### `TEST_ASSERT_EQUAL_UINT64_ARRAY (expected, actual, num_elements)`
460

T
toby 已提交
461
##### `TEST_ASSERT_EQUAL_HEX_ARRAY (expected, actual, num_elements)`
462

T
toby 已提交
463
##### `TEST_ASSERT_EQUAL_HEX8_ARRAY (expected, actual, num_elements)`
464

T
toby 已提交
465
##### `TEST_ASSERT_EQUAL_HEX16_ARRAY (expected, actual, num_elements)`
466

T
toby 已提交
467
##### `TEST_ASSERT_EQUAL_HEX32_ARRAY (expected, actual, num_elements)`
468

T
toby 已提交
469
##### `TEST_ASSERT_EQUAL_HEX64_ARRAY (expected, actual, num_elements)`
470

471 472
##### `TEST_ASSERT_EQUAL_CHAR_ARRAY (expected, actual, num_elements)`

T
toby 已提交
473
##### `TEST_ASSERT_EQUAL_PTR_ARRAY (expected, actual, num_elements)`
474

T
toby 已提交
475
##### `TEST_ASSERT_EQUAL_STRING_ARRAY (expected, actual, num_elements)`
476

T
toby 已提交
477
##### `TEST_ASSERT_EQUAL_MEMORY_ARRAY (expected, actual, len, num_elements)`
478

T
toby 已提交
479 480
`len` is the memory in bytes to be compared at each array element.

D
Dom Postorivo 已提交
481 482 483 484 485 486 487
### Integer Array Ranges (of all sizes)

These assertions verify that the `expected` array parameter is within +/- `delta`
(inclusive) of the `actual` array parameter. For example, if the expected value is
\[10, 12\] and the delta is 3 then the assertion will fail for any value
outside the range of \[7 - 13, 9 - 15\].

488
##### `TEST_ASSERT_INT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
489

490
##### `TEST_ASSERT_INT8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
491

492
##### `TEST_ASSERT_INT16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
493

494
##### `TEST_ASSERT_INT32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
495

496
##### `TEST_ASSERT_INT64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
497

498
##### `TEST_ASSERT_UINT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
499

500
##### `TEST_ASSERT_UINT8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
501

502
##### `TEST_ASSERT_UINT16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
503

504
##### `TEST_ASSERT_UINT32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
505

506
##### `TEST_ASSERT_UINT64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
507

508
##### `TEST_ASSERT_HEX_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
509

510
##### `TEST_ASSERT_HEX8_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
511

512
##### `TEST_ASSERT_HEX16_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
513

514
##### `TEST_ASSERT_HEX32_ARRAY_WITHIN (delta, expected, actual, num_elements)`
D
Dom Postorivo 已提交
515

516
##### `TEST_ASSERT_HEX64_ARRAY_WITHIN (delta, expected, actual, num_elements)`
517

518 519
##### `TEST_ASSERT_CHAR_ARRAY_WITHIN (delta, expected, actual, num_elements)`

T
toby 已提交
520 521 522 523 524
### 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.

M
Mark VanderVoord 已提交
525
`_HEX` assertions produce failure messages with expected and actual array
T
toby 已提交
526 527
contents formatted in hexadecimal.

M
Mark VanderVoord 已提交
528
Assertions fail upon the first element in the compared arrays found not to
T
toby 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
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)`

561 562
#### `TEST_ASSERT_EACH_EQUAL_CHAR (expected, actual, num_elements)`

T
toby 已提交
563 564 565 566 567 568 569 570 571
#### `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.


T
toby 已提交
572
### Floating Point (If enabled)
573

T
toby 已提交
574
##### `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`
575 576 577

Asserts that the `actual` value is within +/- `delta` of the `expected` value.
The nature of floating point representation is such that exact evaluations of
T
toby 已提交
578 579
equality are not guaranteed.

580

T
toby 已提交
581
##### `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
582 583 584 585 586

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
T
toby 已提交
587 588
code generation conventions for CMock.

589

T
toby 已提交
590
##### `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
591 592 593 594

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
T
toby 已提交
595 596
array assertion.

597

T
toby 已提交
598
##### `TEST_ASSERT_FLOAT_IS_INF (actual)`
599 600

Asserts that `actual` parameter is equivalent to positive infinity floating
T
toby 已提交
601 602
point representation.

603

T
toby 已提交
604
##### `TEST_ASSERT_FLOAT_IS_NEG_INF (actual)`
605 606

Asserts that `actual` parameter is equivalent to negative infinity floating
T
toby 已提交
607 608
point representation.

609

T
toby 已提交
610
##### `TEST_ASSERT_FLOAT_IS_NAN (actual)`
611

T
toby 已提交
612 613
Asserts that `actual` parameter is a Not A Number floating point representation.

614

T
toby 已提交
615
##### `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`
616 617 618

Asserts that ?actual?parameter is a floating point representation usable for
mathematical operations. That is, the `actual` parameter is neither positive
T
toby 已提交
619 620
infinity nor negative infinity nor Not A Number floating point representations.

621

T
toby 已提交
622
##### `TEST_ASSERT_FLOAT_IS_NOT_INF (actual)`
623 624

Asserts that `actual` parameter is a value other than positive infinity floating
T
toby 已提交
625 626
point representation.

627

T
toby 已提交
628
##### `TEST_ASSERT_FLOAT_IS_NOT_NEG_INF (actual)`
629 630

Asserts that `actual` parameter is a value other than negative infinity floating
T
toby 已提交
631 632
point representation.

633

T
toby 已提交
634
##### `TEST_ASSERT_FLOAT_IS_NOT_NAN (actual)`
635 636

Asserts that `actual` parameter is a value other than Not A Number floating
T
toby 已提交
637 638
point representation.

639

T
toby 已提交
640
##### `TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE (actual)`
641 642 643

Asserts that `actual` parameter is not usable for mathematical operations. That
is, the `actual` parameter is either positive infinity or negative infinity or
T
toby 已提交
644 645
Not A Number floating point representations.

646

T
toby 已提交
647
### Double (If enabled)
648

T
toby 已提交
649
##### `TEST_ASSERT_DOUBLE_WITHIN (delta, expected, actual)`
650 651 652

Asserts that the `actual` value is within +/- `delta` of the `expected` value.
The nature of floating point representation is such that exact evaluations of
T
toby 已提交
653 654
equality are not guaranteed.

655

T
toby 已提交
656
##### `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
657 658 659 660 661 662 663

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.

T
toby 已提交
664 665

##### `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
666 667 668 669

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
T
toby 已提交
670 671
assertion.

672

T
toby 已提交
673
##### `TEST_ASSERT_DOUBLE_IS_INF (actual)`
674 675

Asserts that `actual` parameter is equivalent to positive infinity floating
T
toby 已提交
676 677
point representation.

678

T
toby 已提交
679
##### `TEST_ASSERT_DOUBLE_IS_NEG_INF (actual)`
680 681

Asserts that `actual` parameter is equivalent to negative infinity floating point
T
toby 已提交
682 683
representation.

684

T
toby 已提交
685
##### `TEST_ASSERT_DOUBLE_IS_NAN (actual)`
686

T
toby 已提交
687 688
Asserts that `actual` parameter is a Not A Number floating point representation.

689

T
toby 已提交
690
##### `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`
691 692 693

Asserts that `actual` parameter is a floating point representation usable for
mathematical operations. That is, the ?actual?parameter is neither positive
T
toby 已提交
694 695
infinity nor negative infinity nor Not A Number floating point representations.

696

T
toby 已提交
697
##### `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
698 699

Asserts that `actual` parameter is a value other than positive infinity floating
T
toby 已提交
700 701
point representation.

702

T
toby 已提交
703
##### `TEST_ASSERT_DOUBLE_IS_NOT_NEG_INF (actual)`
704 705

Asserts that `actual` parameter is a value other than negative infinity floating
T
toby 已提交
706 707
point representation.

708

T
toby 已提交
709
##### `TEST_ASSERT_DOUBLE_IS_NOT_NAN (actual)`
710 711

Asserts that `actual` parameter is a value other than Not A Number floating
T
toby 已提交
712 713
point representation.

714

T
toby 已提交
715
##### `TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE (actual)`
716 717 718

Asserts that `actual` parameter is not usable for mathematical operations. That
is, the `actual` parameter is either positive infinity or negative infinity or
T
toby 已提交
719 720
Not A Number floating point representations.

721

T
toby 已提交
722
## Advanced Asserting: Details On Tricky Assertions
723 724 725 726 727 728 729

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.

T
toby 已提交
730 731

### How do the EQUAL assertions work for FLOAT and DOUBLE?
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

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.

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
T
toby 已提交
748 749
range around the expected value.

750 751 752 753
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
T
toby 已提交
754 755
point value.

756 757
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
D
Deryew 已提交
758
0 x 20 or 0 x 263.It's still zero, right? Luckily, if you
759 760
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
T
toby 已提交
761 762
works!

763
Double precision floating point numbers use a much smaller multiplier, again
T
toby 已提交
764 765
approximating a single bit of error.

766 767 768
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
T
toby 已提交
769 770
documentation for more.

771

T
toby 已提交
772
### How do we deal with targets with non-standard int sizes?
773 774 775 776 777

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:
T
toby 已提交
778 779 780 781 782

```C
char <= short <= int <= long <= long long
```

783 784
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,
T
toby 已提交
785 786
and this remains perfectly standard C.

787 788 789 790 791
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
T
toby 已提交
792 793 794 795 796 797
something like this:

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

798
Um... yikes. It's obviously breaking a rule or two... but they had to break SOME
T
toby 已提交
799 800
rules, so they made a choice.

801 802 803 804
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
T
toby 已提交
805 806
above. Sometimes because they don't feel like it?).

807 808 809
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
T
toby 已提交
810 811
you're going to do this effectively on some of these more idiosyncratic targets.

812 813 814
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
T
toby 已提交
815 816
on both of these in Unity's documentation.

817 818 819 820
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
T
toby 已提交
821 822
affect you:

823
1. When Unity displays errors for you, it's going to pad the upper unused bits
T
toby 已提交
824
with zeros.
825 826 827
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
M
Mark VanderVoord 已提交
828 829 830 831
always back down to a simple `TEST_ASSERT` and do the operations yourself.


*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*