Test: Add Atan2 tests to math suite.
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
diff --git a/test/testautomation_math.c b/test/testautomation_math.c
index 74ea7ac..1e34128 100644
--- a/test/testautomation_math.c
+++ b/test/testautomation_math.c
@@ -22,6 +22,9 @@
/* Margin of error for imprecise tests */
#define EPSILON 1.0E-10
+/* Square root of 3 */
+#define SQRT3 1.7320508075688771931766041234368458390235900878906250
+
/* ================= Test Structs ================== */
/**
@@ -109,7 +112,8 @@ helper_dtod_inexact(const char *func_name, d_to_d_func func,
}
/**
- * \brief Runs all the cases on a given function with a signature (double, double) -> double
+ * \brief Runs all the cases on a given function with a signature
+ * (double, double) -> double. The result is expected to be exact.
*
* \param func_name, the name of the tested function.
* \param func, the function to call.
@@ -134,6 +138,35 @@ helper_ddtod(const char *func_name, dd_to_d_func func,
}
/**
+ * \brief Runs all the cases on a given function with a signature
+ * (double, double) -> double. Checks if the result between expected +/- EPSILON.
+ *
+ * \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_ddtod_inexact(const char *func_name, dd_to_d_func func,
+ const dd_to_d *cases, const size_t cases_size)
+{
+ Uint32 i;
+ for (i = 0; i < cases_size; i++) {
+ const double result = func(cases[i].x_input, cases[i].y_input);
+ SDLTest_AssertCheck(result >= cases[i].expected - EPSILON &&
+ result <= cases[i].expected + EPSILON,
+ "%s(%f,%f), expected [%f,%f], got %f",
+ func_name,
+ cases[i].x_input, cases[i].y_input,
+ cases[i].expected - EPSILON,
+ cases[i].expected + EPSILON,
+ result);
+ }
+
+ return TEST_COMPLETED;
+}
+
+/**
* \brief Runs a range of values on a given function with a signature double -> double
*
* This function is only meant to test functions that returns the input value if it is
@@ -2338,6 +2371,234 @@ atan_precisionTest(void *args)
return helper_dtod_inexact("Atan", SDL_atan, precision_cases, SDL_arraysize(precision_cases));
}
+/* SDL_atan2 tests functions */
+
+/* Zero cases */
+
+/**
+ * \brief Checks for both argument being 0.
+ */
+static int
+atan2_bothZeroCases(void *args)
+{
+ const dd_to_d cases[] = {
+ { 0.0, 0.0, 0.0 },
+ { -0.0, 0.0, -0.0 },
+ { 0.0, -0.0, M_PI },
+ { -0.0, -0.0, -M_PI },
+ };
+ return helper_ddtod("SDL_atan2", SDL_atan2, cases, SDL_arraysize(cases));
+}
+
+/**
+ * \brief Checks for the cases with Y=+/-0.
+ */
+static int
+atan2_yZeroCases(void *args)
+{
+ const dd_to_d cases[] = {
+ { 0.0, 1.0, 0.0 },
+ { 0.0, -1.0, M_PI },
+ { -0.0, 1.0, -0.0 },
+ { -0.0, -1.0, -M_PI }
+ };
+ return helper_ddtod("SDL_atan2", SDL_atan2, cases, SDL_arraysize(cases));
+}
+
+/**
+ * \brief Checks for the cases with X=+/-0.
+ */
+static int
+atan2_xZeroCases(void *args)
+{
+ const dd_to_d cases[] = {
+ { 1.0, 0.0, M_PI / 2.0 },
+ { -1.0, 0.0, -M_PI / 2.0 },
+ { 1.0, -0.0, M_PI / 2.0 },
+ { -1.0, -0.0, -M_PI / 2.0 }
+ };
+ return helper_ddtod("SDL_atan2", SDL_atan2, cases, SDL_arraysize(cases));
+}
+
+/* Infinity cases */
+
+/**
+ * \brief Checks for both argument being infinity.
+ */
+static int
+atan2_bothInfCases(void *args)
+{
+ double result;
+
+ result = SDL_atan2(INFINITY, INFINITY);
+ SDLTest_AssertCheck(M_PI / 4.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ INFINITY, INFINITY, M_PI / 4.0, result);
+
+ result = SDL_atan2(INFINITY, -INFINITY);
+ SDLTest_AssertCheck(3.0 * M_PI / 4.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ INFINITY, -INFINITY, 3.0 * M_PI / 4.0, result);
+
+ result = SDL_atan2(-INFINITY, INFINITY);
+ SDLTest_AssertCheck(-M_PI / 4.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -INFINITY, INFINITY, -M_PI / 4.0, result);
+
+ result = SDL_atan2(-INFINITY, -INFINITY);
+ SDLTest_AssertCheck(-3.0 * M_PI / 4.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -INFINITY, -INFINITY, -3.0 * M_PI / 4.0, result);
+
+ return TEST_COMPLETED;
+}
+
+/**
+ * \brief Checks for the cases with Y=+/-inf.
+ */
+static int
+atan2_yInfCases(void *args)
+{
+ double result;
+
+ result = SDL_atan2(INFINITY, 1.0);
+ SDLTest_AssertCheck(M_PI / 2.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ INFINITY, 1.0, M_PI / 2.0, result);
+
+ result = SDL_atan2(INFINITY, -1.0);
+ SDLTest_AssertCheck(M_PI / 2.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ INFINITY, -1.0, M_PI / 2.0, result);
+
+ result = SDL_atan2(-INFINITY, 1.0);
+ SDLTest_AssertCheck(-M_PI / 2.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -INFINITY, 1.0, -M_PI / 2.0, result);
+
+ result = SDL_atan2(-INFINITY, -1.0);
+ SDLTest_AssertCheck(-M_PI / 2.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -INFINITY, -1.0, -M_PI / 2.0, result);
+
+ return TEST_COMPLETED;
+}
+
+/**
+ * \brief Checks for the cases with X=+/-inf.
+ */
+static int
+atan2_xInfCases(void *args)
+{
+ double result;
+
+ result = SDL_atan2(1.0, INFINITY);
+ SDLTest_AssertCheck(0.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ 1.0, INFINITY, 0.0, result);
+
+ result = SDL_atan2(-1.0, INFINITY);
+ SDLTest_AssertCheck(-0.0 == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -1.0, INFINITY, -0.0, result);
+
+ result = SDL_atan2(1.0, -INFINITY);
+ SDLTest_AssertCheck(M_PI == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ 1.0, -INFINITY, M_PI, result);
+
+ result = SDL_atan2(-1.0, -INFINITY);
+ SDLTest_AssertCheck(-M_PI == result,
+ "Atan2(%f,%f), expected %f, got %f",
+ -1.0, -INFINITY, -M_PI, result);
+
+ return TEST_COMPLETED;
+}
+
+/* Miscelanious cases */
+
+/**
+ * \brief Checks for the nan cases.
+ */
+static int
+atan2_nanCases(void *args)
+{
+ double result;
+
+ result = SDL_atan2(NAN, NAN);
+ SDLTest_AssertCheck(isnan(result),
+ "Atan2(%f,%f), expected %f, got %f",
+ NAN, NAN, NAN, result);
+
+ result = SDL_atan2(NAN, 1.0);
+ SDLTest_AssertCheck(isnan(result),
+ "Atan2(%f,%f), expected %f, got %f",
+ NAN, 1.0, NAN, result);
+
+ result = SDL_atan2(1.0, NAN);
+ SDLTest_AssertCheck(isnan(result),
+ "Atan2(%f,%f), expected %f, got %f",
+ 1.0, NAN, NAN, result);
+
+ return TEST_COMPLETED;
+}
+
+/**
+ * \brief Checks for the top right quadrant (X > 0, Y > 0).
+ */
+static int
+atan2_topRightQuadrantTest(void *args)
+{
+ const dd_to_d top_right_cases[] = {
+ { 1.0, 1.0, M_PI / 4.0 },
+ { SQRT3, 3.0, M_PI / 6.0 },
+ { SQRT3, 1.0, M_PI / 3.0 }
+ };
+ return helper_ddtod_inexact("SDL_atan2", SDL_atan2, top_right_cases, SDL_arraysize(top_right_cases));
+}
+
+/**
+ * \brief Checks for the top left quadrant (X < 0, Y > 0).
+ */
+static int
+atan2_topLeftQuadrantTest(void *args)
+{
+ const dd_to_d top_left_cases[] = {
+ { 1.0, -1.0, 3.0 * M_PI / 4.0 },
+ { SQRT3, -3.0, 5.0 * M_PI / 6.0 },
+ { SQRT3, -1.0, 2.0 * M_PI / 3.0 }
+ };
+ return helper_ddtod_inexact("SDL_atan2", SDL_atan2, top_left_cases, SDL_arraysize(top_left_cases));
+}
+
+/**
+ * \brief Checks for the bottom right quadrant (X > 0, Y < 0).
+ */
+static int
+atan2_bottomRightQuadrantTest(void *args)
+{
+ const dd_to_d bottom_right_cases[] = {
+ { -1.0, 1.0, -M_PI / 4 },
+ { -SQRT3, 3.0, -M_PI / 6.0 },
+ { -SQRT3, 1.0, -M_PI / 3.0 }
+ };
+ return helper_ddtod_inexact("SDL_atan2", SDL_atan2, bottom_right_cases, SDL_arraysize(bottom_right_cases));
+}
+
+/**
+ * \brief Checks for the bottom left quadrant (X < 0, Y < 0).
+ */
+static int
+atan2_bottomLeftQuadrantTest(void *args)
+{
+ const dd_to_d bottom_left_cases[] = {
+ { -1.0, -1.0, -3.0 * M_PI / 4.0 },
+ { -SQRT3, -3.0, -5.0 * M_PI / 6.0 },
+ { -SQRT3, -1.0, -4.0 * M_PI / 6.0 }
+ };
+ return helper_ddtod_inexact("SDL_atan2", SDL_atan2, bottom_left_cases, SDL_arraysize(bottom_left_cases));
+}
+
/* ================= Test References ================== */
/* SDL_floor test cases */
@@ -2813,6 +3074,53 @@ static const SDLTest_TestCaseReference atanTestPrecision = {
"Check atan precision to the tenth decimal", TEST_ENABLED
};
+/* SDL_atan2 test cases */
+
+static const SDLTest_TestCaseReference atan2TestZero1 = {
+ (SDLTest_TestCaseFp) atan2_bothZeroCases, "atan2_bothZeroCases",
+ "Check for both arguments being zero", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestZero2 = {
+ (SDLTest_TestCaseFp) atan2_yZeroCases, "atan2_yZeroCases",
+ "Check for y=0", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestZero3 = {
+ (SDLTest_TestCaseFp) atan2_xZeroCases, "atan2_xZeroCases",
+ "Check for x=0", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestInf1 = {
+ (SDLTest_TestCaseFp) atan2_bothInfCases, "atan2_bothInfCases",
+ "Check for both arguments being infinity", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestInf2 = {
+ (SDLTest_TestCaseFp) atan2_yInfCases, "atan2_yInfCases",
+ "Check for y=0", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestInf3 = {
+ (SDLTest_TestCaseFp) atan2_xInfCases, "atan2_xInfCases",
+ "Check for x=0", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestNan = {
+ (SDLTest_TestCaseFp) atan2_nanCases, "atan2_nanCases",
+ "Check the NaN special cases", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestQuadrantTopRight = {
+ (SDLTest_TestCaseFp) atan2_topRightQuadrantTest, "atan2_topRightQuadrantTest",
+ "Checks values in the top right quadrant", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestQuadrantTopLeft = {
+ (SDLTest_TestCaseFp) atan2_topLeftQuadrantTest, "atan2_topLeftQuadrantTest",
+ "Checks values in the top left quadrant", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestQuadrantBottomRight = {
+ (SDLTest_TestCaseFp) atan2_bottomRightQuadrantTest, "atan2_bottomRightQuadrantTest",
+ "Checks values in the bottom right quadrant", TEST_ENABLED
+};
+static const SDLTest_TestCaseReference atan2TestQuadrantBottomLeft = {
+ (SDLTest_TestCaseFp) atan2_bottomLeftQuadrantTest, "atan2_bottomLeftQuadrantTest",
+ "Checks values in the bottom left quadrant", TEST_ENABLED
+};
+
static const SDLTest_TestCaseReference *mathTests[] = {
&floorTestInf, &floorTestZero, &floorTestNan,
&floorTestRound, &floorTestFraction, &floorTestRange,
@@ -2868,6 +3176,11 @@ static const SDLTest_TestCaseReference *mathTests[] = {
&atanTestLimit, &atanTestZero, &atanTestNan, &atanTestPrecision,
+ &atan2TestZero1, &atan2TestZero2, &atan2TestZero3,
+ &atan2TestInf1, &atan2TestInf2, &atan2TestInf3,
+ &atan2TestNan, &atan2TestQuadrantTopRight, &atan2TestQuadrantTopLeft,
+ &atan2TestQuadrantBottomRight, &atan2TestQuadrantBottomLeft,
+
NULL
};