| 1 | #include <cgreen/assertions.h> |
| 2 | #include <cgreen/boxed_double.h> |
| 3 | #include <cgreen/constraint_syntax_helpers.h> |
| 4 | #include <cgreen/message_formatting.h> |
| 5 | #include <cgreen/reporter.h> |
| 6 | #include <cgreen/string_comparison.h> |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | #include <inttypes.h> |
| 10 | #include <math.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <float.h> |
| 15 | |
| 16 | |
| 17 | #include "cgreen_value_internal.h" |
| 18 | #include "constraint_internal.h" |
| 19 | |
| 20 | |
| 21 | const char *show_null_as_the_string_null(const char *string); |
| 22 | |
| 23 | |
| 24 | void assert_core_(const char *file, int line, const char *actual_string, intptr_t actual, |
| 25 | Constraint* constraint) { |
| 26 | |
| 27 | char *failure_message; |
| 28 | |
| 29 | if (NULL != constraint && is_not_comparing(constraint)) { |
| 30 | (*get_test_reporter()->assert_true)( |
| 31 | get_test_reporter(), |
| 32 | file, |
| 33 | line, |
| 34 | false, |
| 35 | "Got constraint of type [%s],\n" |
| 36 | "\t\tbut they are not allowed for assertions, only in mock expectations.", |
| 37 | constraint->name); |
| 38 | |
| 39 | constraint->destroy(constraint); |
| 40 | |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (constraint->type == CGREEN_DOUBLE_COMPARER_CONSTRAINT) { |
| 45 | (*get_test_reporter()->assert_true)( |
| 46 | get_test_reporter(), |
| 47 | file, |
| 48 | line, |
| 49 | false, |
| 50 | "Constraints of double type, such as [%s],\n" |
| 51 | "\t\tshould only be used with 'assert_that_double()' to ensure proper comparison.", |
| 52 | constraint->name); |
| 53 | } |
| 54 | |
| 55 | if (parameters_are_not_valid_for(constraint, actual)) { |
| 56 | char *validation_message = validation_failure_message_for(constraint, actual); |
| 57 | |
| 58 | (*get_test_reporter()->assert_true)( |
| 59 | get_test_reporter(), |
| 60 | file, |
| 61 | line, |
| 62 | false, |
| 63 | validation_message); |
| 64 | |
| 65 | constraint->destroy(constraint); |
| 66 | free(validation_message); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | failure_message = constraint->failure_message(constraint, actual_string, actual); |
| 71 | |
| 72 | (*get_test_reporter()->assert_true)( |
| 73 | get_test_reporter(), |
| 74 | file, |
| 75 | line, |
| 76 | (*constraint->compare)(constraint, make_cgreen_integer_value(actual)), |
| 77 | failure_message |
| 78 | ); |
| 79 | |
| 80 | constraint->destroy(constraint); |
| 81 | free(failure_message); |
| 82 | } |
| 83 | |
| 84 | void assert_that_double_(const char *file, int line, const char *expression, double actual, Constraint* constraint) { |
| 85 | BoxedDouble* boxed_actual; |
| 86 | |
| 87 | if (NULL != constraint && is_not_comparing(constraint)) { |
| 88 | (*get_test_reporter()->assert_true)( |
| 89 | get_test_reporter(), |
| 90 | file, |
| 91 | line, |
| 92 | false, |
| 93 | "Got constraint of type [%s],\n" |
| 94 | "\t\tbut they are not allowed for assertions, only in mock expectations.", |
| 95 | constraint->name); |
| 96 | |
| 97 | constraint->destroy(constraint); |
| 98 | |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (constraint->type != CGREEN_DOUBLE_COMPARER_CONSTRAINT) { |
| 103 | (*get_test_reporter()->assert_true)( |
| 104 | get_test_reporter(), |
| 105 | file, |
| 106 | line, |
| 107 | false, |
| 108 | "Only constraints of double type should be used with 'assert_that_double()'.\n" |
| 109 | "\t\tOther types of constraints, such as [%s], will probably fail comparison.", |
| 110 | constraint->name); |
| 111 | } |
| 112 | |
| 113 | boxed_actual = (BoxedDouble*)box_double(actual); |
| 114 | |
| 115 | (*get_test_reporter()->assert_true)(get_test_reporter(), file, line, |
| 116 | (*constraint->compare)(constraint, make_cgreen_double_value(actual)), |
| 117 | "Expected [%s] to [%s] [%s] within [%d] significant figures\n" |
| 118 | "\t\tactual value:\t\t\t[%08f]\n" |
| 119 | "\t\texpected value:\t\t\t[%08f]", |
| 120 | expression, |
| 121 | constraint->name, |
| 122 | constraint->expected_value_name, |
| 123 | get_significant_figures(), |
| 124 | actual, |
| 125 | constraint->expected_value.value.double_value); |
| 126 | |
| 127 | free(boxed_actual); |
| 128 | constraint->destroy(constraint); |
| 129 | } |
| 130 | |
| 131 | void assert_equal_(const char *file, int line, const char *expression, intptr_t tried, intptr_t expected) { |
| 132 | (*get_test_reporter()->assert_true)( |
| 133 | get_test_reporter(), |
| 134 | file, |
| 135 | line, |
| 136 | (tried == expected), |
| 137 | "[%s] should be [%" PRIdPTR "] but was [%" PRIdPTR "]\n", expression, expected, tried); |
| 138 | } |
| 139 | |
| 140 | void assert_not_equal_(const char *file, int line, const char *expression, intptr_t tried, intptr_t expected) { |
| 141 | (*get_test_reporter()->assert_true)( |
| 142 | get_test_reporter(), |
| 143 | file, |
| 144 | line, |
| 145 | (tried != expected), |
| 146 | "[%s] should not be [%" PRIdPTR "] but was\n", expression, expected); |
| 147 | } |
| 148 | |
| 149 | void assert_double_equal_(const char *file, int line, const char *expression, double tried, double expected) { |
| 150 | (*get_test_reporter()->assert_true)( |
| 151 | get_test_reporter(), |
| 152 | file, |
| 153 | line, |
| 154 | doubles_are_equal(tried, expected), |
| 155 | "[%s] should be [%f] within %d significant figures but was [%f]\n", expression, expected, get_significant_figures(), tried); |
| 156 | } |
| 157 | |
| 158 | void assert_double_not_equal_(const char *file, int line, const char *expression, double tried, double expected) { |
| 159 | (*get_test_reporter()->assert_true)( |
| 160 | get_test_reporter(), |
| 161 | file, |
| 162 | line, |
| 163 | ! doubles_are_equal(tried, expected), |
| 164 | "[%s] should not be [%f] within %d significant figures but was [%f]\n", expression, expected, get_significant_figures(), tried); |
| 165 | } |
| 166 | |
| 167 | void assert_string_equal_(const char *file, int line, const char *expression, const char *tried, const char *expected) { |
| 168 | (*get_test_reporter()->assert_true)( |
| 169 | get_test_reporter(), |
| 170 | file, |
| 171 | line, |
| 172 | strings_are_equal(tried, expected), |
| 173 | "[%s] should be [%s] but was [%s]\n", expression, show_null_as_the_string_null(expected), show_null_as_the_string_null(tried)); |
| 174 | } |
| 175 | |
| 176 | void assert_string_not_equal_(const char *file, int line, const char *expression, const char *tried, const char *expected) { |
| 177 | (*get_test_reporter()->assert_true)( |
| 178 | get_test_reporter(), |
| 179 | file, |
| 180 | line, |
| 181 | ! strings_are_equal(tried, expected), |
| 182 | "[%s] should not be [%s] but was\n", expression, show_null_as_the_string_null(expected)); |
| 183 | } |
| 184 | |
| 185 | const char *show_null_as_the_string_null(const char *string) { |
| 186 | return (string == NULL ? "NULL" : string); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /* vim: set ts=4 sw=4 et cindent: */ |