135375f98Sopenharmony_ci# Unity Test ![CI][] 235375f98Sopenharmony_ci 335375f98Sopenharmony_ci__Copyright (c) 2007 - 2023 Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams__ 435375f98Sopenharmony_ci 535375f98Sopenharmony_ciWelcome to the Unity Test Project, one of the main projects of ThrowTheSwitch.org. 635375f98Sopenharmony_ciUnity Test is a unit testing framework built for C, with a focus on working with embedded toolchains. 735375f98Sopenharmony_ci 835375f98Sopenharmony_ciThis project is made to test code targetting microcontrollers big and small. 935375f98Sopenharmony_ciThe core project is a single C file and a pair of headers, allowing it to be added to your existing build setup without too much headache. 1035375f98Sopenharmony_ciYou may use any compiler you wish, and may use most existing build systems including Make, CMake, etc. 1135375f98Sopenharmony_ciIf you'd like to leave the hard work to us, you might be interested in Ceedling, a build tool also by ThrowTheSwitch.org. 1235375f98Sopenharmony_ci 1335375f98Sopenharmony_ciIf you're new to Unity, we encourage you to tour the [getting started guide][]. 1435375f98Sopenharmony_ci 1535375f98Sopenharmony_ciYou can also find the [change log][] and [known issues][] in our documentation. 1635375f98Sopenharmony_ci 1735375f98Sopenharmony_ci## Getting Started 1835375f98Sopenharmony_ci 1935375f98Sopenharmony_ciThe [docs][] folder contains a [getting started guide][] and much more tips about using Unity. 2035375f98Sopenharmony_ci 2135375f98Sopenharmony_ci## Unity Assertion Summary 2235375f98Sopenharmony_ci 2335375f98Sopenharmony_ciFor the full list, see [UnityAssertionsReference.md][]. 2435375f98Sopenharmony_ci 2535375f98Sopenharmony_ci### Basic Validity Tests 2635375f98Sopenharmony_ci 2735375f98Sopenharmony_ci TEST_ASSERT_TRUE(condition) 2835375f98Sopenharmony_ci 2935375f98Sopenharmony_ciEvaluates whatever code is in condition and fails if it evaluates to false 3035375f98Sopenharmony_ci 3135375f98Sopenharmony_ci TEST_ASSERT_FALSE(condition) 3235375f98Sopenharmony_ci 3335375f98Sopenharmony_ciEvaluates whatever code is in condition and fails if it evaluates to true 3435375f98Sopenharmony_ci 3535375f98Sopenharmony_ci TEST_ASSERT(condition) 3635375f98Sopenharmony_ci 3735375f98Sopenharmony_ciAnother way of calling `TEST_ASSERT_TRUE` 3835375f98Sopenharmony_ci 3935375f98Sopenharmony_ci TEST_ASSERT_UNLESS(condition) 4035375f98Sopenharmony_ci 4135375f98Sopenharmony_ciAnother way of calling `TEST_ASSERT_FALSE` 4235375f98Sopenharmony_ci 4335375f98Sopenharmony_ci TEST_FAIL() 4435375f98Sopenharmony_ci TEST_FAIL_MESSAGE(message) 4535375f98Sopenharmony_ci 4635375f98Sopenharmony_ciThis test is automatically marked as a failure. 4735375f98Sopenharmony_ciThe message is output stating why. 4835375f98Sopenharmony_ci 4935375f98Sopenharmony_ci### Numerical Assertions: Integers 5035375f98Sopenharmony_ci 5135375f98Sopenharmony_ci TEST_ASSERT_EQUAL_INT(expected, actual) 5235375f98Sopenharmony_ci TEST_ASSERT_EQUAL_INT8(expected, actual) 5335375f98Sopenharmony_ci TEST_ASSERT_EQUAL_INT16(expected, actual) 5435375f98Sopenharmony_ci TEST_ASSERT_EQUAL_INT32(expected, actual) 5535375f98Sopenharmony_ci TEST_ASSERT_EQUAL_INT64(expected, actual) 5635375f98Sopenharmony_ci 5735375f98Sopenharmony_ciCompare two integers for equality and display errors as signed integers. 5835375f98Sopenharmony_ciA cast will be performed to your natural integer size so often this can just be used. 5935375f98Sopenharmony_ciWhen you need to specify the exact size, you can use a specific version. 6035375f98Sopenharmony_ci 6135375f98Sopenharmony_ci TEST_ASSERT_EQUAL_UINT(expected, actual) 6235375f98Sopenharmony_ci TEST_ASSERT_EQUAL_UINT8(expected, actual) 6335375f98Sopenharmony_ci TEST_ASSERT_EQUAL_UINT16(expected, actual) 6435375f98Sopenharmony_ci TEST_ASSERT_EQUAL_UINT32(expected, actual) 6535375f98Sopenharmony_ci TEST_ASSERT_EQUAL_UINT64(expected, actual) 6635375f98Sopenharmony_ci 6735375f98Sopenharmony_ciCompare two integers for equality and display errors as unsigned integers. 6835375f98Sopenharmony_ciLike INT, there are variants for different sizes also. 6935375f98Sopenharmony_ci 7035375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX(expected, actual) 7135375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX8(expected, actual) 7235375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX16(expected, actual) 7335375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX32(expected, actual) 7435375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX64(expected, actual) 7535375f98Sopenharmony_ci 7635375f98Sopenharmony_ciCompares two integers for equality and display errors as hexadecimal. 7735375f98Sopenharmony_ciLike the other integer comparisons, you can specify the size... 7835375f98Sopenharmony_cihere the size will also effect how many nibbles are shown (for example, `HEX16` will show 4 nibbles). 7935375f98Sopenharmony_ci 8035375f98Sopenharmony_ci TEST_ASSERT_EQUAL(expected, actual) 8135375f98Sopenharmony_ci 8235375f98Sopenharmony_ciAnother way of calling TEST_ASSERT_EQUAL_INT 8335375f98Sopenharmony_ci 8435375f98Sopenharmony_ci TEST_ASSERT_INT_WITHIN(delta, expected, actual) 8535375f98Sopenharmony_ci 8635375f98Sopenharmony_ciAsserts that the actual value is within plus or minus delta of the expected value. 8735375f98Sopenharmony_ciThis also comes in size specific variants. 8835375f98Sopenharmony_ci 8935375f98Sopenharmony_ci TEST_ASSERT_GREATER_THAN(threshold, actual) 9035375f98Sopenharmony_ci 9135375f98Sopenharmony_ciAsserts that the actual value is greater than the threshold. 9235375f98Sopenharmony_ciThis also comes in size specific variants. 9335375f98Sopenharmony_ci 9435375f98Sopenharmony_ci TEST_ASSERT_LESS_THAN(threshold, actual) 9535375f98Sopenharmony_ci 9635375f98Sopenharmony_ciAsserts that the actual value is less than the threshold. 9735375f98Sopenharmony_ciThis also comes in size specific variants. 9835375f98Sopenharmony_ci 9935375f98Sopenharmony_ci### Arrays 10035375f98Sopenharmony_ci 10135375f98Sopenharmony_ci _ARRAY 10235375f98Sopenharmony_ci 10335375f98Sopenharmony_ciYou can append `_ARRAY` to any of these macros to make an array comparison of that type. 10435375f98Sopenharmony_ciHere you will need to care a bit more about the actual size of the value being checked. 10535375f98Sopenharmony_ciYou will also specify an additional argument which is the number of elements to compare. 10635375f98Sopenharmony_ciFor example: 10735375f98Sopenharmony_ci 10835375f98Sopenharmony_ci TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, elements) 10935375f98Sopenharmony_ci 11035375f98Sopenharmony_ci _EACH_EQUAL 11135375f98Sopenharmony_ci 11235375f98Sopenharmony_ciAnother array comparison option is to check that EVERY element of an array is equal to a single expected value. 11335375f98Sopenharmony_ciYou do this by specifying the EACH_EQUAL macro. 11435375f98Sopenharmony_ciFor example: 11535375f98Sopenharmony_ci 11635375f98Sopenharmony_ci TEST_ASSERT_EACH_EQUAL_INT32(expected, actual, elements) 11735375f98Sopenharmony_ci 11835375f98Sopenharmony_ci### Numerical Assertions: Bitwise 11935375f98Sopenharmony_ci 12035375f98Sopenharmony_ci TEST_ASSERT_BITS(mask, expected, actual) 12135375f98Sopenharmony_ci 12235375f98Sopenharmony_ciUse an integer mask to specify which bits should be compared between two other integers. 12335375f98Sopenharmony_ciHigh bits in the mask are compared, low bits ignored. 12435375f98Sopenharmony_ci 12535375f98Sopenharmony_ci TEST_ASSERT_BITS_HIGH(mask, actual) 12635375f98Sopenharmony_ci 12735375f98Sopenharmony_ciUse an integer mask to specify which bits should be inspected to determine if they are all set high. 12835375f98Sopenharmony_ciHigh bits in the mask are compared, low bits ignored. 12935375f98Sopenharmony_ci 13035375f98Sopenharmony_ci TEST_ASSERT_BITS_LOW(mask, actual) 13135375f98Sopenharmony_ci 13235375f98Sopenharmony_ciUse an integer mask to specify which bits should be inspected to determine if they are all set low. 13335375f98Sopenharmony_ciHigh bits in the mask are compared, low bits ignored. 13435375f98Sopenharmony_ci 13535375f98Sopenharmony_ci TEST_ASSERT_BIT_HIGH(bit, actual) 13635375f98Sopenharmony_ci 13735375f98Sopenharmony_ciTest a single bit and verify that it is high. 13835375f98Sopenharmony_ciThe bit is specified 0-31 for a 32-bit integer. 13935375f98Sopenharmony_ci 14035375f98Sopenharmony_ci TEST_ASSERT_BIT_LOW(bit, actual) 14135375f98Sopenharmony_ci 14235375f98Sopenharmony_ciTest a single bit and verify that it is low. 14335375f98Sopenharmony_ciThe bit is specified 0-31 for a 32-bit integer. 14435375f98Sopenharmony_ci 14535375f98Sopenharmony_ci### Numerical Assertions: Floats 14635375f98Sopenharmony_ci 14735375f98Sopenharmony_ci TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) 14835375f98Sopenharmony_ci TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual) 14935375f98Sopenharmony_ci 15035375f98Sopenharmony_ciAsserts that the actual value is within plus or minus delta of the expected value. 15135375f98Sopenharmony_ci 15235375f98Sopenharmony_ci TEST_ASSERT_FLOAT_NOT_WITHIN(delta, expected, actual) 15335375f98Sopenharmony_ci TEST_ASSERT_DOUBLE_NOT_WITHIN(delta, expected, actual) 15435375f98Sopenharmony_ci 15535375f98Sopenharmony_ciAsserts that the actual value is NOT within plus or minus delta of the expected value. 15635375f98Sopenharmony_ci 15735375f98Sopenharmony_ci TEST_ASSERT_EQUAL_FLOAT(expected, actual) 15835375f98Sopenharmony_ci TEST_ASSERT_EQUAL_DOUBLE(expected, actual) 15935375f98Sopenharmony_ci 16035375f98Sopenharmony_ciAsserts that two floating point values are "equal" within a small % delta of the expected value. 16135375f98Sopenharmony_ci 16235375f98Sopenharmony_ci TEST_ASSERT_NOT_EQUAL_FLOAT(expected, actual) 16335375f98Sopenharmony_ci TEST_ASSERT_NOT_EQUAL_DOUBLE(expected, actual) 16435375f98Sopenharmony_ci 16535375f98Sopenharmony_ciAsserts that two floating point values are NOT "equal" within a small % delta of the expected value. 16635375f98Sopenharmony_ci 16735375f98Sopenharmony_ci TEST_ASSERT_LESS_THAN_FLOAT(threshold, actual) 16835375f98Sopenharmony_ci TEST_ASSERT_LESS_THAN_DOUBLE(threshold, actual) 16935375f98Sopenharmony_ci TEST_ASSERT_GREATER_THAN_FLOAT(threshold, actual) 17035375f98Sopenharmony_ci TEST_ASSERT_GREATER_THAN_DOUBLE(threshold, actual) 17135375f98Sopenharmony_ci 17235375f98Sopenharmony_ciAsserts that the actual value is less than or greater than the threshold. 17335375f98Sopenharmony_ci 17435375f98Sopenharmony_ciThere are also `LESS_OR_EQUAL` and `GREATER_OR_EQUAL` variations. 17535375f98Sopenharmony_ciThese obey the same rules for equality as do `TEST_ASSERT_EQUAL_FLOAT` and `TEST_ASSERT_EQUAL_DOUBLE`: 17635375f98Sopenharmony_ciIf the two values are within a small % delta of the expected value, the assertion will pass. 17735375f98Sopenharmony_ci 17835375f98Sopenharmony_ci### String Assertions 17935375f98Sopenharmony_ci 18035375f98Sopenharmony_ci TEST_ASSERT_EQUAL_STRING(expected, actual) 18135375f98Sopenharmony_ci 18235375f98Sopenharmony_ciCompare two null-terminate strings. 18335375f98Sopenharmony_ciFail if any character is different or if the lengths are different. 18435375f98Sopenharmony_ci 18535375f98Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) 18635375f98Sopenharmony_ci 18735375f98Sopenharmony_ciCompare two strings. 18835375f98Sopenharmony_ciFail if any character is different, stop comparing after len characters. 18935375f98Sopenharmony_ci 19035375f98Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) 19135375f98Sopenharmony_ci 19235375f98Sopenharmony_ciCompare two null-terminate strings. 19335375f98Sopenharmony_ciFail if any character is different or if the lengths are different. 19435375f98Sopenharmony_ciOutput a custom message on failure. 19535375f98Sopenharmony_ci 19635375f98Sopenharmony_ci TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) 19735375f98Sopenharmony_ci 19835375f98Sopenharmony_ciCompare two strings. 19935375f98Sopenharmony_ciFail if any character is different, stop comparing after len characters. 20035375f98Sopenharmony_ciOutput a custom message on failure. 20135375f98Sopenharmony_ci 20235375f98Sopenharmony_ci### Pointer Assertions 20335375f98Sopenharmony_ci 20435375f98Sopenharmony_ciMost pointer operations can be performed by simply using the integer comparisons above. 20535375f98Sopenharmony_ciHowever, a couple of special cases are added for clarity. 20635375f98Sopenharmony_ci 20735375f98Sopenharmony_ci TEST_ASSERT_NULL(pointer) 20835375f98Sopenharmony_ci 20935375f98Sopenharmony_ciFails if the pointer is not equal to NULL 21035375f98Sopenharmony_ci 21135375f98Sopenharmony_ci TEST_ASSERT_NOT_NULL(pointer) 21235375f98Sopenharmony_ci 21335375f98Sopenharmony_ciFails if the pointer is equal to NULL 21435375f98Sopenharmony_ci 21535375f98Sopenharmony_ci### Memory Assertions 21635375f98Sopenharmony_ci 21735375f98Sopenharmony_ci TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) 21835375f98Sopenharmony_ci 21935375f98Sopenharmony_ciCompare two blocks of memory. 22035375f98Sopenharmony_ciThis is a good generic assertion for types that can't be coerced into acting like standard types... 22135375f98Sopenharmony_cibut since it's a memory compare, you have to be careful that your data types are packed. 22235375f98Sopenharmony_ci 22335375f98Sopenharmony_ci### \_MESSAGE 22435375f98Sopenharmony_ci 22535375f98Sopenharmony_ciYou can append `\_MESSAGE` to any of the macros to make them take an additional argument. 22635375f98Sopenharmony_ciThis argument is a string that will be printed at the end of the failure strings. 22735375f98Sopenharmony_ciThis is useful for specifying more information about the problem. 22835375f98Sopenharmony_ci 22935375f98Sopenharmony_ci[CI]: https://github.com/ThrowTheSwitch/Unity/workflows/CI/badge.svg 23035375f98Sopenharmony_ci[getting started guide]: docs/UnityGettingStartedGuide.md 23135375f98Sopenharmony_ci[change log]: docs/UnityChangeLog.md 23235375f98Sopenharmony_ci[known issues]: docs/UnityKnownIssues.md 23335375f98Sopenharmony_ci[docs]: docs/ 23435375f98Sopenharmony_ci[UnityAssertionsReference.md]: docs/UnityAssertionsReference.md 235