test/test-automation/tests/testrect/testrect.c
author Andreas Schiffler
Fri, 16 Sep 2011 08:26:20 -0700
changeset 5948 05e6bfac079f
parent 5912 98471d04c8db
child 5951 b4c936b80652
permissions -rw-r--r--
Add test cases for SDL_RectEmpty
markus@5677
     1
/**
markus@5677
     2
 * Original code: automated SDL rect test written by Edgar Simo "bobbens"
aschiffler@5912
     3
 * New/updated tests: aschiffler at ferzkopp dot net
markus@5677
     4
 */
markus@5677
     5
markus@5677
     6
#include <stdio.h>
markus@5677
     7
markus@5677
     8
#include <SDL/SDL.h>
markus@5677
     9
markus@5804
    10
#include "../../include/SDL_test.h"
markus@5677
    11
aschiffler@5912
    12
/* SDL_IntersectRectAndLine */
markus@5677
    13
static const TestCaseReference test1 =
aschiffler@5874
    14
		(TestCaseReference){ "rect_testIntersectRectAndLine", "Tests SDL_IntersectRectAndLine clipping cases", TEST_ENABLED, 0, 0 };
aschiffler@5868
    15
aschiffler@5868
    16
static const TestCaseReference test2 =
aschiffler@5874
    17
		(TestCaseReference){ "rect_testIntersectRectAndLineInside", "Tests SDL_IntersectRectAndLine with line fully contained in rect", TEST_ENABLED, 0, 0 };
aschiffler@5868
    18
aschiffler@5868
    19
static const TestCaseReference test3 =
aschiffler@5874
    20
		(TestCaseReference){ "rect_testIntersectRectAndLineOutside", "Tests SDL_IntersectRectAndLine with line fully contained in rect", TEST_ENABLED, 0, 0 };
aschiffler@5868
    21
aschiffler@5868
    22
static const TestCaseReference test4 =
aschiffler@5874
    23
		(TestCaseReference){ "rect_testIntersectRectAndLineParam", "Negative tests against SDL_IntersectRectAndLine with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5868
    24
aschiffler@5912
    25
/* SDL_IntersectRect */
aschiffler@5868
    26
static const TestCaseReference test5 =
aschiffler@5874
    27
		(TestCaseReference){ "rect_testIntersectRectInside", "Tests SDL_IntersectRect with B fully contained in A", TEST_ENABLED, 0, 0 };
markus@5677
    28
aschiffler@5870
    29
static const TestCaseReference test6 =
aschiffler@5874
    30
		(TestCaseReference){ "rect_testIntersectRectOutside", "Tests SDL_IntersectRect with B fully outside of A", TEST_ENABLED, 0, 0 };
aschiffler@5870
    31
aschiffler@5870
    32
static const TestCaseReference test7 =
aschiffler@5874
    33
		(TestCaseReference){ "rect_testIntersectRectPartial", "Tests SDL_IntersectRect with B partially intersecting A", TEST_ENABLED, 0, 0 };
aschiffler@5874
    34
aschiffler@5874
    35
static const TestCaseReference test8 =
aschiffler@5874
    36
		(TestCaseReference){ "rect_testIntersectRectPoint", "Tests SDL_IntersectRect with 1x1 sized rectangles", TEST_ENABLED, 0, 0 };
aschiffler@5874
    37
aschiffler@5874
    38
static const TestCaseReference test9 =
aschiffler@5874
    39
		(TestCaseReference){ "rect_testIntersectRectParam", "Negative tests against SDL_IntersectRect with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5874
    40
aschiffler@5912
    41
/* SDL_HasIntersection */
aschiffler@5874
    42
static const TestCaseReference test10 =
aschiffler@5870
    43
		(TestCaseReference){ "rect_testHasIntersectionInside", "Tests SDL_HasIntersection with B fully contained in A", TEST_ENABLED, 0, 0 };
aschiffler@5870
    44
aschiffler@5874
    45
static const TestCaseReference test11 =
aschiffler@5870
    46
		(TestCaseReference){ "rect_testHasIntersectionOutside", "Tests SDL_HasIntersection with B fully outside of A", TEST_ENABLED, 0, 0 };
aschiffler@5870
    47
aschiffler@5874
    48
static const TestCaseReference test12 =
aschiffler@5870
    49
		(TestCaseReference){ "rect_testHasIntersectionPartial", "Tests SDL_HasIntersection with B partially intersecting A", TEST_ENABLED, 0, 0 };
aschiffler@5870
    50
aschiffler@5874
    51
static const TestCaseReference test13 =
aschiffler@5870
    52
		(TestCaseReference){ "rect_testHasIntersectionPoint", "Tests SDL_HasIntersection with 1x1 sized rectangles", TEST_ENABLED, 0, 0 };
aschiffler@5870
    53
aschiffler@5874
    54
static const TestCaseReference test14 =
aschiffler@5870
    55
		(TestCaseReference){ "rect_testHasIntersectionParam", "Negative tests against SDL_HasIntersection with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5870
    56
aschiffler@5912
    57
/* SDL_EnclosePoints */
aschiffler@5903
    58
static const TestCaseReference test15 =
aschiffler@5903
    59
		(TestCaseReference){ "rect_testEnclosePoints", "Tests SDL_EnclosePoints without clipping", TEST_ENABLED, 0, 0 };
aschiffler@5903
    60
aschiffler@5903
    61
static const TestCaseReference test16 =
aschiffler@5903
    62
		(TestCaseReference){ "rect_testEnclosePointsWithClipping", "Tests SDL_EnclosePoints with clipping", TEST_ENABLED, 0, 0 };
aschiffler@5903
    63
aschiffler@5903
    64
static const TestCaseReference test17 =
aschiffler@5903
    65
		(TestCaseReference){ "rect_testEnclosePointsRepeatedInput", "Tests SDL_EnclosePoints with repeated input", TEST_ENABLED, 0, 0 };
aschiffler@5903
    66
aschiffler@5903
    67
static const TestCaseReference test18 =
aschiffler@5903
    68
		(TestCaseReference){ "rect_testEnclosePointsParam", "Negative tests against SDL_EnclosePoints with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5903
    69
aschiffler@5912
    70
/* SDL_UnionRect */
aschiffler@5912
    71
static const TestCaseReference test19 =
aschiffler@5912
    72
		(TestCaseReference){ "rect_testUnionRectInside", "Tests SDL_UnionRect where rect B is inside rect A", TEST_ENABLED, 0, 0 };
markus@5780
    73
aschiffler@5912
    74
static const TestCaseReference test20 =
aschiffler@5912
    75
		(TestCaseReference){ "rect_testUnionRectOutside", "Tests SDL_UnionRect where rect B is outside rect A", TEST_ENABLED, 0, 0 };
aschiffler@5912
    76
aschiffler@5912
    77
static const TestCaseReference test21 =
aschiffler@5912
    78
		(TestCaseReference){ "rect_testUnionRectParam", "Negative tests against SDL_UnionRect with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5912
    79
aschiffler@5948
    80
/* SDL_RectEmpty */
aschiffler@5948
    81
static const TestCaseReference test22 =
aschiffler@5948
    82
		(TestCaseReference){ "rect_testRectEmpty", "Tests SDL_RectEmpty with various inputs", TEST_ENABLED, 0, 0 };
aschiffler@5948
    83
aschiffler@5948
    84
static const TestCaseReference test23 =
aschiffler@5948
    85
		(TestCaseReference){ "rect_testRectEmptyParam", "Negative tests against SDL_RectEmpty with invalid parameters", TEST_ENABLED, 0, 0 };
aschiffler@5948
    86
aschiffler@5912
    87
/* TODO: SDL_RectEquals */
aschiffler@5912
    88
aschiffler@5912
    89
/*!
aschiffler@5912
    90
 * \brief Test suite for functions that handle simple rectangles including overlaps and merges.
aschiffler@5912
    91
 *
aschiffler@5912
    92
 * \sa
aschiffler@5912
    93
 * http://wiki.libsdl.org/moin.cgi/CategoryRect
aschiffler@5912
    94
 */
markus@5677
    95
extern const TestCaseReference *testSuite[] =  {
aschiffler@5903
    96
	&test1, &test2, &test3, &test4, &test5, &test6, &test7, &test8, &test9, &test10, &test11, &test12, &test13, &test14, 
aschiffler@5948
    97
	&test15, &test16, &test17, &test18, &test19, &test20, &test21, &test22, &test23, NULL
markus@5677
    98
};
markus@5677
    99
markus@5677
   100
TestCaseReference **QueryTestSuite() {
markus@5677
   101
	return (TestCaseReference **)testSuite;
markus@5677
   102
}
markus@5677
   103
markus@5678
   104
/*!
aschiffler@5874
   105
 * \brief Private helper to check SDL_IntersectRectAndLine results
aschiffler@5874
   106
 */
aschiffler@5874
   107
void _validateIntersectRectAndLineResults(
aschiffler@5874
   108
    SDL_bool intersection, SDL_bool expectedIntersection,
aschiffler@5874
   109
    SDL_Rect *rect, SDL_Rect * refRect,
aschiffler@5874
   110
    int x1, int y1, int x2, int y2,
aschiffler@5874
   111
    int x1Ref, int y1Ref, int x2Ref, int y2Ref)
aschiffler@5874
   112
{
aschiffler@5874
   113
    AssertTrue(intersection == expectedIntersection, 
aschiffler@5874
   114
        "Incorrect intersection result: expected %s, got %s intersecting rect (%d,%d,%d,%d) with line (%d,%d - %d,%d)\n",
aschiffler@5874
   115
        (expectedIntersection == SDL_TRUE) ? "true" : "false",
aschiffler@5874
   116
        (intersection == SDL_TRUE) ? "true" : "false",
aschiffler@5874
   117
        refRect->x, refRect->y, refRect->w, refRect->h,
aschiffler@5874
   118
        x1Ref, y1Ref, x2Ref, y2Ref);
aschiffler@5874
   119
    AssertTrue(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
aschiffler@5874
   120
        "Source rectangle was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5874
   121
        rect->x, rect->y, rect->w, rect->h,
aschiffler@5874
   122
        refRect->x, refRect->y, refRect->w, refRect->h);
aschiffler@5874
   123
    AssertTrue(x1 == x1Ref && y1 == y1Ref && x2 == x2Ref && y2 == y2Ref,
aschiffler@5874
   124
        "Line was incorrectly clipped or modified: got (%d,%d - %d,%d) expected (%d,%d - %d,%d)",
aschiffler@5874
   125
        x1, y1, x2, y2,
aschiffler@5874
   126
        x1Ref, y1Ref, x2Ref, y2Ref);
aschiffler@5874
   127
}
aschiffler@5874
   128
aschiffler@5874
   129
/*!
aschiffler@5874
   130
 * \brief Tests SDL_IntersectRectAndLine() clipping cases
markus@5780
   131
 *
markus@5780
   132
 * \sa
markus@5780
   133
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
markus@5677
   134
 */
markus@5677
   135
int rect_testIntersectRectAndLine (void *arg)
markus@5677
   136
{
aschiffler@5874
   137
    SDL_Rect refRect = { 0, 0, 32, 32 };
aschiffler@5874
   138
    SDL_Rect rect;
aschiffler@5874
   139
    int x1, y1;
aschiffler@5874
   140
    int x2, y2;
aschiffler@5874
   141
    SDL_bool intersected;
aschiffler@5874
   142
aschiffler@5874
   143
    int xLeft = -RandomIntegerInRange(1, refRect.w);
aschiffler@5874
   144
    int xRight = refRect.w + RandomIntegerInRange(1, refRect.w);
aschiffler@5874
   145
    int yTop = -RandomIntegerInRange(1, refRect.h);
aschiffler@5874
   146
    int yBottom = refRect.h + RandomIntegerInRange(1, refRect.h);
aschiffler@5874
   147
aschiffler@5874
   148
    x1 = xLeft;
aschiffler@5874
   149
    y1 = 15;
aschiffler@5874
   150
    x2 = xRight;
aschiffler@5874
   151
    y2 = 15;
aschiffler@5874
   152
    rect = refRect;
aschiffler@5874
   153
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   154
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 15, 31, 15);
aschiffler@5874
   155
aschiffler@5874
   156
    x1 = 15;
aschiffler@5874
   157
    y1 = yTop;
aschiffler@5874
   158
    x2 = 15;
aschiffler@5874
   159
    y2 = yBottom;
aschiffler@5874
   160
    rect = refRect;
aschiffler@5874
   161
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   162
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 15, 0, 15, 31);
aschiffler@5874
   163
aschiffler@5874
   164
    x1 = -refRect.w;
aschiffler@5874
   165
    y1 = -refRect.h;
aschiffler@5874
   166
    x2 = 2*refRect.w;
aschiffler@5874
   167
    y2 = 2*refRect.h;
aschiffler@5874
   168
    rect = refRect;
aschiffler@5874
   169
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   170
     _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 0, 31, 31);
aschiffler@5874
   171
aschiffler@5874
   172
    x1 = 2*refRect.w;
aschiffler@5874
   173
    y1 = 2*refRect.h;
aschiffler@5874
   174
    x2 = -refRect.w;
aschiffler@5874
   175
    y2 = -refRect.h;
aschiffler@5874
   176
    rect = refRect;
aschiffler@5874
   177
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   178
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 31, 31, 0, 0);
aschiffler@5874
   179
aschiffler@5874
   180
    x1 = -1;
aschiffler@5874
   181
    y1 = 32;
aschiffler@5874
   182
    x2 = 32;
aschiffler@5874
   183
    y2 = -1;
aschiffler@5874
   184
    rect = refRect;
aschiffler@5874
   185
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   186
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 0, 31, 31, 0);
aschiffler@5874
   187
aschiffler@5874
   188
    x1 = 32;
aschiffler@5874
   189
    y1 = -1;
aschiffler@5874
   190
    x2 = -1;
aschiffler@5874
   191
    y2 = 32;
aschiffler@5874
   192
    rect = refRect;
aschiffler@5874
   193
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   194
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, 31, 0, 0, 31);
aschiffler@5874
   195
}
aschiffler@5874
   196
