summaryrefslogtreecommitdiff
path: root/tests/boundingBoxTest.js
blob: 707059bec6d97da675f8b3cb99155fb26ff74a6d (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
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2020 Marcus Lundblad
 *
 * GNOME Maps 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.
 *
 * GNOME Maps 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 GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Marcus Lundblad <ml@update.uu.se>
 */

const JsUnit = imports.jsUnit;

import {BoundingBox} from './boundingBox.js';
import * as Constants from './constants.js';

function main() {
    constructTest();
    copyTest();
    setTest();
    getCenterTest();
    composeTest();
    extendTest();
    isValidTest();
    coversTest();
}

function constructTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });

    JsUnit.assertEquals(60.0, bbox.top);
    JsUnit.assertEquals(15.0, bbox.left);
    JsUnit.assertEquals(59.0, bbox.bottom);
    JsUnit.assertEquals(16.0, bbox.right);

    // test default values
    bbox = new BoundingBox();

    JsUnit.assertEquals(Constants.MIN_LATITUDE, bbox.top);
    JsUnit.assertEquals(Constants.MAX_LONGITUDE, bbox.left);
    JsUnit.assertEquals(Constants.MAX_LATITUDE, bbox.bottom);
    JsUnit.assertEquals(Constants.MIN_LONGITUDE, bbox.right);
}

function copyTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });
    let copy = bbox.copy();

    // update original box
    bbox.top = 65.0;
    bbox.left = 16.0;
    bbox.bottom = 55.0;
    bbox.right = 17.0;

    // copy should be uneffected
    JsUnit.assertEquals(60.0, copy.top);
    JsUnit.assertEquals(15.0, copy.left);
    JsUnit.assertEquals(59.0, copy.bottom);
    JsUnit.assertEquals(16.0, copy.right);
}

function setTest() {
    let bbox = new BoundingBox();

    bbox.top = 0;
    bbox.left = 0;
    bbox.bottom = -10;
    bbox.right = 10;

    JsUnit.assertEquals(0.0, bbox.top);
    JsUnit.assertEquals(0.0, bbox.left);
    JsUnit.assertEquals(-10.0, bbox.bottom);
    JsUnit.assertEquals(10.0, bbox.right);
}

function getCenterTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });
    let center = bbox.getCenter();

    JsUnit.assertTrue(center instanceof Array);
    JsUnit.assertEquals(2, center.length);
    JsUnit.assertEquals(15.5, center[0]);
    JsUnit.assertEquals(59.5, center[1]);
}

function composeTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });
    let other = new BoundingBox({ top: 60.0, left: 14.0,
                                  bottom: 59.0, right: 15.0 });

    bbox.compose(other);

    JsUnit.assertEquals(60.0, bbox.top);
    JsUnit.assertEquals(14.0, bbox.left);
    JsUnit.assertEquals(59.0, bbox.bottom);
    JsUnit.assertEquals(16.0, bbox.right);
}

function extendTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });

    bbox.extend(58.0, 14.0);

    JsUnit.assertEquals(60.0, bbox.top);
    JsUnit.assertEquals(14.0, bbox.left);
    JsUnit.assertEquals(58.0, bbox.bottom);
    JsUnit.assertEquals(16.0, bbox.right);
}

function isValidTest() {
    let valid = new BoundingBox({ top: 60.0, left: 15.0,
                                  bottom: 59.0, right: 16.0 });

    JsUnit.assertTrue(valid.isValid());

    let unset = new BoundingBox();

    JsUnit.assertFalse(unset.isValid());

    let overflowNorth = new BoundingBox({ top: 100.0, left: 15.0,
                                          bottom: 0.0, right: 16.0 });

    JsUnit.assertFalse(overflowNorth.isValid());

    let flipped = new BoundingBox({ top: 59.0, left: 16.0,
                                    bottom: 60.0, right: 15.0 });

    JsUnit.assertFalse(flipped.isValid());
}

function coversTest() {
    let bbox = new BoundingBox({ top: 60.0, left: 15.0,
                                 bottom: 59.0, right: 16.0 });

    JsUnit.assertTrue(bbox.covers(59.5, 15.5));
    JsUnit.assertFalse(bbox.covers(0.0, 0.0));
    JsUnit.assertFalse(bbox.covers(59.0, -180.0));
}

main();