aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/Intersection/QuarticRoot_Test.cpp
blob: 48431c8f0a04ac791e215556799e5242f896cd0d (plain)
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
#include "CubicUtilities.h"
#include "Intersection_Tests.h"
#include "QuadraticUtilities.h"
#include "QuarticRoot.h"

double mulA[] = {-3, -1, 1, 3};
size_t mulACount = sizeof(mulA) / sizeof(mulA[0]);
double rootB[] = {-9, -6, -3, -1, 0, 1, 3, 6, 9};
size_t rootBCount = sizeof(rootB) / sizeof(rootB[0]);
double rootC[] = {-8, -6, -2, -1, 0, 1, 2, 6, 8};
size_t rootCCount = sizeof(rootC) / sizeof(rootC[0]);
double rootD[] = {-7, -4, -1, 0, 1, 2, 5};
size_t rootDCount = sizeof(rootD) / sizeof(rootD[0]);
double rootE[] = {-5, -1, 0, 1, 7};
size_t rootECount = sizeof(rootE) / sizeof(rootE[0]);


static void quadraticTest(bool limit) {
    // (x - a)(x - b) == x^2 - (a + b)x + ab
    for (size_t aIndex = 0; aIndex < mulACount; ++aIndex) {
        for (size_t bIndex = 0; bIndex < rootBCount; ++bIndex) {
            for (size_t cIndex = 0; cIndex < rootCCount; ++cIndex) {
                const double A = mulA[aIndex];
                double B = rootB[bIndex];
                double C = rootC[cIndex];
                if (limit) {
                    B = (B - 6) / 12;
                    C = (C - 6) / 12;
                }
                const double b = A * (B + C);
                const double c = A * B * C;
                double roots[2];
                const int rootCount = limit ? quadraticRootsValidT(A, b, c, roots)
                    : quadraticRootsReal(A, b, c, roots);
                int expected;
                if (limit) {
                    expected = B <= 0 && B >= -1;
                    expected += B != C && C <= 0 && C >= -1;
                } else {
                    expected = 1 + (B != C);
                }
                SkASSERT(rootCount == expected);
                if (!rootCount) {
                    continue;
                }
                SkASSERT(approximately_equal(roots[0], -B)
                        || approximately_equal(roots[0], -C));
                if (expected > 1) {
                    SkASSERT(!approximately_equal(roots[0], roots[1]));
                    SkASSERT(approximately_equal(roots[1], -B)
                            || approximately_equal(roots[1], -C));
                }
            }
        }
    }
}

static void testOneCubic(bool limit, size_t aIndex, size_t bIndex, size_t cIndex, size_t dIndex) {
    const double A = mulA[aIndex];
    double B = rootB[bIndex];
    double C = rootC[cIndex];
    double D = rootD[dIndex];
    if (limit) {
        B = (B - 6) / 12;
        C = (C - 6) / 12;
        D = (C - 2) / 6;
    }
    const double b = A * (B + C + D);
    const double c = A * (B * C + C * D + B * D);
    const double d = A * B * C * D;
    double roots[3];
    const int rootCount = limit ? cubicRootsValidT(A, b, c, d, roots)
            : cubicRootsReal(A, b, c, d, roots);
    int expected;
    if (limit) {
        expected = B <= 0 && B >= -1;
        expected += B != C && C <= 0 && C >= -1;
        expected += B != D && C != D && D <= 0 && D >= -1;
    } else {
        expected = 1 + (B != C) + (B != D && C != D);
    }
    SkASSERT(rootCount == expected);
    if (!rootCount) {
        return;
    }
    SkASSERT(approximately_equal(roots[0], -B)
            || approximately_equal(roots[0], -C)
            || approximately_equal(roots[0], -D));
    if (expected <= 1) {
        return;
    }
    SkASSERT(!approximately_equal(roots[0], roots[1]));
    SkASSERT(approximately_equal(roots[1], -B)
            || approximately_equal(roots[1], -C)
            || approximately_equal(roots[1], -D));
    if (expected <= 2) {
        return;
    }
    SkASSERT(!approximately_equal(roots[0], roots[2])
            && !approximately_equal(roots[1], roots[2]));
    SkASSERT(approximately_equal(roots[2], -B)
            || approximately_equal(roots[2], -C)
            || approximately_equal(roots[2], -D));
}

