3-phase PV router
Loading...
Searching...
No Matches
test_main.cpp File Reference

Unity tests for power and RMS calculations: old vs new (left-aligned ADC) More...

#include <unity.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cmath>
Include dependency graph for test_main.cpp:

Go to the source code of this file.

Classes

struct  NewImplementation
 
struct  OldImplementation
 
class  SimpleRNG
 Simple deterministic PRNG (xorshift32) for reproducible noise. More...
 
struct  TestStats
 Structure to hold test statistics for comparison. More...
 

Functions

uint16_t generateSineADC (uint16_t sampleIndex, uint16_t dcLevel, int16_t amplitude, float phaseShift=0.0f)
 Generate sinusoidal ADC value.
 
uint16_t generateSineADCWithNoise (uint16_t sampleIndex, uint16_t dcLevel, int16_t amplitude, int16_t noiseAmplitude, float phaseShift=0.0f)
 Generate sinusoidal ADC value with noise.
 
int main (int argc, char **argv)
 
void setUp (void)
 
void tearDown (void)
 
void test_cloud_shadow_simulation (void)
 Test simulating cloud passing over solar panels.
 
void test_daily_solar_profile (void)
 Test simulating a full day solar profile.
 
void test_dc_offset_compensation (void)
 Test with DC offset drift.
 
void test_extended_10000_cycles (void)
 Extended test: 10,000 cycles (~200 seconds at 50Hz)
 
void test_full_cycle_equivalence (void)
 Test complete mains cycle processing equivalence.
 
void test_import_export_transitions (void)
 Test with import/export transitions (bidirectional power flow)
 
void test_instant_power_equivalence (void)
 Test instantaneous power calculation equivalence.
 
void test_mathematical_identity (void)
 CRITICAL TEST: Prove implementations are IDENTICAL over a full cycle.
 
void test_multiple_current_amplitudes (void)
 Test with multiple current amplitudes (light to heavy load)
 
void test_near_adc_limits (void)
 Test with maximum values (near ADC limits)
 
void test_no_accumulator_overflow (void)
 Test accumulator behavior over datalog period.
 
void test_power_export (void)
 Test power calculation with 180° phase shift (export)
 
void test_power_reactive_load (void)
 Test power calculation with 90° phase shift (reactive load)
 
void test_power_unity_power_factor (void)
 Test power calculation with unity power factor (V and I in phase)
 
void test_random_load_steps (void)
 Test with random step changes in current (simulating appliance switching)
 
void test_rms_theoretical_value (void)
 Test RMS calculation for known amplitude.
 
void test_scaling_equivalence (void)
 Test scaling factor equivalence.
 
void test_sinusoidal_load_variation (void)
 Test with sinusoidally varying current (simulating slow load changes)
 
void test_stress_50000_cycles (void)
 Stress test: 50,000 cycles (~17 minutes at 50Hz)
 
void test_three_phase_simulation (void)
 Test multiple phases (3-phase simulation)
 
void test_varying_power_factor_with_noise (void)
 Test with varying power factor and noise.
 
void test_vsquared_accumulation_equivalence (void)
 Test V² accumulation equivalence.
 
void test_with_higher_noise_5lsb (void)
 Extended test with higher noise (±5 LSB)
 
void test_with_realistic_noise_2lsb (void)
 Extended test with realistic ADC noise (±2 LSB)
 
void test_zero_load (void)
 Test with zero load (no current)
 
constexpr uint16_t toLeftAligned (uint16_t rightAligned)
 Convert right-aligned ADC to left-aligned.
 

Variables

constexpr uint16_t ADC_MID_POINT = 512
 
constexpr uint16_t CURRENT_AMPLITUDE = 200
 
NewImplementation newImpl
 
OldImplementation oldImpl
 
constexpr float PI = 3.14159265358979f
 
SimpleRNG rng
 
constexpr uint16_t SAMPLES_PER_CYCLE = 160
 
constexpr uint16_t SAMPLES_PER_HALF_CYCLE = 80
 
