Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,36 @@ The bit is specified 0-31 for a 32-bit integer.
### Numerical Assertions: Floats

TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual)
TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual)

Asserts that the actual value is within plus or minus delta of the expected value.

TEST_ASSERT_FLOAT_NOT_WITHIN(delta, expected, actual)
TEST_ASSERT_DOUBLE_NOT_WITHIN(delta, expected, actual)

Asserts that the actual value is NOT within plus or minus delta of the expected value.

TEST_ASSERT_EQUAL_FLOAT(expected, actual)
TEST_ASSERT_EQUAL_DOUBLE(expected, actual)

Asserts that two floating point values are "equal" within a small % delta of the expected value.

TEST_ASSERT_NOT_EQUAL_FLOAT(expected, actual)
TEST_ASSERT_NOT_EQUAL_DOUBLE(expected, actual)

Asserts that two floating point values are NOT "equal" within a small % delta of the expected value.

TEST_ASSERT_LESS_THAN_FLOAT(threshold, actual)
TEST_ASSERT_LESS_THAN_DOUBLE(threshold, actual)
TEST_ASSERT_GREATER_THAN_FLOAT(threshold, actual)
TEST_ASSERT_GREATER_THAN_DOUBLE(threshold, actual)

Asserts that the actual value is less than or greater than the threshold.

There are also `LESS_OR_EQUAL` and `GREATER_OR_EQUAL` variations.
These obey the same rules for equality as do `TEST_ASSERT_EQUAL_FLOAT` and `TEST_ASSERT_EQUAL_DOUBLE`:
If the two values are within a small % delta of the expected value, the assertion will pass.

### String Assertions

TEST_ASSERT_EQUAL_STRING(expected, actual)
Expand Down
78 changes: 70 additions & 8 deletions docs/UnityAssertionsReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,21 +555,52 @@ Asserts that the `actual` value is within +/- `delta` of the `expected` value.
The nature of floating point representation is such that exact evaluations of
equality are not guaranteed.

#### `TEST_ASSERT_FLOAT_WITHIN (delta, expected, actual)`

Asserts that the `actual` value is NOT within +/- `delta` of the `expected` value.

#### `TEST_ASSERT_EQUAL_FLOAT (expected, actual)`

Asserts that the ?actual?value is "close enough to be considered equal" to the
Asserts that the `actual` value is "close enough to be considered equal" to the
`expected` value. If you are curious about the details, refer to the Advanced
Asserting section for more details on this. Omitting a user-specified delta in a
floating point assertion is both a shorthand convenience and a requirement of
code generation conventions for CMock.

#### `TEST_ASSERT_NOT_EQUAL_FLOAT (expected, actual)`

Asserts that the `actual` value is NOT "close enough to be considered equal" to the
`expected` value.

#### `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`

See Array assertion section for details. Note that individual array element
float comparisons are executed using T?EST_ASSERT_EQUAL_FLOAT?.That is, user
float comparisons are executed using `TEST_ASSERT_EQUAL_FLOAT`.That is, user
specified delta comparison values requires a custom-implemented floating point
array assertion.

#### `TEST_ASSERT_LESS_THAN_FLOAT (threshold, actual)`

Asserts that the `actual` parameter is less than `threshold` (exclusive).
For example, if the threshold value is 1.0f, the assertion will fail if it is
greater than 1.0f.

#### `TEST_ASSERT_GREATER_THAN_FLOAT (threshold, actual)`

Asserts that the `actual` parameter is greater than `threshold` (exclusive).
For example, if the threshold value is 1.0f, the assertion will fail if it is
less than 1.0f.

#### `TEST_ASSERT_LESS_OR_EQUAL_FLOAT (threshold, actual)`

Asserts that the `actual` parameter is less than or equal to `threshold`.
The rules for equality are the same as for `TEST_ASSERT_EQUAL_FLOAT`.

#### `TEST_ASSERT_GREATER_OR_EQUAL_FLOAT (threshold, actual)`

Asserts that the `actual` parameter is greater than `threshold`.
The rules for equality are the same as for `TEST_ASSERT_EQUAL_FLOAT`.

#### `TEST_ASSERT_FLOAT_IS_INF (actual)`

Asserts that `actual` parameter is equivalent to positive infinity floating
Expand All @@ -586,7 +617,7 @@ Asserts that `actual` parameter is a Not A Number floating point representation.

#### `TEST_ASSERT_FLOAT_IS_DETERMINATE (actual)`

Asserts that ?actual?parameter is a floating point representation usable for
Asserts that `actual` parameter is a floating point representation usable for
mathematical operations. That is, the `actual` parameter is neither positive
infinity nor negative infinity nor Not A Number floating point representations.

Expand Down Expand Up @@ -619,6 +650,10 @@ Asserts that the `actual` value is within +/- `delta` of the `expected` value.
The nature of floating point representation is such that exact evaluations of
equality are not guaranteed.

#### `TEST_ASSERT_DOUBLE_NOT_WITHIN (delta, expected, actual)`

Asserts that the `actual` value is NOT within +/- `delta` of the `expected` value.

#### `TEST_ASSERT_EQUAL_DOUBLE (expected, actual)`

Asserts that the `actual` value is "close enough to be considered equal" to the
Expand All @@ -627,13 +662,40 @@ Asserting section for more details. Omitting a user-specified delta in a
floating point assertion is both a shorthand convenience and a requirement of
code generation conventions for CMock.

