File: coordinate_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (182 lines) | stat: -rw-r--r-- 5,886 bytes parent folder | download | duplicates (2)
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
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.math.CoordinateTest');
goog.setTestOnly('goog.math.CoordinateTest');

goog.require('goog.math.Coordinate');
goog.require('goog.testing.jsunit');

function testCoordinate1() {
  var dim1 = new goog.math.Coordinate();
  assertEquals(0, dim1.x);
  assertEquals(0, dim1.y);
  assertEquals('(0, 0)', dim1.toString());
}

function testCoordinate2() {
  var dim2 = new goog.math.Coordinate(10);
  assertEquals(10, dim2.x);
  assertEquals(0, dim2.y);
  assertEquals('(10, 0)', dim2.toString());
}

function testCoordinate3() {
  var dim3 = new goog.math.Coordinate(10, 20);
  assertEquals(10, dim3.x);
  assertEquals(20, dim3.y);
  assertEquals('(10, 20)', dim3.toString());
}

function testCoordinate4() {
  var dim4 = new goog.math.Coordinate(10.5, 20.897);
  assertEquals(10.5, dim4.x);
  assertEquals(20.897, dim4.y);
  assertEquals('(10.5, 20.897)', dim4.toString());
}

function testCoordinate5() {
  var dim5 = new goog.math.Coordinate(NaN, 1000);
  assertTrue(isNaN(dim5.x));
  assertEquals(1000, dim5.y);
  assertEquals('(NaN, 1000)', dim5.toString());
}

function testCoordinateSquaredDistance() {
  var a = new goog.math.Coordinate(7, 11);
  var b = new goog.math.Coordinate(3, -1);
  assertEquals(160, goog.math.Coordinate.squaredDistance(a, b));
}

function testCoordinateDistance() {
  var a = new goog.math.Coordinate(-2, -3);
  var b = new goog.math.Coordinate(2, 0);
  assertEquals(5, goog.math.Coordinate.distance(a, b));
}

function testCoordinateMagnitude() {
  var a = new goog.math.Coordinate(5, 5);
  assertEquals(Math.sqrt(50), goog.math.Coordinate.magnitude(a));
}

function testCoordinateAzimuth() {
  var a = new goog.math.Coordinate(5, 5);
  assertEquals(45, goog.math.Coordinate.azimuth(a));
}

function testCoordinateClone() {
  var c = new goog.math.Coordinate();
  assertEquals(c.toString(), c.clone().toString());
  c.x = -12;
  c.y = 13;
  assertEquals(c.toString(), c.clone().toString());
}

function testCoordinateEquals() {
  var a = new goog.math.Coordinate(1, 2);

  assertFalse(a.equals(null));
  assertFalse(a.equals({}));
  assertFalse(a.equals(new goog.math.Coordinate(1, 3)));
  assertFalse(a.equals(new goog.math.Coordinate(2, 2)));

  assertTrue(a.equals(a));
  assertTrue(a.equals(new goog.math.Coordinate(1, 2)));
}

function testCoordinateDifference() {
  assertObjectEquals(
      new goog.math.Coordinate(3, -40),
      goog.math.Coordinate.difference(
          new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50)));
}

function testCoordinateSum() {
  assertObjectEquals(
      new goog.math.Coordinate(7, 60),
      goog.math.Coordinate.sum(
          new goog.math.Coordinate(5, 10), new goog.math.Coordinate(2, 50)));
}

function testCoordinateCeil() {
  var c = new goog.math.Coordinate(5.2, 7.6);
  assertObjectEquals(new goog.math.Coordinate(6, 8), c.ceil());
  c = new goog.math.Coordinate(-1.2, -3.9);
  assertObjectEquals(new goog.math.Coordinate(-1, -3), c.ceil());
}

function testCoordinateFloor() {
  var c = new goog.math.Coordinate(5.2, 7.6);
  assertObjectEquals(new goog.math.Coordinate(5, 7), c.floor());
  c = new goog.math.Coordinate(-1.2, -3.9);
  assertObjectEquals(new goog.math.Coordinate(-2, -4), c.floor());
}

function testCoordinateRound() {
  var c = new goog.math.Coordinate(5.2, 7.6);
  assertObjectEquals(new goog.math.Coordinate(5, 8), c.round());
  c = new goog.math.Coordinate(-1.2, -3.9);
  assertObjectEquals(new goog.math.Coordinate(-1, -4), c.round());
}

function testCoordinateTranslateCoordinate() {
  var c = new goog.math.Coordinate(10, 20);
  var t = new goog.math.Coordinate(5, 10);
  // The translate function modifies the coordinate instead of
  // returning a new one.
  assertEquals(c, c.translate(t));
  assertObjectEquals(new goog.math.Coordinate(15, 30), c);
}

function testCoordinateTranslateXY() {
  var c = new goog.math.Coordinate(10, 20);
  // The translate function modifies the coordinate instead of
  // returning a new one.
  assertEquals(c, c.translate(25, 5));
  assertObjectEquals(new goog.math.Coordinate(35, 25), c);
}

function testCoordinateTranslateX() {
  var c = new goog.math.Coordinate(10, 20);
  // The translate function modifies the coordinate instead of
  // returning a new one.
  assertEquals(c, c.translate(5));
  assertObjectEquals(new goog.math.Coordinate(15, 20), c);
}

function testCoordinateScaleXY() {
  var c = new goog.math.Coordinate(10, 15);
  // The scale function modifies the coordinate instead of returning a new one.
  assertEquals(c, c.scale(2, 3));
  assertObjectEquals(new goog.math.Coordinate(20, 45), c);
}

function testCoordinateScaleFactor() {
  var c = new goog.math.Coordinate(10, 15);
  // The scale function modifies the coordinate instead of returning a new one.
  assertEquals(c, c.scale(2));
  assertObjectEquals(new goog.math.Coordinate(20, 30), c);
}

function testCoordinateRotateRadians() {
  var c = new goog.math.Coordinate(15, 75);
  c.rotateRadians(Math.PI / 2, new goog.math.Coordinate(10, 70));
  assertObjectEquals(new goog.math.Coordinate(5, 75), c);
}

function testCoordinateRotateDegrees() {
  var c = new goog.math.Coordinate(15, 75);
  c.rotateDegrees(90, new goog.math.Coordinate(10, 70));
  assertObjectEquals(new goog.math.Coordinate(5, 75), c);
}