DEF_CHK_VAL()
Validates a value as greater than or equal to a specified minimum value and less than or equal to a specified maximum value.
Files
lib_def.h
Prototype
DEF_CHK_VAL(val, val_min, val_max);
Arguments
val
Value to validate.
val_min
Minimum value to test.
val_max
Maximum value to test.
Returned Value
DEF_OK
Value is greater than or equal to minimum value AND less than or equal to maximum value;
DEF_FAIL
otherwise.
Required Configuration
None.
Notes / Warnings
DEF_CHK_VAL()
avoids directly comparing any two values if only one of the values is negative since the negative value might be incorrectly promoted to an arbitrary unsigned value if the other value to compare is unsigned.
Validation of values is limited to the range supported by the compiler and/or target environment. All other values that underflow/overflow the supported range will modulo/wrap into the supported range as arbitrary signed or unsigned values. Therefore, any values that underflow the most negative signed value or overflow the most positive unsigned value supported by the compiler and/or target environment cannot be validated:
( N-1 N ]
( -(2 ) , 2 - 1 ]
( ]
where N
is the number of data word bits supported by the compiler and/or target environment. Note that the most negative value, -2^(N-1)
, is not included in the supported range since many compilers do not always correctly handle this value.
DEF_CHK_VAL()
does not validate that the maximum value (val_max
) is greater than or equal to the minimum value (val_min
).
Example Usage
#define CFG_VAL -1 #if (DEF_CHK_VAL_MAX(CFG_VAL, 0u, 1000u) != DEF_OK) /* Signed CFG_VAL NOT promoted to unsigned. */ #error "CFG_VAL must be >= 0 and <= 100" #endif