0001 #include <cgreen/assertions.h>
0002 #include <cgreen/boxed_double.h>
0003 #include <cgreen/constraint_syntax_helpers.h>
0004 #include <cgreen/message_formatting.h>
0005 #include <cgreen/reporter.h>
0006 #include <cgreen/string_comparison.h>
0007 #include <stdint.h>
0008 #include <math.h>
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <string.h>
0012 #include <ndrx_config.h>
0013
0014 #ifdef __cplusplus
0015 namespace cgreen {
0016 #endif
0017
0018
0019 #ifdef max
0020 #undef max
0021 #endif
0022
0023 #ifdef min
0024 #undef min
0025 #endif
0026
0027 #define max(a,b) ((a) > (b) ? (a) : (b))
0028 #define min(a,b) ((a) > (b) ? (b) : (a))
0029
0030 static double accuracy(int significant_figures, double largest);
0031
0032 static int significant_figures = 8;
0033
0034 void assert_that_(const char *file, int line, const char *actual_string, intptr_t actual, Constraint* constraint) {
0035
0036 char *failure_message;
0037 if (NULL != constraint && is_not_comparing(constraint)) {
0038 (*get_test_reporter()->assert_true)(
0039 get_test_reporter(),
0040 file,
0041 line,
0042 false,
0043 "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
0044 constraint->name);
0045
0046 constraint->destroy(constraint);
0047
0048 return;
0049 }
0050
0051 if (parameters_are_not_valid_for(constraint, actual)) {
0052 char *validation_message = validation_failure_message_for(constraint, actual);
0053
0054 (*get_test_reporter()->assert_true)(
0055 get_test_reporter(),
0056 file,
0057 line,
0058 false,
0059 validation_message);
0060
0061 constraint->destroy(constraint);
0062 free(validation_message);
0063 return;
0064 }
0065
0066 failure_message = constraint->failure_message(constraint, actual_string, actual);
0067
0068 (*get_test_reporter()->assert_true)(
0069 get_test_reporter(),
0070 file,
0071 line,
0072 (*constraint->compare)(constraint, actual),
0073 failure_message
0074 );
0075
0076 constraint->destroy(constraint);
0077 free(failure_message);
0078 }
0079
0080 void assert_that_double_(const char *file, int line, const char *expression, double actual, Constraint* constraint) {
0081 BoxedDouble* boxed_actual;
0082 if (NULL != constraint && is_not_comparing(constraint)) {
0083 (*get_test_reporter()->assert_true)(
0084 get_test_reporter(),
0085 file,
0086 line,
0087 false,
0088 "\tGot constraint of type [%s], but they are not allowed for assertions, only in mock expectations.",
0089 constraint->name);
0090
0091 constraint->destroy(constraint);
0092
0093 return;
0094 }
0095
0096 boxed_actual = (BoxedDouble*)box_double(actual);
0097
0098 (*get_test_reporter()->assert_true)(get_test_reporter(), file, line, (*constraint->compare)(constraint, (intptr_t)boxed_actual),
0099 "Expected [%s] to [%s] [%s] within [%d] significant figures"
0100 "\t\tactual value:\t%08f\n"
0101 "\t\texpected value:\t%08f",
0102 expression,
0103 constraint->name,
0104 constraint->expected_value_name,
0105 significant_figures,
0106 actual,
0107 as_double(constraint->expected_value));
0108
0109 free(boxed_actual);
0110 constraint->destroy(constraint);
0111 }
0112
0113 void assert_equal_(const char *file, int line, const char *expression, intptr_t tried, intptr_t expected) {
0114 (*get_test_reporter()->assert_true)(
0115 get_test_reporter(),
0116 file,
0117 line,
0118 (tried == expected),
0119 "[%s] should be [%d] but was [%d]\n", expression, expected, tried);
0120 }
0121
0122 void assert_not_equal_(const char *file, int line, const char *expression, intptr_t tried, intptr_t expected) {
0123 (*get_test_reporter()->assert_true)(
0124 get_test_reporter(),
0125 file,
0126 line,
0127 (tried != expected),
0128 "[%s] should not be [%d] but was\n", expression, expected, tried);
0129 }
0130
0131 void assert_double_equal_(const char *file, int line, const char *expression, double tried, double expected) {
0132 (*get_test_reporter()->assert_true)(
0133 get_test_reporter(),
0134 file,
0135 line,
0136 doubles_are_equal(tried, expected),
0137 "[%s] should be [%f] within %d significant figures but was [%f]\n", expression, expected, significant_figures, tried);
0138 }
0139
0140 void assert_double_not_equal_(const char *file, int line, const char *expression, double tried, double expected) {
0141 (*get_test_reporter()->assert_true)(
0142 get_test_reporter(),
0143 file,
0144 line,
0145 ! doubles_are_equal(tried, expected),
0146 "[%s] should not be [%f] within %d significant figures but was [%f]\n", expression, expected, significant_figures, tried);
0147 }
0148
0149 void assert_string_equal_(const char *file, int line, const char *expression, const char *tried, const char *expected) {
0150 (*get_test_reporter()->assert_true)(
0151 get_test_reporter(),
0152 file,
0153 line,
0154 strings_are_equal(tried, expected),
0155 "[%s] should be [%s] but was [%s]\n", expression, show_null_as_the_string_null(expected), show_null_as_the_string_null(tried));
0156 }
0157
0158 void assert_string_not_equal_(const char *file, int line, const char *expression, const char *tried, const char *expected) {
0159 (*get_test_reporter()->assert_true)(
0160 get_test_reporter(),
0161 file,
0162 line,
0163 ! strings_are_equal(tried, expected),
0164 "[%s] should not be [%s] but was\n", expression, show_null_as_the_string_null(expected));
0165 }
0166
0167 void significant_figures_for_assert_double_are(int figures) {
0168 significant_figures = figures;
0169 }
0170
0171 const char *show_null_as_the_string_null(const char *string) {
0172 return (string == NULL ? "NULL" : string);
0173 }
0174
0175 bool doubles_are_equal(double tried, double expected) {
0176
0177 #ifdef EX_OS_AIX
0178
0179
0180 if (fabs(tried-expected)<0.1) {
0181 return true;
0182 }
0183
0184 return false;
0185
0186 #else
0187 return max(tried, expected) - min(tried, expected) < accuracy(significant_figures, max(tried, expected));
0188 #endif
0189
0190 }
0191
0192
0193 static double accuracy(int figures, double largest) {
0194 return pow(10, 1 + (int)log10(largest) - figures);
0195 }
0196
0197 #ifdef __cplusplus
0198 }
0199 #endif
0200
0201