aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/assertions.h
diff options
context:
space:
mode:
authorMichele Papalini <micpapal+fdio@cisco.com>2017-02-24 08:00:33 +0000
committerGerrit Code Review <gerrit@fd.io>2017-02-24 08:00:33 +0000
commit4df7f4cc98b6288177df256e1db70ddc3f7d00db (patch)
tree55e71277b419e4830ae641868ab8e751c8b86972 /longbow/src/LongBow/assertions.h
parentf28308bd99381ef5f1e178e2e1f870f245e35873 (diff)
parentec688b4723a041044226358bcd4dd6e2da39da49 (diff)
Merge "Initial commit: cframework. Longbow and Libparc" into cframework/master
Diffstat (limited to 'longbow/src/LongBow/assertions.h')
-rw-r--r--longbow/src/LongBow/assertions.h152
1 files changed, 152 insertions, 0 deletions
diff --git a/longbow/src/LongBow/assertions.h b/longbow/src/LongBow/assertions.h
new file mode 100644
index 00000000..c175bb1d
--- /dev/null
+++ b/longbow/src/LongBow/assertions.h
@@ -0,0 +1,152 @@
+/*
+ * 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.
+ */
+
+/**
+ * @file assertions.h
+ * @ingroup runtime
+ * @brief Runtime and Test Assertions
+ *
+ */
+#ifndef LongBow_assertions_h
+#define LongBow_assertions_h
+
+#ifndef LongBow_runtime_h
+#error "Do not include LongBow/assertions.h directly. Include LongBow/runtime.h"
+#endif
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <LongBow/traps.h>
+#include <LongBow/tests.h>
+
+#include <LongBow/longBow_Location.h>
+#include <LongBow/longBow_Event.h>
+
+/**
+ * @def assertTrue
+ * @brief Assert that a condition is true.
+ *
+ * @param condition If false, the assertion triggers.
+ * @param ... A printf formatting string (required) and parameters (optional).
+ *
+ * Example:
+ * @code
+ * {
+ * assertTrue(2 + 2 == 4, "Adding 2 + 2 did not equal 4.");
+ * }
+ * @endcode
+ */
+#define assertTrue(condition, ...) longBowAssert(&LongBowAssertEvent, (condition), __VA_ARGS__)
+
+/**
+ * @def assertFalse
+ * @brief Assert that a condition is false.
+ *
+ * @param condition If true, the assertion triggers.
+ * @param ... A printf formatting string (required) and parameters (optional).
+ * Example:
+ * @code
+ * {
+ * assertFalse(2 + 2 == 5, "Adding 2 + 2 must not equal 5.");
+ * }
+ * @endcode
+ */
+#define assertFalse(condition, ...) longBowAssert(&LongBowAssertEvent, !(condition), __VA_ARGS__)
+
+/**
+ * @def assertNotNull
+ * @brief Assert that the given value is not NULL.
+ *
+ * @param x If the value is NULL, the assertion triggers.
+ * @param ... A printf formatting string (required) and parameters (optional).
+ * Example:
+ * @code
+ * void
+ * function(char *p)
+ * {
+ * assertNotNull(p, "Parameter must not be NULL");
+ * }
+ * @endcode
+ */
+#define assertNotNull(x, ...) longBowAssert(&LongBowAssertEvent, (x) != NULL, __VA_ARGS__)
+
+/**
+ * @def assertNull
+ * @brief Assert that the given value is NULL.
+ *
+ * @param x The value to test.
+ * @param ... A printf formatting string (required) and parameters (optional).
+ * Example:
+ * @code
+ * void
+ * function(char *p)
+ * {
+ * assertNull(p, "Parameter must be NULL");
+ * }
+ * @endcode
+ */
+#define assertNull(x, ...) longBowAssert(&LongBowAssertEvent, (x) == NULL, __VA_ARGS__)
+
+/**
+ * @def assertAligned
+ * @brief Assert that the given address is aligned according to `alignment`.
+ *
+ * Return true of the given address is aligned according to alignment.
+ * The value for alignment must be a power of 2.
+ *
+ * @param address A pointer to memory
+ * @param alignment A power of 2 representing the memory alignment of address.
+ * @param ... A printf formatting string (required) and parameters (optional).
+ *
+ * Example:
+ * @code
+ * void
+ * function(void *p)
+ * {
+ * assertAligned(p, 4, "Parameter must be aligned on 4 byte boundaries");
+ * }
+ * @endcode
+ */
+#define assertAligned(address, alignment, ...) \
+ longBowAssert(&LongBowAssertEvent, longBowRuntime_TestAddressIsAligned((address), (alignment)), __VA_ARGS__)
+
+/**
+ * @def assertEqualStrings
+ * @brief Assert that two strings are equal.
+ *
+ * @param expected
+ * @param actual
+ * @param ... A printf formatting string (required) and parameters (optional).
+ *
+ * Example:
+ * @code
+ * {
+ * assertEqualStrings("hello", "world");
+ * }
+ * @endcode
+ */
+#define assertEqualStrings(expected, actual) \
+ longBowAssert(&LongBowAssertEvent, strcmp((expected), (actual)) == 0, "Expected '%s' actual '%s'", (expected), (actual))
+
+/**
+ * @def assertEqual
+ * @brief Assert that two values are equal, using a canonical string message.
+ */
+#define assertEqual(expected, actual, format) \
+ assertTrue((expected) == (actual), "Expected=" format " actual=" format, (expected), (actual))
+#endif /* ASSERTIONS_H_ */
+