File: kis_fixed_point_maths_test.cpp

package info (click to toggle)
calligra 1%3A2.8.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 397,516 kB
  • ctags: 185,064
  • sloc: cpp: 1,505,875; python: 45,987; ansic: 28,421; xml: 26,472; sh: 23,986; java: 18,214; objc: 3,965; makefile: 2,816; perl: 2,792; yacc: 2,601; lex: 1,410; sql: 903; ruby: 737; asm: 236; lisp: 121
file content (114 lines) | stat: -rw-r--r-- 3,395 bytes parent folder | download
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
/*
 *  Copyright (c) 2012 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_fixed_point_maths_test.h"

#include <qtest_kde.h>

#include "kis_fixed_point_maths.h"


void KisFixedPointMathsTest::testOperators()
{
    KisFixedPoint fp1(1);
    KisFixedPoint fp2(2);
    KisFixedPoint fp3(qreal(2.5));
    KisFixedPoint fp4(qreal(2.0));

    QVERIFY(fp1 < fp2);
    QVERIFY(fp2 < fp3);

    QVERIFY(fp2 > fp1);
    QVERIFY(fp3 > fp2);

    QVERIFY(fp1 != fp2);
    QVERIFY(fp2 == fp4);

    QCOMPARE(fp1 + fp2, KisFixedPoint(3));
    QCOMPARE(fp1 - fp2, KisFixedPoint(-1));
    QCOMPARE(fp1 * fp2, KisFixedPoint(2));
    QCOMPARE(fp2 * fp3, KisFixedPoint(5));
    QCOMPARE(fp1 / fp2, KisFixedPoint(qreal(0.5)));
    QCOMPARE(fp1 / fp3, KisFixedPoint(qreal(0.4)));
    QCOMPARE(fp2 / fp3, KisFixedPoint(qreal(0.8)));
}

void KisFixedPointMathsTest::testOperatorsNegative()
{
    KisFixedPoint fp1(qreal(-2.5));
    KisFixedPoint fp2(2);
    KisFixedPoint fp3(-3);

    QCOMPARE(fp1 + fp2, KisFixedPoint(qreal(-0.5)));
    QCOMPARE(fp1 - fp2, KisFixedPoint(qreal(-4.5)));
    QCOMPARE(fp1 * fp2, KisFixedPoint(-5));
    QCOMPARE(fp1 * fp3, KisFixedPoint(qreal(7.5)));

    QCOMPARE(fp2 / fp1, KisFixedPoint(qreal(-0.8)));
    QCOMPARE(fp1 / fp2, KisFixedPoint(qreal(-1.25)));
    QCOMPARE(fp3 / fp1, KisFixedPoint(qreal(1.2)));
}

void KisFixedPointMathsTest::testConversions()
{
    KisFixedPoint fp1(qreal(2.5));
    KisFixedPoint fp2(qreal(2.73));
    KisFixedPoint fp3(qreal(2.34));

    QCOMPARE(fp1.toInt(), 2);
    QCOMPARE(fp1.toIntRound(), 3);
    QCOMPARE(fp1.toIntFloor(), 2);
    QCOMPARE(fp1.toIntCeil(), 3);
    QCOMPARE(fp1.toFloat(), qreal(2.5));

    QCOMPARE(fp2.toInt(), 2);
    QCOMPARE(fp2.toIntRound(), 3);
    QCOMPARE(fp2.toIntFloor(), 2);
    QCOMPARE(fp2.toIntCeil(), 3);
    QCOMPARE(fp2.toFloat(), qreal(698.0/256.0));

    QCOMPARE(fp3.toInt(), 2);
    QCOMPARE(fp3.toIntRound(), 2);
    QCOMPARE(fp3.toIntFloor(), 2);
    QCOMPARE(fp3.toIntCeil(), 3);
    QCOMPARE(fp3.toFloat(), qreal(599.0/256.0));
}

void KisFixedPointMathsTest::testConversionsNegative()
{
    KisFixedPoint fp1(qreal(-2.5));
    KisFixedPoint fp2(qreal(-2.73));
    KisFixedPoint fp3(qreal(-2.34));

    QCOMPARE(fp1.toInt(), -2);
    QCOMPARE(fp1.toIntRound(), -3);
    QCOMPARE(fp1.toIntFloor(), -3);
    QCOMPARE(fp1.toIntCeil(), -2);

    QCOMPARE(fp2.toInt(), -2);
    QCOMPARE(fp2.toIntRound(), -3);
    QCOMPARE(fp2.toIntFloor(), -3);
    QCOMPARE(fp2.toIntCeil(), -2);

    QCOMPARE(fp3.toInt(), -2);
    QCOMPARE(fp3.toIntRound(), -2);
    QCOMPARE(fp3.toIntFloor(), -3);
    QCOMPARE(fp3.toIntCeil(), -2);
}

QTEST_KDEMAIN(KisFixedPointMathsTest, GUI)