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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
/*
* Copyright (c) 2012, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include <limits.h>
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
template <class T>
class LayoutUnitTypedTest : public testing::Test {};
using LayoutUnitTypes =
::testing::Types<LayoutUnit, TextRunLayoutUnit, InlineLayoutUnit>;
TYPED_TEST_SUITE(LayoutUnitTypedTest, LayoutUnitTypes);
TEST(LayoutUnitTest, LayoutUnitInt) {
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(INT_MIN).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(INT_MIN / 2).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(LayoutUnit::kIntMin - 1).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(LayoutUnit::kIntMin).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin + 1,
LayoutUnit(LayoutUnit::kIntMin + 1).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin / 2,
LayoutUnit(LayoutUnit::kIntMin / 2).ToInt());
EXPECT_EQ(-10000, LayoutUnit(-10000).ToInt());
EXPECT_EQ(-1000, LayoutUnit(-1000).ToInt());
EXPECT_EQ(-100, LayoutUnit(-100).ToInt());
EXPECT_EQ(-10, LayoutUnit(-10).ToInt());
EXPECT_EQ(-1, LayoutUnit(-1).ToInt());
EXPECT_EQ(0, LayoutUnit(0).ToInt());
EXPECT_EQ(1, LayoutUnit(1).ToInt());
EXPECT_EQ(100, LayoutUnit(100).ToInt());
EXPECT_EQ(1000, LayoutUnit(1000).ToInt());
EXPECT_EQ(10000, LayoutUnit(10000).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax / 2,
LayoutUnit(LayoutUnit::kIntMax / 2).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax - 1,
LayoutUnit(LayoutUnit::kIntMax - 1).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(LayoutUnit::kIntMax).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(LayoutUnit::kIntMax + 1).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(INT_MAX / 2).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(INT_MAX).ToInt());
// Test the raw unsaturated value
EXPECT_EQ(0, LayoutUnit(0).RawValue());
// Internally the max number we can represent (without saturating)
// is all the (non-sign) bits set except for the bottom n fraction bits
const int max_internal_representation =
std::numeric_limits<int>::max() ^
((1 << LayoutUnit::kFractionalBits) - 1);
EXPECT_EQ(max_internal_representation,
LayoutUnit(LayoutUnit::kIntMax).RawValue());
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit(LayoutUnit::kIntMax + 100).RawValue());
EXPECT_EQ((LayoutUnit::kIntMax - 100) << LayoutUnit::kFractionalBits,
LayoutUnit(LayoutUnit::kIntMax - 100).RawValue());
EXPECT_EQ(GetMinSaturatedSetResultForTesting(),
LayoutUnit(LayoutUnit::kIntMin).RawValue());
EXPECT_EQ(GetMinSaturatedSetResultForTesting(),
LayoutUnit(LayoutUnit::kIntMin - 100).RawValue());
// Shifting negative numbers left has undefined behavior, so use
// multiplication instead of direct shifting here.
EXPECT_EQ((LayoutUnit::kIntMin + 100) * (1 << LayoutUnit::kFractionalBits),
LayoutUnit(LayoutUnit::kIntMin + 100).RawValue());
}
TEST(LayoutUnitTest, LayoutUnitUnsigned) {
// Test the raw unsaturated value
EXPECT_EQ(0, LayoutUnit((unsigned)0).RawValue());
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit((unsigned)LayoutUnit::kIntMax).RawValue());
const unsigned kOverflowed = LayoutUnit::kIntMax + 100;
EXPECT_EQ(GetMaxSaturatedSetResultForTesting(),
LayoutUnit(kOverflowed).RawValue());
const unsigned kNotOverflowed = LayoutUnit::kIntMax - 100;
EXPECT_EQ((LayoutUnit::kIntMax - 100) << LayoutUnit::kFractionalBits,
LayoutUnit(kNotOverflowed).RawValue());
}
TEST(LayoutUnitTest, Int64) {
constexpr int raw_min = std::numeric_limits<int>::min();
EXPECT_EQ(LayoutUnit(static_cast<int64_t>(raw_min) - 100), LayoutUnit::Min());
constexpr int raw_max = std::numeric_limits<int>::max();
EXPECT_EQ(LayoutUnit(static_cast<int64_t>(raw_max) + 100), LayoutUnit::Max());
EXPECT_EQ(LayoutUnit(static_cast<uint64_t>(raw_max) + 100),
LayoutUnit::Max());
}
TEST(LayoutUnitTest, LayoutUnitFloat) {
const float kTolerance = 1.0f / LayoutUnit::kFixedPointDenominator;
EXPECT_FLOAT_EQ(1.0f, LayoutUnit(1.0f).ToFloat());
EXPECT_FLOAT_EQ(1.25f, LayoutUnit(1.25f).ToFloat());
EXPECT_EQ(LayoutUnit(1.25f), LayoutUnit(1.25f + kTolerance / 2));
EXPECT_EQ(LayoutUnit(-2.0f), LayoutUnit(-2.0f - kTolerance / 2));
EXPECT_NEAR(LayoutUnit(1.1f).ToFloat(), 1.1f, kTolerance);
EXPECT_NEAR(LayoutUnit(1.33f).ToFloat(), 1.33f, kTolerance);
EXPECT_NEAR(LayoutUnit(1.3333f).ToFloat(), 1.3333f, kTolerance);
EXPECT_NEAR(LayoutUnit(1.53434f).ToFloat(), 1.53434f, kTolerance);
EXPECT_NEAR(LayoutUnit(345634).ToFloat(), 345634.0f, kTolerance);
EXPECT_NEAR(LayoutUnit(345634.12335f).ToFloat(), 345634.12335f, kTolerance);
EXPECT_NEAR(LayoutUnit(-345634.12335f).ToFloat(), -345634.12335f, kTolerance);
EXPECT_NEAR(LayoutUnit(-345634).ToFloat(), -345634.0f, kTolerance);
using Limits = std::numeric_limits<float>;
// Larger than Max()
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit(Limits::max()));
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit(Limits::infinity()));
// Smaller than Min()
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit(Limits::lowest()));
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit(-Limits::infinity()));
EXPECT_EQ(LayoutUnit(), LayoutUnit::Clamp(Limits::quiet_NaN()));
}
// Test that `constexpr` constructors are constant expressions.
TEST(LayoutUnitTest, ConstExprCtor) {
[[maybe_unused]] constexpr LayoutUnit from_int(1);
[[maybe_unused]] constexpr LayoutUnit from_float(1.0f);
[[maybe_unused]] constexpr LayoutUnit from_raw = LayoutUnit::FromRawValue(1);
[[maybe_unused]] constexpr LayoutUnit from_raw_with_clamp =
LayoutUnit::FromRawValueWithClamp(1);
}
TEST(LayoutUnitTest, FromFloatCeil) {
const float kTolerance = 1.0f / LayoutUnit::kFixedPointDenominator;
EXPECT_EQ(LayoutUnit(1.25f), LayoutUnit::FromFloatCeil(1.25f));
EXPECT_EQ(LayoutUnit(1.25f + kTolerance),
LayoutUnit::FromFloatCeil(1.25f + kTolerance / 2));
EXPECT_EQ(LayoutUnit(), LayoutUnit::FromFloatCeil(-kTolerance / 2));
using Limits = std::numeric_limits<float>;
// Larger than Max()
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatCeil(Limits::max()));
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatCeil(Limits::infinity()));
// Smaller than Min()
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatCeil(Limits::lowest()));
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatCeil(-Limits::infinity()));
EXPECT_EQ(LayoutUnit(), LayoutUnit::FromFloatCeil(Limits::quiet_NaN()));
}
TEST(LayoutUnitTest, FromFloatFloor) {
const float kTolerance = 1.0f / LayoutUnit::kFixedPointDenominator;
EXPECT_EQ(LayoutUnit(1.25f), LayoutUnit::FromFloatFloor(1.25f));
EXPECT_EQ(LayoutUnit(1.25f),
LayoutUnit::FromFloatFloor(1.25f + kTolerance / 2));
EXPECT_EQ(LayoutUnit(-kTolerance),
LayoutUnit::FromFloatFloor(-kTolerance / 2));
using Limits = std::numeric_limits<float>;
// Larger than Max()
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatFloor(Limits::max()));
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatFloor(Limits::infinity()));
// Smaller than Min()
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatFloor(Limits::lowest()));
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatFloor(-Limits::infinity()));
EXPECT_EQ(LayoutUnit(), LayoutUnit::FromFloatFloor(Limits::quiet_NaN()));
}
TEST(LayoutUnitTest, FromFloatRound) {
const float kTolerance = 1.0f / LayoutUnit::kFixedPointDenominator;
EXPECT_EQ(LayoutUnit(1.25f), LayoutUnit::FromFloatRound(1.25f));
EXPECT_EQ(LayoutUnit(1.25f),
LayoutUnit::FromFloatRound(1.25f + kTolerance / 4));
EXPECT_EQ(LayoutUnit(1.25f + kTolerance),
LayoutUnit::FromFloatRound(1.25f + kTolerance * 3 / 4));
EXPECT_EQ(LayoutUnit(-kTolerance),
LayoutUnit::FromFloatRound(-kTolerance * 3 / 4));
using Limits = std::numeric_limits<float>;
// Larger than Max()
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatRound(Limits::max()));
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit::FromFloatRound(Limits::infinity()));
// Smaller than Min()
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatRound(Limits::lowest()));
EXPECT_EQ(LayoutUnit::Min(), LayoutUnit::FromFloatRound(-Limits::infinity()));
EXPECT_EQ(LayoutUnit(), LayoutUnit::FromFloatRound(Limits::quiet_NaN()));
}
TEST(LayoutUnitTest, LayoutUnitRounding) {
EXPECT_EQ(-2, LayoutUnit(-1.9f).Round());
EXPECT_EQ(-2, LayoutUnit(-1.6f).Round());
EXPECT_EQ(-2, LayoutUnit::FromFloatRound(-1.51f).Round());
EXPECT_EQ(-1, LayoutUnit::FromFloatRound(-1.5f).Round());
EXPECT_EQ(-1, LayoutUnit::FromFloatRound(-1.49f).Round());
EXPECT_EQ(-1, LayoutUnit(-1.0f).Round());
EXPECT_EQ(-1, LayoutUnit::FromFloatRound(-0.99f).Round());
EXPECT_EQ(-1, LayoutUnit::FromFloatRound(-0.51f).Round());
EXPECT_EQ(0, LayoutUnit::FromFloatRound(-0.50f).Round());
EXPECT_EQ(0, LayoutUnit::FromFloatRound(-0.49f).Round());
EXPECT_EQ(0, LayoutUnit(-0.1f).Round());
EXPECT_EQ(0, LayoutUnit(0.0f).Round());
EXPECT_EQ(0, LayoutUnit(0.1f).Round());
EXPECT_EQ(0, LayoutUnit::FromFloatRound(0.49f).Round());
EXPECT_EQ(1, LayoutUnit::FromFloatRound(0.50f).Round());
EXPECT_EQ(1, LayoutUnit::FromFloatRound(0.51f).Round());
EXPECT_EQ(1, LayoutUnit(0.99f).Round());
EXPECT_EQ(1, LayoutUnit(1.0f).Round());
EXPECT_EQ(1, LayoutUnit::FromFloatRound(1.49f).Round());
EXPECT_EQ(2, LayoutUnit::FromFloatRound(1.5f).Round());
EXPECT_EQ(2, LayoutUnit::FromFloatRound(1.51f).Round());
// The fractional part of LayoutUnit::Max() is 0x3f, so it should round up.
EXPECT_EQ(
((std::numeric_limits<int>::max() / LayoutUnit::kFixedPointDenominator) +
1),
LayoutUnit::Max().Round());
// The fractional part of LayoutUnit::Min() is 0, so the next bigger possible
// value should round down.
LayoutUnit epsilon;
epsilon.SetRawValue(1);
EXPECT_EQ(
((std::numeric_limits<int>::min() / LayoutUnit::kFixedPointDenominator)),
(LayoutUnit::Min() + epsilon).Round());
}
TEST(LayoutUnitTest, LayoutUnitSnapSizeToPixel) {
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1), LayoutUnit(0)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1), LayoutUnit(0.5)));
EXPECT_EQ(2, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0)));
EXPECT_EQ(2, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.49)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.5)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.75)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.99)));
EXPECT_EQ(2, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1)));
// 0.046875 is 3/64, lower than 4 * LayoutUnit::Epsilon()
EXPECT_EQ(0, SnapSizeToPixel(LayoutUnit(0.046875), LayoutUnit(0)));
// 0.078125 is 5/64, higher than 4 * LayoutUnit::Epsilon()
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(0.078125), LayoutUnit(0)));
// Negative versions
EXPECT_EQ(0, SnapSizeToPixel(LayoutUnit(-0.046875), LayoutUnit(0)));
EXPECT_EQ(-1, SnapSizeToPixel(LayoutUnit(-0.078125), LayoutUnit(0)));
// The next 2 would snap to zero but for the requirement that we not snap
// sizes greater than 4 * LayoutUnit::Epsilon() to 0.
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(0.5), LayoutUnit(1.5)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(0.99), LayoutUnit(1.5)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.0), LayoutUnit(1.5)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.49), LayoutUnit(1.5)));
EXPECT_EQ(1, SnapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1.5)));
EXPECT_EQ(101, SnapSizeToPixel(LayoutUnit(100.5), LayoutUnit(100)));
EXPECT_EQ(LayoutUnit::kIntMax,
SnapSizeToPixel(LayoutUnit(LayoutUnit::kIntMax), LayoutUnit(0.3)));
EXPECT_EQ(LayoutUnit::kIntMin,
SnapSizeToPixel(LayoutUnit(LayoutUnit::kIntMin), LayoutUnit(-0.3)));
}
TEST(LayoutUnitTest, LayoutUnitMultiplication) {
EXPECT_EQ(1, (LayoutUnit(1) * LayoutUnit(1)).ToInt());
EXPECT_EQ(2, (LayoutUnit(1) * LayoutUnit(2)).ToInt());
EXPECT_EQ(2, (LayoutUnit(2) * LayoutUnit(1)).ToInt());
EXPECT_EQ(1, (LayoutUnit(2) * LayoutUnit(0.5)).ToInt());
EXPECT_EQ(1, (LayoutUnit(0.5) * LayoutUnit(2)).ToInt());
EXPECT_EQ(100, (LayoutUnit(100) * LayoutUnit(1)).ToInt());
EXPECT_EQ(-1, (LayoutUnit(-1) * LayoutUnit(1)).ToInt());
EXPECT_EQ(-2, (LayoutUnit(-1) * LayoutUnit(2)).ToInt());
EXPECT_EQ(-2, (LayoutUnit(-2) * LayoutUnit(1)).ToInt());
EXPECT_EQ(-1, (LayoutUnit(-2) * LayoutUnit(0.5)).ToInt());
EXPECT_EQ(-1, (LayoutUnit(-0.5) * LayoutUnit(2)).ToInt());
EXPECT_EQ(-100, (LayoutUnit(-100) * LayoutUnit(1)).ToInt());
EXPECT_EQ(1, (LayoutUnit(-1) * LayoutUnit(-1)).ToInt());
EXPECT_EQ(2, (LayoutUnit(-1) * LayoutUnit(-2)).ToInt());
EXPECT_EQ(2, (LayoutUnit(-2) * LayoutUnit(-1)).ToInt());
EXPECT_EQ(1, (LayoutUnit(-2) * LayoutUnit(-0.5)).ToInt());
EXPECT_EQ(1, (LayoutUnit(-0.5) * LayoutUnit(-2)).ToInt());
EXPECT_EQ(100, (LayoutUnit(-100) * LayoutUnit(-1)).ToInt());
EXPECT_EQ(333, (LayoutUnit(100) * LayoutUnit(3.33)).Round());
EXPECT_EQ(-333, (LayoutUnit(-100) * LayoutUnit(3.33)).Round());
EXPECT_EQ(333, (LayoutUnit(-100) * LayoutUnit(-3.33)).Round());
size_t a_hundred_size_t = 100;
EXPECT_EQ(100, (LayoutUnit(a_hundred_size_t) * LayoutUnit(1)).ToInt());
EXPECT_EQ(400, (a_hundred_size_t * LayoutUnit(4)).ToInt());
EXPECT_EQ(400, (LayoutUnit(4) * a_hundred_size_t).ToInt());
int quarter_max = LayoutUnit::kIntMax / 4;
EXPECT_EQ(quarter_max * 2, (LayoutUnit(quarter_max) * LayoutUnit(2)).ToInt());
EXPECT_EQ(quarter_max * 3, (LayoutUnit(quarter_max) * LayoutUnit(3)).ToInt());
EXPECT_EQ(quarter_max * 4, (LayoutUnit(quarter_max) * LayoutUnit(4)).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax,
(LayoutUnit(quarter_max) * LayoutUnit(5)).ToInt());
size_t overflow_int_size_t = LayoutUnit::kIntMax * 4;
EXPECT_EQ(LayoutUnit::kIntMax,
(LayoutUnit(overflow_int_size_t) * LayoutUnit(2)).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, (overflow_int_size_t * LayoutUnit(4)).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, (LayoutUnit(4) * overflow_int_size_t).ToInt());
{
// Multiple by float 1.0 can produce a different value.
LayoutUnit source = LayoutUnit::FromRawValue(2147483009);
EXPECT_NE(source, LayoutUnit(source * 1.0f));
LayoutUnit updated = source;
updated *= 1.0f;
EXPECT_NE(source, updated);
}
}
TYPED_TEST(LayoutUnitTypedTest, MultiplicationByInt) {
const auto quarter_max = TypeParam::kIntMax / 4;
EXPECT_EQ(TypeParam(quarter_max * 2), TypeParam(quarter_max) * 2);
EXPECT_EQ(TypeParam(quarter_max * 3), TypeParam(quarter_max) * 3);
EXPECT_EQ(TypeParam(quarter_max * 4), TypeParam(quarter_max) * 4);
EXPECT_EQ(TypeParam::Max(), TypeParam(quarter_max) * 5);
}
TEST(LayoutUnitTest, LayoutUnitDivision) {
EXPECT_EQ(1, (LayoutUnit(1) / LayoutUnit(1)).ToInt());
EXPECT_EQ(0, (LayoutUnit(1) / LayoutUnit(2)).ToInt());
EXPECT_EQ(2, (LayoutUnit(2) / LayoutUnit(1)).ToInt());
EXPECT_EQ(4, (LayoutUnit(2) / LayoutUnit(0.5)).ToInt());
EXPECT_EQ(0, (LayoutUnit(0.5) / LayoutUnit(2)).ToInt());
EXPECT_EQ(10, (LayoutUnit(100) / LayoutUnit(10)).ToInt());
EXPECT_FLOAT_EQ(0.5f, (LayoutUnit(1) / LayoutUnit(2)).ToFloat());
EXPECT_FLOAT_EQ(0.25f, (LayoutUnit(0.5) / LayoutUnit(2)).ToFloat());
EXPECT_EQ(-1, (LayoutUnit(-1) / LayoutUnit(1)).ToInt());
EXPECT_EQ(0, (LayoutUnit(-1) / LayoutUnit(2)).ToInt());
EXPECT_EQ(-2, (LayoutUnit(-2) / LayoutUnit(1)).ToInt());
EXPECT_EQ(-4, (LayoutUnit(-2) / LayoutUnit(0.5)).ToInt());
EXPECT_EQ(0, (LayoutUnit(-0.5) / LayoutUnit(2)).ToInt());
EXPECT_EQ(-10, (LayoutUnit(-100) / LayoutUnit(10)).ToInt());
EXPECT_FLOAT_EQ(-0.5f, (LayoutUnit(-1) / LayoutUnit(2)).ToFloat());
EXPECT_FLOAT_EQ(-0.25f, (LayoutUnit(-0.5) / LayoutUnit(2)).ToFloat());
EXPECT_EQ(1, (LayoutUnit(-1) / LayoutUnit(-1)).ToInt());
EXPECT_EQ(0, (LayoutUnit(-1) / LayoutUnit(-2)).ToInt());
EXPECT_EQ(2, (LayoutUnit(-2) / LayoutUnit(-1)).ToInt());
EXPECT_EQ(4, (LayoutUnit(-2) / LayoutUnit(-0.5)).ToInt());
EXPECT_EQ(0, (LayoutUnit(-0.5) / LayoutUnit(-2)).ToInt());
EXPECT_EQ(10, (LayoutUnit(-100) / LayoutUnit(-10)).ToInt());
EXPECT_FLOAT_EQ(0.5f, (LayoutUnit(-1) / LayoutUnit(-2)).ToFloat());
EXPECT_FLOAT_EQ(0.25f, (LayoutUnit(-0.5) / LayoutUnit(-2)).ToFloat());
size_t a_hundred_size_t = 100;
EXPECT_EQ(50, (LayoutUnit(a_hundred_size_t) / LayoutUnit(2)).ToInt());
EXPECT_EQ(25, (a_hundred_size_t / LayoutUnit(4)).ToInt());
EXPECT_EQ(4, (LayoutUnit(400) / a_hundred_size_t).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax / 2,
(LayoutUnit(LayoutUnit::kIntMax) / LayoutUnit(2)).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax,
(LayoutUnit(LayoutUnit::kIntMax) / LayoutUnit(0.5)).ToInt());
}
TEST(LayoutUnitTest, LayoutUnitDivisionByInt) {
EXPECT_EQ(LayoutUnit(1), LayoutUnit(1) / 1);
EXPECT_EQ(LayoutUnit(0.5), LayoutUnit(1) / 2);
EXPECT_EQ(LayoutUnit(-0.5), LayoutUnit(1) / -2);
EXPECT_EQ(LayoutUnit(-0.5), LayoutUnit(-1) / 2);
EXPECT_EQ(LayoutUnit(0.5), LayoutUnit(-1) / -2);
EXPECT_DOUBLE_EQ(LayoutUnit::kIntMax / 2.0,
(LayoutUnit(LayoutUnit::kIntMax) / 2).ToDouble());
EXPECT_DOUBLE_EQ(
InlineLayoutUnit::kIntMax / 2.0,
(InlineLayoutUnit(InlineLayoutUnit::kIntMax) / 2).ToDouble());
}
TEST(LayoutUnitTest, LayoutUnitMulDiv) {
const LayoutUnit kMaxValue = LayoutUnit::Max();
const LayoutUnit kMinValue = LayoutUnit::Min();
const LayoutUnit kEpsilon = LayoutUnit().AddEpsilon();
EXPECT_EQ(kMaxValue, kMaxValue.MulDiv(kMaxValue, kMaxValue));
EXPECT_EQ(kMinValue, kMinValue.MulDiv(kMinValue, kMinValue));
EXPECT_EQ(kMinValue, kMaxValue.MulDiv(kMinValue, kMaxValue));
EXPECT_EQ(kMaxValue, kMinValue.MulDiv(kMinValue, kMaxValue));
EXPECT_EQ(kMinValue + kEpsilon * 2, kMaxValue.MulDiv(kMaxValue, kMinValue));
EXPECT_EQ(kMaxValue, kMaxValue.MulDiv(LayoutUnit(2), kEpsilon));
EXPECT_EQ(kMinValue, kMinValue.MulDiv(LayoutUnit(2), kEpsilon));
const LayoutUnit kLargerInt(16384);
const LayoutUnit kLargerInt2(32768);
EXPECT_EQ(LayoutUnit(8192), kLargerInt.MulDiv(kLargerInt, kLargerInt2));
}
TEST(LayoutUnitTest, LayoutUnitCeil) {
EXPECT_EQ(0, LayoutUnit(0).Ceil());
EXPECT_EQ(1, LayoutUnit(0.1).Ceil());
EXPECT_EQ(1, LayoutUnit(0.5).Ceil());
EXPECT_EQ(1, LayoutUnit(0.9).Ceil());
EXPECT_EQ(1, LayoutUnit(1.0).Ceil());
EXPECT_EQ(2, LayoutUnit(1.1).Ceil());
EXPECT_EQ(0, LayoutUnit(-0.1).Ceil());
EXPECT_EQ(0, LayoutUnit(-0.5).Ceil());
EXPECT_EQ(0, LayoutUnit(-0.9).Ceil());
EXPECT_EQ(-1, LayoutUnit(-1.0).Ceil());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(LayoutUnit::kIntMax).Ceil());
EXPECT_EQ(LayoutUnit::kIntMax,
(LayoutUnit(LayoutUnit::kIntMax) - LayoutUnit(0.5)).Ceil());
EXPECT_EQ(LayoutUnit::kIntMax - 1,
(LayoutUnit(LayoutUnit::kIntMax) - LayoutUnit(1)).Ceil());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(LayoutUnit::kIntMin).Ceil());
}
TEST(LayoutUnitTest, LayoutUnitFloor) {
EXPECT_EQ(0, LayoutUnit(0).Floor());
EXPECT_EQ(0, LayoutUnit(0.1).Floor());
EXPECT_EQ(0, LayoutUnit(0.5).Floor());
EXPECT_EQ(0, LayoutUnit(0.9).Floor());
EXPECT_EQ(1, LayoutUnit(1.0).Floor());
EXPECT_EQ(1, LayoutUnit(1.1).Floor());
EXPECT_EQ(-1, LayoutUnit(-0.1).Floor());
EXPECT_EQ(-1, LayoutUnit(-0.5).Floor());
EXPECT_EQ(-1, LayoutUnit(-0.9).Floor());
EXPECT_EQ(-1, LayoutUnit(-1.0).Floor());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(LayoutUnit::kIntMax).Floor());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(LayoutUnit::kIntMin).Floor());
EXPECT_EQ(LayoutUnit::kIntMin,
(LayoutUnit(LayoutUnit::kIntMin) + LayoutUnit(0.5)).Floor());
EXPECT_EQ(LayoutUnit::kIntMin + 1,
(LayoutUnit(LayoutUnit::kIntMin) + LayoutUnit(1)).Floor());
}
TEST(LayoutUnitTest, LayoutUnitFloatOverflow) {
// These should overflow to the max/min according to their sign.
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(176972000.0f).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(-176972000.0f).ToInt());
EXPECT_EQ(LayoutUnit::kIntMax, LayoutUnit(176972000.0).ToInt());
EXPECT_EQ(LayoutUnit::kIntMin, LayoutUnit(-176972000.0).ToInt());
}
TEST(LayoutUnitTest, UnaryMinus) {
EXPECT_EQ(LayoutUnit(), -LayoutUnit());
EXPECT_EQ(LayoutUnit(999), -LayoutUnit(-999));
EXPECT_EQ(LayoutUnit(-999), -LayoutUnit(999));
LayoutUnit negative_max;
negative_max.SetRawValue(LayoutUnit::Min().RawValue() + 1);
EXPECT_EQ(negative_max, -LayoutUnit::Max());
EXPECT_EQ(LayoutUnit::Max(), -negative_max);
// -LayoutUnit::min() is saturated to LayoutUnit::max()
EXPECT_EQ(LayoutUnit::Max(), -LayoutUnit::Min());
}
TEST(LayoutUnitTest, LayoutUnitPlusPlus) {
EXPECT_EQ(LayoutUnit(-1), LayoutUnit(-2)++);
EXPECT_EQ(LayoutUnit(0), LayoutUnit(-1)++);
EXPECT_EQ(LayoutUnit(1), LayoutUnit(0)++);
EXPECT_EQ(LayoutUnit(2), LayoutUnit(1)++);
EXPECT_EQ(LayoutUnit::Max(), LayoutUnit(LayoutUnit::Max())++);
}
TEST(LayoutUnitTest, IntMod) {
EXPECT_EQ(LayoutUnit(5), IntMod(LayoutUnit(55), LayoutUnit(10)));
EXPECT_EQ(LayoutUnit(5), IntMod(LayoutUnit(55), LayoutUnit(-10)));
EXPECT_EQ(LayoutUnit(-5), IntMod(LayoutUnit(-55), LayoutUnit(10)));
EXPECT_EQ(LayoutUnit(-5), IntMod(LayoutUnit(-55), LayoutUnit(-10)));
EXPECT_EQ(LayoutUnit(1.5), IntMod(LayoutUnit(7.5), LayoutUnit(3)));
EXPECT_EQ(LayoutUnit(1.25), IntMod(LayoutUnit(7.5), LayoutUnit(3.125)));
EXPECT_EQ(LayoutUnit(), IntMod(LayoutUnit(7.5), LayoutUnit(2.5)));
EXPECT_EQ(LayoutUnit(), IntMod(LayoutUnit(), LayoutUnit(123)));
}
TEST(LayoutUnitTest, Fraction) {
EXPECT_TRUE(LayoutUnit(-1.9f).HasFraction());
EXPECT_TRUE(LayoutUnit(-1.6f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-1.51f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-1.5f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-1.49f).HasFraction());
EXPECT_FALSE(LayoutUnit(-1.0f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-0.95f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-0.51f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-0.50f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(-0.49f).HasFraction());
EXPECT_TRUE(LayoutUnit(-0.1f).HasFraction());
EXPECT_FALSE(LayoutUnit(-1.0f).HasFraction());
EXPECT_FALSE(LayoutUnit(0.0f).HasFraction());
EXPECT_TRUE(LayoutUnit(0.1f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(0.49f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(0.50f).HasFraction());
EXPECT_TRUE(LayoutUnit::FromFloatRound(0.51f).HasFraction());
EXPECT_TRUE(LayoutUnit(0.95f).HasFraction());
EXPECT_FALSE(LayoutUnit(1.0f).HasFraction());
}
TEST(LayoutUnitTest, FixedConsts) {
EXPECT_EQ(LayoutUnit::kFractionalBits, 6u);
EXPECT_EQ(LayoutUnit::kIntegralBits, 26u);
EXPECT_EQ(TextRunLayoutUnit::kFractionalBits, 16u);
EXPECT_EQ(TextRunLayoutUnit::kIntegralBits, 16u);
EXPECT_EQ(InlineLayoutUnit::kFractionalBits, 16u);
EXPECT_EQ(InlineLayoutUnit::kIntegralBits, 48u);
}
TEST(LayoutUnitTest, Fixed) {
constexpr int raw_value16 = 0x12345678;
constexpr int raw_value6 = raw_value16 >> 10;
const auto value16 = TextRunLayoutUnit::FromRawValue(raw_value16);
const auto value6 = LayoutUnit::FromRawValue(raw_value6);
EXPECT_EQ(value16.To<LayoutUnit>(), value6);
}
TEST(LayoutUnitTest, Raw64FromInt32) {
constexpr int32_t int32_max_plus = LayoutUnit::kIntMax + 10;
LayoutUnit int32_max_plus_32(int32_max_plus);
EXPECT_NE(int32_max_plus_32.ToInt(), int32_max_plus);
InlineLayoutUnit int32_max_plus_64(int32_max_plus);
EXPECT_EQ(int32_max_plus_64.ToInt(), int32_max_plus);
constexpr int32_t int32_min_minus = LayoutUnit::kIntMin - 10;
LayoutUnit int32_min_minus_32(int32_min_minus);
EXPECT_NE(int32_min_minus_32.ToInt(), int32_min_minus);
InlineLayoutUnit int32_min_minus_64(int32_min_minus);
EXPECT_EQ(int32_min_minus_64.ToInt(), int32_min_minus);
constexpr int64_t raw32_max_plus =
static_cast<int64_t>(LayoutUnit::kRawValueMax) + 10;
LayoutUnit raw32_max_plus_32(raw32_max_plus);
EXPECT_NE(raw32_max_plus_32.ToInt(), raw32_max_plus);
InlineLayoutUnit raw32_max_plus_64(raw32_max_plus);
EXPECT_EQ(raw32_max_plus_64.ToInt(), raw32_max_plus);
constexpr int64_t raw32_min_minus =
static_cast<int64_t>(LayoutUnit::kRawValueMin) - 10;
LayoutUnit raw32_min_minus_32(raw32_min_minus);
EXPECT_NE(raw32_min_minus_32.ToInt(), raw32_min_minus);
InlineLayoutUnit raw32_min_minus_64(raw32_min_minus);
EXPECT_EQ(raw32_min_minus_64.ToInt(), raw32_min_minus);
}
TEST(LayoutUnitTest, Raw64FromRaw32) {
constexpr float value = 1.f + LayoutUnit::Epsilon() * 234;
LayoutUnit value32_6(value);
EXPECT_EQ(InlineLayoutUnit(value32_6), InlineLayoutUnit(value));
TextRunLayoutUnit value32_16(value);
EXPECT_EQ(InlineLayoutUnit(value32_16), InlineLayoutUnit(value));
// The following code should fail to compile.
// TextRunLayoutUnit back_to_32{InlineLayoutUnit(value)};
}
TEST(LayoutUnitTest, To) {
#define TEST_ROUND_TRIP(T1, T2) \
EXPECT_EQ(T1(value), T2(value).To<T1>()) << value; \
EXPECT_EQ(T2(value), T1(value).To<T2>()) << value;
for (const float value : {1.0f, 1.5f, -1.0f}) {
TEST_ROUND_TRIP(LayoutUnit, TextRunLayoutUnit);
TEST_ROUND_TRIP(LayoutUnit, InlineLayoutUnit);
TEST_ROUND_TRIP(TextRunLayoutUnit, InlineLayoutUnit);
}
#undef TEST_ROUND_TRIP
}
TEST(LayoutUnitTest, ToClampSameFractional64To32) {
EXPECT_EQ(
TextRunLayoutUnit::Max(),
InlineLayoutUnit(TextRunLayoutUnit::kIntMax + 1).To<TextRunLayoutUnit>());
EXPECT_EQ(
TextRunLayoutUnit::Min(),
InlineLayoutUnit(TextRunLayoutUnit::kIntMin - 1).To<TextRunLayoutUnit>());
}
TEST(LayoutUnitTest, ToClampLessFractional64To32) {
EXPECT_EQ(LayoutUnit::Max(),
InlineLayoutUnit(LayoutUnit::kIntMax + 1).To<LayoutUnit>());
EXPECT_EQ(LayoutUnit::Min(),
InlineLayoutUnit(LayoutUnit::kIntMin - 1).To<LayoutUnit>());
}
TEST(LayoutUnitTest, ToClampMoreFractional) {
EXPECT_EQ(TextRunLayoutUnit::Max(),
LayoutUnit(TextRunLayoutUnit::kIntMax + 1).To<TextRunLayoutUnit>());
EXPECT_EQ(TextRunLayoutUnit::Min(),
LayoutUnit(TextRunLayoutUnit::kIntMin - 1).To<TextRunLayoutUnit>());
}
TEST(LayoutUnitTest, Raw64Ceil) {
LayoutUnit layout(1.234);
InlineLayoutUnit inline_value(layout);
EXPECT_EQ(layout, inline_value.ToCeil<LayoutUnit>());
inline_value = inline_value.AddEpsilon();
EXPECT_NE(layout, inline_value.ToCeil<LayoutUnit>());
EXPECT_EQ(layout.AddEpsilon(), inline_value.ToCeil<LayoutUnit>());
}
} // namespace blink
|