aschiffler@5874
   197
/*!
aschiffler@5874
   198
 * \brief Tests SDL_IntersectRectAndLine() non-clipping case line inside
aschiffler@5874
   199
 *
aschiffler@5874
   200
 * \sa
aschiffler@5874
   201
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
aschiffler@5874
   202
 */
aschiffler@5874
   203
int rect_testIntersectRectAndLineInside (void *arg)
aschiffler@5874
   204
{
aschiffler@5874
   205
    SDL_Rect refRect = { 0, 0, 32, 32 };
aschiffler@5874
   206
    SDL_Rect rect;
markus@5677
   207
    int x1, y1;
markus@5677
   208
    int x2, y2;
aschiffler@5874
   209
    SDL_bool intersected;
aschiffler@5874
   210
aschiffler@5874
   211
    int xmin = refRect.x;
aschiffler@5874
   212
    int xmax = refRect.x + refRect.w - 1;
aschiffler@5874
   213
    int ymin = refRect.y;
aschiffler@5874
   214
    int ymax = refRect.y + refRect.h - 1;
aschiffler@5874
   215
    int x1Ref = RandomIntegerInRange(xmin + 1, xmax - 1);
aschiffler@5874
   216
    int y1Ref = RandomIntegerInRange(ymin + 1, ymax - 1);
aschiffler@5874
   217
    int x2Ref = RandomIntegerInRange(xmin + 1, xmax - 1);
aschiffler@5874
   218
    int y2Ref = RandomIntegerInRange(ymin + 1, ymax - 1);
aschiffler@5874
   219
aschiffler@5874
   220
    x1 = x1Ref;
aschiffler@5874
   221
    y1 = y1Ref;
aschiffler@5874
   222
    x2 = x2Ref;
aschiffler@5874
   223
    y2 = y2Ref;
aschiffler@5874
   224
    rect = refRect;
aschiffler@5874
   225
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   226
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, x2Ref, y2Ref);
aschiffler@5874
   227
aschiffler@5874
   228
    x1 = x1Ref;
aschiffler@5874
   229
    y1 = y1Ref;
aschiffler@5874
   230
    x2 = xmax;
aschiffler@5874
   231
    y2 = ymax;
aschiffler@5874
   232
    rect = refRect;
aschiffler@5874
   233
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   234
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, x1Ref, y1Ref, xmax, ymax);
aschiffler@5874
   235
aschiffler@5874
   236
    x1 = xmin;
aschiffler@5874
   237
    y1 = ymin;
aschiffler@5874
   238
    x2 = x2Ref;
aschiffler@5874
   239
    y2 = y2Ref;
aschiffler@5874
   240
    rect = refRect;
aschiffler@5874
   241
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   242
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, x2Ref, y2Ref);
markus@5677
   243
aschiffler@5874
   244
    x1 = xmin;
aschiffler@5874
   245
    y1 = ymin;
aschiffler@5874
   246
    x2 = xmax;
aschiffler@5874
   247
    y2 = ymax;
aschiffler@5874
   248
    rect = refRect;
aschiffler@5874
   249
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   250
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymin, xmax, ymax);
aschiffler@5874
   251
aschiffler@5874
   252
    x1 = xmin;
aschiffler@5874
   253
    y1 = ymax;
aschiffler@5874
   254
    x2 = xmax;
aschiffler@5874
   255
    y2 = ymin;
aschiffler@5874
   256
    rect = refRect;
aschiffler@5874
   257
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   258
    _validateIntersectRectAndLineResults(intersected, SDL_TRUE, &rect, &refRect, x1, y1, x2, y2, xmin, ymax, xmax, ymin);
aschiffler@5874
   259
}
aschiffler@5874
   260
aschiffler@5874
   261
aschiffler@5874
   262
/*!
aschiffler@5874
   263
 * \brief Tests SDL_IntersectRectAndLine() non-clipping cases outside
aschiffler@5874
   264
 *
aschiffler@5874
   265
 * \sa
aschiffler@5874
   266
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
aschiffler@5874
   267
 */
aschiffler@5874
   268
int rect_testIntersectRectAndLineOutside (void *arg)
aschiffler@5874
   269
{
aschiffler@5874
   270
    SDL_Rect refRect = { 0, 0, 32, 32 };
aschiffler@5874
   271
    SDL_Rect rect;
aschiffler@5874
   272
    int x1, y1;
aschiffler@5874
   273
    int x2, y2;
aschiffler@5874
   274
    SDL_bool intersected;
aschiffler@5874
   275
aschiffler@5874
   276
    int xLeft = -RandomIntegerInRange(1, refRect.w);
aschiffler@5874
   277
    int xRight = refRect.w + RandomIntegerInRange(1, refRect.w);
aschiffler@5874
   278
    int yTop = -RandomIntegerInRange(1, refRect.h);
aschiffler@5874
   279
    int yBottom = refRect.h + RandomIntegerInRange(1, refRect.h);
aschiffler@5874
   280
aschiffler@5874
   281
    x1 = xLeft;
markus@5677
   282
    y1 = 0;
aschiffler@5874
   283
    x2 = xLeft;
markus@5677
   284
    y2 = 31;
aschiffler@5874
   285
    rect = refRect;
aschiffler@5874
   286
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   287
    _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, xLeft, 0, xLeft, 31);
markus@5677
   288
aschiffler@5874
   289
    x1 = xRight;
aschiffler@5874
   290
    y1 = 0;
aschiffler@5874
   291
    x2 = xRight;
aschiffler@5874
   292
    y2 = 31;
aschiffler@5874
   293
    rect = refRect;
aschiffler@5874
   294
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   295
    _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, xRight, 0, xRight, 31);
markus@5677
   296
markus@5677
   297
    x1 = 0;
aschiffler@5874
   298
    y1 = yTop;
markus@5677
   299
    x2 = 31;
aschiffler@5874
   300
    y2 = yTop;
aschiffler@5874
   301
    rect = refRect;