constexpr uint16_t VOLTAGE_AMPLITUDE = 400
 

Detailed Description

Unity tests for power and RMS calculations: old vs new (left-aligned ADC)

Author
Based on Florian's suggestions in issue #121
Date
2026-11-24

Tests the power and RMS calculation implementations comparing:

  • Old: Right-aligned ADC (0-1023), x256 scaling
  • New: Left-aligned ADC (0-65472), x64 scaling

Key functions tested:

Definition in file test_main.cpp.

Function Documentation

◆ generateSineADC()

uint16_t generateSineADC ( uint16_t sampleIndex,
uint16_t dcLevel,
int16_t amplitude,
float phaseShift = 0.0f )

Generate sinusoidal ADC value.

Definition at line 339 of file test_main.cpp.

Here is the caller graph for this function:

◆ generateSineADCWithNoise()

uint16_t generateSineADCWithNoise ( uint16_t sampleIndex,
uint16_t dcLevel,
int16_t amplitude,
int16_t noiseAmplitude,
float phaseShift = 0.0f )

Generate sinusoidal ADC value with noise.

Definition at line 352 of file test_main.cpp.

Here is the caller graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 1698 of file test_main.cpp.

Here is the call graph for this function:

◆ setUp()

void setUp ( void )

Definition at line 379 of file test_main.cpp.

◆ tearDown()

void tearDown ( void )

Definition at line 385 of file test_main.cpp.

◆ test_cloud_shadow_simulation()

void test_cloud_shadow_simulation ( void )

Test simulating cloud passing over solar panels.

Current (generation) varies with "cloud shadows" - rapid dips and recoveries.

Definition at line 1445 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_daily_solar_profile()

void test_daily_solar_profile ( void )

Test simulating a full day solar profile.

24-hour simulation with sunrise ramp, midday peak, sunset ramp. ~86,400 cycles at 50Hz = 1 day, scaled down to 8,640 cycles.

Definition at line 1527 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_dc_offset_compensation()

void test_dc_offset_compensation ( void )

Test with DC offset drift.

Simulates real-world scenario where DC level isn't exactly at mid-point

Definition at line 655 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_extended_10000_cycles()

void test_extended_10000_cycles ( void )

Extended test: 10,000 cycles (~200 seconds at 50Hz)

Proves long-term stability and equivalence over extended operation.

Definition at line 957 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_full_cycle_equivalence()

void test_full_cycle_equivalence ( void )

Test complete mains cycle processing equivalence.

Definition at line 603 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_import_export_transitions()

void test_import_export_transitions ( void )

Test with import/export transitions (bidirectional power flow)

Simulates a household with solar: exporting during day, importing at night, with rapid transitions as clouds pass or loads switch.

Definition at line 1616 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_instant_power_equivalence()

void test_instant_power_equivalence ( void )

Test instantaneous power calculation equivalence.

With matching DC offset (512), both implementations produce IDENTICAL results. The rounding (| 32) adds ~0.75% per sample but averages out over a cycle.

Definition at line 399 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_mathematical_identity()

void test_mathematical_identity ( void )

CRITICAL TEST: Prove implementations are IDENTICAL over a full cycle.

With matching DC offset, the rounding (| 32) averages out over a sine cycle, proving the implementations are mathematically identical algorithms.

Definition at line 883 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_multiple_current_amplitudes()

void test_multiple_current_amplitudes ( void )

Test with multiple current amplitudes (light to heavy load)

Simulates different load levels from very light (10A) to heavy (200A equivalent).

Definition at line 1246 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_near_adc_limits()

void test_near_adc_limits ( void )

Test with maximum values (near ADC limits)

Definition at line 788 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_no_accumulator_overflow()

void test_no_accumulator_overflow ( void )

Test accumulator behavior over datalog period.

In real operation, accumulators are reset at the end of each datalog period. This test verifies that single-cycle power values remain valid and consistent.

Definition at line 820 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_power_export()

void test_power_export ( void )

