@@ -10,46 +10,46 @@ Upon boolean False, an assertion stops execution and reports the failure.
10
10
and easily execute those assertions.
11
11
- The structure of Unity allows you to easily separate test assertions from
12
12
source code in, well, test code.
13
- - Unity' s assertions:
14
- - Come in many, many flavors to handle different C types and assertion cases.
15
- - Use context to provide detailed and helpful failure messages.
16
- - Document types, expected values, and basic behavior in your source code for
13
+ - Unity’ s assertions:
14
+ - Come in many, many flavors to handle different C types and assertion cases.
15
+ - Use context to provide detailed and helpful failure messages.
16
+ - Document types, expected values, and basic behavior in your source code for
17
17
free.
18
18
19
- ### Unity Is Several Things But Mainly It' s Assertions
19
+ ### Unity Is Several Things But Mainly It’ s Assertions
20
20
21
21
One way to think of Unity is simply as a rich collection of assertions you can
22
22
use to establish whether your source code behaves the way you think it does.
23
23
Unity provides a framework to easily organize and execute those assertions in
24
24
test code separate from your source code.
25
25
26
- ### What' s an Assertion?
26
+ ### What’ s an Assertion?
27
27
28
28
At their core, assertions are an establishment of truth - boolean truth. Was this
29
29
thing equal to that thing? Does that code doohickey have such-and-such property
30
- or not? You get the idea. Assertions are executable code (to appreciate the big
31
- picture on this read up on the difference between
32
- [ link : Dynamic Verification and Static Analysis ] ) . A failing assertion stops
33
- execution and reports an error through some appropriate I/O channel (e.g.
34
- stdout, GUI, file, blinky light).
30
+ or not? You get the idea. Assertions are executable code. Static analysis is a
31
+ valuable approach to improving code quality, but it is not executing your code
32
+ in the way an assertion can . A failing assertion stops execution and reports an
33
+ error through some appropriate I/O channel (e.g. stdout, GUI, output file,
34
+ blinky light).
35
35
36
36
Fundamentally, for dynamic verification all you need is a single assertion
37
- mechanism. In fact, that' s what the [ assert() macro] [ ] in C' s standard library
37
+ mechanism. In fact, that’ s what the [ assert() macro] [ ] in C’ s standard library
38
38
is for. So why not just use it? Well, we can do far better in the reporting
39
- department. C' s ` assert() ` is pretty dumb as-is and is particularly poor for
39
+ department. C’ s ` assert() ` is pretty dumb as-is and is particularly poor for
40
40
handling common data types like arrays, structs, etc. And, without some other
41
- support, it' s far too tempting to litter source code with C' s ` assert() ` ' s. It' s
41
+ support, it’ s far too tempting to litter source code with C’ s ` assert() ` ’ s. It’ s
42
42
generally much cleaner, manageable, and more useful to separate test and source
43
43
code in the way Unity facilitates.
44
44
45
- ### Unity' s Assertions: Helpful Messages _ and_ Free Source Code Documentation
45
+ ### Unity’ s Assertions: Helpful Messages _ and_ Free Source Code Documentation
46
46
47
47
Asserting a simple truth condition is valuable, but using the context of the
48
- assertion is even more valuable. For instance, if you know you' re comparing bit
48
+ assertion is even more valuable. For instance, if you know you’ re comparing bit
49
49
flags and not just integers, then why not use that context to give explicit,
50
50
readable, bit-level feedback when an assertion fails?
51
51
52
- That' s what Unity' s collection of assertions do - capture context to give you
52
+ That’ s what Unity’ s collection of assertions do - capture context to give you
53
53
helpful, meaningful assertion failure messages. In fact, the assertions
54
54
themselves also serve as executable documentation about types and values in your
55
55
source code. So long as your tests remain current with your source and all those
@@ -73,12 +73,12 @@ a simple null check).
73
73
- `Actual` is the value being tested and unlike the other parameters in an
74
74
assertion construction is the only parameter present in all assertion variants.
75
75
- `Modifiers` are masks, ranges, bit flag specifiers, floating point deltas.
76
- - `Expected` is your expected value (duh) to compare to an `actual` value; it' s
76
+ - `Expected` is your expected value (duh) to compare to an `actual` value; it’ s
77
77
marked as an optional parameter because some assertions only need a single
78
78
`actual` parameter (e.g. null check).
79
79
- `Size/count` refers to string lengths, number of array elements, etc.
80
80
81
- Many of Unity' s assertions are clear duplications in that the same data type
81
+ Many of Unity’ s assertions are clear duplications in that the same data type
82
82
is handled by several assertions. The differences among these are in how failure
83
83
messages are presented. For instance, a `_HEX` variant of an assertion prints
84
84
the expected and actual values of that assertion formatted as hexadecimal.
@@ -99,7 +99,7 @@ _Example:_
99
99
TEST_ASSERT_X( {modifiers}, {expected}, actual, {size/count} )
100
100
```
101
101
102
- becomes messageified like thus...
102
+ becomes messageified like thus…
103
103
104
104
``` c
105
105
TEST_ASSERT_X_MESSAGE ( {modifiers}, {expected}, actual, {size/count}, message )
@@ -108,7 +108,7 @@ TEST_ASSERT_X_MESSAGE( {modifiers}, {expected}, actual, {size/count}, message )
108
108
Notes:
109
109
110
110
- The `_MESSAGE` variants intentionally do not support `printf` style formatting
111
- since many embedded projects don' t support or avoid `printf` for various reasons.
111
+ since many embedded projects don’ t support or avoid `printf` for various reasons.
112
112
It is possible to use `sprintf` before the assertion to assemble a complex fail
113
113
message, if necessary.
114
114
- If you want to output a counter value within an assertion fail message (e.g. from
@@ -119,7 +119,7 @@ Notes:
119
119
120
120
Unity provides a collection of assertions for arrays containing a variety of
121
121
types. These are documented in the Array section below. These are almost on par
122
- with the `_MESSAGE`variants of Unity' s Asserts in that for pretty much any Unity
122
+ with the `_MESSAGE`variants of Unity’ s Asserts in that for pretty much any Unity
123
123
type assertion you can tack on `_ARRAY` and run assertions on an entire block of
124
124
memory.
125
125
@@ -144,7 +144,7 @@ Notes:
144
144
Unity provides a collection of assertions for arrays containing a variety of
145
145
types which can be compared to a single value as well. These are documented in
146
146
the Each Equal section below. these are almost on par with the ` _MESSAGE `
147
- variants of Unity' s Asserts in that for pretty much any Unity type assertion you
147
+ variants of Unity’ s Asserts in that for pretty much any Unity type assertion you
148
148
can inject ` _EACH_EQUAL ` and run assertions on an entire block of memory.
149
149
150
150
``` c
@@ -203,7 +203,7 @@ code then verifies as a final step.
203
203
#### `TEST_PASS_MESSAGE("message")`
204
204
205
205
This will abort the remainder of the test, but count the test as a pass. Under
206
- normal circumstances, it is not necessary to include this macro in your tests...
206
+ normal circumstances, it is not necessary to include this macro in your tests…
207
207
a lack of failure will automatically be counted as a `PASS`. It is occasionally
208
208
useful for tests with `#ifdef`s and such.
209
209
@@ -392,7 +392,7 @@ Asserts that the pointers point to the same memory location.
392
392
393
393
#### `TEST_ASSERT_EQUAL_STRING (expected, actual)`
394
394
395
- Asserts that the null terminated (`'\0' `)strings are identical. If strings are
395
+ Asserts that the null terminated (`’\0’ `)strings are identical. If strings are
396
396
of different lengths or any portion of the strings before their terminators
397
397
differ, the assertion fails. Two NULL strings (i.e. zero length) are considered
398
398
equivalent.
@@ -561,15 +561,15 @@ Asserts that the `actual` value is NOT within +/- `delta` of the `expected` valu
561
561
562
562
#### `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`
563
563
564
- Asserts that the `actual` value is " close enough to be considered equal" to the
564
+ Asserts that the `actual` value is “ close enough to be considered equal” to the
565
565
`expected` value. If you are curious about the details, refer to the Advanced
566
566
Asserting section for more details on this. Omitting a user-specified delta in a
567
567
floating point assertion is both a shorthand convenience and a requirement of
568
568
code generation conventions for CMock.
569
569
570
570
#### `TEST_ASSERT_NOT_EQUAL_FLOAT (expected, actual)`
571
571
572
- Asserts that the `actual` value is NOT " close enough to be considered equal" to the
572
+ Asserts that the `actual` value is NOT “ close enough to be considered equal” to the
573
573
`expected` value.
574
574
575
575
#### `TEST_ASSERT_FLOAT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
@@ -662,15 +662,15 @@ Asserts that the `actual` value is NOT within +/- `delta` of the `expected` valu
662
662
663
663
#### `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`
664
664
665
- Asserts that the `actual` value is " close enough to be considered equal" to the
665
+ Asserts that the `actual` value is “ close enough to be considered equal” to the
666
666
`expected` value. If you are curious about the details, refer to the Advanced
667
667
Asserting section for more details. Omitting a user-specified delta in a
668
668
floating point assertion is both a shorthand convenience and a requirement of
669
669
code generation conventions for CMock.
670
670
671
671
#### `TEST_ASSERT_NOT_EQUAL_DOUBLE (expected, actual)`
672
672
673
- Asserts that the `actual` value is NOT " close enough to be considered equal" to the
673
+ Asserts that the `actual` value is NOT “ close enough to be considered equal” to the
674
674
`expected` value.
675
675
676
676
#### `TEST_ASSERT_DOUBLE_ARRAY_WITHIN (delta, expected, actual, num_elements)`
@@ -753,7 +753,7 @@ Not A Number floating point representations.
753
753
754
754
This section helps you understand how to deal with some of the trickier
755
755
assertion situations you may run into. It will give you a glimpse into some of
756
- the under-the-hood details of Unity' s assertion mechanisms. If you' re one of
756
+ the under-the-hood details of Unity’ s assertion mechanisms. If you’ re one of
757
757
those people who likes to know what is going on in the background, read on. If
758
758
not, feel free to ignore the rest of this document until you need it.
759
759
@@ -768,9 +768,9 @@ mathematical operations might result in a representation of 8 x 2-2
768
768
that also evaluates to a value of 2. At some point repeated operations cause
769
769
equality checks to fail.
770
770
771
- So Unity doesn' t do direct floating point comparisons for equality. Instead, it
772
- checks if two floating point values are " really close." If you leave Unity
773
- running with defaults, " really close" means " within a significant bit or two."
771
+ So Unity doesn’ t do direct floating point comparisons for equality. Instead, it
772
+ checks if two floating point values are “ really close.” If you leave Unity
773
+ running with defaults, “ really close” means “ within a significant bit or two.”
774
774
Under the hood, `TEST_ASSERT_EQUAL_FLOAT` is really `TEST_ASSERT_FLOAT_WITHIN`
775
775
with the `delta` parameter calculated on the fly. For single precision, delta is
776
776
the expected value multiplied by 0.00001, producing a very small proportional
@@ -779,28 +779,27 @@ range around the expected value.
779
779
If you are expecting a value of 20,000.0 the delta is calculated to be 0.2. So
780
780
any value between 19,999.8 and 20,000.2 will satisfy the equality check. This
781
781
works out to be roughly a single bit of range for a single-precision number, and
782
- that' s just about as tight a tolerance as you can reasonably get from a floating
782
+ that’ s just about as tight a tolerance as you can reasonably get from a floating
783
783
point value.
784
784
785
- So what happens when it's zero? Zero - even more than other floating point
786
- values - can be represented many different ways. It doesn't matter if you have
787
- 0 x 20 or 0 x 263.It's still zero, right? Luckily, if you
788
- subtract these values from each other, they will always produce a difference of
789
- zero, which will still fall between 0 plus or minus a delta of 0. So it still
790
- works!
785
+ So what happens when it’s zero? Zero - even more than other floating point
786
+ values - can be represented many different ways. It doesn’t matter if you have
787
+ 0x20 or 0x263. It’s still zero, right? Luckily, if you subtract these
788
+ values from each other, they will always produce a difference of zero, which
789
+ will still fall between 0 plus or minus a delta of 0. So it still works!
791
790
792
791
Double precision floating point numbers use a much smaller multiplier, again
793
792
approximating a single bit of error.
794
793
795
- If you don' t like these ranges and you want to make your floating point equality
794
+ If you don’ t like these ranges and you want to make your floating point equality
796
795
assertions less strict, you can change these multipliers to whatever you like by
797
796
defining UNITY_FLOAT_PRECISION and UNITY_DOUBLE_PRECISION. See Unity
798
797
documentation for more.
799
798
800
799
### How do we deal with targets with non-standard int sizes?
801
800
802
- It's " fun" that C is a standard where something as fundamental as an integer
803
- varies by target. According to the C standard, an `int` is to be the target' s
801
+ It’s “ fun” that C is a standard where something as fundamental as an integer
802
+ varies by target. According to the C standard, an `int` is to be the target’ s
804
803
natural register size, and it should be at least 16-bits and a multiple of a
805
804
byte. It also guarantees an order of sizes:
806
805
@@ -814,7 +813,7 @@ and this remains perfectly standard C.
814
813
815
814
To make things even more interesting, there are compilers and targets out there
816
815
that have a hard choice to make. What if their natural register size is 10-bits
817
- or 12-bits? Clearly they can' t fulfill _ both_ the requirement to be at least
816
+ or 12-bits? Clearly they can’ t fulfill _ both_ the requirement to be at least
818
817
16-bits AND the requirement to match the natural register size. In these
819
818
situations, they often choose the natural register size, leaving us with
820
819
something like this:
@@ -823,34 +822,34 @@ something like this:
823
822
char (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit)
824
823
```
825
824
826
- Um... yikes. It' s obviously breaking a rule or two... but they had to break SOME
825
+ Um… yikes. It’ s obviously breaking a rule or two… but they had to break SOME
827
826
rules, so they made a choice.
828
827
829
828
When the C99 standard rolled around, it introduced alternate standard-size types.
830
829
It also introduced macros for pulling in MIN/MAX values for your integer types.
831
- It' s glorious! Unfortunately, many embedded compilers can' t be relied upon to
830
+ It’ s glorious! Unfortunately, many embedded compilers can’ t be relied upon to
832
831
use the C99 types (Sometimes because they have weird register sizes as described
833
- above. Sometimes because they don' t feel like it?).
832
+ above. Sometimes because they don’ t feel like it?).
834
833
835
834
A goal of Unity from the beginning was to support every combination of
836
- microcontroller or microprocessor and C compiler. Over time, we' ve gotten really
835
+ microcontroller or microprocessor and C compiler. Over time, we’ ve gotten really
837
836
close to this. There are a few tricks that you should be aware of, though, if
838
- you' re going to do this effectively on some of these more idiosyncratic targets.
837
+ you’ re going to do this effectively on some of these more idiosyncratic targets.
839
838
840
- First, when setting up Unity for a new target, you' re going to want to pay
839
+ First, when setting up Unity for a new target, you’ re going to want to pay
841
840
special attention to the macros for automatically detecting types
842
841
(where available) or manually configuring them yourself. You can get information
843
- on both of these in Unity' s documentation.
842
+ on both of these in Unity’ s documentation.
844
843
845
844
What about the times where you suddenly need to deal with something odd, like a
846
845
24-bit ` int ` ? The simplest solution is to use the next size up. If you have a
847
846
24-bit ` int ` , configure Unity to use 32-bit integers. If you have a 12-bit
848
847
` int ` , configure Unity to use 16 bits. There are two ways this is going to
849
848
affect you:
850
849
851
- 1 . When Unity displays errors for you, it' s going to pad the upper unused bits
850
+ 1 . When Unity displays errors for you, it’ s going to pad the upper unused bits
852
851
with zeros.
853
- 2 . You' re going to have to be careful of assertions that perform signed
852
+ 2 . You’ re going to have to be careful of assertions that perform signed
854
853
operations, particularly ` TEST_ASSERT_INT_WITHIN ` . Such assertions might wrap
855
854
your ` int ` in the wrong place, and you could experience false failures. You can
856
855
always back down to a simple ` TEST_ASSERT ` and do the operations yourself.
0 commit comments