Skip to content

Commit

Permalink
migrate to unity testing
Browse files Browse the repository at this point in the history
+ add couple more tests
+ fixed github actions
  • Loading branch information
dernasherbrezon committed Dec 17, 2023
1 parent 6ee1cd5 commit 685a099
Show file tree
Hide file tree
Showing 9 changed files with 4,337 additions and 329 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: main

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]
workflow_dispatch:

jobs:
Expand All @@ -16,10 +16,10 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Set up JDK 11
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 11
java-version: 17
distribution: 'liberica'
- name: Cache SonarQube packages
uses: actions/cache@v3
Expand All @@ -32,7 +32,7 @@ jobs:
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get install -y valgrind check cmake pkg-config
sudo apt-get install -y valgrind cmake pkg-config
- name: Run build-wrapper
run: |
cd test
Expand Down
7 changes: 3 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ find_package(PkgConfig REQUIRED)

enable_testing()

pkg_check_modules(PC_CHECK REQUIRED check)
include_directories(${PC_CHECK_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/unity-2.5.2/src/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
link_directories(${PC_CHECK_LIBRARY_DIRS})

add_executable(test_sx127x
${CMAKE_CURRENT_SOURCE_DIR}/test_sx127x.c
${CMAKE_CURRENT_SOURCE_DIR}/sx127x_mock_spi.c
${CMAKE_CURRENT_SOURCE_DIR}/unity-2.5.2/src/unity.c
)
target_link_libraries(test_sx127x sx127xlib ${PC_CHECK_LIBRARIES})
target_link_libraries(test_sx127x sx127xlib)
add_test(NAME test_sx127x COMMAND test_sx127x)

if(CMAKE_BUILD_TYPE MATCHES Debug)
Expand Down
8 changes: 4 additions & 4 deletions test/sx127x_mock_spi.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <check.h>
#include "unity.h"
#include <string.h>
#include <sx127x_spi.h>

Expand Down Expand Up @@ -34,7 +34,7 @@ int sx127x_spi_read_registers(int reg, void *spi_device, size_t data_length, uin
}

int sx127x_spi_read_buffer(int reg, uint8_t *buffer, size_t buffer_length, void *spi_device) {
ck_assert_int_eq(sx127x_mock_expected_response_current + buffer_length <= sx127x_mock_expected_response_length, 1);
TEST_ASSERT_EQUAL_INT(sx127x_mock_expected_response_current + buffer_length <= sx127x_mock_expected_response_length, 1);
memcpy(buffer, sx127x_mock_expected_response + sx127x_mock_expected_response_current, buffer_length);
sx127x_mock_expected_response_current += buffer_length;
if (sx127x_mock_expected_response_current == sx127x_mock_expected_response_length) {
Expand Down Expand Up @@ -76,9 +76,9 @@ void spi_mock_fifo(uint8_t *expected, size_t expected_length, int code) {
}

void spi_assert_write(uint8_t *expected, size_t expected_length) {
ck_assert_int_eq(expected_length, sx127x_mock_actual_request_length);
TEST_ASSERT_EQUAL_INT(expected_length, sx127x_mock_actual_request_length);
for (size_t i = 0; i < expected_length; i++) {
ck_assert_int_eq(expected[i], actual_request[i]);
TEST_ASSERT_EQUAL_INT(expected[i], actual_request[i]);
}
}

Expand Down
610 changes: 294 additions & 316 deletions test/test_sx127x.c

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions test/unity-2.5.2/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) <year> 2007-21 Mike Karlesky, Mark VanderVoord, Greg Williams

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
200 changes: 200 additions & 0 deletions test/unity-2.5.2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
Unity Test ![CI](https://github.com/ThrowTheSwitch/Unity/workflows/CI/badge.svg)
==========
__Copyright (c) 2007 - 2021 Unity Project by Mike Karlesky, Mark VanderVoord, and Greg Williams__

Welcome to the Unity Test Project, one of the main projects of ThrowTheSwitch.org. Unity Test is a
unit testing framework built for C, with a focus on working with embedded toolchains.

This project is made to test code targetting microcontrollers big and small. The core project is a
single C file and a pair of headers, allowing it to the added to your existing build setup without
too much headache. You may use any compiler you wish, and may use most existing build systems
including make, cmake, etc. If you'd like to leave the hard work to us, you might be interested
in Ceedling, a build tool also by ThrowTheSwitch.org.

If you're new to Unity, we encourage you to tour the [getting started guide](docs/UnityGettingStartedGuide.md)

Getting Started
===============
The [docs](docs/) folder contains a [getting started guide](docs/UnityGettingStartedGuide.md)
and much more tips about using Unity.

Unity Assertion Summary
=======================
For the full list, see [UnityAssertionsReference.md](docs/UnityAssertionsReference.md).

Basic Validity Tests
--------------------

TEST_ASSERT_TRUE(condition)

Evaluates whatever code is in condition and fails if it evaluates to false

TEST_ASSERT_FALSE(condition)

Evaluates whatever code is in condition and fails if it evaluates to true

TEST_ASSERT(condition)

Another way of calling `TEST_ASSERT_TRUE`

TEST_ASSERT_UNLESS(condition)

Another way of calling `TEST_ASSERT_FALSE`

TEST_FAIL()
TEST_FAIL_MESSAGE(message)

This test is automatically marked as a failure. The message is output stating why.

Numerical Assertions: Integers
------------------------------

TEST_ASSERT_EQUAL_INT(expected, actual)
TEST_ASSERT_EQUAL_INT8(expected, actual)
TEST_ASSERT_EQUAL_INT16(expected, actual)
TEST_ASSERT_EQUAL_INT32(expected, actual)
TEST_ASSERT_EQUAL_INT64(expected, actual)

Compare two integers for equality and display errors as signed integers. A cast will be performed
to your natural integer size so often this can just be used. When you need to specify the exact size,
like when comparing arrays, you can use a specific version:

TEST_ASSERT_EQUAL_UINT(expected, actual)
TEST_ASSERT_EQUAL_UINT8(expected, actual)
TEST_ASSERT_EQUAL_UINT16(expected, actual)
TEST_ASSERT_EQUAL_UINT32(expected, actual)
TEST_ASSERT_EQUAL_UINT64(expected, actual)

Compare two integers for equality and display errors as unsigned integers. Like INT, there are
variants for different sizes also.

TEST_ASSERT_EQUAL_HEX(expected, actual)
TEST_ASSERT_EQUAL_HEX8(expected, actual)
TEST_ASSERT_EQUAL_HEX16(expected, actual)
TEST_ASSERT_EQUAL_HEX32(expected, actual)
TEST_ASSERT_EQUAL_HEX64(expected, actual)

Compares two integers for equality and display errors as hexadecimal. Like the other integer comparisons,
you can specify the size... here the size will also effect how many nibbles are shown (for example, `HEX16`
will show 4 nibbles).

TEST_ASSERT_EQUAL(expected, actual)

Another way of calling TEST_ASSERT_EQUAL_INT

TEST_ASSERT_INT_WITHIN(delta, expected, actual)

Asserts that the actual value is within plus or minus delta of the expected value. This also comes in
size specific variants.


TEST_ASSERT_GREATER_THAN(threshold, actual)

Asserts that the actual value is greater than the threshold. This also comes in size specific variants.


TEST_ASSERT_LESS_THAN(threshold, actual)

Asserts that the actual value is less than the threshold. This also comes in size specific variants.


Arrays
------

_ARRAY

You can append `_ARRAY` to any of these macros to make an array comparison of that type. Here you will
need to care a bit more about the actual size of the value being checked. You will also specify an
additional argument which is the number of elements to compare. For example:

TEST_ASSERT_EQUAL_HEX8_ARRAY(expected, actual, elements)

_EACH_EQUAL

Another array comparison option is to check that EVERY element of an array is equal to a single expected
value. You do this by specifying the EACH_EQUAL macro. For example:

TEST_ASSERT_EACH_EQUAL_INT32(expected, actual, elements)

Numerical Assertions: Bitwise
-----------------------------

TEST_ASSERT_BITS(mask, expected, actual)

Use an integer mask to specify which bits should be compared between two other integers. High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BITS_HIGH(mask, actual)

Use an integer mask to specify which bits should be inspected to determine if they are all set high. High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BITS_LOW(mask, actual)

Use an integer mask to specify which bits should be inspected to determine if they are all set low. High bits in the mask are compared, low bits ignored.

TEST_ASSERT_BIT_HIGH(bit, actual)

Test a single bit and verify that it is high. The bit is specified 0-31 for a 32-bit integer.

TEST_ASSERT_BIT_LOW(bit, actual)

Test a single bit and verify that it is low. The bit is specified 0-31 for a 32-bit integer.

Numerical Assertions: Floats
----------------------------

TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual)

Asserts that the actual value is within plus or minus delta of the expected value.

TEST_ASSERT_EQUAL_FLOAT(expected, actual)
TEST_ASSERT_EQUAL_DOUBLE(expected, actual)

Asserts that two floating point values are "equal" within a small % delta of the expected value.

String Assertions
-----------------

TEST_ASSERT_EQUAL_STRING(expected, actual)

Compare two null-terminate strings. Fail if any character is different or if the lengths are different.

TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len)

Compare two strings. Fail if any character is different, stop comparing after len characters.

TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message)

Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure.

TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message)

Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure.

Pointer Assertions
------------------

Most pointer operations can be performed by simply using the integer comparisons above. However, a couple of special cases are added for clarity.

TEST_ASSERT_NULL(pointer)

Fails if the pointer is not equal to NULL

TEST_ASSERT_NOT_NULL(pointer)

Fails if the pointer is equal to NULL

Memory Assertions
-----------------

TEST_ASSERT_EQUAL_MEMORY(expected, actual, len)

Compare two blocks of memory. This is a good generic assertion for types that can't be coerced into acting like
standard types... but since it's a memory compare, you have to be careful that your data types are packed.

\_MESSAGE
---------

you can append \_MESSAGE to any of the macros to make them take an additional argument. This argument
is a string that will be printed at the end of the failure strings. This is useful for specifying more
information about the problem.

Loading

0 comments on commit 685a099

Please sign in to comment.