aschiffler@5874
   302
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   303
    _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, 0, yTop, 31, yTop);
markus@5677
   304
aschiffler@5874
   305
    x1 = 0;
aschiffler@5874
   306
    y1 = yBottom;
aschiffler@5874
   307
    x2 = 31;
aschiffler@5874
   308
    y2 = yBottom;
aschiffler@5874
   309
    rect = refRect;
aschiffler@5874
   310
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   311
    _validateIntersectRectAndLineResults(intersected, SDL_FALSE, &rect, &refRect, x1, y1, x2, y2, 0, yBottom, 31, yBottom);
aschiffler@5874
   312
}
markus@5677
   313
aschiffler@5874
   314
/*!
aschiffler@5874
   315
 * \brief Negative tests against SDL_IntersectRectAndLine() with invalid parameters
aschiffler@5874
   316
 *
aschiffler@5874
   317
 * \sa
aschiffler@5874
   318
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRectAndLine
aschiffler@5874
   319
 */
aschiffler@5874
   320
int rect_testIntersectRectAndLineParam (void *arg)
aschiffler@5874
   321
{
aschiffler@5874
   322
    SDL_Rect rect = { 0, 0, 32, 32 };
aschiffler@5874
   323
    int x1 = rect.w / 2;
aschiffler@5874
   324
    int y1 = rect.h / 2;
aschiffler@5874
   325
    int x2 = x1;
aschiffler@5874
   326
    int y2 = 2 * rect.h;
aschiffler@5874
   327
    SDL_bool intersected;
aschiffler@5874
   328
    
aschiffler@5874
   329
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, &y2);
aschiffler@5874
   330
    AssertTrue(intersected == SDL_TRUE, "Test variables not intersected as expected");
aschiffler@5874
   331
    
aschiffler@5874
   332
    intersected = SDL_IntersectRectAndLine((SDL_Rect *)NULL, &x1, &y1, &x2, &y2);
aschiffler@5874
   333
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 1st parameter is NULL");
aschiffler@5874
   334
    intersected = SDL_IntersectRectAndLine(&rect, (int *)NULL, &y1, &x2, &y2);
aschiffler@5874
   335
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 2nd parameter is NULL");
aschiffler@5874
   336
    intersected = SDL_IntersectRectAndLine(&rect, &x1, (int *)NULL, &x2, &y2);
aschiffler@5874
   337
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 3rd parameter is NULL");
aschiffler@5874
   338
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, (int *)NULL, &y2);
aschiffler@5874
   339
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 4th parameter is NULL");
aschiffler@5874
   340
    intersected = SDL_IntersectRectAndLine(&rect, &x1, &y1, &x2, (int *)NULL);
aschiffler@5874
   341
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when 5th parameter is NULL");
aschiffler@5874
   342
    intersected = SDL_IntersectRectAndLine((SDL_Rect *)NULL, (int *)NULL, (int *)NULL, (int *)NULL, (int *)NULL);
aschiffler@5874
   343
    AssertTrue(intersected == SDL_FALSE, "Incorrect intersected result when all parameters are NULL");
markus@5677
   344
}
aschiffler@5868
   345
aschiffler@5868
   346
/*!
aschiffler@5870
   347
 * \brief Private helper to check SDL_HasIntersection results
aschiffler@5870
   348
 */
aschiffler@5870
   349
void _validateHasIntersectionResults(
aschiffler@5870
   350
    SDL_bool intersection, SDL_bool expectedIntersection, 
aschiffler@5870
   351
    SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB)
aschiffler@5870
   352
{
aschiffler@5870
   353
    AssertTrue(intersection == expectedIntersection, 
aschiffler@5870
   354
        "Incorrect intersection result: expected %s, got %s intersecting A (%d,%d,%d,%d) with B (%d,%d,%d,%d)\n",
aschiffler@5870
   355
        (expectedIntersection == SDL_TRUE) ? "true" : "false",
aschiffler@5870
   356
        (intersection == SDL_TRUE) ? "true" : "false",
aschiffler@5870
   357
        rectA->x, rectA->y, rectA->w, rectA->h, 
aschiffler@5870
   358
        rectB->x, rectB->y, rectB->w, rectB->h);
aschiffler@5870
   359
    AssertTrue(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
aschiffler@5870
   360
        "Source rectangle A was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5870
   361
        rectA->x, rectA->y, rectA->w, rectA->h,
aschiffler@5870
   362
        refRectA->x, refRectA->y, refRectA->w, refRectA->h);
aschiffler@5870
   363
    AssertTrue(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
aschiffler@5870
   364
        "Source rectangle B was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5870
   365
        rectB->x, rectB->y, rectB->w, rectB->h,
aschiffler@5870
   366
        refRectB->x, refRectB->y, refRectB->w, refRectB->h);
aschiffler@5870
   367
}
aschiffler@5870
   368
aschiffler@5870
   369
/*!
aschiffler@5870
   370
 * \brief Private helper to check SDL_IntersectRect results
aschiffler@5870
   371
 */
aschiffler@5870
   372
void _validateIntersectRectResults(
aschiffler@5870
   373
    SDL_bool intersection, SDL_bool expectedIntersection, 
aschiffler@5870
   374
    SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB, 
aschiffler@5870
   375
    SDL_Rect *result, SDL_Rect *expectedResult)
aschiffler@5870
   376
{
aschiffler@5870
   377
    _validateHasIntersectionResults(intersection, expectedIntersection, rectA, rectB, refRectA, refRectB);
aschiffler@5870
   378
    if (result && expectedResult) {
aschiffler@5870
   379
        AssertTrue(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
aschiffler@5874
   380
            "Intersection of rectangles A (%d,%d,%d,%d) and B (%d,%d,%d,%d) was incorrectly calculated, got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5874
   381
            rectA->x, rectA->y, rectA->w, rectA->h, 
aschiffler@5874
   382
            rectB->x, rectB->y, rectB->w, rectB->h,
aschiffler@5870
   383
            result->x, result->y, result->w, result->h,
aschiffler@5870
   384
            expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
aschiffler@5870
   385
    }
aschiffler@5870
   386
}
aschiffler@5870
   387
aschiffler@5870
   388
/*!
aschiffler@5912
   389
 * \brief Private helper to check SDL_UnionRect results
aschiffler@5912
   390
 */
aschiffler@5912
   391
void _validateUnionRectResults(
aschiffler@5912
   392
    SDL_Rect *rectA, SDL_Rect *rectB, SDL_Rect *refRectA, SDL_Rect *refRectB, 
aschiffler@5912
   393
    SDL_Rect *result, SDL_Rect *expectedResult)
aschiffler@5912
   394
{
aschiffler@5912
   395
    AssertTrue(rectA->x == refRectA->x && rectA->y == refRectA->y && rectA->w == refRectA->w && rectA->h == refRectA->h,
aschiffler@5912
   396
        "Source rectangle A was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5912
   397
        rectA->x, rectA->y, rectA->w, rectA->h,
aschiffler@5912
   398
        refRectA->x, refRectA->y, refRectA->w, refRectA->h);
aschiffler@5912
   399
    AssertTrue(rectB->x == refRectB->x && rectB->y == refRectB->y && rectB->w == refRectB->w && rectB->h == refRectB->h,
aschiffler@5912
   400
        "Source rectangle B was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5912
   401
        rectB->x, rectB->y, rectB->w, rectB->h,
aschiffler@5912
   402
        refRectB->x, refRectB->y, refRectB->w, refRectB->h);
aschiffler@5912
   403
    AssertTrue(result->x == expectedResult->x && result->y == expectedResult->y && result->w == expectedResult->w && result->h == expectedResult->h,
aschiffler@5912
   404
        "Union of rectangles A (%d,%d,%d,%d) and B (%d,%d,%d,%d) was incorrectly calculated, got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5912
   405
        rectA->x, rectA->y, rectA->w, rectA->h, 
aschiffler@5912
   406
        rectB->x, rectB->y, rectB->w, rectB->h,
aschiffler@5912
   407
        result->x, result->y, result->w, result->h,
aschiffler@5912
   408
        expectedResult->x, expectedResult->y, expectedResult->w, expectedResult->h);
aschiffler@5912
   409
}
aschiffler@5912
   410
aschiffler@5912
   411
/*!
aschiffler@5948
   412
 * \brief Private helper to check SDL_RectEmpty results
aschiffler@5948
   413
 */
aschiffler@5948
   414
void _validateRectEmptyResults(
aschiffler@5948
   415
    SDL_bool empty, SDL_bool expectedEmpty, 
aschiffler@5948
   416
    SDL_Rect *rect, SDL_Rect *refRect)
aschiffler@5948
   417
{
aschiffler@5948
   418
    AssertTrue(empty == expectedEmpty, 
aschiffler@5948
   419
        "Incorrect empty result: expected %s, got %s testing (%d,%d,%d,%d)\n",
aschiffler@5948
   420
        (expectedEmpty == SDL_TRUE) ? "true" : "false",
aschiffler@5948
   421
        (empty == SDL_TRUE) ? "true" : "false",
aschiffler@5948
   422
        rect->x, rect->y, rect->w, rect->h);
aschiffler@5948
   423
    AssertTrue(rect->x == refRect->x && rect->y == refRect->y && rect->w == refRect->w && rect->h == refRect->h,
aschiffler@5948
   424
        "Source rectangle was modified: got (%d,%d,%d,%d) expected (%d,%d,%d,%d)",
aschiffler@5948
   425
        rect->x, rect->y, rect->w, rect->h,
aschiffler@5948
   426
        refRect->x, refRect->y, refRect->w, refRect->h);
aschiffler@5948
   427
}
aschiffler@5948
   428
aschiffler@5948
   429
/*!
aschiffler@5868
   430
 * \brief Tests SDL_IntersectRect() with B fully inside A
aschiffler@5868
   431
 *
aschiffler@5868
   432
 * \sa
aschiffler@5868
   433
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
aschiffler@5868
   434
 */
aschiffler@5868
   435
int rect_testIntersectRectInside (void *arg)
aschiffler@5868
   436
{
aschiffler@5868
   437
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5868
   438
    SDL_Rect refRectB;
aschiffler@5868
   439
    SDL_Rect rectA;
aschiffler@5868
   440
    SDL_Rect rectB;
aschiffler@5868
   441
    SDL_Rect result;
aschiffler@5868
   442
    SDL_bool intersection;
aschiffler@5868
   443
aschiffler@5868
   444
    // rectB fully contained in rectA
aschiffler@5868
   445
    refRectB.x = 0;
aschiffler@5868
   446
    refRectB.y = 0;
aschiffler@5868
   447
    refRectB.w = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
aschiffler@5868
   448
    refRectB.h = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
aschiffler@5868
   449
    rectA = refRectA;
aschiffler@5868
   450
    rectB = refRectB;
aschiffler@5868
   451
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5870
   452
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectB);
aschiffler@5868
   453
}
aschiffler@5868
   454
