aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/math/size_test.html
blob: 7ab7abae31549d7f515559822193ab83bbf58197 (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
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.math.Size</title>
<script src="../base.js"></script>
<script>
goog.require('goog.math.Size');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>

function testSize1() {
  var s = new goog.math.Size(undefined, undefined);
  assertUndefined(s.width);
  assertUndefined(s.height);
  assertEquals('(undefined x undefined)', s.toString());
}

function testSize3() {
  var s = new goog.math.Size(10, 20);
  assertEquals(10, s.width);
  assertEquals(20, s.height);
  assertEquals('(10 x 20)', s.toString());
}

function testSize4() {
  var s = new goog.math.Size(10.5, 20.897);
  assertEquals(10.5, s.width);
  assertEquals(20.897, s.height);
  assertEquals('(10.5 x 20.897)', s.toString());
}

function testSizeClone() {
  var s = new goog.math.Size(undefined, undefined);
  assertEquals(s.toString(), s.clone().toString());
  s.width = 4;
  s.height = 5;
  assertEquals(s.toString(), s.clone().toString());
}

function testSizeEquals() {
  var a = new goog.math.Size(4, 5);

  assertTrue(goog.math.Size.equals(a, a));
  assertFalse(goog.math.Size.equals(a, null));
  assertFalse(goog.math.Size.equals(null, a));

  var b = new goog.math.Size(4, 5);
  var c = new goog.math.Size(4, 6);
  assertTrue(goog.math.Size.equals(a, b));
  assertFalse(goog.math.Size.equals(a, c));
}

function testSizeArea() {
  var s = new goog.math.Size(4, 5);
  assertEquals(20, s.area());
}

function testSizePerimeter() {
  var s = new goog.math.Size(4, 5);
  assertEquals(18, s.perimeter());
}

function testSizeAspectRatio() {
  var s = new goog.math.Size(undefined, undefined);
  assertNaN(s.aspectRatio());

  s.width = 4;
  s.height = 0;
  assertEquals(Infinity, s.aspectRatio());

  s.height = 5;
  assertEquals(0.8, s.aspectRatio());
}

function testSizeFitsInside() {
  var target = new goog.math.Size(10, 10);

  var a = new goog.math.Size(5, 8);
  var b = new goog.math.Size(5, 12);
  var c = new goog.math.Size(19, 7);


  assertTrue(a.fitsInside(target));
  assertFalse(b.fitsInside(target));
  assertFalse(c.fitsInside(target));
}

function testSizeScaleToFit() {
  var target = new goog.math.Size(512, 640);

  var a = new goog.math.Size(1600, 1200);
  var b = new goog.math.Size(1200, 1600);
  var c = new goog.math.Size(400, 300);
  var d = new goog.math.Size(undefined, undefined);

  assertEquals('(512 x 384)', a.scaleToFit(target).toString());
  assertEquals('(480 x 640)', b.scaleToFit(target).toString());
  assertEquals('(512 x 384)', c.scaleToFit(target).toString());
  assertEquals('(512 x 640)', target.scaleToFit(target).toString());

  assertEquals('(NaN x NaN)', d.scaleToFit(target).toString());
  assertEquals('(NaN x NaN)', a.scaleToFit(d).toString());
}

function testSizeIsEmpty() {
  var s = new goog.math.Size(undefined, undefined);
  assertTrue(s.isEmpty());
  s.width = 0;
  s.height = 5;
  assertTrue(s.isEmpty());
  s.width = 4;
  assertFalse(s.isEmpty());
}

function testSizeScale() {
  var s = new goog.math.Size(4, 5);
  assertEquals('(8 x 10)', s.scale(2).toString());
  assertEquals('(0.8 x 1)', s.scale(0.1).toString());
}

function testSizeCeil() {
  var s = new goog.math.Size(2.3, 4.7);
  assertEquals('(3 x 5)', s.ceil().toString());
}

function testSizeFloor() {
  var s = new goog.math.Size(2.3, 4.7);
  assertEquals('(2 x 4)', s.floor().toString());
}

function testSizeRound() {
  var s = new goog.math.Size(2.3, 4.7);
  assertEquals('(2 x 5)', s.round().toString());
}

function testSizeGetLongest() {
  var s = new goog.math.Size(3, 4);
  assertEquals(4, s.getLongest());

  s.height = 3;
  assertEquals(3, s.getLongest());

  s.height = 2;
  assertEquals(3, s.getLongest());

  assertNaN(new goog.math.Size(undefined, undefined).getLongest());
}

function testSizeGetShortest() {
  var s = new goog.math.Size(3, 4);
  assertEquals(3, s.getShortest());

  s.height = 3;
  assertEquals(3, s.getShortest());

  s.height = 2;
  assertEquals(2, s.getShortest());

  assertNaN(new goog.math.Size(undefined, undefined).getShortest());
}

</script>
</body>
</html>