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

Native unit tests for datalog_math.h. More...

#include <unity.h>
#include <math.h>
#include "datalog_math.h"
Include dependency graph for test_main.cpp:

Go to the source code of this file.

Functions

static void checkShiftedAverage (const int32_t amplitude, const int32_t offset, const int32_t noise, const int32_t delta)
 
int main (int argc, char **argv)
 
static double referenceVrms (uint32_t meanSquare, uint16_t factor, float cal)
 The float arithmetic being replaced, in hundredths of a volt (x4 for long periods).
 
static double referenceWatts (int32_t averagePower, float cal)
 The float arithmetic being replaced, in watts (import = +ve).
 
void setUp (void)
 
void tearDown (void)
 
void test_average_power_shifted_is_the_exact_average (void)
 
void test_average_power_unshifted_is_the_quotient (void)
 
void test_isqrt_at_every_boundary (void)
 Every rounding boundary of every root: round(sqrt(x)) changes from r to r + 1 between r^2 + r and r^2 + r + 1, since (r + 0.5)^2 = r^2 + r + 0.25.
 
void test_isqrt_edge_cases (void)
 
void test_isqrt_matches_the_reference (void)
 2 million values over the whole 32-bit range, against sqrt() in double precision (exact here).
 
void test_isqrt_of_the_largest_value_saturates (void)
 
void test_power_calibration_is_constexpr (void)
 
void test_power_shift_follows_the_sample_count (void)
 
void test_power_shift_keeps_30_s_of_any_signal_in_32_bits (void)
 
void test_shifted_sum_is_within_half_the_shift_over_30_s (void)
 
void test_shifted_sum_keeps_the_average_of_a_noisy_signal_over_30_s (void)
 
void test_sqrtScaled_keeps_15_significant_bits (void)
 
void test_sqrtScaled_of_zero (void)
 
void test_valid_voltage_calibration (void)
 
void test_voltage_calibration_is_constexpr (void)
 
void test_vrms_at_230_V (void)
 
void test_vrms_factor_follows_the_scaling_of_the_isr (void)
 
void test_vrms_matches_the_reference_long_period (void)
 
void test_vrms_matches_the_reference_short_period (void)
 
void test_vrms_of_zero (void)
 
void test_vrms_saturates (void)
 
void test_watts_differs_from_the_float_code_by_at_most_1_W (void)
 
void test_watts_is_sign_symmetric (void)
 
void test_watts_matches_the_reference_extremes (void)
 
void test_watts_matches_the_reference_new_board (void)
 
void test_watts_matches_the_reference_old_board (void)
 
void test_watts_saturates_beyond_int16 (void)
 
template<uint8_t N>
static double worstVrmsError (uint16_t factor, const float(&cal)[N], uint32_t maxMeanSquare)
 Largest error of vrms() against the reference, from 0 to the largest ADC voltage, beyond what the 16-bit square root and calibration constant explain.
 
template<uint8_t N>
static double worstWattsError (const float(&cal)[N])
 Largest error of watts() against the reference, over the full input range, beyond what the 16-bit calibration constant explains.
 

Variables

static constexpr int32_t FULL_SCALE { 1L << 18 }
 
static constexpr float largeCal [] { 0.5F, 0.5F, 0.5F }
 
static constexpr float newBoard20Cal [] { 0.057281F, 0.057281F, 0.057281F }
 
static constexpr float newBoard50Cal [] { 0.1432025F, 0.1432025F, 0.1432025F }
 
static constexpr float newBoardVCal [] { 0.888065256F, 0.888065256F, 0.888065256F }
 
static constexpr float oldBoardCal [] { 0.05F, 0.05F, 0.05F }
 
static constexpr float oldBoardVCal [] { 0.8151F, 0.8184F, 0.8195F }
 
static constexpr float smallCal [] { 0.02F, 0.02F, 0.02F }
 

Detailed Description

Native unit tests for datalog_math.h.

