| OLD | NEW |
| (Empty) |
| 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 #define ENABLE_SUBPIXEL_LAYOUT 1 | |
| 32 #define ENABLE_SATURATED_LAYOUT_ARITHMETIC 1 | |
| 33 #include "config.h" | |
| 34 | |
| 35 #include <WebCore/LayoutUnit.h> | |
| 36 | |
| 37 using namespace WebCore; | |
| 38 | |
| 39 namespace TestWebKitAPI { | |
| 40 | |
| 41 TEST(WebCoreLayoutUnit, LayoutUnitInt) | |
| 42 { | |
| 43 ASSERT_EQ(LayoutUnit(INT_MIN).toInt(), intMinForLayoutUnit); | |
| 44 ASSERT_EQ(LayoutUnit(INT_MIN / 2).toInt(), intMinForLayoutUnit); | |
| 45 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit - 1).toInt(), intMinForLayoutUnit);
| |
| 46 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).toInt(), intMinForLayoutUnit); | |
| 47 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit + 1).toInt(), intMinForLayoutUnit +
1); | |
| 48 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit / 2).toInt(), intMinForLayoutUnit /
2); | |
| 49 ASSERT_EQ(LayoutUnit(-10000).toInt(), -10000); | |
| 50 ASSERT_EQ(LayoutUnit(-1000).toInt(), -1000); | |
| 51 ASSERT_EQ(LayoutUnit(-100).toInt(), -100); | |
| 52 ASSERT_EQ(LayoutUnit(-10).toInt(), -10); | |
| 53 ASSERT_EQ(LayoutUnit(-1).toInt(), -1); | |
| 54 ASSERT_EQ(LayoutUnit(0).toInt(), 0); | |
| 55 ASSERT_EQ(LayoutUnit(1).toInt(), 1); | |
| 56 ASSERT_EQ(LayoutUnit(100).toInt(), 100); | |
| 57 ASSERT_EQ(LayoutUnit(1000).toInt(), 1000); | |
| 58 ASSERT_EQ(LayoutUnit(10000).toInt(), 10000); | |
| 59 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit / 2).toInt(), intMaxForLayoutUnit /
2); | |
| 60 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit - 1).toInt(), intMaxForLayoutUnit -
1); | |
| 61 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).toInt(), intMaxForLayoutUnit); | |
| 62 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit + 1).toInt(), intMaxForLayoutUnit); | |
| 63 ASSERT_EQ(LayoutUnit(INT_MAX / 2).toInt(), intMaxForLayoutUnit); | |
| 64 ASSERT_EQ(LayoutUnit(INT_MAX).toInt(), intMaxForLayoutUnit); | |
| 65 } | |
| 66 | |
| 67 TEST(WebCoreLayoutUnit, LayoutUnitFloat) | |
| 68 { | |
| 69 const float tolerance = 1.0f / kFixedPointDenominator; | |
| 70 ASSERT_FLOAT_EQ(LayoutUnit(1.0f).toFloat(), 1.0f); | |
| 71 ASSERT_FLOAT_EQ(LayoutUnit(1.25f).toFloat(), 1.25f); | |
| 72 ASSERT_NEAR(LayoutUnit(1.1f).toFloat(), 1.1f, tolerance); | |
| 73 ASSERT_NEAR(LayoutUnit(1.33f).toFloat(), 1.33f, tolerance); | |
| 74 ASSERT_NEAR(LayoutUnit(1.3333f).toFloat(), 1.3333f, tolerance); | |
| 75 ASSERT_NEAR(LayoutUnit(1.53434f).toFloat(), 1.53434f, tolerance); | |
| 76 ASSERT_NEAR(LayoutUnit(345634).toFloat(), 345634.0f, tolerance); | |
| 77 ASSERT_NEAR(LayoutUnit(345634.12335f).toFloat(), 345634.12335f, tolerance); | |
| 78 ASSERT_NEAR(LayoutUnit(-345634.12335f).toFloat(), -345634.12335f, tolerance)
; | |
| 79 ASSERT_NEAR(LayoutUnit(-345634).toFloat(), -345634.0f, tolerance); | |
| 80 } | |
| 81 | |
| 82 TEST(WebCoreLayoutUnit, LayoutUnitRounding) | |
| 83 { | |
| 84 ASSERT_EQ(LayoutUnit(-1.9f).round(), -2); | |
| 85 ASSERT_EQ(LayoutUnit(-1.6f).round(), -2); | |
| 86 ASSERT_EQ(LayoutUnit::fromFloatRound(-1.51f).round(), -2); | |
| 87 ASSERT_EQ(LayoutUnit::fromFloatRound(-1.5f).round(), -1); | |
| 88 ASSERT_EQ(LayoutUnit::fromFloatRound(-1.49f).round(), -1); | |
| 89 ASSERT_EQ(LayoutUnit(-1.0f).round(), -1); | |
| 90 ASSERT_EQ(LayoutUnit::fromFloatRound(-0.99f).round(), -1); | |
| 91 ASSERT_EQ(LayoutUnit::fromFloatRound(-0.51f).round(), -1); | |
| 92 ASSERT_EQ(LayoutUnit::fromFloatRound(-0.50f).round(), 0); | |
| 93 ASSERT_EQ(LayoutUnit::fromFloatRound(-0.49f).round(), 0); | |
| 94 ASSERT_EQ(LayoutUnit(-0.1f).round(), 0); | |
| 95 ASSERT_EQ(LayoutUnit(0.0f).round(), 0); | |
| 96 ASSERT_EQ(LayoutUnit(0.1f).round(), 0); | |
| 97 ASSERT_EQ(LayoutUnit::fromFloatRound(0.49f).round(), 0); | |
| 98 ASSERT_EQ(LayoutUnit::fromFloatRound(0.50f).round(), 1); | |
| 99 ASSERT_EQ(LayoutUnit::fromFloatRound(0.51f).round(), 1); | |
| 100 ASSERT_EQ(LayoutUnit(0.99f).round(), 1); | |
| 101 ASSERT_EQ(LayoutUnit(1.0f).round(), 1); | |
| 102 ASSERT_EQ(LayoutUnit::fromFloatRound(1.49f).round(), 1); | |
| 103 ASSERT_EQ(LayoutUnit::fromFloatRound(1.5f).round(), 2); | |
| 104 ASSERT_EQ(LayoutUnit::fromFloatRound(1.51f).round(), 2); | |
| 105 } | |
| 106 | |
| 107 TEST(WebCoreLayoutUnit, LayoutUnitSnapSizeToPixel) | |
| 108 { | |
| 109 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1), LayoutUnit(0)), 1); | |
| 110 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1), LayoutUnit(0.5)), 1); | |
| 111 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0)), 2); | |
| 112 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.49)), 2); | |
| 113 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.5)), 1); | |
| 114 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.75)), 1); | |
| 115 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.99)), 1); | |
| 116 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1)), 2); | |
| 117 | |
| 118 ASSERT_EQ(snapSizeToPixel(LayoutUnit(0.5), LayoutUnit(1.5)), 0); | |
| 119 ASSERT_EQ(snapSizeToPixel(LayoutUnit(0.99), LayoutUnit(1.5)), 0); | |
| 120 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.0), LayoutUnit(1.5)), 1); | |
| 121 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.49), LayoutUnit(1.5)), 1); | |
| 122 ASSERT_EQ(snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1.5)), 1); | |
| 123 | |
| 124 ASSERT_EQ(snapSizeToPixel(LayoutUnit(100.5), LayoutUnit(100)), 101); | |
| 125 ASSERT_EQ(snapSizeToPixel(LayoutUnit(intMaxForLayoutUnit), LayoutUnit(0.3)),
intMaxForLayoutUnit); | |
| 126 ASSERT_EQ(snapSizeToPixel(LayoutUnit(intMinForLayoutUnit), LayoutUnit(-0.3))
, intMinForLayoutUnit); | |
| 127 } | |
| 128 | |
| 129 TEST(WebCoreLayoutUnit, LayoutUnitMultiplication) | |
| 130 { | |
| 131 ASSERT_EQ((LayoutUnit(1) * LayoutUnit(1)).toInt(), 1); | |
| 132 ASSERT_EQ((LayoutUnit(1) * LayoutUnit(2)).toInt(), 2); | |
| 133 ASSERT_EQ((LayoutUnit(2) * LayoutUnit(1)).toInt(), 2); | |
| 134 ASSERT_EQ((LayoutUnit(2) * LayoutUnit(0.5)).toInt(), 1); | |
| 135 ASSERT_EQ((LayoutUnit(0.5) * LayoutUnit(2)).toInt(), 1); | |
| 136 ASSERT_EQ((LayoutUnit(100) * LayoutUnit(1)).toInt(), 100); | |
| 137 | |
| 138 ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(1)).toInt(), -1); | |
| 139 ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(2)).toInt(), -2); | |
| 140 ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(1)).toInt(), -2); | |
| 141 ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(0.5)).toInt(), -1); | |
| 142 ASSERT_EQ((LayoutUnit(-0.5) * LayoutUnit(2)).toInt(), -1); | |
| 143 ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(1)).toInt(), -100); | |
| 144 | |
| 145 ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(-1)).toInt(), 1); | |
| 146 ASSERT_EQ((LayoutUnit(-1) * LayoutUnit(-2)).toInt(), 2); | |
| 147 ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(-1)).toInt(), 2); | |
| 148 ASSERT_EQ((LayoutUnit(-2) * LayoutUnit(-0.5)).toInt(), 1); | |
| 149 ASSERT_EQ((LayoutUnit(-0.5) * LayoutUnit(-2)).toInt(), 1); | |
| 150 ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(-1)).toInt(), 100); | |
| 151 | |
| 152 ASSERT_EQ((LayoutUnit(100) * LayoutUnit(3.33)).round(), 333); | |
| 153 ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(3.33)).round(), -333); | |
| 154 ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(-3.33)).round(), 333); | |
| 155 | |
| 156 size_t aHundredSizeT = 100; | |
| 157 ASSERT_EQ((LayoutUnit(aHundredSizeT) * LayoutUnit(1)).toInt(), 100); | |
| 158 ASSERT_EQ((aHundredSizeT * LayoutUnit(4)).toInt(), 400); | |
| 159 ASSERT_EQ((LayoutUnit(4) * aHundredSizeT).toInt(), 400); | |
| 160 | |
| 161 int quarterMax = intMaxForLayoutUnit / 4; | |
| 162 ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(2)).toInt(), quarterMax * 2); | |
| 163 ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(3)).toInt(), quarterMax * 3); | |
| 164 ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(4)).toInt(), quarterMax * 4); | |
| 165 ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(5)).toInt(), intMaxForLayoutU
nit); | |
| 166 | |
| 167 size_t overflowIntSizeT = intMaxForLayoutUnit * 4; | |
| 168 ASSERT_EQ((LayoutUnit(overflowIntSizeT) * LayoutUnit(2)).toInt(), intMaxForL
ayoutUnit); | |
| 169 ASSERT_EQ((overflowIntSizeT * LayoutUnit(4)).toInt(), intMaxForLayoutUnit); | |
| 170 ASSERT_EQ((LayoutUnit(4) * overflowIntSizeT).toInt(), intMaxForLayoutUnit); | |
| 171 } | |
| 172 | |
| 173 TEST(WebCoreLayoutUnit, LayoutUnitDivision) | |
| 174 { | |
| 175 ASSERT_EQ((LayoutUnit(1) / LayoutUnit(1)).toInt(), 1); | |
| 176 ASSERT_EQ((LayoutUnit(1) / LayoutUnit(2)).toInt(), 0); | |
| 177 ASSERT_EQ((LayoutUnit(2) / LayoutUnit(1)).toInt(), 2); | |
| 178 ASSERT_EQ((LayoutUnit(2) / LayoutUnit(0.5)).toInt(), 4); | |
| 179 ASSERT_EQ((LayoutUnit(0.5) / LayoutUnit(2)).toInt(), 0); | |
| 180 ASSERT_EQ((LayoutUnit(100) / LayoutUnit(10)).toInt(), 10); | |
| 181 ASSERT_FLOAT_EQ((LayoutUnit(1) / LayoutUnit(2)).toFloat(), 0.5f); | |
| 182 ASSERT_FLOAT_EQ((LayoutUnit(0.5) / LayoutUnit(2)).toFloat(), 0.25f); | |
| 183 | |
| 184 ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(1)).toInt(), -1); | |
| 185 ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(2)).toInt(), 0); | |
| 186 ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(1)).toInt(), -2); | |
| 187 ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(0.5)).toInt(), -4); | |
| 188 ASSERT_EQ((LayoutUnit(-0.5) / LayoutUnit(2)).toInt(), 0); | |
| 189 ASSERT_EQ((LayoutUnit(-100) / LayoutUnit(10)).toInt(), -10); | |
| 190 ASSERT_FLOAT_EQ((LayoutUnit(-1) / LayoutUnit(2)).toFloat(), -0.5f); | |
| 191 ASSERT_FLOAT_EQ((LayoutUnit(-0.5) / LayoutUnit(2)).toFloat(), -0.25f); | |
| 192 | |
| 193 ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(-1)).toInt(), 1); | |
| 194 ASSERT_EQ((LayoutUnit(-1) / LayoutUnit(-2)).toInt(), 0); | |
| 195 ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(-1)).toInt(), 2); | |
| 196 ASSERT_EQ((LayoutUnit(-2) / LayoutUnit(-0.5)).toInt(), 4); | |
| 197 ASSERT_EQ((LayoutUnit(-0.5) / LayoutUnit(-2)).toInt(), 0); | |
| 198 ASSERT_EQ((LayoutUnit(-100) / LayoutUnit(-10)).toInt(), 10); | |
| 199 ASSERT_FLOAT_EQ((LayoutUnit(-1) / LayoutUnit(-2)).toFloat(), 0.5f); | |
| 200 ASSERT_FLOAT_EQ((LayoutUnit(-0.5) / LayoutUnit(-2)).toFloat(), 0.25f); | |
| 201 | |
| 202 size_t aHundredSizeT = 100; | |
| 203 ASSERT_EQ((LayoutUnit(aHundredSizeT) / LayoutUnit(2)).toInt(), 50); | |
| 204 ASSERT_EQ((aHundredSizeT / LayoutUnit(4)).toInt(), 25); | |
| 205 ASSERT_EQ((LayoutUnit(400) / aHundredSizeT).toInt(), 4); | |
| 206 | |
| 207 ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(2)).toInt(), intMaxF
orLayoutUnit / 2); | |
| 208 ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(0.5)).toInt(), intMa
xForLayoutUnit); | |
| 209 } | |
| 210 | |
| 211 TEST(WebCoreLayoutUnit, LayoutUnitCeil) | |
| 212 { | |
| 213 ASSERT_EQ(LayoutUnit(0).ceil(), 0); | |
| 214 ASSERT_EQ(LayoutUnit(0.1).ceil(), 1); | |
| 215 ASSERT_EQ(LayoutUnit(0.5).ceil(), 1); | |
| 216 ASSERT_EQ(LayoutUnit(0.9).ceil(), 1); | |
| 217 ASSERT_EQ(LayoutUnit(1.0).ceil(), 1); | |
| 218 ASSERT_EQ(LayoutUnit(1.1).ceil(), 2); | |
| 219 | |
| 220 ASSERT_EQ(LayoutUnit(-0.1).ceil(), 0); | |
| 221 ASSERT_EQ(LayoutUnit(-0.5).ceil(), 0); | |
| 222 ASSERT_EQ(LayoutUnit(-0.9).ceil(), 0); | |
| 223 ASSERT_EQ(LayoutUnit(-1.0).ceil(), -1); | |
| 224 | |
| 225 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).ceil(), intMaxForLayoutUnit); | |
| 226 ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) - LayoutUnit(0.5)).ceil(), intMax
ForLayoutUnit); | |
| 227 ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) - LayoutUnit(1)).ceil(), intMaxFo
rLayoutUnit - 1); | |
| 228 | |
| 229 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).ceil(), intMinForLayoutUnit); | |
| 230 } | |
| 231 | |
| 232 TEST(WebCoreLayoutUnit, LayoutUnitFloor) | |
| 233 { | |
| 234 ASSERT_EQ(LayoutUnit(0).floor(), 0); | |
| 235 ASSERT_EQ(LayoutUnit(0.1).floor(), 0); | |
| 236 ASSERT_EQ(LayoutUnit(0.5).floor(), 0); | |
| 237 ASSERT_EQ(LayoutUnit(0.9).floor(), 0); | |
| 238 ASSERT_EQ(LayoutUnit(1.0).floor(), 1); | |
| 239 ASSERT_EQ(LayoutUnit(1.1).floor(), 1); | |
| 240 | |
| 241 ASSERT_EQ(LayoutUnit(-0.1).floor(), -1); | |
| 242 ASSERT_EQ(LayoutUnit(-0.5).floor(), -1); | |
| 243 ASSERT_EQ(LayoutUnit(-0.9).floor(), -1); | |
| 244 ASSERT_EQ(LayoutUnit(-1.0).floor(), -1); | |
| 245 | |
| 246 ASSERT_EQ(LayoutUnit(intMaxForLayoutUnit).floor(), intMaxForLayoutUnit); | |
| 247 | |
| 248 ASSERT_EQ(LayoutUnit(intMinForLayoutUnit).floor(), intMinForLayoutUnit); | |
| 249 ASSERT_EQ((LayoutUnit(intMinForLayoutUnit) + LayoutUnit(0.5)).floor(), intMi
nForLayoutUnit); | |
| 250 ASSERT_EQ((LayoutUnit(intMinForLayoutUnit) + LayoutUnit(1)).floor(), intMinF
orLayoutUnit + 1); | |
| 251 } | |
| 252 | |
| 253 | |
| 254 } // namespace TestWebKitAPI | |
| OLD | NEW |