DigitalOutputGuard (DOG) Comprehensive Test Suite
Overview
The DigitalOutputGuard Comprehensive Test Suite (dog_test) provides extensive testing of the
DigitalOutputGuard class,
which implements RAII (Resource Acquisition Is Initialization) pattern for GPIO output management.
This test suite validates all aspects of the DigitalOutputGuard functionality,
from basic RAII operations to performance characteristics and concurrent access patterns.
Test Configuration
App Type
- Name:
dog_test - Source File:
DigitalOutputGuardComprehensiveTest.cpp - Category:
utility - Build Types: Debug, Release
- CI Enabled: Yes
- Featured: Yes
Test GPIO Pins
The test suite uses only 3 GPIO pins defined as static constexpr:
TEST_GPIO_PIN_1 = 2TEST_GPIO_PIN_2 = 4TEST_GPIO_PIN_3 = 5
Test Sections
1. Basic Tests (ENABLE_BASIC_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests fundamental RAII functionality and state management:
creation: Basic DigitalOutputGuard creation and validationraii_cleanup: Automatic cleanup verification in scopemanual_state_control: Manual SetActive/SetInactive operations
2. Constructor Tests (ENABLE_CONSTRUCTOR_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests constructor variants and error handling:
pointer_constructor: Constructor with GPIO pointernull_pointer_handling: Null pointer error handlingensure_output_mode: Automatic output mode configurationno_ensure_output_mode: Input mode GPIO rejection
3. State Tests (ENABLE_STATE_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests state transitions and GPIO control:
state_transitions: Multiple active/inactive transitionsget_current_state: State query functionality
4. Move Semantics Tests (ENABLE_MOVE_SEMANTICS_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests move operations and resource management:
move_constructor: Move constructor functionalitymove_assignment: Move assignment operator
5. Edge Case Tests (ENABLE_EDGE_CASE_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests edge cases and error conditions:
invalid_operations: Operations on invalid guardsmultiple_guards_same_gpio: Multiple guards managing same GPIO
6. Concurrent Tests (ENABLE_CONCURRENT_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests concurrent access patterns:
concurrent_access: Multi-threaded access with 3 concurrent tasks- 3 FreeRTOS tasks ร 100 operations = 300 total operations
- Validates thread safety and race condition prevention
7. Performance Tests (ENABLE_PERFORMANCE_TESTS)
Blink Pattern: 5 blinks at section start/end
Tests performance and stress scenarios:
performance: Timing measurements for critical operationsstress: High-load testing with multiple GPIO pins
Performance Metrics
Expected Performance Thresholds
- Guard Creation/Destruction: < 100 ฮผs per cycle
- State Transitions: < 50 ฮผs per operation
- Stress Test: < 200 ฮผs per iteration
Typical Performance (ESP32-C6)
- Guard Creation/Destruction: ~2-5 ฮผs per cycle
- State Transitions: ~1-3 ฮผs per operation
- Stress Test: ~5-15 ฮผs per iteration
Progress Indicators
GPIO14 Test Progress Indicator
- Individual Test Progress: GPIO14 toggles HIGH/LOW after each test completion
- Section Indicators: 5 blinks at section start and end
- Visual Feedback: Easy monitoring of test progression
Test Output Format
text
I (timestamp) DIGITAL_OUTPUT_GUARD_Test: [SUCCESS] PASSED (task): test_name (X.XX ms)
I (timestamp) TestFramework: Test progression indicator: HIGH/LOW
text
Building and Running
Build the Test
```bash
From examples/esp32 directory
./scripts/build_app.sh dog_test Release ```text
Flash and Monitor
```bash
Flash the test
./scripts/flash_app.sh flash dog_test Release
Monitor test output
./scripts/flash_app.sh monitor ```text
Flash and Monitor (Combined)
bash
./scripts/flash_app.sh flash_monitor dog_test Release
text
Test Output Example
```text I (254) DIGITAL_OUTPUT_GUARD_Test: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ I (278) DIGITAL_OUTPUT_GUARD_Test: โ ESP32-C6 DIGITAL OUTPUT GUARD COMPREHENSIVE TEST SUITE v1.0 โ I (288) DIGITAL_OUTPUT_GUARD_Test: โ RAII GPIO Management and State Control โ I (299) DIGITAL_OUTPUT_GUARD_Test: โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
I (14920) DIGITAL_OUTPUT_GUARD_Test: Guard creation/destruction: 1000 iterations in 2.27 ms (avg: 2.27 us per cycle) I (14929) DIGITAL_OUTPUT_GUARD_Test: State transitions: 1000 iterations in 1.13 ms (avg: 1.13 us per operation) I (15206) DIGITAL_OUTPUT_GUARD_Test: Stress test: 2000 iterations in 16.12 ms (avg: 8.06 us per iteration)
I (15895) DIGITAL_OUTPUT_GUARD_Test: Total: 16, Passed: 16, Failed: 0, Success: 100.00%, Time: 3560.80 ms I (15904) DIGITAL_OUTPUT_GUARD_Test: [SUCCESS] ALL DIGITAL_OUTPUT_GUARD TESTS PASSED! ```cpp
Test Configuration
Enabling/Disabling Test Sections
Edit the configuration constants at the top of DigitalOutputGuardComprehensiveTest.cpp:
cpp
// Core DigitalOutputGuard functionality tests
static constexpr bool ENABLE_BASIC_TESTS = true; // Basic RAII and state management
static constexpr bool ENABLE_CONSTRUCTOR_TESTS = true; // Constructor variants and error handling
static constexpr bool ENABLE_STATE_TESTS = true; // State transitions and GPIO control
static constexpr bool ENABLE_MOVE_SEMANTICS_TESTS = true; // Move operations and resource management
static constexpr bool ENABLE_EDGE_CASE_TESTS = true; // Edge cases and error conditions
static constexpr bool ENABLE_CONCURRENT_TESTS = true; // Concurrent access testing
static constexpr bool ENABLE_PERFORMANCE_TESTS = true; // Performance and stress testing
text
Performance Interpretation
Excellent Performance Indicators
- Guard creation/destruction < 5 ฮผs: Minimal RAII overhead
- State transitions < 3 ฮผs: Direct GPIO control efficiency
- Stress test < 15 ฮผs: Good scalability under load
- 100% concurrent test success: Robust thread safety
Performance Degradation Warnings
- Guard creation/destruction > 50 ฮผs: Potential memory allocation issues
- State transitions > 20 ฮผs: GPIO driver inefficiency
- Stress test > 100 ฮผs: Resource contention or memory fragmentation
- Concurrent test failures: Thread safety violations
Test Coverage
RAII Pattern Validation
- โ Automatic GPIO activation on construction
- โ Automatic GPIO deactivation on destruction
- โ Exception safety and cleanup guarantees
- โ Scope-based resource management
GPIO State Management
- โ Output mode enforcement
- โ Active/inactive state transitions
- โ State query functionality
- โ Error handling and validation
Constructor Variants
- โ Reference-based constructor
- โ Pointer-based constructor
- โ Null pointer handling
- โ Output mode configuration options
Move Semantics
- โ Move constructor functionality
- โ Move assignment operator
- โ Resource transfer validation
- โ Moved-from state handling
Edge Cases
- โ Invalid guard operations
- โ Multiple guards on same GPIO
- โ Error condition handling
- โ Boundary condition testing
Concurrent Access
- โ Multi-threaded safety
- โ Race condition prevention
- โ Thread-safe operations
- โ Concurrent load testing
Performance Testing
- โ Timing measurements
- โ Stress testing
- โ Scalability validation
- โ Performance regression detection
Integration with Test Framework
The DOG test integrates with the HardFOC test framework:
- Test Framework: Uses
TestFramework.hfor consistent test execution - Progress Indicators: GPIO14-based visual feedback
- Task Management: FreeRTOS task-based test execution
- Error Reporting: Comprehensive error logging and reporting
- Performance Metrics: Detailed timing and performance analysis
Troubleshooting
Common Issues
- GPIO Initialization Failures
- Check GPIO pin availability
- Verify GPIO configuration
- Ensure proper ESP-IDF setup
- Performance Degradation
- Check for memory fragmentation
- Verify GPIO driver efficiency
- Monitor system load
- Concurrent Test Failures
- Check thread safety implementation
- Verify FreeRTOS configuration
- Monitor task priorities
Debug Information
Enable debug logging by modifying the log level in the test configuration or using the ESP-IDF monitor with increased verbosity.