aschiffler@5868
   455
/*!
aschiffler@5868
   456
 * \brief Tests SDL_IntersectRect() with B fully outside A
aschiffler@5868
   457
 *
aschiffler@5868
   458
 * \sa
aschiffler@5868
   459
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
aschiffler@5868
   460
 */
aschiffler@5868
   461
int rect_testIntersectRectOutside (void *arg)
aschiffler@5868
   462
{
aschiffler@5868
   463
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5868
   464
    SDL_Rect refRectB;
aschiffler@5868
   465
    SDL_Rect rectA;
aschiffler@5868
   466
    SDL_Rect rectB;
aschiffler@5868
   467
    SDL_Rect result;
aschiffler@5868
   468
    SDL_bool intersection;
aschiffler@5868
   469
aschiffler@5868
   470
    // rectB fully outside of rectA
aschiffler@5868
   471
    refRectB.x = refRectA.x + refRectA.w + RandomIntegerInRange(1, 10);
aschiffler@5868
   472
    refRectB.y = refRectA.y + refRectA.h + RandomIntegerInRange(1, 10);
aschiffler@5868
   473
    refRectB.w = refRectA.w;
aschiffler@5868
   474
    refRectB.h = refRectA.h;
aschiffler@5868
   475
    rectA = refRectA;
aschiffler@5868
   476
    rectB = refRectB;
aschiffler@5868
   477
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5870
   478
    _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);    
aschiffler@5868
   479
}
aschiffler@5868
   480
aschiffler@5868
   481
/*!
aschiffler@5868
   482
 * \brief Tests SDL_IntersectRect() with B partially intersecting A
aschiffler@5868
   483
 *
aschiffler@5868
   484
 * \sa
aschiffler@5868
   485
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
aschiffler@5868
   486
 */
aschiffler@5868
   487
int rect_testIntersectRectPartial (void *arg)
aschiffler@5868
   488
{
aschiffler@5868
   489
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5868
   490
    SDL_Rect refRectB;
aschiffler@5868
   491
    SDL_Rect rectA;
aschiffler@5868
   492
    SDL_Rect rectB;
aschiffler@5868
   493
    SDL_Rect result;
aschiffler@5870
   494
    SDL_Rect expectedResult;
aschiffler@5868
   495
    SDL_bool intersection;
aschiffler@5868
   496
aschiffler@5868
   497
    // rectB partially contained in rectA
aschiffler@5868
   498
    refRectB.x = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
aschiffler@5868
   499
    refRectB.y = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
aschiffler@5868
   500
    refRectB.w = refRectA.w;
aschiffler@5868
   501
    refRectB.h = refRectA.h;
aschiffler@5868
   502
    rectA = refRectA;
aschiffler@5868
   503
    rectB = refRectB;
aschiffler@5870
   504
    expectedResult.x = refRectB.x;
aschiffler@5870
   505
    expectedResult.y = refRectB.y;
aschiffler@5870
   506
    expectedResult.w = refRectA.w - refRectB.x;
aschiffler@5870
   507
    expectedResult.h = refRectA.h - refRectB.y;    
aschiffler@5868
   508
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5870
   509
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5874
   510
aschiffler@5874
   511
    // rectB right edge
aschiffler@5874
   512
    refRectB.x = rectA.w - 1;
aschiffler@5874
   513
    refRectB.y = rectA.y;
aschiffler@5874
   514
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   515
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   516
    rectA = refRectA;
aschiffler@5874
   517
    rectB = refRectB;
aschiffler@5874
   518
    expectedResult.x = refRectB.x;
aschiffler@5874
   519
    expectedResult.y = refRectB.y;
aschiffler@5874
   520
    expectedResult.w = 1;
aschiffler@5874
   521
    expectedResult.h = refRectB.h;    
aschiffler@5874
   522
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5874
   523
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5874
   524
aschiffler@5874
   525
    // rectB left edge
aschiffler@5874
   526
    refRectB.x = 1 - rectA.w;
aschiffler@5874
   527
    refRectB.y = rectA.y;
aschiffler@5874
   528
    refRectB.w = refRectA.w;
aschiffler@5874
   529
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   530
    rectA = refRectA;
aschiffler@5874
   531
    rectB = refRectB;
aschiffler@5874
   532
    expectedResult.x = 0;
aschiffler@5874
   533
    expectedResult.y = refRectB.y;
aschiffler@5874
   534
    expectedResult.w = 1;
aschiffler@5874
   535
    expectedResult.h = refRectB.h;    
aschiffler@5874
   536
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5874
   537
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5874
   538
aschiffler@5874
   539
    // rectB bottom edge
aschiffler@5874
   540
    refRectB.x = rectA.x;
aschiffler@5874
   541
    refRectB.y = rectA.h - 1;
aschiffler@5874
   542
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   543
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   544
    rectA = refRectA;
aschiffler@5874
   545
    rectB = refRectB;
aschiffler@5874
   546
    expectedResult.x = refRectB.x;
aschiffler@5874
   547
    expectedResult.y = refRectB.y;
aschiffler@5874
   548
    expectedResult.w = refRectB.w;
aschiffler@5874
   549
    expectedResult.h = 1;    
aschiffler@5874
   550
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5874
   551
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5874
   552
aschiffler@5874
   553
    // rectB top edge
aschiffler@5874
   554
    refRectB.x = rectA.x;
aschiffler@5874
   555
    refRectB.y = 1 - rectA.h;
aschiffler@5874
   556
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   557
    refRectB.h = rectA.h;
aschiffler@5874
   558
    rectA = refRectA;
aschiffler@5874
   559
    rectB = refRectB;
aschiffler@5874
   560
    expectedResult.x = refRectB.x;
aschiffler@5874
   561
    expectedResult.y = 0;
aschiffler@5874
   562
    expectedResult.w = refRectB.w;
aschiffler@5874
   563
    expectedResult.h = 1;    
aschiffler@5874
   564
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5874
   565
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5868
   566
}
aschiffler@5868
   567
aschiffler@5868
   568
/*!
aschiffler@5868
   569
 * \brief Tests SDL_IntersectRect() with 1x1 pixel sized rectangles
aschiffler@5868
   570
 *
aschiffler@5868
   571
 * \sa
aschiffler@5868
   572
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
aschiffler@5868
   573
 */
aschiffler@5868
   574
int rect_testIntersectRectPoint (void *arg)
aschiffler@5868
   575
{
aschiffler@5868
   576
    SDL_Rect refRectA = { 0, 0, 1, 1 };
aschiffler@5868
   577
    SDL_Rect refRectB = { 0, 0, 1, 1 };
aschiffler@5868
   578
    SDL_Rect rectA;
aschiffler@5868
   579
    SDL_Rect rectB;
aschiffler@5868
   580
    SDL_Rect result;
aschiffler@5868
   581
    SDL_bool intersection;
aschiffler@5870
   582
    int offsetX, offsetY;
aschiffler@5868
   583
aschiffler@5868
   584
    // intersecting pixels
aschiffler@5868
   585
    refRectA.x = RandomIntegerInRange(1, 100);
aschiffler@5868
   586
    refRectA.y = RandomIntegerInRange(1, 100);
aschiffler@5868
   587
    refRectB.x = refRectA.x;
aschiffler@5868
   588
    refRectB.y = refRectA.y;
aschiffler@5868
   589
    rectA = refRectA;
aschiffler@5868
   590
    rectB = refRectB;
aschiffler@5868
   591
    intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5870
   592
    _validateIntersectRectResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB, &result, &refRectA);
aschiffler@5870
   593
aschiffler@5870
   594
    // non-intersecting pixels cases
aschiffler@5870
   595
    for (offsetX = -1; offsetX <= 1; offsetX++) {
aschiffler@5870
   596
        for (offsetY = -1; offsetY <= 1; offsetY++) {
aschiffler@5870
   597
            if (offsetX != 0 || offsetY != 0) {
aschiffler@5870
   598
                refRectA.x = RandomIntegerInRange(1, 100);
aschiffler@5870
   599
                refRectA.y = RandomIntegerInRange(1, 100);
aschiffler@5870
   600
                refRectB.x = refRectA.x;
aschiffler@5870
   601
                refRectB.y = refRectA.y;    
aschiffler@5870
   602
                refRectB.x += offsetX;
aschiffler@5870
   603
                refRectB.y += offsetY;
aschiffler@5870
   604
                rectA = refRectA;
aschiffler@5870
   605
                rectB = refRectB;
aschiffler@5870
   606
                intersection = SDL_IntersectRect(&rectA, &rectB, &result);
aschiffler@5870
   607
                _validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5870
   608
            }
aschiffler@5870
   609
        }
aschiffler@5870
   610
    }
aschiffler@5870
   611
}
aschiffler@5870
   612
aschiffler@5870
   613
/*!
aschiffler@5870
   614
 * \brief Negative tests against SDL_IntersectRect() with invalid parameters
aschiffler@5870
   615
 *
aschiffler@5870
   616
 * \sa
aschiffler@5870
   617
 * http://wiki.libsdl.org/moin.cgi/SDL_IntersectRect
aschiffler@5870
   618
 */
aschiffler@5870
   619
