Test: Refactor trigonometric tests into a helper. The precision test of these functions need a special helper, it can also be used for their arc functions down the line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
diff --git a/test/testautomation_math.c b/test/testautomation_math.c
index 729606d..87e208e 100644
--- a/test/testautomation_math.c
+++ b/test/testautomation_math.c
@@ -76,6 +76,37 @@ helper_dtod(const char *func_name, d_to_d_func func,
}
/**
+ * \brief Runs all the cases on a given function with a signature double -> double,
+ * checks the first ten digits of the result (truncated).
+ *
+ * This function is used to test functions with inaccurate results such as trigonometric
+ * functions where angles such as PI/2 can't be accurately represented.
+ *
+ * \note Tests may fail if SDL_trunc is not functional.
+ *
+ * \param func_name, the name of the tested function.
+ * \param func, the function to call.
+ * \param cases, an array of all the cases.
+ * \param cases_size, the size of the cases array.
+ */
+static int
+helper_dtod_approx(const char *func_name, d_to_d_func func,
+ const d_to_d *cases, const size_t cases_size)
+{
+ Uint32 i;
+ for (i = 0; i < cases_size; i++) {
+ const double result = func(cases[i].input) * 1.0E10;
+ SDLTest_AssertCheck(SDL_trunc(result) == cases[i].expected,
+ "%s(%f), expected %f, got %f",
+ func_name,
+ cases[i].input,
+ cases[i].expected, result);
+ }
+
+ return TEST_COMPLETED;
+}
+
+/**
* \brief Runs all the cases on a given function with a signature (double, double) -> double
*
* \param func_name, the name of the tested function.
@@ -1791,29 +1822,31 @@ cos_regularCases(void *args)
/**
* \brief Checks cosine precision for the first 10 decimals.
- *
- * This function depends on SDL_floor functioning.
*/
static int
cos_precisionTest(void *args)
{
- Uint32 i;
- Uint32 iterations = 20;
- double angle = 0.0;
- double step = 2.0 * M_PI / iterations;
- const double expected[] = {
- 10000000000.0, 9510565162.0, 8090169943.0, 5877852522.0, 3090169943.0,
- 0.0, -3090169943.0, -5877852522.0, -8090169943.0, -9510565162.0,
- -10000000000.0, -9510565162.0, -8090169943.0, -5877852522.0, -3090169943.0,
- 0.0, 3090169943.0, 5877852522.0, 8090169943.0, 9510565162.0
+ const d_to_d precision_cases[] = {
+ { M_PI * 1.0 / 10.0, 9510565162.0 },
+ { M_PI * 2.0 / 10.0, 8090169943.0 },
+ { M_PI * 3.0 / 10.0, 5877852522.0 },
+ { M_PI * 4.0 / 10.0, 3090169943.0 },
+ { M_PI * 5.0 / 10.0, 0.0 },
+ { M_PI * 6.0 / 10.0, -3090169943.0 },
+ { M_PI * 7.0 / 10.0, -5877852522.0 },
+ { M_PI * 8.0 / 10.0, -8090169943.0 },
+ { M_PI * 9.0 / 10.0, -9510565162.0 },
+ { M_PI * -1.0 / 10.0, 9510565162.0 },
+ { M_PI * -2.0 / 10.0, 8090169943.0 },
+ { M_PI * -3.0 / 10.0, 5877852522.0 },
+ { M_PI * -4.0 / 10.0, 3090169943.0 },
+ { M_PI * -5.0 / 10.0, 0.0 },
+ { M_PI * -6.0 / 10.0, -3090169943.0 },
+ { M_PI * -7.0 / 10.0, -5877852522.0 },
+ { M_PI * -8.0 / 10.0, -8090169943.0 },
+ { M_PI * -9.0 / 10.0, -9510565162.0 }
};
- for (i = 0; i < iterations; i++, angle += step) {
- double result = SDL_cos(angle) * 1.0E10;
- SDLTest_AssertCheck(SDL_trunc(result) == expected[i],
- "Cos(%f), expected %f, got %f",
- angle, expected[i], result);
- }
- return TEST_COMPLETED;
+ return helper_dtod_approx("Cos", SDL_cos, precision_cases, SDL_arraysize(precision_cases));
}
/**
@@ -1905,23 +1938,27 @@ sin_regularCases(void *args)
static int
sin_precisionTest(void *args)
{
- Uint32 i;
- Uint32 iterations = 20;
- double angle = 0.0;
- double step = 2.0 * M_PI / iterations;
- const double expected[] = {
- 0, 3090169943, 5877852522, 8090169943, 9510565162,
- 10000000000, 9510565162, 8090169943, 5877852522, 3090169943,
- 0, -3090169943, -5877852522, -8090169943, -9510565162,
- -10000000000, -9510565162, -8090169943, -5877852522, -3090169943
+ const d_to_d precision_cases[] = {
+ { M_PI * 1.0 / 10.0, 3090169943.0 },
+ { M_PI * 2.0 / 10.0, 5877852522.0 },
+ { M_PI * 3.0 / 10.0, 8090169943.0 },
+ { M_PI * 4.0 / 10.0, 9510565162.0 },
+ { M_PI * 5.0 / 10.0, 10000000000.0 },
+ { M_PI * 6.0 / 10.0, 9510565162.0 },
+ { M_PI * 7.0 / 10.0, 8090169943.0 },
+ { M_PI * 8.0 / 10.0, 5877852522.0 },
+ { M_PI * 9.0 / 10.0, 3090169943.0 },
+ { M_PI * -1.0 / 10.0, -3090169943.0 },
+ { M_PI * -2.0 / 10.0, -5877852522.0 },
+ { M_PI * -3.0 / 10.0, -8090169943.0 },
+ { M_PI * -4.0 / 10.0, -9510565162.0 },
+ { M_PI * -5.0 / 10.0, -10000000000.0 },
+ { M_PI * -6.0 / 10.0, -9510565162.0 },
+ { M_PI * -7.0 / 10.0, -8090169943.0 },
+ { M_PI * -8.0 / 10.0, -5877852522.0 },
+ { M_PI * -9.0 / 10.0, -3090169943.0 }
};
- for (i = 0; i < iterations; i++, angle += step) {
- double result = SDL_sin(angle) * 1.0E10;
- SDLTest_AssertCheck(SDL_trunc(result) == expected[i],
- "Sin(%f), expected %f, got %f",
- angle, expected[i], result);
- }
- return TEST_COMPLETED;
+ return helper_dtod_approx("Sin", SDL_sin, precision_cases, SDL_arraysize(precision_cases));
}
/**
@@ -2011,21 +2048,29 @@ tan_zeroCases(void *args)
static int
tan_precisionTest(void *args)
{
- Uint32 i;
- Uint32 iterations = 10;
- double angle = 0.0;
- double step = 2.0 * M_PI / iterations;
- const double expected[] = {
- 0.0, 7265425280.0, 30776835371.0, -30776835371.0, -7265425280.0,
- -0.0, 7265425280.0, 30776835371.0, -30776835371.0, -7265425280.0
+ const d_to_d precision_cases[] = {
+ { M_PI * 1.0 / 11.0, 2936264929.0 },
+ { M_PI * 2.0 / 11.0, 6426609771.0 },
+ { M_PI * 3.0 / 11.0, 11540615205.0 },
+ { M_PI * 4.0 / 11.0, 21896945629.0 },
+ { M_PI * 5.0 / 11.0, 69551527717.0 },
+ { M_PI * 6.0 / 11.0, -69551527717.0 },
+ { M_PI * 7.0 / 11.0, -21896945629.0 },
+ { M_PI * 8.0 / 11.0, -11540615205.0 },
+ { M_PI * 9.0 / 11.0, -6426609771.0 },
+ { M_PI * 10.0 / 11.0, -2936264929.0 },
+ { M_PI * -1.0 / 11.0, -2936264929.0 },
+ { M_PI * -2.0 / 11.0, -6426609771.0 },
+ { M_PI * -3.0 / 11.0, -11540615205.0 },
+ { M_PI * -4.0 / 11.0, -21896945629.0 },
+ { M_PI * -5.0 / 11.0, -69551527717.0 },
+ { M_PI * -6.0 / 11.0, 69551527717.0 },
+ { M_PI * -7.0 / 11.0, 21896945629.0 },
+ { M_PI * -8.0 / 11.0, 11540615205.0 },
+ { M_PI * -9.0 / 11.0, 6426609771.0 },
+ { M_PI * -10.0 / 11.0, 2936264929.0 }
};
- for (i = 0; i < iterations; i++, angle += step) {
- double result = SDL_tan(angle) * 1.0E10;
- SDLTest_AssertCheck(SDL_trunc(result) == expected[i],
- "Tan(%f), expected %f, got %f",
- angle, expected[i], result);
- }
- return TEST_COMPLETED;
+ return helper_dtod_approx("Tan", SDL_tan, precision_cases, SDL_arraysize(precision_cases));
}
/* ================= Test References ================== */