From ec688b4723a041044226358bcd4dd6e2da39da49 Mon Sep 17 00:00:00 2001 From: Luca Muscariello Date: Thu, 23 Feb 2017 17:01:02 +0100 Subject: Initial commit: cframework. Longbow and Libparc Change-Id: I90378dbd30da6033b20fb1f829b3b822cf366c59 Signed-off-by: Luca Muscariello --- longbow/src/LongBow/longBow_UnitTesting.c | 121 ++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 longbow/src/LongBow/longBow_UnitTesting.c (limited to 'longbow/src/LongBow/longBow_UnitTesting.c') diff --git a/longbow/src/LongBow/longBow_UnitTesting.c b/longbow/src/LongBow/longBow_UnitTesting.c new file mode 100755 index 00000000..a33846ce --- /dev/null +++ b/longbow/src/LongBow/longBow_UnitTesting.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + */ +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +bool +longBowUnitTesting_AssertEqualsContract(bool (*equalsFunction)(void *a, void *b), void *x, void *y, void *z, ...) +{ + va_list ap; + va_start(ap, z); + + assertNotNull(x, "The value of x cannot be NULL."); + assertNotNull(y, "The value of y cannot be NULL."); + assertNotNull(z, "The value of z cannot be NULL."); + + assertFalse(x == y, "The value x cannot be the same as y"); + assertFalse(x == z, "The value x cannot be the same as z"); + assertFalse(y == z, "The value y cannot be the same as z"); + + assertTrue(equalsFunction(NULL, NULL), "Equality failed: Equals(NULL, NULL) must be true"); + + assertFalse(equalsFunction(x, NULL), "Equality failed: The value of x must not be Equal to NULL."); + assertFalse(equalsFunction(NULL, x), "Equality failed: NULL must not be equal to the value of x."); + + assertTrue(equalsFunction(x, x), + "Reflexive failed: for any non-null reference value x, equals(x, x) must return true."); + + assertTrue(equalsFunction(x, y), + "Equality failed: The values of x and y must be Equal."); + assertTrue(equalsFunction(x, z), + "Equality failed: The values of x and z must be Equal."); + + assertTrue(equalsFunction(x, y) == equalsFunction(y, x), + "Symmetric equality failed: equals(x, y) == equals(y, x) must true."); + + assertTrue((equalsFunction(x, y) == equalsFunction(y, z)) == equalsFunction(z, x), + "Transitive equality failed: equals(x, y) == equals(y, z) == equals(z, x) must true."); + + int index = 0; + for (void *value = va_arg(ap, void *); value != 0; value = va_arg(ap, void *)) { + assertFalse(equalsFunction(x, value), "Value %d (@%p) must not be equal to x", index, value); + assertTrue(equalsFunction(x, value) == equalsFunction(value, x), + "Symmetric equality failed: equals(x, value) == equals(value, x) must true."); + index++; + } + + va_end(ap); + return true; +} + +bool +longBowUnitTesting_AssertCompareToContract(int (*compareTo)(const void *a, const void *b), + void *exemplar, + void **equivalent, + void **lesser, + void **greater) +{ + assertNotNull(exemplar, "Parameter exemplar must not be NULL"); + assertNotNull(equivalent, "Parameter equivalent must not be NULL"); + assertNotNull(lesser, "Parameter less must not be NULL"); + assertNotNull(greater, "Parameter greater must not be NULL"); + + assertTrue(compareTo(NULL, NULL) == 0, + "Comparison of null values must be 0."); + + assertTrue(compareTo(exemplar, NULL) > 0, + "Comparison of a non-null value to a null value must be > 0."); + + assertTrue(compareTo(NULL, exemplar) < 0, + "Comparison of null value to a non-null value must be < 0."); + + assertTrue(compareTo(exemplar, exemplar) == 0, + "Comparison of a value to itself must == 0"); + + for (int i = 0; equivalent[i] != NULL; i++) { + assertTrue(compareTo(exemplar, equivalent[i]) == 0, + "Comparison of the value to equivalent[%d] must == 0", i); + assertTrue(compareTo(exemplar, equivalent[i]) == -compareTo(equivalent[i], exemplar), + "Requires sgn(compareTo(value, equivalent[%d])) == -sgn(equivalent[%d], value)", i, i); + } + for (int i = 0; lesser[i] != NULL; i++) { + assertTrue(compareTo(exemplar, lesser[i]) > 0, + "Compare of value to lesser[%d] must be > 0", i); + assertTrue(compareTo(exemplar, lesser[i]) == -compareTo(lesser[i], exemplar), + "Requires sgn(compareTo(value, lesser[%d])) == -sgn(lesser[%d], value)", i, i); + } + for (int i = 0; greater[i] != NULL; i++) { + assertTrue(compareTo(exemplar, greater[i]) < 0, + "Compare to greater[%d] must be > 0", i); + assertTrue(compareTo(exemplar, greater[i]) == -compareTo(greater[i], exemplar), + "Requires sgn(compareTo(value, greater[%d])) == -sgn(greater[%d], value)", i, i); + } + + return true; +} -- cgit 1.2.3-korg