int rect_testIntersectRectParam(void *arg)
aschiffler@5870
   620
{
aschiffler@5870
   621
    SDL_Rect rectA;
aschiffler@5870
   622
    SDL_Rect rectB;
aschiffler@5870
   623
    SDL_Rect result;
aschiffler@5870
   624
    SDL_bool intersection;
aschiffler@5868
   625
aschiffler@5870
   626
    // invalid parameter combinations
aschiffler@5870
   627
    intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, &result);
aschiffler@5870
   628
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
aschiffler@5870
   629
    intersection = SDL_IntersectRect(&rectA, (SDL_Rect *)NULL, &result);
aschiffler@5870
   630
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 2st parameter was NULL"); 
aschiffler@5870
   631
    intersection = SDL_IntersectRect(&rectA, &rectB, (SDL_Rect *)NULL);
aschiffler@5870
   632
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 3st parameter was NULL"); 
aschiffler@5870
   633
    intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, &result);
aschiffler@5870
   634
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st and 2nd parameters were NULL"); 
aschiffler@5870
   635
    intersection = SDL_IntersectRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
aschiffler@5870
   636
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st and 3rd parameters were NULL "); 
aschiffler@5870
   637
    intersection = SDL_IntersectRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5870
   638
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when all parameters were NULL");     
aschiffler@5870
   639
}
aschiffler@5870
   640
aschiffler@5870
   641
/*!
aschiffler@5870
   642
 * \brief Tests SDL_HasIntersection() with B fully inside A
aschiffler@5870
   643
 *
aschiffler@5870
   644
 * \sa
aschiffler@5870
   645
 * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
aschiffler@5870
   646
 */
aschiffler@5870
   647
int rect_testHasIntersectionInside (void *arg)
aschiffler@5870
   648
{
aschiffler@5870
   649
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5870
   650
    SDL_Rect refRectB;
aschiffler@5870
   651
    SDL_Rect rectA;
aschiffler@5870
   652
    SDL_Rect rectB;
aschiffler@5870
   653
    SDL_bool intersection;
aschiffler@5870
   654
aschiffler@5870
   655
    // rectB fully contained in rectA
aschiffler@5870
   656
    refRectB.x = 0;
aschiffler@5870
   657
    refRectB.y = 0;
aschiffler@5870
   658
    refRectB.w = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
aschiffler@5870
   659
    refRectB.h = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
aschiffler@5870
   660
    rectA = refRectA;
aschiffler@5870
   661
    rectB = refRectB;
aschiffler@5870
   662
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5870
   663
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5870
   664
}
aschiffler@5870
   665
aschiffler@5870
   666
/*!
aschiffler@5870
   667
 * \brief Tests SDL_HasIntersection() with B fully outside A
aschiffler@5870
   668
 *
aschiffler@5870
   669
 * \sa
aschiffler@5870
   670
 * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
aschiffler@5870
   671
 */
aschiffler@5870
   672
int rect_testHasIntersectionOutside (void *arg)
aschiffler@5870
   673
{
aschiffler@5870
   674
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5870
   675
    SDL_Rect refRectB;
aschiffler@5870
   676
    SDL_Rect rectA;
aschiffler@5870
   677
    SDL_Rect rectB;
aschiffler@5870
   678
    SDL_bool intersection;
aschiffler@5870
   679
aschiffler@5870
   680
    // rectB fully outside of rectA
aschiffler@5870
   681
    refRectB.x = refRectA.x + refRectA.w + RandomIntegerInRange(1, 10);
aschiffler@5870
   682
    refRectB.y = refRectA.y + refRectA.h + RandomIntegerInRange(1, 10);
aschiffler@5870
   683
    refRectB.w = refRectA.w;
aschiffler@5870
   684
    refRectB.h = refRectA.h;
aschiffler@5868
   685
    rectA = refRectA;
aschiffler@5868
   686
    rectB = refRectB;
aschiffler@5870
   687
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5870
   688
    _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5870
   689
}
aschiffler@5870
   690
aschiffler@5870
   691
/*!
aschiffler@5870
   692
 * \brief Tests SDL_HasIntersection() with B partially intersecting A
aschiffler@5870
   693
 *
aschiffler@5870
   694
 * \sa
aschiffler@5870
   695
 * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
aschiffler@5870
   696
 */
aschiffler@5870
   697
int rect_testHasIntersectionPartial (void *arg)
aschiffler@5870
   698
{
aschiffler@5870
   699
    SDL_Rect refRectA = { 0, 0, 32, 32 };
aschiffler@5870
   700
    SDL_Rect refRectB;
aschiffler@5870
   701
    SDL_Rect rectA;
aschiffler@5870
   702
    SDL_Rect rectB;
aschiffler@5870
   703
    SDL_bool intersection;
aschiffler@5870
   704
aschiffler@5870
   705
    // rectB partially contained in rectA
aschiffler@5870
   706
    refRectB.x = RandomIntegerInRange(refRectA.x + 1, refRectA.x + refRectA.w - 1);
aschiffler@5870
   707
    refRectB.y = RandomIntegerInRange(refRectA.y + 1, refRectA.y + refRectA.h - 1);
aschiffler@5870
   708
    refRectB.w = refRectA.w;
aschiffler@5870
   709
    refRectB.h = refRectA.h;
aschiffler@5870
   710
    rectA = refRectA;
aschiffler@5870
   711
    rectB = refRectB;
aschiffler@5870
   712
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5870
   713
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5874
   714
aschiffler@5874
   715
    // rectB right edge
aschiffler@5874
   716
    refRectB.x = rectA.w - 1;
aschiffler@5874
   717
    refRectB.y = rectA.y;
aschiffler@5874
   718
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   719
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   720
    rectA = refRectA;
aschiffler@5874
   721
    rectB = refRectB;
aschiffler@5874
   722
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5874
   723
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5874
   724
aschiffler@5874
   725
    // rectB left edge
aschiffler@5874
   726
    refRectB.x = 1 - rectA.w;
aschiffler@5874
   727
    refRectB.y = rectA.y;
aschiffler@5874
   728
    refRectB.w = refRectA.w;
aschiffler@5874
   729
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   730
    rectA = refRectA;
aschiffler@5874
   731
    rectB = refRectB;
aschiffler@5874
   732
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5874
   733
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5874
   734
aschiffler@5874
   735
    // rectB bottom edge
aschiffler@5874
   736
    refRectB.x = rectA.x;
aschiffler@5874
   737
    refRectB.y = rectA.h - 1;
aschiffler@5874
   738
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   739
    refRectB.h = RandomIntegerInRange(1, refRectA.h - 1);
aschiffler@5874
   740
    rectA = refRectA;
aschiffler@5874
   741
    rectB = refRectB;
aschiffler@5874
   742
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5874
   743
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5874
   744
aschiffler@5874
   745
    // rectB top edge
aschiffler@5874
   746
    refRectB.x = rectA.x;
aschiffler@5874
   747
    refRectB.y = 1 - rectA.h;
aschiffler@5874
   748
    refRectB.w = RandomIntegerInRange(1, refRectA.w - 1);
aschiffler@5874
   749
    refRectB.h = rectA.h;
aschiffler@5874
   750
    rectA = refRectA;
aschiffler@5874
   751
    rectB = refRectB;
aschiffler@5874
   752
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5874
   753
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5868
   754
}
aschiffler@5870
   755
aschiffler@5870
   756
/*!
aschiffler@5870
   757
 * \brief Tests SDL_HasIntersection() with 1x1 pixel sized rectangles
aschiffler@5870
   758
 *
aschiffler@5870
   759
 * \sa
aschiffler@5870
   760
 * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
aschiffler@5870
   761
 */
aschiffler@5870
   762
int rect_testHasIntersectionPoint (void *arg)
aschiffler@5870
   763
{
aschiffler@5870
   764
    SDL_Rect refRectA = { 0, 0, 1, 1 };
aschiffler@5870
   765
    SDL_Rect refRectB = { 0, 0, 1, 1 };
aschiffler@5870
   766
    SDL_Rect rectA;
aschiffler@5870
   767
    SDL_Rect rectB;
aschiffler@5870
   768
    SDL_Rect result;
aschiffler@5870
   769
    SDL_bool intersection;
aschiffler@5870
   770
    int offsetX, offsetY;
aschiffler@5870
   771
aschiffler@5870
   772
    // intersecting pixels
aschiffler@5870
   773
    refRectA.x = RandomIntegerInRange(1, 100);
aschiffler@5870
   774
    refRectA.y = RandomIntegerInRange(1, 100);
aschiffler@5870
   775
    refRectB.x = refRectA.x;
aschiffler@5870
   776
    refRectB.y = refRectA.y;
aschiffler@5870
   777
    rectA = refRectA;
aschiffler@5870
   778
    rectB = refRectB;
aschiffler@5870
   779
    intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5870
   780
    _validateHasIntersectionResults(intersection, SDL_TRUE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5870
   781
aschiffler@5870
   782
    // non-intersecting pixels cases
aschiffler@5870
   783
    for (offsetX = -1; offsetX <= 1; offsetX++) {
aschiffler@5870
   784
        for (offsetY = -1; offsetY <= 1; offsetY++) {
aschiffler@5870
   785
            if (offsetX != 0 || offsetY != 0) {
aschiffler@5870
   786
                refRectA.x = RandomIntegerInRange(1, 100);
aschiffler@5870
   787
                refRectA.y = RandomIntegerInRange(1, 100);
aschiffler@5870
   788
                refRectB.x = refRectA.x;
aschiffler@5870
   789
                refRectB.y = refRectA.y;    
aschiffler@5870
   790
                refRectB.x += offsetX;
aschiffler@5870
   791
                refRectB.y += offsetY;
aschiffler@5870
   792
                rectA = refRectA;
aschiffler@5870
   793
                rectB = refRectB;
aschiffler@5870
   794
                intersection = SDL_HasIntersection(&rectA, &rectB);
aschiffler@5870
   795
                _validateHasIntersectionResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB);
aschiffler@5870
   796
            }
aschiffler@5870
   797
        }
aschiffler@5870
   798
    }
aschiffler@5870
   799
}
aschiffler@5870
   800
