ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.02k stars 969 forks source link

No mention about instructions to build from source and support_double option #709

Closed wanghaiqiangk closed 6 months ago

wanghaiqiangk commented 9 months ago

Did I miss something? I can't find any documentation introducing how to build Unity from source and integrate it into an existing project's settings.

There're two possible methods for building Unity: CMake and Meson. Since I'm familiar with CMake, this is my first choice for building Unity. However, CMake doesn't provide any options to toggle the support_double feature, which is clearly documented in meson.build. Therefore, I need to rebuild Unity with support_double enabled in order to make unit tests with double assertions work properly.

Without support_double, you may encounter such a problem like

cc -L./thirdparties/unity/lib angle_diff.o  -lm -lunity -o angle_diff
/usr/bin/ld: angle_diff.o: in function `test_angle_diff':
/home/workspace/angle_diff.c:33: undefined reference to `UnityAssertDoublesWithin'
collect2: error: ld returned 1 exit status
make: *** [Makefile:14: angle_diff] Error 1

I'm surprised that no related information in Github Issues.

Of course, besides enabling support_double, you also need to define a macro of UNITY_INCLUDE_DOUBLE in your code. See Allow double floating point comparisons with Unity unit testing framework from Throw The Switch

dmorash-BT commented 9 months ago

Is this an issue of missing documentation, i.e. you have things working but think there should be clearer documentation, or are you unable to get double support working?

The unity.h header does contain a Configuration Options comment block that describes the options and states that "All options described below should be passed as a compiler flag to all files using Unity. If you must add #defines, place them BEFORE the #include above." (That's from v2.5.1 unity.h).

wanghaiqiangk commented 9 months ago

@dmorash-BT missing documentation.

I find that simply providing options like UNITY_INCLUDE_DOUBLE isn't sufficient to bring the double support from working properly. This is because the necessary function definitions for double-related assertions, such as UnityAssertDoublesWithin which is required for TEST_ASSERT_EQUAL_DOUBLE, are not included.

This can be confirmed by examining the symbol table of libunity.a, produced from building the Unity with CMake.

grep 'UnityAssertDoublesWithin' <(nm /path/to/libunity.a)

To wrap up, it's not possible to actually perform assertions on doubles without the required function definitions in the library even if you have configuration options on.

The meson_options.txt includes support_double, which is a clear indication for enabling double support when building the Unity. Indeed, the library generated from a meson build with support_double=true contains all the necessary function definitions for performing assertions on doubles, including the aforementioned UnityAssertDoublesWithin. Only after obtaining this library and enabling configuration options in the compile flags when building concrete test suites, can assertions on doubles work correctly.

So, the documentation lacks information on how to build Unity, specifically what is needed when double support is required.

Oh, I almost forgot, there is actually a missing equivalent CMake option of support_double.

dmorash-BT commented 9 months ago

Well you'll need to supply that define when building Unity as a library to get those functions. That seems clear to me from the unity.h header. I assume CMake supports CFLAGS, either reading the CFLAGS environment variable or setting them directly in the CMake build file.

wanghaiqiangk commented 9 months ago

Aye.

I have been away from C for a while, but I recently picked it up again, so the sense of C is not very strong yet. Right now I may not smart enough to figure out from the unity.h header. Based on my past experience, either you specify options during the compilation of the library and then use it without further configurations, or you specify the options when using the library while leaving the building process simple. And my case is the latter one. I built Unity using CMake without double support in CFLAGS, then I used it in my tests with my understanding of double support by either defining above unity.h header or defining in CFLAGS.

mvandervoord commented 9 months ago

I'd recommend reading http://www.throwtheswitch.org/build/build-unity and http://www.throwtheswitch.org/build/make. They give a good overview of how unity is meant to be used. If you don't feel like dealing with build configuration, ceedling might be a simpler route. If you're just returning to C, I'd avoid the more complex toolchains like cmake and meson. Both are far more problematic than the other options.