| 1 | /* |
| 2 | * Copyright (c) 2012, Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include "config.h" |
| 32 | |
| 33 | #include <WebCore/LayoutUnit.h> |
| 34 | |
| 35 | using namespace WebCore; |
| 36 | |
| 37 | namespace TestWebKitAPI { |
| 38 | |
| 39 | TEST(WebCoreLayoutUnit, LayoutUnitInt) |
| 40 | { |
| 41 | ASSERT_EQ(LayoutUnit(INT_MIN).toInt(), intMinForLayoutUnit); |
| 42 | ASSERT_EQ(LayoutUnit(INT_MIN / 2).toInt(), intMinForLayoutUnit); |
| 43 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit - 1).toInt(), intMinForLayoutUnit); |
| 44 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).toInt(), intMinForLayoutUnit); |
| 45 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit + 1).toInt(), intMinForLayoutUnit + 1); |
| 46 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit / 2).toInt(), intMinForLayoutUnit / 2); |
| 47 | ASSERT_EQ(LayoutUnit(-10000).toInt(), -10000); |
| 48 | ASSERT_EQ(LayoutUnit(-1000).toInt(), -1000); |
| 49 | ASSERT_EQ(LayoutUnit(-100).toInt(), -100); |
| 50 | ASSERT_EQ(LayoutUnit(-10).toInt(), -10); |
| 51 | ASSERT_EQ(LayoutUnit(-1).toInt(), -1); |
| 52 | ASSERT_EQ(LayoutUnit(0).toInt(), 0); |
| 53 | ASSERT_EQ(LayoutUnit(1).toInt(), 1); |
| 54 | ASSERT_EQ(LayoutUnit(100).toInt(), 100); |
| 55 | ASSERT_EQ(LayoutUnit(1000).toInt(), 1000); |
| 56 | ASSERT_EQ(LayoutUnit(10000).toInt(), 10000); |
| 57 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit / 2).toInt(), intMaxForLayoutUnit / 2); |
| 58 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit - 1).toInt(), intMaxForLayoutUnit - 1); |
| 59 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).toInt(), intMaxForLayoutUnit); |
| 60 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit + 1).toInt(), intMaxForLayoutUnit); |
| 61 | ASSERT_EQ(LayoutUnit(INT_MAX / 2).toInt(), intMaxForLayoutUnit); |
| 62 | ASSERT_EQ(LayoutUnit(INT_MAX).toInt(), intMaxForLayoutUnit); |
| 63 | } |
| 64 | |
| 65 | TEST(WebCoreLayoutUnit, LayoutUnitFloat) |
| 66 | { |
| 67 | const float tolerance = 1.0f / kFixedPointDenominator; |
| 68 | ASSERT_FLOAT_EQ(LayoutUnit(1.0f).toFloat(), 1.0f); |
| 69 | ASSERT_FLOAT_EQ(LayoutUnit(1.25f).toFloat(), 1.25f); |
| 70 | ASSERT_NEAR(LayoutUnit(1.1f).toFloat(), 1.1f, tolerance); |
| 71 | ASSERT_NEAR(LayoutUnit(1.33f).toFloat(), 1.33f, tolerance); |
| 72 | ASSERT_NEAR(LayoutUnit(1.3333f).toFloat(), 1.3333f, tolerance); |
| 73 | ASSERT_NEAR(LayoutUnit(1.53434f).toFloat(), 1.53434f, tolerance); |
| 74 | ASSERT_NEAR(LayoutUnit(345634).toFloat(), 345634.0f, tolerance); |
| 75 | ASSERT_NEAR(LayoutUnit(345634.12335f).toFloat(), 345634.12335f, tolerance); |
| 76 | ASSERT_NEAR(LayoutUnit(-345634.12335f).toFloat(), -345634.12335f, tolerance); |
| 77 | ASSERT_NEAR(LayoutUnit(-345634).toFloat(), -345634.0f, tolerance); |
| 78 | ASSERT_NEAR(LayoutUnit(33554432.f).toFloat(), 33554432.f, tolerance); |
| 79 | ASSERT_NEAR(LayoutUnit(-33554432.f).toFloat(), -33554432.f, tolerance); |
| 80 | ASSERT_NEAR(LayoutUnit(33554432.f).toDouble(), 33554432.f, tolerance); |
| 81 | ASSERT_NEAR(LayoutUnit(-33554432.f).toDouble(), -33554432.f, tolerance); |
| 82 | } |
| 83 | |
| 84 | TEST(WebCoreLayoutUnit, LayoutUnitRounding) |
| 85 | { |
| 86 | ASSERT_EQ(LayoutUnit(-1.9f).round(), -2); |
| 87 | ASSERT_EQ(LayoutUnit(-1.6f).round(), -2); |
| 88 | ASSERT_EQ(LayoutUnit::fromFloatRound(-1.51f).round(), -2); |
| 89 | ASSERT_EQ(LayoutUnit::fromFloatRound(-1.5f).round(), -1); |
| 90 | ASSERT_EQ(LayoutUnit::fromFloatRound(-1.49f).round(), -1); |
| 91 | ASSERT_EQ(LayoutUnit(-1.0f).round(), -1); |
| 92 | ASSERT_EQ(LayoutUnit::fromFloatRound(-0.99f).round(), -1); |
| 93 | ASSERT_EQ(LayoutUnit::fromFloatRound(-0.51f).round(), -1); |
| 94 | ASSERT_EQ(LayoutUnit::fromFloatRound(-0.50f).round(), 0); |
| 95 | ASSERT_EQ(LayoutUnit::fromFloatRound(-0.49f).round(), 0); |
| 96 | ASSERT_EQ(LayoutUnit(-0.1f).round(), 0); |
| 97 | ASSERT_EQ(LayoutUnit(0.0f).round(), 0); |
| 98 | ASSERT_EQ(LayoutUnit(0.1f).round(), 0); |
| 99 | ASSERT_EQ(LayoutUnit::fromFloatRound(0.49f).round(), 0); |
| 100 | ASSERT_EQ(LayoutUnit::fromFloatRound(0.50f).round(), 1); |
| 101 | ASSERT_EQ(LayoutUnit::fromFloatRound(0.51f).round(), 1); |
| 102 | ASSERT_EQ(LayoutUnit(0.99f).round(), 1); |
| 103 | ASSERT_EQ(LayoutUnit(1.0f).round(), 1); |
| 104 | ASSERT_EQ(LayoutUnit::fromFloatRound(1.49f).round(), 1); |
| 105 | ASSERT_EQ(LayoutUnit::fromFloatRound(1.5f).round(), 2); |
| 106 | ASSERT_EQ(LayoutUnit::fromFloatRound(1.51f).round(), 2); |
| 107 | } |
| 108 | |
| 109 | TEST(WebCoreLayoutUnit, LayoutUnitMultiplication) |
| 110 | { |
| 111 | ASSERT_EQ((LayoutUnit(1) * LayoutUnit(1)).toInt(), 1); |
| 112 | ASSERT_EQ((LayoutUnit(1) * LayoutUnit(2)).toInt(), 2); |
| 113 | ASSERT_EQ((LayoutUnit(2) * LayoutUnit(1)).toInt(), 2); |
| 114 | ASSERT_EQ((LayoutUnit(2) * LayoutUnit(0.5)).toInt(), 1); |
| 115 | ASSERT_EQ((LayoutUnit(0.5) * LayoutUnit(2)).toInt(), 1); |
| 116 | ASSERT_EQ((LayoutUnit(100) * LayoutUnit(1)).toInt(), 100); |
| 117 | |
| 118 | ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(1)).toInt(), -1); |
| 119 | ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(2)).toInt(), -2); |
| 120 | ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(1)).toInt(), -2); |
| 121 | ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(0.5)).toInt(), -1); |
| 122 | ASSERT_EQ((LayoutUnit(-0.5) * LayoutUnit(2)).toInt(), -1); |
| 123 | ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(1)).toInt(), -100); |
| 124 | |
| 125 | ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(-1)).toInt(), 1); |
| 126 | ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(-2)).toInt(), 2); |
| 127 | ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(-1)).toInt(), 2); |
| 128 | ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(-0.5)).toInt(), 1); |
| 129 | ASSERT_EQ((LayoutUnit(-0.5) * LayoutUnit(-2)).toInt(), 1); |
| 130 | ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(-1)).toInt(), 100); |
| 131 | |
| 132 | ASSERT_EQ((LayoutUnit(100) * LayoutUnit(3.33)).round(), 333); |
| 133 | ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(3.33)).round(), -333); |
| 134 | ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(-3.33)).round(), 333); |
| 135 | |
| 136 | size_t aHundredSizeT = 100; |
| 137 | ASSERT_EQ((LayoutUnit(aHundredSizeT) * LayoutUnit(1)).toInt(), 100); |
| 138 | ASSERT_EQ((aHundredSizeT * LayoutUnit(4)).toInt(), 400); |
| 139 | ASSERT_EQ((LayoutUnit(4) * aHundredSizeT).toInt(), 400); |
| 140 | |
| 141 | int quarterMax = intMaxForLayoutUnit / 4; |
| 142 | ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(2)).toInt(), quarterMax * 2); |
| 143 | ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(3)).toInt(), quarterMax * 3); |
| 144 | ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(4)).toInt(), quarterMax * 4); |
| 145 | ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(5)).toInt(), intMaxForLayoutUnit); |
| 146 | |
| 147 | size_t overflowIntSizeT = intMaxForLayoutUnit * 4; |
| 148 | ASSERT_EQ((LayoutUnit(overflowIntSizeT) * LayoutUnit(2)).toInt(), intMaxForLayoutUnit); |
| 149 | ASSERT_EQ((overflowIntSizeT * LayoutUnit(4)).toInt(), intMaxForLayoutUnit); |
| 150 | ASSERT_EQ((LayoutUnit(4) * overflowIntSizeT).toInt(), intMaxForLayoutUnit); |
| 151 | } |
| 152 | |
| 153 | TEST(WebCoreLayoutUnit, LayoutUnitDivision) |
| 154 | { |
| 155 | ASSERT_EQ((LayoutUnit(1) / LayoutUnit(1)).toInt(), 1); |
| 156 | ASSERT_EQ((LayoutUnit(1) / LayoutUnit(2)).toInt(), 0); |
| 157 | ASSERT_EQ((LayoutUnit(2) / LayoutUnit(1)).toInt(), 2); |
| 158 | ASSERT_EQ((LayoutUnit(2) / LayoutUnit(0.5)).toInt(), 4); |
| 159 | ASSERT_EQ((LayoutUnit(0.5) / LayoutUnit(2)).toInt(), 0); |
| 160 | ASSERT_EQ((LayoutUnit(100) / LayoutUnit(10)).toInt(), 10); |
| 161 | ASSERT_FLOAT_EQ((LayoutUnit(1) / LayoutUnit(2)).toFloat(), 0.5f); |
| 162 | ASSERT_FLOAT_EQ((LayoutUnit(0.5) / LayoutUnit(2)).toFloat(), 0.25f); |
| 163 | |
| 164 | ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(1)).toInt(), -1); |
| 165 | ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(2)).toInt(), 0); |
| 166 | ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(1)).toInt(), -2); |
| 167 | ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(0.5)).toInt(), -4); |
| 168 | ASSERT_EQ((LayoutUnit(-0.5) / LayoutUnit(2)).toInt(), 0); |
| 169 | ASSERT_EQ((LayoutUnit(-100) / LayoutUnit(10)).toInt(), -10); |
| 170 | ASSERT_FLOAT_EQ((LayoutUnit(-1) / LayoutUnit(2)).toFloat(), -0.5f); |
| 171 | ASSERT_FLOAT_EQ((LayoutUnit(-0.5) / LayoutUnit(2)).toFloat(), -0.25f); |
| 172 | |
| 173 | ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(-1)).toInt(), 1); |
| 174 | ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(-2)).toInt(), 0); |
| 175 | ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(-1)).toInt(), 2); |
| 176 | ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(-0.5)).toInt(), 4); |
| 177 | ASSERT_EQ((LayoutUnit(-0.5) / LayoutUnit(-2)).toInt(), 0); |
| 178 | ASSERT_EQ((LayoutUnit(-100) / LayoutUnit(-10)).toInt(), 10); |
| 179 | ASSERT_FLOAT_EQ((LayoutUnit(-1) / LayoutUnit(-2)).toFloat(), 0.5f); |
| 180 | ASSERT_FLOAT_EQ((LayoutUnit(-0.5) / LayoutUnit(-2)).toFloat(), 0.25f); |
| 181 | |
| 182 | size_t aHundredSizeT = 100; |
| 183 | ASSERT_EQ((LayoutUnit(aHundredSizeT) / LayoutUnit(2)).toInt(), 50); |
| 184 | ASSERT_EQ((aHundredSizeT / LayoutUnit(4)).toInt(), 25); |
| 185 | ASSERT_EQ((LayoutUnit(400) / aHundredSizeT).toInt(), 4); |
| 186 | |
| 187 | ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(2)).toInt(), intMaxForLayoutUnit / 2); |
| 188 | ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(0.5)).toInt(), intMaxForLayoutUnit); |
| 189 | } |
| 190 | |
| 191 | TEST(WebCoreLayoutUnit, LayoutUnitCeil) |
| 192 | { |
| 193 | ASSERT_EQ(LayoutUnit(0).ceil(), 0); |
| 194 | ASSERT_EQ(LayoutUnit(0.1).ceil(), 1); |
| 195 | ASSERT_EQ(LayoutUnit(0.5).ceil(), 1); |
| 196 | ASSERT_EQ(LayoutUnit(0.9).ceil(), 1); |
| 197 | ASSERT_EQ(LayoutUnit(1.0).ceil(), 1); |
| 198 | ASSERT_EQ(LayoutUnit(1.1).ceil(), 2); |
| 199 | |
| 200 | ASSERT_EQ(LayoutUnit(-0.1).ceil(), 0); |
| 201 | ASSERT_EQ(LayoutUnit(-0.5).ceil(), 0); |
| 202 | ASSERT_EQ(LayoutUnit(-0.9).ceil(), 0); |
| 203 | ASSERT_EQ(LayoutUnit(-1.0).ceil(), -1); |
| 204 | |
| 205 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).ceil(), intMaxForLayoutUnit); |
| 206 | ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) - LayoutUnit(0.5)).ceil(), intMaxForLayoutUnit); |
| 207 | ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) - LayoutUnit(1)).ceil(), intMaxForLayoutUnit - 1); |
| 208 | |
| 209 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).ceil(), intMinForLayoutUnit); |
| 210 | } |
| 211 | |
| 212 | TEST(WebCoreLayoutUnit, LayoutUnitFloor) |
| 213 | { |
| 214 | ASSERT_EQ(LayoutUnit(0).floor(), 0); |
| 215 | ASSERT_EQ(LayoutUnit(0.1).floor(), 0); |
| 216 | ASSERT_EQ(LayoutUnit(0.5).floor(), 0); |
| 217 | ASSERT_EQ(LayoutUnit(0.9).floor(), 0); |
| 218 | ASSERT_EQ(LayoutUnit(1.0).floor(), 1); |
| 219 | ASSERT_EQ(LayoutUnit(1.1).floor(), 1); |
| 220 | |
| 221 | ASSERT_EQ(LayoutUnit(-0.1).floor(), -1); |
| 222 | ASSERT_EQ(LayoutUnit(-0.5).floor(), -1); |
| 223 | ASSERT_EQ(LayoutUnit(-0.9).floor(), -1); |
| 224 | ASSERT_EQ(LayoutUnit(-1.0).floor(), -1); |
| 225 | |
| 226 | ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).floor(), intMaxForLayoutUnit); |
| 227 | |
| 228 | ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).floor(), intMinForLayoutUnit); |
| 229 | ASSERT_EQ((LayoutUnit(intMinForLayoutUnit) + LayoutUnit(0.5)).floor(), intMinForLayoutUnit); |
| 230 | ASSERT_EQ((LayoutUnit(intMinForLayoutUnit) + LayoutUnit(1)).floor(), intMinForLayoutUnit + 1); |
| 231 | } |
| 232 | |
| 233 | TEST(WebCoreLayoutUnit, LayoutUnitPixelSnapping) |
| 234 | { |
| 235 | for (int i = -100000; i <= 100000; ++i) { |
| 236 | ASSERT_EQ(roundToDevicePixel(LayoutUnit(i), 1), i); |
| 237 | ASSERT_EQ(roundToDevicePixel(LayoutUnit(i), 2), i); |
| 238 | ASSERT_EQ(roundToDevicePixel(LayoutUnit(i), 3), i); |
| 239 | } |
| 240 | |
| 241 | for (float i = -10000; i < 0; i = i + 0.5) |
| 242 | ASSERT_FLOAT_EQ(roundToDevicePixel(LayoutUnit(i), 2), i); |
| 243 | |
| 244 | for (float i = -10000.25; i < 0; i = i + 0.5) |
| 245 | ASSERT_FLOAT_EQ(roundToDevicePixel(LayoutUnit(i), 2), i + 0.25); |
| 246 | } |
| 247 | |
| 248 | } // namespace TestWebKitAPI |
| 249 | |