Test power calculation with 180° phase shift (export)

Definition at line 506 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_power_reactive_load()

void test_power_reactive_load ( void )

Test power calculation with 90° phase shift (reactive load)

Definition at line 477 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_power_unity_power_factor()

void test_power_unity_power_factor ( void )

Test power calculation with unity power factor (V and I in phase)

Definition at line 444 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_random_load_steps()

void test_random_load_steps ( void )

Test with random step changes in current (simulating appliance switching)

Current amplitude changes randomly every 10-50 cycles, simulating appliances turning on/off.

Definition at line 1377 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_rms_theoretical_value()

void test_rms_theoretical_value ( void )

Test RMS calculation for known amplitude.

For a pure sine wave, RMS = peak / sqrt(2) ≈ 0.707 * peak

Definition at line 568 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_scaling_equivalence()

void test_scaling_equivalence ( void )

Test scaling factor equivalence.

Verifies that both implementations use mathematically equivalent scaling

Definition at line 925 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_sinusoidal_load_variation()

void test_sinusoidal_load_variation ( void )

Test with sinusoidally varying current (simulating slow load changes)

Current amplitude varies sinusoidally over ~100 cycles, simulating gradual load changes like motor spin-up/down or heating cycles.

Definition at line 1315 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_stress_50000_cycles()

void test_stress_50000_cycles ( void )

Stress test: 50,000 cycles (~17 minutes at 50Hz)

Ultimate long-term stability test.

Definition at line 1185 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_three_phase_simulation()

void test_three_phase_simulation ( void )

Test multiple phases (3-phase simulation)

Definition at line 702 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_varying_power_factor_with_noise()

void test_varying_power_factor_with_noise ( void )

Test with varying power factor and noise.

Simulates real-world scenario with mixed loads and noise. Both implementations receive IDENTICAL samples (same RNG seed per test).

Definition at line 1119 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_vsquared_accumulation_equivalence()

void test_vsquared_accumulation_equivalence ( void )

Test V² accumulation equivalence.

Definition at line 541 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_with_higher_noise_5lsb()

void test_with_higher_noise_5lsb ( void )

Extended test with higher noise (±5 LSB)

Stress test with more noise than typical.

Definition at line 1072 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_with_realistic_noise_2lsb()

void test_with_realistic_noise_2lsb ( void )

Extended test with realistic ADC noise (±2 LSB)

Simulates real-world ADC noise. Both implementations receive IDENTICAL noisy samples, so differences reveal algorithm behavior.

Definition at line 1019 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_zero_load()

void test_zero_load ( void )

Test with zero load (no current)

Definition at line 761 of file test_main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toLeftAligned()

uint16_t toLeftAligned ( uint16_t rightAligned)
constexpr

Convert right-aligned ADC to left-aligned.

Definition at line 367 of file test_main.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ ADC_MID_POINT

uint16_t ADC_MID_POINT = 512
constexpr

Definition at line 31 of file test_main.cpp.

◆ CURRENT_AMPLITUDE

uint16_t CURRENT_AMPLITUDE = 200
constexpr

Definition at line 33 of file test_main.cpp.

◆ newImpl

Definition at line 377 of file test_main.cpp.

◆ oldImpl

Definition at line 376 of file test_main.cpp.

◆ PI

float PI = 3.14159265358979f
constexpr

Definition at line 28 of file test_main.cpp.

◆ rng

SimpleRNG rng

Definition at line 290 of file test_main.cpp.

◆ SAMPLES_PER_CYCLE

uint16_t SAMPLES_PER_CYCLE = 160
constexpr

Definition at line 29 of file test_main.cpp.

◆ SAMPLES_PER_HALF_CYCLE

uint16_t SAMPLES_PER_HALF_CYCLE = 80
constexpr

Definition at line 30 of file test_main.cpp.

◆ VOLTAGE_AMPLITUDE

uint16_t VOLTAGE_AMPLITUDE = 400
constexpr

Definition at line 32 of file test_main.cpp.