#### `TEST_ASSERT_NOT_EQUAL_DOUBLE (expected, actual)`

Asserts that the `actual` value is NOT "close enough to be considered equal" to the
`expected` value.

#### `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`

See Array assertion section for details. Note that individual array element
double comparisons are executed using `TEST_ASSERT_EQUAL_DOUBLE`.That is, user
specified delta comparison values requires a custom implemented double array
assertion.

#### `TEST_ASSERT_LESS_THAN_DOUBLE (threshold, actual)`

Asserts that the `actual` parameter is less than `threshold` (exclusive).
For example, if the threshold value is 1.0, the assertion will fail if it is
greater than 1.0.

#### `TEST_ASSERT_LESS_OR_EQUAL_DOUBLE (threshold, actual)`

Asserts that the `actual` parameter is less than or equal to `threshold`.
The rules for equality are the same as for `TEST_ASSERT_EQUAL_DOUBLE`.

#### `TEST_ASSERT_GREATER_THAN_DOUBLE (threshold, actual)`

Asserts that the `actual` parameter is greater than `threshold` (exclusive).
For example, if the threshold value is 1.0, the assertion will fail if it is
less than 1.0.

#### `TEST_ASSERT_GREATER_OR_EQUAL_DOUBLE (threshold, actual)`

Asserts that the `actual` parameter is greater than or equal to `threshold`.
The rules for equality are the same as for `TEST_ASSERT_EQUAL_DOUBLE`.

#### `TEST_ASSERT_DOUBLE_IS_INF (actual)`

Asserts that `actual` parameter is equivalent to positive infinity floating
Expand All @@ -651,7 +713,7 @@ Asserts that `actual` parameter is a Not A Number floating point representation.
#### `TEST_ASSERT_DOUBLE_IS_DETERMINATE (actual)`

Asserts that `actual` parameter is a floating point representation usable for
mathematical operations. That is, the ?actual?parameter is neither positive
mathematical operations. That is, the `actual` parameter is neither positive
infinity nor negative infinity nor Not A Number floating point representations.

#### `TEST_ASSERT_DOUBLE_IS_NOT_INF (actual)`
Expand Down Expand Up @@ -775,11 +837,11 @@ What about the times where you suddenly need to deal with something odd, like a
affect you:

1. When Unity displays errors for you, it's going to pad the upper unused bits
with zeros.
with zeros.
2. You're going to have to be careful of assertions that perform signed
operations, particularly `TEST_ASSERT_INT_WITHIN`.Such assertions might wrap
your `int` in the wrong place, and you could experience false failures. You can
always back down to a simple `TEST_ASSERT` and do the operations yourself.
operations, particularly `TEST_ASSERT_INT_WITHIN`. Such assertions might wrap
your `int` in the wrong place, and you could experience false failures. You can
always back down to a simple `TEST_ASSERT` and do the operations yourself.

*Find The Latest of This And More at [ThrowTheSwitch.org][]*

Expand Down
108 changes: 108 additions & 0 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,60 @@ void UnityAssertFloatsWithin(const UNITY_FLOAT delta,
}
}

/*-----------------------------------------------*/
void UnityAssertFloatsNotWithin(const UNITY_FLOAT delta,
const UNITY_FLOAT expected,
const UNITY_FLOAT actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
RETURN_IF_FAIL_OR_IGNORE;

if (UnityFloatsWithin(delta, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintFloat((UNITY_DOUBLE)expected);
UnityPrint(UnityStrNotEqual);
UnityPrintFloat((UNITY_DOUBLE)actual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertGreaterOrLessFloat(const UNITY_FLOAT threshold,
const UNITY_FLOAT actual,
const UNITY_COMPARISON_T compare,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
int failed;

RETURN_IF_FAIL_OR_IGNORE;

failed = 0;

/* Checking for "not success" rather than failure to get the right result for NaN */
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }

if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }

if (failed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintFloat(actual);
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
UnityPrintFloat(threshold);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
const char* msg,
Expand Down Expand Up @@ -1117,6 +1171,60 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
}
}

/*-----------------------------------------------*/
void UnityAssertDoublesNotWithin(const UNITY_DOUBLE delta,
const UNITY_DOUBLE expected,
const UNITY_DOUBLE actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
RETURN_IF_FAIL_OR_IGNORE;

if (UnityDoublesWithin(delta, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintFloat((UNITY_DOUBLE)expected);
UnityPrint(UnityStrNotEqual);
UnityPrintFloat((UNITY_DOUBLE)actual);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertGreaterOrLessDouble(const UNITY_DOUBLE threshold,
const UNITY_DOUBLE actual,
const UNITY_COMPARISON_T compare,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
int failed;

RETURN_IF_FAIL_OR_IGNORE;

failed = 0;

/* Checking for "not success" rather than failure to get the right result for NaN */
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }

if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }

if (failed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintFloat(actual);
if (compare & UNITY_GREATER_THAN) { UnityPrint(UnityStrGt); }
if (compare & UNITY_SMALLER_THAN) { UnityPrint(UnityStrLt); }
if (compare & UNITY_EQUAL_TO) { UnityPrint(UnityStrOrEqual); }
UnityPrintFloat(threshold);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual,
const char* msg,
Expand Down
Loading