-
Notifications
You must be signed in to change notification settings - Fork 2
Add host unit test infrastructure with GoogleTest #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Host unit tests for sendspin-cpp | ||
| # | ||
| # Enabled from the top-level CMakeLists.txt with -DSENDSPIN_BUILD_TESTS=ON. | ||
| # Tests link against the `sendspin` static library and exercise its internal | ||
| # logic directly (white-box: they include private headers from src/). | ||
|
|
||
| include(FetchContent) | ||
|
|
||
| # GoogleTest — fetched the same way as the library's other dependencies. | ||
| FetchContent_Declare( | ||
| googletest | ||
| GIT_REPOSITORY https://github.com/google/googletest.git | ||
| GIT_TAG v1.15.2 | ||
| GIT_SHALLOW TRUE | ||
| ) | ||
| set(INSTALL_GTEST OFF CACHE BOOL "" FORCE) | ||
| set(BUILD_GMOCK OFF CACHE BOOL "" FORCE) # only gtest_main is used; skip building gmock | ||
| FetchContent_MakeAvailable(googletest) | ||
|
|
||
| # One executable aggregating every test_*.cpp file. gtest_discover_tests still | ||
| # registers the individual TEST() cases with CTest so failures are reported per case. | ||
| add_executable(sendspin_tests | ||
| test_audio_stream_info.cpp | ||
| test_time_filter.cpp | ||
| test_protocol.cpp | ||
| ) | ||
|
|
||
| # Reach the library's private headers (protocol_messages.h, time_filter.h, ...). | ||
| # The public include/ dir and ArduinoJson propagate transitively from `sendspin`. | ||
| # Use CMAKE_CURRENT_SOURCE_DIR (not CMAKE_SOURCE_DIR) so the path stays correct even | ||
| # if sendspin is ever added as a subdirectory of a larger superproject. | ||
| target_include_directories(sendspin_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src) | ||
|
|
||
| target_link_libraries(sendspin_tests PRIVATE sendspin GTest::gtest_main) | ||
|
|
||
| target_compile_features(sendspin_tests PRIVATE cxx_std_20) | ||
| target_compile_options(sendspin_tests PRIVATE -Wall -Wextra) | ||
|
|
||
| # Match the library's sanitizer configuration so the test binary is instrumented too. | ||
| if(ENABLE_SANITIZERS) | ||
| target_compile_options(sendspin_tests PRIVATE | ||
| -fsanitize=address,undefined -fno-omit-frame-pointer) | ||
| target_link_options(sendspin_tests PRIVATE -fsanitize=address,undefined) | ||
| endif() | ||
|
|
||
| include(GoogleTest) | ||
| gtest_discover_tests(sendspin_tests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Unit tests | ||
|
|
||
| Host-only unit tests for the cross-platform logic in `src/`. They link against the `sendspin` | ||
| static library and run on macOS/Linux with [GoogleTest](https://github.com/google/googletest) | ||
| (fetched automatically via CMake `FetchContent`). | ||
|
|
||
| ## Running | ||
|
|
||
| From the repository root: | ||
|
|
||
| ```bash | ||
| cmake -B build-tests -DSENDSPIN_BUILD_TESTS=ON . | ||
| cmake --build build-tests --target sendspin_tests | ||
| ctest --test-dir build-tests --output-on-failure | ||
| ``` | ||
|
|
||
| Run the test binary directly to use GoogleTest filters: | ||
|
|
||
| ```bash | ||
| ./build-tests/tests/sendspin_tests --gtest_filter='Protocol.*' | ||
| ``` | ||
|
|
||
| ## Running under sanitizers | ||
|
|
||
| Add `-DENABLE_SANITIZERS=ON` to build with AddressSanitizer and UndefinedBehaviorSanitizer. This | ||
| is what CI runs, and it is the recommended way to exercise the pointer-heavy code (the message | ||
| formatter, JSON parsing): | ||
|
|
||
| ```bash | ||
| cmake -B build-tests-asan -DSENDSPIN_BUILD_TESTS=ON -DENABLE_SANITIZERS=ON . | ||
| cmake --build build-tests-asan --target sendspin_tests | ||
| ctest --test-dir build-tests-asan --output-on-failure | ||
| ``` | ||
|
|
||
| ## Layout | ||
|
|
||
| Each `test_*.cpp` file covers one unit of cross-platform logic: | ||
|
|
||
| - `test_protocol.cpp` — wire-protocol parsing/formatting: enum round-trips, message dispatch, the | ||
| tri-state metadata/color deltas, and the hand-rolled `client/time` formatter checked against | ||
| `snprintf`. | ||
| - `test_time_filter.cpp` — `SendspinTimeFilter` invariants (monotonic-timestamp rejection, reset, | ||
| offset round-trip, convergence). | ||
| - `test_audio_stream_info.cpp` — byte/frame/sample/duration conversions. | ||
|
|
||
| These are white-box tests: they include private headers from `src/`, so the test target adds | ||
| `src/` to its include path. To add a new test file, create `test_<unit>.cpp` here and add it to | ||
| the `add_executable(sendspin_tests ...)` list in `tests/CMakeLists.txt`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright 2026 Sendspin Contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Unit tests for AudioStreamInfo's byte/frame/sample/duration conversions. These are exact | ||
| // integer arithmetic, so the assertions are exact: a wrong conversion silently corrupts | ||
| // buffering and A/V sync, which is exactly the kind of bug worth pinning down. | ||
|
|
||
| #include "audio_stream_info.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| using sendspin::AudioStreamInfo; | ||
|
|
||
| // 16-bit stereo at 44.1 kHz: 2 bytes/sample, 4 bytes/frame. | ||
| TEST(AudioStreamInfo, CdQualityConversions) { | ||
| const AudioStreamInfo info(16, 2, 44100); | ||
|
|
||
| EXPECT_EQ(info.get_bits_per_sample(), 16); | ||
| EXPECT_EQ(info.get_channels(), 2); | ||
| EXPECT_EQ(info.get_sample_rate(), 44100u); | ||
|
|
||
| EXPECT_EQ(info.frames_to_bytes(100), 400u); | ||
| EXPECT_EQ(info.bytes_to_frames(400), 100u); | ||
| EXPECT_EQ(info.samples_to_bytes(8), 16u); | ||
|
|
||
| // 1 second of audio == sample_rate frames == sample_rate * frame_size bytes. | ||
| EXPECT_EQ(info.ms_to_bytes(1000), 176400u); | ||
| EXPECT_EQ(info.bytes_to_ms(176400), 1000u); | ||
|
|
||
| EXPECT_EQ(info.frames_to_microseconds(441), 10000); // 10 ms | ||
| EXPECT_EQ(info.frames_to_microseconds(2205), 50000); // 50 ms | ||
| } | ||
|
|
||
| // frames_to_microseconds() widens to 64-bit internally, so it stays exact well past the point | ||
| // where a 32-bit (frames * 1e6) product overflows (~4295 frames, ~97 ms at 44.1 kHz). These | ||
| // counts would have silently wrapped under the old implementation. | ||
| TEST(AudioStreamInfo, FramesToMicrosecondsNoOverflowAtLargeCounts) { | ||
| const AudioStreamInfo info(16, 2, 44100); | ||
|
|
||
| EXPECT_EQ(info.frames_to_microseconds(44100), 1000000); // exactly 1 s | ||
| EXPECT_EQ(info.frames_to_microseconds(88200), 2000000); // exactly 2 s | ||
| EXPECT_EQ(info.frames_to_microseconds(4410000), 100000000); // 100 s, far past the old wrap | ||
| } | ||
|
|
||
| // 16-bit mono at 8 kHz, the library's default-ish low-rate case. | ||
| TEST(AudioStreamInfo, MonoLowRateConversions) { | ||
| const AudioStreamInfo info(16, 1, 8000); | ||
|
|
||
| EXPECT_EQ(info.ms_to_bytes(125), 2000u); // 125 ms * 16000 B/s | ||
| EXPECT_EQ(info.bytes_to_ms(2000), 125u); | ||
| EXPECT_EQ(info.frames_to_microseconds(800), 100000); // 100 ms | ||
| } | ||
|
|
||
| // Bit depths that are not multiples of 8 round up to whole bytes per sample. | ||
| TEST(AudioStreamInfo, BytesPerSampleRounding) { | ||
| EXPECT_EQ(AudioStreamInfo(8, 1, 48000).frames_to_bytes(10), 10u); // 1 byte/sample | ||
| EXPECT_EQ(AudioStreamInfo(24, 2, 48000).frames_to_bytes(10), 60u); // 3 bytes/sample, stereo | ||
| } | ||
|
|
||
| TEST(AudioStreamInfo, Equality) { | ||
| const AudioStreamInfo a(16, 2, 44100); | ||
| EXPECT_TRUE(a == AudioStreamInfo(16, 2, 44100)); | ||
| EXPECT_TRUE(a != AudioStreamInfo(16, 2, 48000)); // sample rate differs | ||
| EXPECT_TRUE(a != AudioStreamInfo(24, 2, 44100)); // bit depth differs | ||
| EXPECT_TRUE(a != AudioStreamInfo(16, 1, 44100)); // channel count differs | ||
| } | ||
|
|
||
| TEST(AudioStreamInfo, DefaultConstruction) { | ||
| const AudioStreamInfo info; | ||
| EXPECT_EQ(info.get_bits_per_sample(), 16); | ||
| EXPECT_EQ(info.get_channels(), 1); | ||
| EXPECT_EQ(info.get_sample_rate(), sendspin::DEFAULT_SAMPLE_RATE_HZ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.