vector2d_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/geometry/vector2d.h"
  5. #include <stddef.h>
  6. #include <cmath>
  7. #include <limits>
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace gfx {
  10. TEST(Vector2dTest, IsZero) {
  11. EXPECT_TRUE(Vector2d().IsZero());
  12. EXPECT_TRUE(Vector2d(0, 0).IsZero());
  13. EXPECT_FALSE(Vector2d(1, 0).IsZero());
  14. EXPECT_FALSE(Vector2d(0, -2).IsZero());
  15. EXPECT_FALSE(Vector2d(1, -2).IsZero());
  16. }
  17. TEST(Vector2dTest, Add) {
  18. Vector2d i1(3, 5);
  19. Vector2d i2(4, -1);
  20. EXPECT_EQ(Vector2d(3, 5), i1 + Vector2d());
  21. EXPECT_EQ(Vector2d(3 + 4, 5 - 1), i1 + i2);
  22. EXPECT_EQ(Vector2d(3 - 4, 5 + 1), i1 - i2);
  23. }
  24. TEST(Vector2dTest, Negative) {
  25. EXPECT_EQ(Vector2d(0, 0), -Vector2d(0, 0));
  26. EXPECT_EQ(Vector2d(-3, -3), -Vector2d(3, 3));
  27. EXPECT_EQ(Vector2d(3, 3), -Vector2d(-3, -3));
  28. EXPECT_EQ(Vector2d(-3, 3), -Vector2d(3, -3));
  29. EXPECT_EQ(Vector2d(3, -3), -Vector2d(-3, 3));
  30. }
  31. TEST(Vector2dTest, Length) {
  32. int values[][2] = {
  33. {0, 0}, {10, 20}, {20, 10}, {-10, -20}, {-20, 10}, {10, -20},
  34. };
  35. for (auto& value : values) {
  36. int v0 = value[0];
  37. int v1 = value[1];
  38. double length_squared =
  39. static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1;
  40. double length = std::sqrt(length_squared);
  41. Vector2d vector(v0, v1);
  42. EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
  43. EXPECT_EQ(static_cast<float>(length), vector.Length());
  44. }
  45. }
  46. TEST(Vector2dTest, SetToMinMax) {
  47. Vector2d a;
  48. a = Vector2d(3, 5);
  49. EXPECT_EQ(Vector2d(3, 5), a);
  50. a.SetToMax(Vector2d(2, 4));
  51. EXPECT_EQ(Vector2d(3, 5), a);
  52. a.SetToMax(Vector2d(3, 5));
  53. EXPECT_EQ(Vector2d(3, 5), a);
  54. a.SetToMax(Vector2d(4, 2));
  55. EXPECT_EQ(Vector2d(4, 5), a);
  56. a.SetToMax(Vector2d(8, 10));
  57. EXPECT_EQ(Vector2d(8, 10), a);
  58. a.SetToMin(Vector2d(9, 11));
  59. EXPECT_EQ(Vector2d(8, 10), a);
  60. a.SetToMin(Vector2d(8, 10));
  61. EXPECT_EQ(Vector2d(8, 10), a);
  62. a.SetToMin(Vector2d(11, 9));
  63. EXPECT_EQ(Vector2d(8, 9), a);
  64. a.SetToMin(Vector2d(7, 11));
  65. EXPECT_EQ(Vector2d(7, 9), a);
  66. a.SetToMin(Vector2d(3, 5));
  67. EXPECT_EQ(Vector2d(3, 5), a);
  68. }
  69. TEST(Vector2dTest, IntegerOverflow) {
  70. int int_max = std::numeric_limits<int>::max();
  71. int int_min = std::numeric_limits<int>::min();
  72. Vector2d max_vector(int_max, int_max);
  73. Vector2d min_vector(int_min, int_min);
  74. Vector2d test;
  75. test = Vector2d();
  76. test += Vector2d(int_max, int_max);
  77. EXPECT_EQ(test, max_vector);
  78. test = Vector2d();
  79. test += Vector2d(int_min, int_min);
  80. EXPECT_EQ(test, min_vector);
  81. test = Vector2d(10, 20);
  82. test += Vector2d(int_max, int_max);
  83. EXPECT_EQ(test, max_vector);
  84. test = Vector2d(-10, -20);
  85. test += Vector2d(int_min, int_min);
  86. EXPECT_EQ(test, min_vector);
  87. test = Vector2d();
  88. test -= Vector2d(int_max, int_max);
  89. EXPECT_EQ(test, Vector2d(-int_max, -int_max));
  90. test = Vector2d();
  91. test -= Vector2d(int_min, int_min);
  92. EXPECT_EQ(test, max_vector);
  93. test = Vector2d(10, 20);
  94. test -= Vector2d(int_min, int_min);
  95. EXPECT_EQ(test, max_vector);
  96. test = Vector2d(-10, -20);
  97. test -= Vector2d(int_max, int_max);
  98. EXPECT_EQ(test, min_vector);
  99. test = Vector2d();
  100. test -= Vector2d(int_min, int_min);
  101. EXPECT_EQ(test, max_vector);
  102. test = -Vector2d(int_min, int_min);
  103. EXPECT_EQ(test, max_vector);
  104. }
  105. TEST(Vector2dTest, Transpose) {
  106. gfx::Vector2d v(1, -2);
  107. EXPECT_EQ(gfx::Vector2d(-2, 1), TransposeVector2d(v));
  108. v.Transpose();
  109. EXPECT_EQ(gfx::Vector2d(-2, 1), v);
  110. }
  111. } // namespace gfx