aschiffler@5870
   801
/*!
aschiffler@5870
   802
 * \brief Negative tests against SDL_HasIntersection() with invalid parameters
aschiffler@5870
   803
 *
aschiffler@5870
   804
 * \sa
aschiffler@5870
   805
 * http://wiki.libsdl.org/moin.cgi/SDL_HasIntersection
aschiffler@5870
   806
 */
aschiffler@5870
   807
int rect_testHasIntersectionParam(void *arg)
aschiffler@5870
   808
{
aschiffler@5870
   809
    SDL_Rect rectA;
aschiffler@5870
   810
    SDL_Rect rectB;
aschiffler@5870
   811
    SDL_bool intersection;
aschiffler@5870
   812
aschiffler@5870
   813
    // invalid parameter combinations
aschiffler@5870
   814
    intersection = SDL_HasIntersection((SDL_Rect *)NULL, &rectB);
aschiffler@5870
   815
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
aschiffler@5870
   816
    intersection = SDL_HasIntersection(&rectA, (SDL_Rect *)NULL);
aschiffler@5870
   817
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when 2st parameter was NULL"); 
aschiffler@5870
   818
    intersection = SDL_HasIntersection((SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5870
   819
    AssertTrue(intersection == SDL_FALSE, "Function did not return false when all parameters were NULL");     
aschiffler@5870
   820
}
aschiffler@5903
   821
aschiffler@5903
   822
/*!
aschiffler@5903
   823
 * \brief Test SDL_EnclosePoints() without clipping
aschiffler@5903
   824
 *
aschiffler@5903
   825
 * \sa
aschiffler@5903
   826
 * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
aschiffler@5903
   827
 */
aschiffler@5903
   828
int rect_testEnclosePoints(void *arg)
aschiffler@5903
   829
{
aschiffler@5903
   830
    const int numPoints = 16;
aschiffler@5903
   831
    SDL_Point refPoints[numPoints];
aschiffler@5903
   832
    SDL_Point points[numPoints];
aschiffler@5903
   833
    int count;
aschiffler@5903
   834
    SDL_Rect result;
aschiffler@5903
   835
    SDL_bool anyEnclosed;
aschiffler@5903
   836
    SDL_bool anyEnclosedNoResult;
aschiffler@5903
   837
aschiffler@5903
   838
    // Create input data, tracking result
aschiffler@5903
   839
    SDL_bool expectedEnclosed = SDL_TRUE;
aschiffler@5903
   840
    int newx, newy;
aschiffler@5903
   841
    int minx, maxx, miny, maxy;
aschiffler@5903
   842
    int i;
aschiffler@5903
   843
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   844
        newx = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   845
        newy = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   846
        refPoints[i].x = newx;
aschiffler@5903
   847
        refPoints[i].y = newy;
aschiffler@5903
   848
        points[i].x = newx;
aschiffler@5903
   849
        points[i].y = newy;
aschiffler@5903
   850
        if (i==0) {
aschiffler@5903
   851
            minx=maxx=newx;
aschiffler@5903
   852
            miny=maxy=newy;
aschiffler@5903
   853
        } else {
aschiffler@5903
   854
            if (newx<minx) minx=newx;
aschiffler@5903
   855
            if (newx>maxx) maxx=newx;
aschiffler@5903
   856
            if (newy<miny) miny=newy;
aschiffler@5903
   857
            if (newy>maxy) maxy=newy;
aschiffler@5903
   858
        }
aschiffler@5903
   859
    }
aschiffler@5903
   860
    
aschiffler@5903
   861
    // Call function and validate - special case: no result requested
aschiffler@5903
   862
    anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5903
   863
    AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
aschiffler@5903
   864
        "Expected return value %s, got %s", 
aschiffler@5903
   865
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
aschiffler@5903
   866
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   867
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
   868
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
   869
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
   870
    }
aschiffler@5903
   871
aschiffler@5903
   872
    // Call function and validate
aschiffler@5903
   873
    anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
aschiffler@5903
   874
    AssertTrue(expectedEnclosed==anyEnclosed, 
aschiffler@5903
   875
        "Expected return value %s, got %s", 
aschiffler@5903
   876
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
aschiffler@5903
   877
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   878
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
   879
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
   880
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
   881
    }
aschiffler@5903
   882
    AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
aschiffler@5903
   883
        "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
aschiffler@5903
   884
        minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
aschiffler@5903
   885
}
aschiffler@5903
   886
aschiffler@5903
   887
/*!
aschiffler@5903
   888
 * \brief Test SDL_EnclosePoints() with repeated input points
aschiffler@5903
   889
 *
aschiffler@5903
   890
 * \sa
aschiffler@5903
   891
 * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
aschiffler@5903
   892
 */
aschiffler@5903
   893
int rect_testEnclosePointsRepeatedInput(void *arg)
aschiffler@5903
   894
{
aschiffler@5903
   895
    const int numPoints = 8;
aschiffler@5903
   896
    const int halfPoints = numPoints/2;
aschiffler@5903
   897
    SDL_Point refPoints[numPoints];
aschiffler@5903
   898
    SDL_Point points[numPoints];
aschiffler@5903
   899
    int count;
aschiffler@5903
   900
    SDL_Rect result;
aschiffler@5903
   901
    SDL_bool anyEnclosed;
aschiffler@5903
   902
    SDL_bool anyEnclosedNoResult;
aschiffler@5903
   903
aschiffler@5903
   904
    // Create input data, tracking result
aschiffler@5903
   905
    SDL_bool expectedEnclosed = SDL_TRUE;
aschiffler@5903
   906
    int newx, newy;
aschiffler@5903
   907
    int minx, maxx, miny, maxy;
aschiffler@5903
   908
    int i;
aschiffler@5903
   909
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   910
        if (i < halfPoints) {
aschiffler@5903
   911
            newx = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   912
            newy = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   913
        } else {
aschiffler@5903
   914
            newx = refPoints[i-halfPoints].x;
aschiffler@5903
   915
            newy = refPoints[i-halfPoints].y;
aschiffler@5903
   916
        }
aschiffler@5903
   917
        refPoints[i].x = newx;
aschiffler@5903
   918
        refPoints[i].y = newy;
aschiffler@5903
   919
        points[i].x = newx;
aschiffler@5903
   920
        points[i].y = newy;
aschiffler@5903
   921
        if (i==0) {
aschiffler@5903
   922
            minx=maxx=newx;
aschiffler@5903
   923
            miny=maxy=newy;
aschiffler@5903
   924
        } else {
aschiffler@5903
   925
            if (newx<minx) minx=newx;
aschiffler@5903
   926
            if (newx>maxx) maxx=newx;
aschiffler@5903
   927
            if (newy<miny) miny=newy;
aschiffler@5903
   928
            if (newy>maxy) maxy=newy;
aschiffler@5903
   929
        }
aschiffler@5903
   930
    }
aschiffler@5903
   931
aschiffler@5903
   932
    // Call function and validate - special case: no result requested
aschiffler@5903
   933
    anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5903
   934
    AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
aschiffler@5903
   935
        "Expected return value %s, got %s", 
aschiffler@5903
   936
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
aschiffler@5903
   937
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   938
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
   939
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
   940
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
   941
    }
aschiffler@5903
   942
    
aschiffler@5903
   943
    // Call function and validate
aschiffler@5903
   944
    anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)NULL, &result);
aschiffler@5903
   945
    AssertTrue(expectedEnclosed==anyEnclosed, 
aschiffler@5903
   946
        "Expected return value %s, got %s", 
aschiffler@5903
   947
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
aschiffler@5903
   948
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   949
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
   950
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
   951
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
   952
    }
aschiffler@5903
   953
    AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
aschiffler@5903
   954
        "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
aschiffler@5903
   955
        minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
aschiffler@5903
   956
}
aschiffler@5903
   957
aschiffler@5903
   958
/*!
aschiffler@5903
   959
 * \brief Test SDL_EnclosePoints() with clipping
aschiffler@5903
   960
 *
aschiffler@5903
   961
 * \sa
aschiffler@5903
   962
 * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
aschiffler@5903
   963
 */
aschiffler@5903
   964
int rect_testEnclosePointsWithClipping(void *arg)
aschiffler@5903
   965
{
aschiffler@5903
   966
    const int numPoints = 16;
aschiffler@5903
   967
    SDL_Point refPoints[numPoints];
aschiffler@5903
   968
    SDL_Point points[numPoints];
aschiffler@5903
   969
    int count;
aschiffler@5903
   970
    SDL_Rect refClip;
aschiffler@5903
   971
    SDL_Rect clip;
aschiffler@5903
   972
    SDL_Rect result;
aschiffler@5903
   973
    SDL_bool anyEnclosed;
aschiffler@5903
   974
    SDL_bool anyEnclosedNoResult;
aschiffler@5903
   975
aschiffler@5903
   976
    // Setup clipping rectangle
aschiffler@5903
   977
    refClip.x = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   978
    refClip.y = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   979
    refClip.w = RandomIntegerInRange(1, 1024);
aschiffler@5903
   980
    refClip.h = RandomIntegerInRange(1, 1024);
aschiffler@5903
   981
aschiffler@5903
   982
    // Create input data, tracking result
aschiffler@5903
   983
    SDL_bool expectedEnclosed = SDL_FALSE;
aschiffler@5903
   984
    int newx, newy;
aschiffler@5903
   985
    int minx, maxx, miny, maxy;
aschiffler@5903
   986
    int i;
aschiffler@5903
   987
    for (i=0; i<numPoints; i++) {
aschiffler@5903
   988
        newx = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   989
        newy = RandomIntegerInRange(-1024, 1024);
aschiffler@5903
   990
        refPoints[i].x = newx;
aschiffler@5903
   991
        refPoints[i].y = newy;
aschiffler@5903
   992
        points[i].x = newx;
aschiffler@5903
   993
        points[i].y = newy;
aschiffler@5903
   994
        if ((newx>=refClip.x) && (newx<(refClip.x + refClip.w)) &&
aschiffler@5903
   995
            (newy>=refClip.y) && (newy<(refClip.y + refClip.h))) {
aschiffler@5903
   996
            if (expectedEnclosed==SDL_FALSE) {
aschiffler@5903
   997
                minx=maxx=newx;
aschiffler@5903
   998
                miny=maxy=newy;
aschiffler@5903
   999
            } else {
aschiffler@5903
  1000
                if (newx<minx) minx=newx;
aschiffler@5903
  1001
                if (newx>maxx) maxx=newx;
aschiffler@5903
  1002
                if (newy<miny) miny=newy;
aschiffler@5903
  1003
                if (newy>maxy) maxy=newy;
aschiffler@5903
  1004
            }
aschiffler@5903
  1005
            expectedEnclosed = SDL_TRUE;
aschiffler@5903
  1006
        }
aschiffler@5903
  1007
    }
aschiffler@5903
  1008
aschiffler@5903
  1009
    // Call function and validate - special case: no result requested
aschiffler@5903
  1010
    clip = refClip;
aschiffler@5903
  1011
    anyEnclosedNoResult = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, (SDL_Rect *)NULL);
