size_test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.math.SizeTest');
  15. goog.setTestOnly('goog.math.SizeTest');
  16. goog.require('goog.math.Size');
  17. goog.require('goog.testing.jsunit');
  18. function testSize1() {
  19. var s = new goog.math.Size(undefined, undefined);
  20. assertUndefined(s.width);
  21. assertUndefined(s.height);
  22. assertEquals('(undefined x undefined)', s.toString());
  23. }
  24. function testSize3() {
  25. var s = new goog.math.Size(10, 20);
  26. assertEquals(10, s.width);
  27. assertEquals(20, s.height);
  28. assertEquals('(10 x 20)', s.toString());
  29. }
  30. function testSize4() {
  31. var s = new goog.math.Size(10.5, 20.897);
  32. assertEquals(10.5, s.width);
  33. assertEquals(20.897, s.height);
  34. assertEquals('(10.5 x 20.897)', s.toString());
  35. }
  36. function testSizeClone() {
  37. var s = new goog.math.Size(undefined, undefined);
  38. assertEquals(s.toString(), s.clone().toString());
  39. s.width = 4;
  40. s.height = 5;
  41. assertEquals(s.toString(), s.clone().toString());
  42. }
  43. function testSizeEquals() {
  44. var a = new goog.math.Size(4, 5);
  45. assertTrue(goog.math.Size.equals(a, a));
  46. assertFalse(goog.math.Size.equals(a, null));
  47. assertFalse(goog.math.Size.equals(null, a));
  48. var b = new goog.math.Size(4, 5);
  49. var c = new goog.math.Size(4, 6);
  50. assertTrue(goog.math.Size.equals(a, b));
  51. assertFalse(goog.math.Size.equals(a, c));
  52. }
  53. function testSizeArea() {
  54. var s = new goog.math.Size(4, 5);
  55. assertEquals(20, s.area());
  56. }
  57. function testSizePerimeter() {
  58. var s = new goog.math.Size(4, 5);
  59. assertEquals(18, s.perimeter());
  60. }
  61. function testSizeAspectRatio() {
  62. var s = new goog.math.Size(undefined, undefined);
  63. assertNaN(s.aspectRatio());
  64. s.width = 4;
  65. s.height = 0;
  66. assertEquals(Infinity, s.aspectRatio());
  67. s.height = 5;
  68. assertEquals(0.8, s.aspectRatio());
  69. }
  70. function testSizeFitsInside() {
  71. var target = new goog.math.Size(10, 10);
  72. var a = new goog.math.Size(5, 8);
  73. var b = new goog.math.Size(5, 12);
  74. var c = new goog.math.Size(19, 7);
  75. assertTrue(a.fitsInside(target));
  76. assertFalse(b.fitsInside(target));
  77. assertFalse(c.fitsInside(target));
  78. }
  79. function testSizeScaleToCover() {
  80. var target = new goog.math.Size(512, 640);
  81. var a = new goog.math.Size(1000, 1600);
  82. var b = new goog.math.Size(1600, 1000);
  83. var c = new goog.math.Size(500, 800);
  84. var d = new goog.math.Size(undefined, undefined);
  85. assertEquals('(512 x 819.2)', a.scaleToCover(target).toString());
  86. assertEquals('(1024 x 640)', b.scaleToCover(target).toString());
  87. assertEquals('(512 x 819.2)', c.scaleToCover(target).toString());
  88. assertEquals('(512 x 640)', target.scaleToCover(target).toString());
  89. assertEquals('(NaN x NaN)', d.scaleToCover(target).toString());
  90. assertEquals('(NaN x NaN)', a.scaleToCover(d).toString());
  91. }
  92. function testSizeScaleToFit() {
  93. var target = new goog.math.Size(512, 640);
  94. var a = new goog.math.Size(1600, 1200);
  95. var b = new goog.math.Size(1200, 1600);
  96. var c = new goog.math.Size(400, 300);
  97. var d = new goog.math.Size(undefined, undefined);
  98. assertEquals('(512 x 384)', a.scaleToFit(target).toString());
  99. assertEquals('(480 x 640)', b.scaleToFit(target).toString());
  100. assertEquals('(512 x 384)', c.scaleToFit(target).toString());
  101. assertEquals('(512 x 640)', target.scaleToFit(target).toString());
  102. assertEquals('(NaN x NaN)', d.scaleToFit(target).toString());
  103. assertEquals('(NaN x NaN)', a.scaleToFit(d).toString());
  104. }
  105. function testSizeIsEmpty() {
  106. var s = new goog.math.Size(undefined, undefined);
  107. assertTrue(s.isEmpty());
  108. s.width = 0;
  109. s.height = 5;
  110. assertTrue(s.isEmpty());
  111. s.width = 4;
  112. assertFalse(s.isEmpty());
  113. }
  114. function testSizeScaleFactor() {
  115. var s = new goog.math.Size(4, 5);
  116. assertEquals('(8 x 10)', s.scale(2).toString());
  117. assertEquals('(0.8 x 1)', s.scale(0.1).toString());
  118. }
  119. function testSizeCeil() {
  120. var s = new goog.math.Size(2.3, 4.7);
  121. assertEquals('(3 x 5)', s.ceil().toString());
  122. }
  123. function testSizeFloor() {
  124. var s = new goog.math.Size(2.3, 4.7);
  125. assertEquals('(2 x 4)', s.floor().toString());
  126. }
  127. function testSizeRound() {
  128. var s = new goog.math.Size(2.3, 4.7);
  129. assertEquals('(2 x 5)', s.round().toString());
  130. }
  131. function testSizeGetLongest() {
  132. var s = new goog.math.Size(3, 4);
  133. assertEquals(4, s.getLongest());
  134. s.height = 3;
  135. assertEquals(3, s.getLongest());
  136. s.height = 2;
  137. assertEquals(3, s.getLongest());
  138. assertNaN(new goog.math.Size(undefined, undefined).getLongest());
  139. }
  140. function testSizeGetShortest() {
  141. var s = new goog.math.Size(3, 4);
  142. assertEquals(3, s.getShortest());
  143. s.height = 3;
  144. assertEquals(3, s.getShortest());
  145. s.height = 2;
  146. assertEquals(2, s.getShortest());
  147. assertNaN(new goog.math.Size(undefined, undefined).getShortest());
  148. }
  149. function testSizeScaleXY() {
  150. var s = new goog.math.Size(5, 10);
  151. assertEquals('(20 x 30)', s.scale(4, 3).toString());
  152. }