summaryrefslogtreecommitdiffstats
path: root/tests/polynomial-test.cpp
blob: 699820aa4dab3865c52a2b6392b824e019ca9a89 (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
/** @file
 * @brief Unit tests for Polynomial and related functions.
 * Uses the Google Testing Framework
 *//*
 * Authors:
 *   Krzysztof Kosiński <tweenk.pl@gmail.com>
 *
 * Copyright 2015-2019 Authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */

#include "testing.h"
#include <iostream>
#include <glib.h>

#include <2geom/polynomial.h>

using namespace Geom;

TEST(PolynomialTest, SolveQuadratic) {
    for (unsigned i = 0; i < 1000; ++i) {
        Coord x1 = g_random_double_range(-100, 100);
        Coord x2 = g_random_double_range(-100, 100);

        Coord a = g_random_double_range(-10, 10);
        Coord b = -a * (x1 + x2);
        Coord c = a * x1 * x2;

        std::vector<Coord> result = solve_quadratic(a, b, c);

        EXPECT_EQ(result.size(), 2u);
        if (x1 < x2) {
            EXPECT_FLOAT_EQ(result[0], x1);
            EXPECT_FLOAT_EQ(result[1], x2);
        } else {
            EXPECT_FLOAT_EQ(result[0], x2);
            EXPECT_FLOAT_EQ(result[1], x1);
        }
    }
}

TEST(PolynomialTest, SolvePathologicalQuadratic) {
    std::vector<Coord> r;

    r = solve_quadratic(1, -1e9, 1);
    ASSERT_EQ(r.size(), 2u);
    EXPECT_FLOAT_EQ(r[0], 1e-9);
    EXPECT_FLOAT_EQ(r[1], 1e9);

    r = solve_quadratic(1, -4, 3.999999);
    ASSERT_EQ(r.size(), 2u);
    EXPECT_FLOAT_EQ(r[0], 1.999);
    EXPECT_FLOAT_EQ(r[1], 2.001);

    r = solve_quadratic(1, 0, -4);
    ASSERT_EQ(r.size(), 2u);
    EXPECT_FLOAT_EQ(r[0], -2);
    EXPECT_FLOAT_EQ(r[1], 2);

    r = solve_quadratic(1, 0, -16);
    ASSERT_EQ(r.size(), 2u);
    EXPECT_FLOAT_EQ(r[0], -4);
    EXPECT_FLOAT_EQ(r[1], 4);

    r = solve_quadratic(1, 0, -100);
    ASSERT_EQ(r.size(), 2u);
    EXPECT_FLOAT_EQ(r[0], -10);
    EXPECT_FLOAT_EQ(r[1], 10);
}

TEST(PolynomialTest, SolveCubic) {
    for (unsigned i = 0; i < 1000; ++i) {
        Coord x1 = g_random_double_range(-100, 100);
        Coord x2 = g_random_double_range(-100, 100);
        Coord x3 = g_random_double_range(-100, 100);

        Coord a = g_random_double_range(-10, 10);
        Coord b = -a * (x1 + x2 + x3);
        Coord c = a * (x1*x2 + x2*x3 + x1*x3);
        Coord d = -a * x1 * x2 * x3;

        std::vector<Coord> result = solve_cubic(a, b, c, d);
        std::vector<Coord> x(3); x[0] = x1; x[1] = x2; x[2] = x3;
        std::sort(x.begin(), x.end());

        ASSERT_EQ(result.size(), 3u);
        EXPECT_FLOAT_EQ(result[0], x[0]);
        EXPECT_FLOAT_EQ(result[1], x[1]);
        EXPECT_FLOAT_EQ(result[2], x[2]);
    }

    // corner cases
    // (x^2 + 7)(x - 2)
    std::vector<Coord> r1 = solve_cubic(1, -2, 7, -14);
    EXPECT_EQ(r1.size(), 1u);
    EXPECT_FLOAT_EQ(r1[0], 2);

    // (x + 1)^2 (x-2)
    std::vector<Coord> r2 = solve_cubic(1, 0, -3, -2);
    ASSERT_EQ(r2.size(), 3u);
    EXPECT_FLOAT_EQ(r2[0], -1);
    EXPECT_FLOAT_EQ(r2[1], -1);
    EXPECT_FLOAT_EQ(r2[2], 2);
}