aschiffler@5903
  1012
    AssertTrue(expectedEnclosed==anyEnclosedNoResult, 
aschiffler@5903
  1013
        "Expected return value %s, got %s", 
aschiffler@5903
  1014
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosedNoResult==SDL_TRUE) ? "true" : "false");
aschiffler@5903
  1015
    for (i=0; i<numPoints; i++) {
aschiffler@5903
  1016
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
  1017
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
  1018
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
  1019
    }
aschiffler@5903
  1020
    AssertTrue(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
aschiffler@5903
  1021
        "Source clipping rectangle was incorrectly modified");
aschiffler@5903
  1022
    
aschiffler@5903
  1023
    // Call function and validate
aschiffler@5903
  1024
    anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, numPoints, (const SDL_Rect *)&clip, &result);
aschiffler@5903
  1025
    AssertTrue(expectedEnclosed==anyEnclosed, 
aschiffler@5903
  1026
        "Expected return value %s, got %s", 
aschiffler@5903
  1027
        (expectedEnclosed==SDL_TRUE) ? "true" : "false", (anyEnclosed==SDL_TRUE) ? "true" : "false");
aschiffler@5903
  1028
    for (i=0; i<numPoints; i++) {
aschiffler@5903
  1029
        AssertTrue(refPoints[i].x==points[i].x && refPoints[i].y==points[i].y,
aschiffler@5903
  1030
            "Source point %i was incorrectly modified: expected (%i,%i) actual (%i,%i)", 
aschiffler@5903
  1031
            i, refPoints[i].x, refPoints[i].y, points[i].x, points[i].y);
aschiffler@5903
  1032
    }
aschiffler@5903
  1033
    AssertTrue(refClip.x==clip.x && refClip.y==clip.y && refClip.w==clip.w && refClip.h==clip.h,
aschiffler@5903
  1034
        "Source clipping rectangle was incorrectly modified");
aschiffler@5903
  1035
    if (expectedEnclosed==SDL_TRUE) {
aschiffler@5903
  1036
        AssertTrue(result.x==minx && result.y==miny && result.w==(maxx - minx + 1) && result.h==(maxy - miny + 1),
aschiffler@5903
  1037
            "Resulting enclosing rectangle incorrect: expected (%i,%i - %i,%i), actual (%i,%i - %i,%i)",
aschiffler@5903
  1038
            minx, miny, maxx, maxy, result.x, result.y, result.x + result.w - 1, result.y + result.h - 1);
aschiffler@5903
  1039
    }    
aschiffler@5903
  1040
}
aschiffler@5903
  1041
aschiffler@5903
  1042
/*!
aschiffler@5903
  1043
 * \brief Negative tests against SDL_EnclosePoints() with invalid parameters
aschiffler@5903
  1044
 *
aschiffler@5903
  1045
 * \sa
aschiffler@5903
  1046
 * http://wiki.libsdl.org/moin.cgi/SDL_EnclosePoints
aschiffler@5903
  1047
 */
aschiffler@5903
  1048
int rect_testEnclosePointsParam(void *arg)
aschiffler@5903
  1049
{
aschiffler@5903
  1050
    SDL_Point points[1];
aschiffler@5903
  1051
    int count;
aschiffler@5903
  1052
    SDL_Rect clip;
aschiffler@5903
  1053
    SDL_Rect result;
aschiffler@5903
  1054
    SDL_bool anyEnclosed;
aschiffler@5903
  1055
aschiffler@5903
  1056
    // invalid parameter combinations
aschiffler@5903
  1057
    anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 1, (const SDL_Rect *)&clip, &result);
aschiffler@5903
  1058
    AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 1st parameter was NULL"); 
aschiffler@5903
  1059
    anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, 0, (const SDL_Rect *)&clip, &result);
aschiffler@5903
  1060
    AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 2nd parameter was 0");
aschiffler@5903
  1061
    count = RandomIntegerInRange(-100, -1);
aschiffler@5903
  1062
    anyEnclosed = SDL_EnclosePoints((const SDL_Point *)points, count, (const SDL_Rect *)&clip, &result);
aschiffler@5903
  1063
    AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 2nd parameter was %i", count); 
aschiffler@5903
  1064
    anyEnclosed = SDL_EnclosePoints((SDL_Point *)NULL, 0, (const SDL_Rect *)&clip, &result);
aschiffler@5903
  1065
    AssertTrue(anyEnclosed == SDL_FALSE, "Function did not return false when 1st parameter was NULL and 2nd parameter was 0"); 
aschiffler@5903
  1066
}
aschiffler@5912
  1067
aschiffler@5912
  1068
aschiffler@5912
  1069
/*!
aschiffler@5912
  1070
 * \brief Tests SDL_UnionRect() where rect B is outside rect A
aschiffler@5912
  1071
 *
aschiffler@5912
  1072
 * \sa
aschiffler@5912
  1073
 * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
aschiffler@5912
  1074
 */
aschiffler@5912
  1075
int rect_testUnionRectOutside(void *arg)
aschiffler@5912
  1076
{
aschiffler@5912
  1077
    SDL_Rect refRectA, refRectB;
aschiffler@5912
  1078
    SDL_Rect rectA, rectB;
aschiffler@5912
  1079
    SDL_Rect expectedResult;
aschiffler@5912
  1080
    SDL_Rect result;
aschiffler@5912
  1081
    int minx, maxx, miny, maxy;
aschiffler@5912
  1082
    int dx, dy;
aschiffler@5912
  1083
    
aschiffler@5912
  1084
    /* Union 1x1 outside */
aschiffler@5912
  1085
    for (dx = -1; dx < 2; dx++) {     
aschiffler@5912
  1086
        for (dy = -1; dy < 2; dy++) {
aschiffler@5912
  1087
            if ((dx != 0) || (dy != 0)) {
aschiffler@5912
  1088
                refRectA.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1089
                refRectA.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1090
                refRectA.w=1;
aschiffler@5912
  1091
                refRectA.h=1;
aschiffler@5912
  1092
                refRectB.x=RandomIntegerInRange(-1024, 1024) + dx*2048;
aschiffler@5912
  1093
                refRectB.y=RandomIntegerInRange(-1024, 1024) + dx*2048;
aschiffler@5912
  1094
                refRectB.w=1;
aschiffler@5912
  1095
                refRectB.h=1;
aschiffler@5912
  1096
                minx = (refRectA.x<refRectB.x) ? refRectA.x : refRectB.x;
aschiffler@5912
  1097
                maxx = (refRectA.x>refRectB.x) ? refRectA.x : refRectB.x;
aschiffler@5912
  1098
                miny = (refRectA.y<refRectB.y) ? refRectA.y : refRectB.y;
aschiffler@5912
  1099
                maxy = (refRectA.y>refRectB.y) ? refRectA.y : refRectB.y;                
aschiffler@5912
  1100
                expectedResult.x = minx;
aschiffler@5912
  1101
                expectedResult.y = miny;
aschiffler@5912
  1102
                expectedResult.w = maxx - minx + 1;
aschiffler@5912
  1103
                expectedResult.h = maxy - miny + 1;
aschiffler@5912
  1104
                rectA = refRectA;
aschiffler@5912
  1105
                rectB = refRectB;
aschiffler@5912
  1106
                SDL_UnionRect(&rectA, &rectB, &result);
aschiffler@5912
  1107
                _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5912
  1108
            }
aschiffler@5912
  1109
        }
aschiffler@5912
  1110
    }
aschiffler@5912
  1111
aschiffler@5912
  1112
    /* Union outside overlap */
aschiffler@5912
  1113
    for (dx = -1; dx < 2; dx++) {     
aschiffler@5912
  1114
        for (dy = -1; dy < 2; dy++) {
aschiffler@5912
  1115
            if ((dx != 0) || (dy != 0)) {
aschiffler@5912
  1116
                refRectA.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1117
                refRectA.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1118
                refRectA.w=RandomIntegerInRange(256, 512);
aschiffler@5912
  1119
                refRectA.h=RandomIntegerInRange(256, 512);
aschiffler@5912
  1120
                refRectB.x=refRectA.x + 1 + dx*2;
aschiffler@5912
  1121
                refRectB.y=refRectA.y + 1 + dy*2;
aschiffler@5912
  1122
                refRectB.w=refRectA.w - 2;
aschiffler@5912
  1123
                refRectB.h=refRectA.h - 2;
aschiffler@5912
  1124
                expectedResult = refRectA;
aschiffler@5912
  1125
                if (dx == -1) expectedResult.x--;
aschiffler@5912
  1126
                if (dy == -1) expectedResult.y--;
aschiffler@5912
  1127
                if ((dx == 1) || (dx == -1)) expectedResult.w++;
aschiffler@5912
  1128
                if ((dy == 1) || (dy == -1)) expectedResult.h++;
aschiffler@5912
  1129
                rectA = refRectA;
aschiffler@5912
  1130
                rectB = refRectB;
aschiffler@5912
  1131
                SDL_UnionRect(&rectA, &rectB, &result);
aschiffler@5912
  1132
                _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5912
  1133
            }
aschiffler@5912
  1134
        }
aschiffler@5912
  1135
    }
aschiffler@5912
  1136
}
aschiffler@5912
  1137