static void cubicTest(bool limit) {
    // (x - a)(x - b)(x - c) == x^3 - (a + b + c)x^2 + (ab + bc + ac)x - abc
    for (size_t aIndex = 0; aIndex < mulACount; ++aIndex) {
        for (size_t bIndex = 0; bIndex < rootBCount; ++bIndex) {
            for (size_t cIndex = 0; cIndex < rootCCount; ++cIndex) {
                for (size_t dIndex = 0; dIndex < rootDCount; ++dIndex) {
                    testOneCubic(limit, aIndex, bIndex, cIndex, dIndex);
                }
            }
        }
    }
}

static void testOneQuartic(size_t aIndex, size_t bIndex, size_t cIndex, size_t dIndex,
        size_t eIndex) {
    const double A = mulA[aIndex];
    const double B = rootB[bIndex];
    const double C = rootC[cIndex];
    const double D = rootD[dIndex];
    const double E = rootE[eIndex];
    const double b = A * (B + C + D + E);
    const double c = A * (B * C + C * D + B * D + B * E + C * E + D * E);
    const double d = A * (B * C * D + B * C * E + B * D * E + C * D * E);
    const double e = A * B * C * D * E;
    double roots[4];
    bool oneHint = approximately_zero(A + b + c + d + e);
    int rootCount = reducedQuarticRoots(A, b, c, d, e, oneHint, roots);
    if (rootCount < 0) {
        rootCount = quarticRootsReal(0, A, b, c, d, e, roots);
    }
    const int expected = 1 + (B != C) + (B != D && C != D) + (B != E && C != E && D != E);
    SkASSERT(rootCount == expected);
    SkASSERT(AlmostEqualUlps(roots[0], -B)
            || AlmostEqualUlps(roots[0], -C)
            || AlmostEqualUlps(roots[0], -D)
            || AlmostEqualUlps(roots[0], -E));
    if (expected <= 1) {
        return;
    }
    SkASSERT(!AlmostEqualUlps(roots[0], roots[1]));
    SkASSERT(AlmostEqualUlps(roots[1], -B)
            || AlmostEqualUlps(roots[1], -C)
            || AlmostEqualUlps(roots[1], -D)
            || AlmostEqualUlps(roots[1], -E));
    if (expected <= 2) {
        return;
    }
    SkASSERT(!AlmostEqualUlps(roots[0], roots[2])
            && !AlmostEqualUlps(roots[1], roots[2]));
    SkASSERT(AlmostEqualUlps(roots[2], -B)
            || AlmostEqualUlps(roots[2], -C)
            || AlmostEqualUlps(roots[2], -D)
            || AlmostEqualUlps(roots[2], -E));
    if (expected <= 3) {
        return;
    }
    SkASSERT(!AlmostEqualUlps(roots[0], roots[3])
            && !AlmostEqualUlps(roots[1], roots[3])
            && !AlmostEqualUlps(roots[2], roots[3]));
    SkASSERT(AlmostEqualUlps(roots[3], -B)
            || AlmostEqualUlps(roots[3], -C)
            || AlmostEqualUlps(roots[3], -D)
            || AlmostEqualUlps(roots[3], -E));
}

static void quarticTest() {
    // (x - a)(x - b)(x - c)(x - d) == x^4 - (a + b + c + d)x^3
    //   + (ab + bc + cd + ac + bd + cd)x^2 - (abc + bcd + abd + acd) * x + abcd
    for (size_t aIndex = 0; aIndex < mulACount; ++aIndex) {
        for (size_t bIndex = 0; bIndex < rootBCount; ++bIndex) {
            for (size_t cIndex = 0; cIndex < rootCCount; ++cIndex) {
                for (size_t dIndex = 0; dIndex < rootDCount; ++dIndex) {
                    for (size_t eIndex = 0; eIndex < rootECount; ++eIndex) {
                        testOneQuartic(aIndex, bIndex, cIndex, dIndex, eIndex);
                    }
                }
            }
        }
    }
}

void QuarticRoot_Test() {
    testOneCubic(false, 0, 5, 5, 4);
    testOneQuartic(0, 0, 2, 4, 3);
    quadraticTest(true);
    quadraticTest(false);
    cubicTest(true);
    cubicTest(false);
    quarticTest();
}