The integer datalog arithmetic must reproduce the float arithmetic it replaces (#120), power_L = -(sumP / n) * f_powerCal[phase] Vrms_L_x100 = factor * f_voltageCal[phase] * sqrt(sum_Vsquared / n) over the full input range, without overflow. The float expressions, evaluated in double precision, are the reference. No config.h, no stubs.

Definition in file test_main.cpp.

Function Documentation

◆ checkShiftedAverage()

static void checkShiftedAverage ( const int32_t amplitude,
const int32_t offset,
const int32_t noise,
const int32_t delta )
static

30 s at 50 Hz, 32 sample sets per cycle, summed as the ISR does (rounded, / 16), against the exact average (64-bit sum, here only); noise: pseudo-random, uniform over +-noise

Definition at line 288 of file test_main.cpp.

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

◆ main()

int main ( int argc,
char ** argv )

Definition at line 399 of file test_main.cpp.

Here is the call graph for this function:

◆ referenceVrms()

static double referenceVrms ( uint32_t meanSquare,
uint16_t factor,
float cal )
static

The float arithmetic being replaced, in hundredths of a volt (x4 for long periods).

Definition at line 46 of file test_main.cpp.

Here is the caller graph for this function:

◆ referenceWatts()

static double referenceWatts ( int32_t averagePower,
float cal )
static

The float arithmetic being replaced, in watts (import = +ve).

Definition at line 40 of file test_main.cpp.

Here is the caller graph for this function:

◆ setUp()

void setUp ( void )

Definition at line 19 of file test_main.cpp.

◆ tearDown()

void tearDown ( void )

Definition at line 20 of file test_main.cpp.

◆ test_average_power_shifted_is_the_exact_average()

void test_average_power_shifted_is_the_exact_average ( void )

Definition at line 271 of file test_main.cpp.

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

◆ test_average_power_unshifted_is_the_quotient()

void test_average_power_unshifted_is_the_quotient ( void )

Definition at line 264 of file test_main.cpp.

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

◆ test_isqrt_at_every_boundary()

void test_isqrt_at_every_boundary ( void )

Every rounding boundary of every root: round(sqrt(x)) changes from r to r + 1 between r^2 + r and r^2 + r + 1, since (r + 0.5)^2 = r^2 + r + 0.25.

Definition at line 116 of file test_main.cpp.

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

◆ test_isqrt_edge_cases()

void test_isqrt_edge_cases ( void )

Definition at line 149 of file test_main.cpp.

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

◆ test_isqrt_matches_the_reference()

void test_isqrt_matches_the_reference ( void )

2 million values over the whole 32-bit range, against sqrt() in double precision (exact here).

Definition at line 135 of file test_main.cpp.

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

◆ test_isqrt_of_the_largest_value_saturates()

void test_isqrt_of_the_largest_value_saturates ( void )

Definition at line 161 of file test_main.cpp.

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

◆ test_power_calibration_is_constexpr()

void test_power_calibration_is_constexpr ( void )

Definition at line 188 of file test_main.cpp.

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

◆ test_power_shift_follows_the_sample_count()

void test_power_shift_follows_the_sample_count ( void )

Definition at line 245 of file test_main.cpp.

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

◆ test_power_shift_keeps_30_s_of_any_signal_in_32_bits()

void test_power_shift_keeps_30_s_of_any_signal_in_32_bits ( void )

Definition at line 257 of file test_main.cpp.

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

◆ test_shifted_sum_is_within_half_the_shift_over_30_s()

void test_shifted_sum_is_within_half_the_shift_over_30_s ( void )

Definition at line 305 of file test_main.cpp.

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

◆ test_shifted_sum_keeps_the_average_of_a_noisy_signal_over_30_s()

void test_shifted_sum_keeps_the_average_of_a_noisy_signal_over_30_s ( void )

Definition at line 319 of file test_main.cpp.

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

◆ test_sqrtScaled_keeps_15_significant_bits()

void test_sqrtScaled_keeps_15_significant_bits ( void )

Definition at line 166 of file test_main.cpp.

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

◆ test_sqrtScaled_of_zero()

void test_sqrtScaled_of_zero ( void )

Definition at line 177 of file test_main.cpp.

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

◆ test_valid_voltage_calibration()

void test_valid_voltage_calibration ( void )

Definition at line 386 of file test_main.cpp.

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

◆ test_voltage_calibration_is_constexpr()

void test_voltage_calibration_is_constexpr ( void )

Definition at line 333 of file test_main.cpp.

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

◆ test_vrms_at_230_V()

void test_vrms_at_230_V ( void )

Definition at line 355 of file test_main.cpp.

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

◆ test_vrms_factor_follows_the_scaling_of_the_isr()

void test_vrms_factor_follows_the_scaling_of_the_isr ( void )

Definition at line 377 of file test_main.cpp.

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

◆ test_vrms_matches_the_reference_long_period()

void test_vrms_matches_the_reference_long_period ( void )

Definition at line 348 of file test_main.cpp.

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

◆ test_vrms_matches_the_reference_short_period()

void test_vrms_matches_the_reference_short_period ( void )

Definition at line 341 of file test_main.cpp.

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

◆ test_vrms_of_zero()

void test_vrms_of_zero ( void )

Definition at line 365 of file test_main.cpp.

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

◆ test_vrms_saturates()

void test_vrms_saturates ( void )

Definition at line 371 of file test_main.cpp.

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

◆ test_watts_differs_from_the_float_code_by_at_most_1_W()

void test_watts_differs_from_the_float_code_by_at_most_1_W ( void )

Definition at line 229 of file test_main.cpp.

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

◆ test_watts_is_sign_symmetric()

void test_watts_is_sign_symmetric ( void )

Definition at line 213 of file test_main.cpp.

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

◆ test_watts_matches_the_reference_extremes()

void test_watts_matches_the_reference_extremes ( void )

Definition at line 207 of file test_main.cpp.

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

◆ test_watts_matches_the_reference_new_board()

void test_watts_matches_the_reference_new_board ( void )

Definition at line 201 of file test_main.cpp.

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

◆ test_watts_matches_the_reference_old_board()

void test_watts_matches_the_reference_old_board ( void )

Definition at line 196 of file test_main.cpp.

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

◆ test_watts_saturates_beyond_int16()

void test_watts_saturates_beyond_int16 ( void )

Definition at line 222 of file test_main.cpp.

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

◆ worstVrmsError()

template<uint8_t N>
static double worstVrmsError ( uint16_t factor,
const float(&) cal[N],
uint32_t maxMeanSquare )
static

Largest error of vrms() against the reference, from 0 to the largest ADC voltage, beyond what the 16-bit square root and calibration constant explain.

Both have at least 16 significant bits (relative error <= 2^-16 each, 2^-14 with a margin), and the result is rounded (<= 0.5): the returned value must stay <= 0.5, i.e. 0.012 V at most at 230 V.

Definition at line 89 of file test_main.cpp.

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

◆ worstWattsError()

template<uint8_t N>
static double worstWattsError ( const float(&) cal[N])
static

Largest error of watts() against the reference, over the full input range, beyond what the 16-bit calibration constant explains.

The calibration constant has at least 16 significant bits (relative error <= 2^-16, the same constant as in the ISR), and the result is rounded (<= 0.5 W): the returned value must stay <= 0.5 W.

Definition at line 60 of file test_main.cpp.

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

Variable Documentation

◆ FULL_SCALE

int32_t FULL_SCALE { 1L << 18 }
staticconstexpr

Definition at line 26 of file test_main.cpp.

◆ largeCal

float largeCal[] { 0.5F, 0.5F, 0.5F }
staticconstexpr

Definition at line 33 of file test_main.cpp.

◆ newBoard20Cal

float newBoard20Cal[] { 0.057281F, 0.057281F, 0.057281F }
staticconstexpr

Definition at line 31 of file test_main.cpp.

◆ newBoard50Cal

float newBoard50Cal[] { 0.1432025F, 0.1432025F, 0.1432025F }
staticconstexpr

Definition at line 30 of file test_main.cpp.

◆ newBoardVCal

float newBoardVCal[] { 0.888065256F, 0.888065256F, 0.888065256F }
staticconstexpr

Definition at line 37 of file test_main.cpp.

◆ oldBoardCal

float oldBoardCal[] { 0.05F, 0.05F, 0.05F }
staticconstexpr

Definition at line 29 of file test_main.cpp.

◆ oldBoardVCal

float oldBoardVCal[] { 0.8151F, 0.8184F, 0.8195F }
staticconstexpr

Definition at line 36 of file test_main.cpp.

◆ smallCal

float smallCal[] { 0.02F, 0.02F, 0.02F }
staticconstexpr

Definition at line 32 of file test_main.cpp.