Skip to content

Commit 2d73548

Browse files
adding unit testing
1 parent 3c56c9e commit 2d73548

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

.github/workflows/unit-tests.yml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Unit Tests
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- ".github/workflows/unit-tests.yml"
7+
- 'extras/test/**'
8+
- 'src/**'
9+
10+
push:
11+
paths:
12+
- ".github/workflows/unit-tests.yml"
13+
- 'extras/test/**'
14+
- 'src/**'
15+
16+
jobs:
17+
test:
18+
name: Run unit tests
19+
runs-on: ubuntu-latest
20+
21+
env:
22+
COVERAGE_DATA_PATH: extras/coverage-data/coverage.info
23+
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v4
27+
28+
- uses: arduino/cpp-test-action@main
29+
with:
30+
runtime-paths: |
31+
- extras/test/build/bin/testArduinoIoTCloud
32+
coverage-exclude-paths: |
33+
- '*/extras/test/*'
34+
- '/usr/*'
35+
- '*/src/cbor/lib/*'
36+
coverage-data-path: ${{ env.COVERAGE_DATA_PATH }}
37+
38+
# A token is used to avoid intermittent spurious job failures caused by rate limiting.
39+
- name: Set up Codecov upload token
40+
run: |
41+
if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoIoTCloud" ]]; then
42+
# In order to avoid uploads of data from forks, only use the token for runs in the parent repo.
43+
# Token is intentionally exposed.
44+
# See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
45+
CODECOV_TOKEN="47827969-3fda-4ba1-9506-e8d0834ed88c"
46+
else
47+
# codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input.
48+
CODECOV_TOKEN=""
49+
fi
50+
echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV"
51+
52+
- name: Upload coverage report to Codecov
53+
uses: codecov/codecov-action@v3
54+
with:
55+
file: "${{ env.COVERAGE_DATA_PATH }}"
56+
fail_ci_if_error: true
57+
token: ${{ env.CODECOV_TOKEN }}

extras/test/CMakeLists.txt

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(testArduinoCloudUtils)
3+
4+
Include(FetchContent)
5+
6+
FetchContent_Declare(
7+
Catch2
8+
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
9+
GIT_TAG v3.4.0
10+
)
11+
12+
FetchContent_MakeAvailable(Catch2)
13+
14+
set(TEST_TARGET ${CMAKE_PROJECT_NAME})
15+
16+
##########################################################################
17+
18+
set(CMAKE_CXX_STANDARD 11)
19+
20+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
21+
22+
include_directories(../../src)
23+
include_directories(../../src/lzss)
24+
25+
set(TEST_SRCS
26+
src/lzss/test_lzssDecoder.cpp
27+
)
28+
29+
set(TEST_DUT_SRCS
30+
../../src/lzss/lzssDecoder.cpp
31+
)
32+
33+
##########################################################################
34+
35+
add_compile_definitions(HOST)
36+
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
37+
add_compile_options(-Wno-cast-function-type)
38+
39+
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
40+
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy")
41+
42+
add_executable( ${TEST_TARGET} ${TEST_SRCS} ${TEST_DUT_SRCS} )
43+
44+
target_link_libraries( ${TEST_TARGET} Catch2WithMain )

extras/test/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# adding tests
2+
3+
follow guide in https://github.com/catchorg/Catch2/tree/devel/docs in order to add more tests
4+
5+
Add the source file for the test in `extras/test/CMakeLists.txt` inside of `${TEST_SRCS}` variable and eventually the source file you want to test in `${TEST_DUT_SRCS}`
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <lzssDecoder.h>
4+
#include <stdio.h>
5+
6+
using namespace arduino::lzss;
7+
8+
SCENARIO( "Decoding an LZSS stream of data", "[lzss::Decoder]" ) {
9+
GIVEN( "A LZSS compressed file and an LZSS Decoder" ) {
10+
// the lzss file provided as test when decompressed are made by incrementing a 4 byte integer
11+
// starting from 0, up to (file size-1)/4, thus it is easy to validate the correctness of the result
12+
13+
FILE *f = fopen("test_files/test-64k.lzss", "r");
14+
uint32_t value = 0;
15+
uint64_t position = 0;
16+
17+
auto decoder = arduino::lzss::Decoder([&value, &position](const uint8_t b) {
18+
REQUIRE(((value >> ((3-(position%4))<<3)) & 0xFF) == b); // make this an assertion, not a require
19+
20+
if(position % 4 == 0 && position != 0) {
21+
value++;
22+
}
23+
position++;
24+
});
25+
26+
THEN( "File is open" ) {
27+
REQUIRE( f != nullptr );
28+
}
29+
30+
WHEN( "Decompress is called on the decoder" ) {
31+
uint8_t buf[64];
32+
size_t read_bytes;
33+
do {
34+
read_bytes = fread(buf, 1, 64, f);
35+
36+
if(read_bytes > 0) {
37+
decoder.decompress(buf, read_bytes);
38+
}
39+
} while(read_bytes > 0);
40+
41+
REQUIRE(value == (1<<14) -1);
42+
}
43+
THEN( "There is no error on the file" ) {
44+
REQUIRE(feof(f) == 0);
45+
REQUIRE(ferror(f) == 0);
46+
REQUIRE(fclose(f) == 0);
47+
}
48+
}
49+
}

extras/test/test_files/test-64k

64 KB
Binary file not shown.

extras/test/test_files/test-64k.lzss

50 KB
Binary file not shown.

0 commit comments

Comments
 (0)