• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Unity Configuration Guide
2
3## C Standards, Compilers and Microcontrollers
4
5The embedded software world contains its challenges. Compilers support different
6revisions of the C Standard. They ignore requirements in places, sometimes to
7make the language more usable in some special regard. Sometimes it's to simplify
8their support. Sometimes it's due to specific quirks of the microcontroller they
9are targeting. Simulators add another dimension to this menagerie.
10
11Unity is designed to run on almost anything that is targeted by a C compiler. It
12would be awesome if this could be done with zero configuration. While there are
13some targets that come close to this dream, it is sadly not universal. It is
14likely that you are going to need at least a couple of the configuration options
15described in this document.
16
17All of Unity's configuration options are `#defines`. Most of these are simple
18definitions. A couple are macros with arguments. They live inside the
19unity_internals.h header file. We don't necessarily recommend opening that file
20unless you really need to. That file is proof that a cross-platform library is
21challenging to build. From a more positive perspective, it is also proof that a
22great deal of complexity can be centralized primarily to one place to
23provide a more consistent and simple experience elsewhere.
24
25
26### Using These Options
27
28It doesn't matter if you're using a target-specific compiler and a simulator or
29a native compiler. In either case, you've got a couple choices for configuring
30these options:
31
321. Because these options are specified via C defines, you can pass most of these
33options to your compiler through command line compiler flags. Even if you're
34using an embedded target that forces you to use their overbearing IDE for all
35configuration, there will be a place somewhere in your project to configure
36defines for your compiler.
372. You can create a custom `unity_config.h` configuration file (present in your
38toolchain's search paths). In this file, you will list definitions and macros
39specific to your target. All you must do is define `UNITY_INCLUDE_CONFIG_H` and
40Unity will rely on `unity_config.h` for any further definitions it may need.
41
42Unfortunately, it doesn't usually work well to just #define these things in the
43test itself. These defines need to take effect where ever unity.h is included.
44This would be test test, the test runner (if you're generating one), and from
45unity.c when it's compiled.
46
47## The Options
48
49### Integer Types
50
51If you've been a C developer for long, you probably already know that C's
52concept of an integer varies from target to target. The C Standard has rules
53about the `int` matching the register size of the target microprocessor. It has
54rules about the `int` and how its size relates to other integer types. An `int`
55on one target might be 16 bits while on another target it might be 64. There are
56more specific types in compilers compliant with C99 or later, but that's
57certainly not every compiler you are likely to encounter. Therefore, Unity has a
58number of features for helping to adjust itself to match your required integer
59sizes. It starts off by trying to do it automatically.
60
61
62##### `UNITY_EXCLUDE_STDINT_H`
63
64The first thing that Unity does to guess your types is check `stdint.h`.
65This file includes defines like `UINT_MAX` that Unity can use to
66learn a lot about your system. It's possible you don't want it to do this
67(um. why not?) or (more likely) it's possible that your system doesn't
68support `stdint.h`. If that's the case, you're going to want to define this.
69That way, Unity will know to skip the inclusion of this file and you won't
70be left with a compiler error.
71
72_Example:_
73```C
74#define UNITY_EXCLUDE_STDINT_H
75```
76
77
78##### `UNITY_EXCLUDE_LIMITS_H`
79
80The second attempt to guess your types is to check `limits.h`. Some compilers
81that don't support `stdint.h` could include `limits.h` instead. If you don't
82want Unity to check this file either, define this to make it skip the inclusion.
83
84_Example:_
85```C
86#define UNITY_EXCLUDE_LIMITS_H
87```
88
89If you've disabled both of the automatic options above, you're going to have to
90do the configuration yourself. Don't worry. Even this isn't too bad... there are
91just a handful of defines that you are going to specify if you don't like the
92defaults.
93
94
95##### `UNITY_INT_WIDTH`
96
97Define this to be the number of bits an `int` takes up on your system. The
98default, if not autodetected, is 32 bits.
99
100_Example:_
101```C
102#define UNITY_INT_WIDTH 16
103```
104
105
106##### `UNITY_LONG_WIDTH`
107
108Define this to be the number of bits a `long` takes up on your system. The
109default, if not autodetected, is 32 bits. This is used to figure out what kind
110of 64-bit support your system can handle. Does it need to specify a `long` or a
111`long long` to get a 64-bit value. On 16-bit systems, this option is going to be
112ignored.
113
114_Example:_
115```C
116#define UNITY_LONG_WIDTH 16
117```
118
119
120##### `UNITY_POINTER_WIDTH`
121
122Define this to be the number of bits a pointer takes up on your system. The
123default, if not autodetected, is 32-bits. If you're getting ugly compiler
124warnings about casting from pointers, this is the one to look at.
125
126_Hint:_ In order to support exotic processors (for example TI C55x with a pointer
127width of 23-bit), choose the next power of two (in this case 32-bit).
128
129_Supported values:_ 16, 32 and 64
130
131_Example:_
132```C
133// Choose on of these #defines to set your pointer width (if not autodetected)
134//#define UNITY_POINTER_WIDTH 16
135//#define UNITY_POINTER_WIDTH 32
136#define UNITY_POINTER_WIDTH 64 // Set UNITY_POINTER_WIDTH to 64-bit
137```
138
139
140##### `UNITY_SUPPORT_64`
141
142Unity will automatically include 64-bit support if it auto-detects it, or if
143your `int`, `long`, or pointer widths are greater than 32-bits. Define this to
144enable 64-bit support if none of the other options already did it for you. There
145can be a significant size and speed impact to enabling 64-bit support on small
146targets, so don't define it if you don't need it.
147
148_Example:_
149```C
150#define UNITY_SUPPORT_64
151```
152
153
154### Floating Point Types
155
156In the embedded world, it's not uncommon for targets to have no support for
157floating point operations at all or to have support that is limited to only
158single precision. We are able to guess integer sizes on the fly because integers
159are always available in at least one size. Floating point, on the other hand, is
160sometimes not available at all. Trying to include `float.h` on these platforms
161would result in an error. This leaves manual configuration as the only option.
162
163
164##### `UNITY_INCLUDE_FLOAT`
165
166##### `UNITY_EXCLUDE_FLOAT`
167
168##### `UNITY_INCLUDE_DOUBLE`
169
170##### `UNITY_EXCLUDE_DOUBLE`
171
172By default, Unity guesses that you will want single precision floating point
173support, but not double precision. It's easy to change either of these using the
174include and exclude options here. You may include neither, either, or both, as
175suits your needs. For features that are enabled, the following floating point
176options also become available.
177
178_Example:_
179```C
180//what manner of strange processor is this?
181#define UNITY_EXCLUDE_FLOAT
182#define UNITY_INCLUDE_DOUBLE
183```
184
185
186##### `UNITY_EXCLUDE_FLOAT_PRINT`
187
188Unity aims for as small of a footprint as possible and avoids most standard
189library calls (some embedded platforms don’t have a standard library!). Because
190of this, its routines for printing integer values are minimalist and hand-coded.
191Therefore, the display of floating point values during a failure are optional.
192By default, Unity will print the actual results of floating point assertion
193failure (e.g. ”Expected 4.56 Was 4.68”). To not include this extra support, you
194can use this define to instead respond to a failed assertion with a message like
195”Values Not Within Delta”. If you would like verbose failure messages for floating
196point assertions, use these options to give more explicit failure messages.
197
198_Example:_
199```C
200#define UNITY_EXCLUDE_FLOAT_PRINT
201```
202
203
204##### `UNITY_FLOAT_TYPE`
205
206If enabled, Unity assumes you want your `FLOAT` asserts to compare standard C
207floats. If your compiler supports a specialty floating point type, you can
208always override this behavior by using this definition.
209
210_Example:_
211```C
212#define UNITY_FLOAT_TYPE float16_t
213```
214
215
216##### `UNITY_DOUBLE_TYPE`
217
218If enabled, Unity assumes you want your `DOUBLE` asserts to compare standard C
219doubles. If you would like to change this, you can specify something else by
220using this option. For example, defining `UNITY_DOUBLE_TYPE` to `long double`
221could enable gargantuan floating point types on your 64-bit processor instead of
222the standard `double`.
223
224_Example:_
225```C
226#define UNITY_DOUBLE_TYPE long double
227```
228
229
230##### `UNITY_FLOAT_PRECISION`
231
232##### `UNITY_DOUBLE_PRECISION`
233
234If you look up `UNITY_ASSERT_EQUAL_FLOAT` and `UNITY_ASSERT_EQUAL_DOUBLE` as
235documented in the big daddy Unity Assertion Guide, you will learn that they are
236not really asserting that two values are equal but rather that two values are
237"close enough" to equal. "Close enough" is controlled by these precision
238configuration options. If you are working with 32-bit floats and/or 64-bit
239doubles (the normal on most processors), you should have no need to change these
240options. They are both set to give you approximately 1 significant bit in either
241direction. The float precision is 0.00001 while the double is 10-12.
242For further details on how this works, see the appendix of the Unity Assertion
243Guide.
244
245_Example:_
246```C
247#define UNITY_FLOAT_PRECISION 0.001f
248```
249
250
251### Miscellaneous
252
253##### `UNITY_EXCLUDE_STDDEF_H`
254
255Unity uses the `NULL` macro, which defines the value of a null pointer constant,
256defined in `stddef.h` by default. If you want to provide
257your own macro for this, you should exclude the `stddef.h` header file by adding this
258define to your configuration.
259
260_Example:_
261```C
262#define UNITY_EXCLUDE_STDDEF_H
263```
264
265
266#### `UNITY_INCLUDE_PRINT_FORMATTED`
267
268Unity provides a simple (and very basic) printf-like string output implementation,
269which is able to print a string modified by the following format string modifiers:
270
271- __%d__ - signed value (decimal)
272- __%i__ - same as __%i__
273- __%u__ - unsigned value (decimal)
274- __%f__ - float/Double (if float support is activated)
275- __%g__ - same as __%f__
276- __%b__ - binary prefixed with "0b"
277- __%x__ - hexadecimal (upper case) prefixed with "0x"
278- __%X__ - same as __%x__
279- __%p__ - pointer (same as __%x__ or __%X__)
280- __%c__ - a single character
281- __%s__ - a string (e.g. "string")
282- __%%__ - The "%" symbol (escaped)
283
284_Example:_
285```C
286#define UNITY_INCLUDE_PRINT_FORMATTED
287
288int a = 0xfab1;
289TEST_PRINTF("Decimal   %d\n", -7);
290TEST_PRINTF("Unsigned  %u\n", 987);
291TEST_PRINTF("Float     %f\n", 3.1415926535897932384);
292TEST_PRINTF("Binary    %b\n", 0xA);
293TEST_PRINTF("Hex       %X\n", 0xFAB);
294TEST_PRINTF("Pointer   %p\n", &a);
295TEST_PRINTF("Character %c\n", 'F');
296TEST_PRINTF("String    %s\n", "My string");
297TEST_PRINTF("Percent   %%\n");
298TEST_PRINTF("Color Red \033[41mFAIL\033[00m\n");
299TEST_PRINTF("\n");
300TEST_PRINTF("Multiple (%d) (%i) (%u) (%x)\n", -100, 0, 200, 0x12345);
301```
302
303
304### Toolset Customization
305
306In addition to the options listed above, there are a number of other options
307which will come in handy to customize Unity's behavior for your specific
308toolchain. It is possible that you may not need to touch any of these... but
309certain platforms, particularly those running in simulators, may need to jump
310through extra hoops to run properly. These macros will help in those
311situations.
312
313
314##### `UNITY_OUTPUT_CHAR(a)`
315
316##### `UNITY_OUTPUT_FLUSH()`
317
318##### `UNITY_OUTPUT_START()`
319
320##### `UNITY_OUTPUT_COMPLETE()`
321
322By default, Unity prints its results to `stdout` as it runs. This works
323perfectly fine in most situations where you are using a native compiler for
324testing. It works on some simulators as well so long as they have `stdout`
325routed back to the command line. There are times, however, where the simulator
326will lack support for dumping results or you will want to route results
327elsewhere for other reasons. In these cases, you should define the
328`UNITY_OUTPUT_CHAR` macro. This macro accepts a single character at a time (as
329an `int`, since this is the parameter type of the standard C `putchar` function
330most commonly used). You may replace this with whatever function call you like.
331
332_Example:_
333Say you are forced to run your test suite on an embedded processor with no
334`stdout` option. You decide to route your test result output to a custom serial
335`RS232_putc()` function you wrote like thus:
336```C
337#include "RS232_header.h"
338...
339#define UNITY_OUTPUT_CHAR(a)    RS232_putc(a)
340#define UNITY_OUTPUT_START()    RS232_config(115200,1,8,0)
341#define UNITY_OUTPUT_FLUSH()    RS232_flush()
342#define UNITY_OUTPUT_COMPLETE() RS232_close()
343```
344
345_Note:_
346`UNITY_OUTPUT_FLUSH()` can be set to the standard out flush function simply by
347specifying `UNITY_USE_FLUSH_STDOUT`. No other defines are required.
348
349
350##### `UNITY_OUTPUT_FOR_ECLIPSE`
351
352##### `UNITY_OUTPUT_FOR_IAR_WORKBENCH`
353
354##### `UNITY_OUTPUT_FOR_QT_CREATOR`
355
356When managing your own builds, it is often handy to have messages output in a format which is
357recognized by your IDE. These are some standard formats which can be supported. If you're using
358Ceedling to manage your builds, it is better to stick with the standard format (leaving these
359all undefined) and allow Ceedling to use its own decorators.
360
361
362##### `UNITY_PTR_ATTRIBUTE`
363
364Some compilers require a custom attribute to be assigned to pointers, like
365`near` or `far`. In these cases, you can give Unity a safe default for these by
366defining this option with the attribute you would like.
367
368_Example:_
369```C
370#define UNITY_PTR_ATTRIBUTE __attribute__((far))
371#define UNITY_PTR_ATTRIBUTE near
372```
373
374##### `UNITY_PRINT_EOL`
375
376By default, Unity outputs \n at the end of each line of output. This is easy
377to parse by the scripts, by Ceedling, etc, but it might not be ideal for YOUR
378system. Feel free to override this and to make it whatever you wish.
379
380_Example:_
381```C
382#define UNITY_PRINT_EOL { UNITY_OUTPUT_CHAR('\r'); UNITY_OUTPUT_CHAR('\n') }
383```
384
385
386##### `UNITY_EXCLUDE_DETAILS`
387
388This is an option for if you absolutely must squeeze every byte of memory out of
389your system. Unity stores a set of internal scratchpads which are used to pass
390extra detail information around. It's used by systems like CMock in order to
391report which function or argument flagged an error. If you're not using CMock and
392you're not using these details for other things, then you can exclude them.
393
394_Example:_
395```C
396#define UNITY_EXCLUDE_DETAILS
397```
398
399##### `UNITY_PRINT_TEST_CONTEXT`
400
401This option allows you to specify your own function to print additional context
402as part of the error message when a test has failed. It can be useful if you
403want to output some specific information about the state of the test at the point
404of failure, and `UNITY_SET_DETAILS` isn't flexible enough for your needs.
405
406_Example:_
407```C
408#define UNITY_PRINT_TEST_CONTEXT PrintIterationCount
409
410extern int iteration_count;
411
412void PrintIterationCount(void)
413{
414  UnityPrintFormatted("At iteration #%d: ", iteration_count);
415}
416```
417
418##### `UNITY_EXCLUDE_SETJMP`
419
420If your embedded system doesn't support the standard library setjmp, you can
421exclude Unity's reliance on this by using this define. This dropped dependence
422comes at a price, though. You will be unable to use custom helper functions for
423your tests, and you will be unable to use tools like CMock. Very likely, if your
424compiler doesn't support setjmp, you wouldn't have had the memory space for those
425things anyway, though... so this option exists for those situations.
426
427_Example:_
428```C
429#define UNITY_EXCLUDE_SETJMP
430```
431
432##### `UNITY_OUTPUT_COLOR`
433
434If you want to add color using ANSI escape codes you can use this define.
435
436_Example:_
437```C
438#define UNITY_OUTPUT_COLOR
439```
440
441##### `UNITY_SHORTHAND_AS_INT`
442##### `UNITY_SHORTHAND_AS_MEM`
443##### `UNITY_SHORTHAND_AS_RAW`
444##### `UNITY_SHORTHAND_AS_NONE`
445
446These options  give you control of the `TEST_ASSERT_EQUAL` and the
447`TEST_ASSERT_NOT_EQUAL` shorthand assertions. Historically, Unity treated the
448former as an alias for an integer comparison. It treated the latter as a direct
449comparison using `!=`. This assymetry was confusing, but there was much
450disagreement as to how best to treat this pair of assertions. These four options
451will allow you to specify how Unity will treat these assertions.
452
453  - AS INT - the values will be cast to integers and directly compared. Arguments
454             that don't cast easily to integers will cause compiler errors.
455  - AS MEM - the address of both values will be taken and the entire object's
456             memory footprint will be compared byte by byte. Directly placing
457             constant numbers like `456` as expected values will cause errors.
458  - AS_RAW - Unity assumes that you can compare the two values using `==` and `!=`
459             and will do so. No details are given about mismatches, because it
460             doesn't really know what type it's dealing with.
461  - AS_NONE - Unity will disallow the use of these shorthand macros altogether,
462             insisting that developers choose a more descriptive option.
463
464#### `UNITY_SUPPORT_VARIADIC_MACROS`
465
466This will force Unity to support variadic macros when using its own built-in
467RUN_TEST macro. This will rarely be necessary. Most often, Unity will automatically
468detect if the compiler supports variadic macros by checking to see if it's C99+
469compatible. In the event that the compiler supports variadic macros, but is primarily
470C89 (ANSI), defining this option will allow you to use them. This option is also not
471necessary when using Ceedling or the test runner generator script.
472
473## Getting Into The Guts
474
475There will be cases where the options above aren't quite going to get everything
476perfect. They are likely sufficient for any situation where you are compiling
477and executing your tests with a native toolchain (e.g. clang on Mac). These
478options may even get you through the majority of cases encountered in working
479with a target simulator run from your local command line. But especially if you
480must run your test suite on your target hardware, your Unity configuration will
481require special help. This special help will usually reside in one of two
482places: the `main()` function or the `RUN_TEST` macro. Let's look at how these
483work.
484
485
486##### `main()`
487
488Each test module is compiled and run on its own, separate from the other test
489files in your project. Each test file, therefore, has a `main` function. This
490`main` function will need to contain whatever code is necessary to initialize
491your system to a workable state. This is particularly true for situations where
492you must set up a memory map or initialize a communication channel for the
493output of your test results.
494
495A simple main function looks something like this:
496
497```C
498int main(void) {
499    UNITY_BEGIN();
500    RUN_TEST(test_TheFirst);
501    RUN_TEST(test_TheSecond);
502    RUN_TEST(test_TheThird);
503    return UNITY_END();
504}
505```
506
507You can see that our main function doesn't bother taking any arguments. For our
508most barebones case, we'll never have arguments because we just run all the
509tests each time. Instead, we start by calling `UNITY_BEGIN`. We run each test
510(in whatever order we wish). Finally, we call `UNITY_END`, returning its return
511value (which is the total number of failures).
512
513It should be easy to see that you can add code before any test cases are run or
514after all the test cases have completed. This allows you to do any needed
515system-wide setup or teardown that might be required for your special
516circumstances.
517
518
519##### `RUN_TEST`
520
521The `RUN_TEST` macro is called with each test case function. Its job is to
522perform whatever setup and teardown is necessary for executing a single test
523case function. This includes catching failures, calling the test module's
524`setUp()` and `tearDown()` functions, and calling `UnityConcludeTest()`. If
525using CMock or test coverage, there will be additional stubs in use here. A
526simple minimalist RUN_TEST macro looks something like this:
527
528```C
529#define RUN_TEST(testfunc) \
530    UNITY_NEW_TEST(#testfunc) \
531    if (TEST_PROTECT()) { \
532        setUp(); \
533        testfunc(); \
534    } \
535    if (TEST_PROTECT() && (!TEST_IS_IGNORED)) \
536        tearDown(); \
537    UnityConcludeTest();
538```
539
540So that's quite a macro, huh? It gives you a glimpse of what kind of stuff Unity
541has to deal with for every single test case. For each test case, we declare that
542it is a new test. Then we run `setUp` and our test function. These are run
543within a `TEST_PROTECT` block, the function of which is to handle failures that
544occur during the test. Then, assuming our test is still running and hasn't been
545ignored, we run `tearDown`. No matter what, our last step is to conclude this
546test before moving on to the next.
547
548Let's say you need to add a call to `fsync` to force all of your output data to
549flush to a file after each test. You could easily insert this after your
550`UnityConcludeTest` call. Maybe you want to write an xml tag before and after
551each result set. Again, you could do this by adding lines to this macro. Updates
552to this macro are for the occasions when you need an action before or after
553every single test case throughout your entire suite of tests.
554
555
556## Happy Porting
557
558The defines and macros in this guide should help you port Unity to just about
559any C target we can imagine. If you run into a snag or two, don't be afraid of
560asking for help on the forums. We love a good challenge!
561
562
563*Find The Latest of This And More at [ThrowTheSwitch.org](https://throwtheswitch.org)*
564