aschiffler@5912
  1138
/*!
aschiffler@5912
  1139
 * \brief Tests SDL_UnionRect() where rect B is inside rect A
aschiffler@5912
  1140
 *
aschiffler@5912
  1141
 * \sa
aschiffler@5912
  1142
 * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
aschiffler@5912
  1143
 */
aschiffler@5912
  1144
int rect_testUnionRectInside(void *arg)
aschiffler@5912
  1145
{
aschiffler@5912
  1146
    SDL_Rect refRectA, refRectB;
aschiffler@5912
  1147
    SDL_Rect rectA, rectB;
aschiffler@5912
  1148
    SDL_Rect expectedResult;
aschiffler@5912
  1149
    SDL_Rect result;
aschiffler@5912
  1150
    int minx, maxx, miny, maxy;
aschiffler@5912
  1151
    int dx, dy;
aschiffler@5912
  1152
    
aschiffler@5912
  1153
    /* Union 1x1 with itself */
aschiffler@5912
  1154
    refRectA.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1155
    refRectA.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1156
    refRectA.w=1;
aschiffler@5912
  1157
    refRectA.h=1;
aschiffler@5912
  1158
    expectedResult = refRectA;
aschiffler@5912
  1159
    rectA = refRectA;
aschiffler@5912
  1160
    SDL_UnionRect(&rectA, &rectA, &result);
aschiffler@5912
  1161
    _validateUnionRectResults(&rectA, &rectA, &refRectA, &refRectA, &result, &expectedResult);
aschiffler@5912
  1162
aschiffler@5912
  1163
    /* Union 1x1 somewhere inside */
aschiffler@5912
  1164
    refRectA.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1165
    refRectA.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1166
    refRectA.w=RandomIntegerInRange(256, 1024);
aschiffler@5912
  1167
    refRectA.h=RandomIntegerInRange(256, 1024);
aschiffler@5912
  1168
    refRectB.x=refRectA.x + 1 + RandomIntegerInRange(1, refRectA.w - 2);
aschiffler@5912
  1169
    refRectB.y=refRectA.y + 1 + RandomIntegerInRange(1, refRectA.h - 2);
aschiffler@5912
  1170
    refRectB.w=1;
aschiffler@5912
  1171
    refRectB.h=1;
aschiffler@5912
  1172
    expectedResult = refRectA;
aschiffler@5912
  1173
    rectA = refRectA;
aschiffler@5912
  1174
    rectB = refRectB;
aschiffler@5912
  1175
    SDL_UnionRect(&rectA, &rectB, &result);
aschiffler@5912
  1176
    _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5912
  1177
aschiffler@5912
  1178
    /* Union inside with edges modified */
aschiffler@5912
  1179
    for (dx = -1; dx < 2; dx++) {     
aschiffler@5912
  1180
        for (dy = -1; dy < 2; dy++) {
aschiffler@5912
  1181
            if ((dx != 0) || (dy != 0)) {
aschiffler@5912
  1182
                refRectA.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1183
                refRectA.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5912
  1184
                refRectA.w=RandomIntegerInRange(256, 1024);
aschiffler@5912
  1185
                refRectA.h=RandomIntegerInRange(256, 1024);
aschiffler@5912
  1186
                refRectB = refRectA;
aschiffler@5912
  1187
                if (dx == -1) refRectB.x++;
aschiffler@5912
  1188
                if ((dx == 1) || (dx == -1)) refRectB.w--;
aschiffler@5912
  1189
                if (dy == -1) refRectB.y++;
aschiffler@5912
  1190
                if ((dy == 1) || (dy == -1)) refRectB.h--;                
aschiffler@5912
  1191
                expectedResult = refRectA;
aschiffler@5912
  1192
                rectA = refRectA;
aschiffler@5912
  1193
                rectB = refRectB;
aschiffler@5912
  1194
                SDL_UnionRect(&rectA, &rectB, &result);
aschiffler@5912
  1195
                _validateUnionRectResults(&rectA, &rectB, &refRectA, &refRectB, &result, &expectedResult);
aschiffler@5912
  1196
            }
aschiffler@5912
  1197
        }
aschiffler@5912
  1198
    }
aschiffler@5912
  1199
}
aschiffler@5912
  1200
aschiffler@5912
  1201
/*!
aschiffler@5912
  1202
 * \brief Negative tests against SDL_UnionRect() with invalid parameters
aschiffler@5912
  1203
 *
aschiffler@5912
  1204
 * \sa
aschiffler@5912
  1205
 * http://wiki.libsdl.org/moin.cgi/SDL_UnionRect
aschiffler@5912
  1206
 */
aschiffler@5912
  1207
int rect_testUnionRectParam(void *arg)
aschiffler@5912
  1208
{
aschiffler@5912
  1209
    SDL_Rect rectA, rectB;
aschiffler@5912
  1210
    SDL_Rect result;
aschiffler@5912
  1211
aschiffler@5912
  1212
    // invalid parameter combinations
aschiffler@5912
  1213
    SDL_UnionRect((SDL_Rect *)NULL, &rectB, &result);
aschiffler@5912
  1214
    AssertPass("Function did return when 1st parameter was NULL"); 
aschiffler@5912
  1215
    SDL_UnionRect(&rectA, (SDL_Rect *)NULL, &result);
aschiffler@5912
  1216
    AssertPass("Function did return when 2nd parameter was NULL"); 
aschiffler@5912
  1217
    SDL_UnionRect(&rectA, &rectB, (SDL_Rect *)NULL);
aschiffler@5912
  1218
    AssertPass("Function did return when 3rd parameter was NULL"); 
aschiffler@5912
  1219
    SDL_UnionRect((SDL_Rect *)NULL, &rectB, (SDL_Rect *)NULL);
aschiffler@5912
  1220
    AssertPass("Function did return when 1st and 3rd parameter were NULL"); 
aschiffler@5912
  1221
    SDL_UnionRect(&rectA, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5912
  1222
    AssertPass("Function did return when 2nd and 3rd parameter were NULL"); 
aschiffler@5912
  1223
    SDL_UnionRect((SDL_Rect *)NULL, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
aschiffler@5912
  1224
    AssertPass("Function did return when all parameters were NULL"); 
aschiffler@5912
  1225
}
aschiffler@5948
  1226
aschiffler@5948
  1227
/*!
aschiffler@5948
  1228
 * \brief Tests SDL_RectEmpty() with various inputs
aschiffler@5948
  1229
 *
aschiffler@5948
  1230
 * \sa
aschiffler@5948
  1231
 * http://wiki.libsdl.org/moin.cgi/SDL_RectEmpty
aschiffler@5948
  1232
 */
aschiffler@5948
  1233
int rect_testRectEmpty(void *arg)
aschiffler@5948
  1234
{
aschiffler@5948
  1235
    SDL_Rect refRect;
aschiffler@5948
  1236
    SDL_Rect rect;
aschiffler@5948
  1237
    SDL_bool expectedResult;
aschiffler@5948
  1238
    SDL_bool result;
aschiffler@5948
  1239
    int w, h;
aschiffler@5948
  1240
aschiffler@5948
  1241
    // Non-empty case
aschiffler@5948
  1242
    refRect.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5948
  1243
    refRect.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5948
  1244
    refRect.w=RandomIntegerInRange(256, 1024);
aschiffler@5948
  1245
    refRect.h=RandomIntegerInRange(256, 1024);
aschiffler@5948
  1246
    expectedResult = SDL_FALSE;
aschiffler@5948
  1247
    rect = refRect;
aschiffler@5948
  1248
    result = SDL_RectEmpty((const SDL_Rect *)&rect);
aschiffler@5948
  1249
    _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
aschiffler@5948
  1250
    
aschiffler@5948
  1251
    // Empty case
aschiffler@5948
  1252
    for (w=-1; w<2; w++) {
aschiffler@5948
  1253
        for (h=-1; h<2; h++) {
aschiffler@5948
  1254
            if ((w != 1) || (h != 1)) {
aschiffler@5948
  1255
                refRect.x=RandomIntegerInRange(-1024, 1024);
aschiffler@5948
  1256
                refRect.y=RandomIntegerInRange(-1024, 1024);
aschiffler@5948
  1257
                refRect.w=w;
aschiffler@5948
  1258
                refRect.h=h;
aschiffler@5948
  1259
                expectedResult = SDL_TRUE;
aschiffler@5948
  1260
                rect = refRect;
aschiffler@5948
  1261
                result = SDL_RectEmpty((const SDL_Rect *)&rect);
aschiffler@5948
  1262
                _validateRectEmptyResults(result, expectedResult, &rect, &refRect);
aschiffler@5948
  1263
            }
aschiffler@5948
  1264
        }
aschiffler@5948
  1265
    }
aschiffler@5948
  1266
}
aschiffler@5948
  1267
aschiffler@5948
  1268
/*!
aschiffler@5948
  1269
 * \brief Negative tests against SDL_RectEmpty() with invalid parameters
aschiffler@5948
  1270
 *
aschiffler@5948
  1271
 * \sa
aschiffler@5948
  1272
 * http://wiki.libsdl.org/moin.cgi/SDL_RectEmpty
aschiffler@5948
  1273
 */
aschiffler@5948
  1274
int rect_testRectEmptyParam(void *arg)
aschiffler@5948
  1275
{
aschiffler@5948
  1276
    SDL_Rect rect;
aschiffler@5948
  1277
    SDL_bool result;
aschiffler@5948
  1278
aschiffler@5948
  1279
    // invalid parameter combinations
aschiffler@5948
  1280
    result = SDL_RectEmpty((const SDL_Rect *)NULL);
aschiffler@5948
  1281
    AssertTrue(result = SDL_TRUE, "Function did not return TRUE when 1st parameter was NULL"); 
aschiffler@5948
  1282
}