OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "base/basictypes.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "ui/gfx/vector2d.h" |
| 8 #include "ui/gfx/vector2d_f.h" |
| 9 |
| 10 #include <cmath> |
| 11 #include <limits> |
| 12 |
| 13 namespace gfx { |
| 14 |
| 15 TEST(Vector2dTest, ConversionToFloat) { |
| 16 gfx::Vector2d i(3, 4); |
| 17 gfx::Vector2dF f = i; |
| 18 EXPECT_EQ(i, f); |
| 19 } |
| 20 |
| 21 TEST(Vector2dTest, IsZero) { |
| 22 gfx::Vector2d int_zero(0, 0); |
| 23 gfx::Vector2d int_nonzero(2, -2); |
| 24 gfx::Vector2dF float_zero(0, 0); |
| 25 gfx::Vector2dF float_nonzero(0.1f, -0.1f); |
| 26 |
| 27 EXPECT_TRUE(int_zero.IsZero()); |
| 28 EXPECT_FALSE(int_nonzero.IsZero()); |
| 29 EXPECT_TRUE(float_zero.IsZero()); |
| 30 EXPECT_FALSE(float_nonzero.IsZero()); |
| 31 } |
| 32 |
| 33 TEST(Vector2dTest, Add) { |
| 34 gfx::Vector2d i1(3, 5); |
| 35 gfx::Vector2d i2(4, -1); |
| 36 |
| 37 const struct { |
| 38 gfx::Vector2d expected; |
| 39 gfx::Vector2d actual; |
| 40 } int_tests[] = { |
| 41 { gfx::Vector2d(3, 5), i1 + gfx::Vector2d() }, |
| 42 { gfx::Vector2d(3 + 4, 5 - 1), i1 + i2 }, |
| 43 { gfx::Vector2d(3 - 4, 5 + 1), i1 - i2 } |
| 44 }; |
| 45 |
| 46 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) |
| 47 EXPECT_EQ(int_tests[i].expected.ToString(), |
| 48 int_tests[i].actual.ToString()); |
| 49 |
| 50 gfx::Vector2dF f1(3.1f, 5.1f); |
| 51 gfx::Vector2dF f2(4.3f, -1.3f); |
| 52 |
| 53 const struct { |
| 54 gfx::Vector2dF expected; |
| 55 gfx::Vector2dF actual; |
| 56 } float_tests[] = { |
| 57 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2d() }, |
| 58 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2dF() }, |
| 59 { gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2 }, |
| 60 { gfx::Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2 } |
| 61 }; |
| 62 |
| 63 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) |
| 64 EXPECT_EQ(float_tests[i].expected.ToString(), |
| 65 float_tests[i].actual.ToString()); |
| 66 } |
| 67 |
| 68 TEST(Vector2dTest, Negative) { |
| 69 const struct { |
| 70 gfx::Vector2d expected; |
| 71 gfx::Vector2d actual; |
| 72 } int_tests[] = { |
| 73 { gfx::Vector2d(0, 0), -gfx::Vector2d(0, 0) }, |
| 74 { gfx::Vector2d(-3, -3), -gfx::Vector2d(3, 3) }, |
| 75 { gfx::Vector2d(3, 3), -gfx::Vector2d(-3, -3) }, |
| 76 { gfx::Vector2d(-3, 3), -gfx::Vector2d(3, -3) }, |
| 77 { gfx::Vector2d(3, -3), -gfx::Vector2d(-3, 3) } |
| 78 }; |
| 79 |
| 80 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) |
| 81 EXPECT_EQ(int_tests[i].expected.ToString(), |
| 82 int_tests[i].actual.ToString()); |
| 83 |
| 84 const struct { |
| 85 gfx::Vector2dF expected; |
| 86 gfx::Vector2dF actual; |
| 87 } float_tests[] = { |
| 88 { gfx::Vector2dF(0, 0), -gfx::Vector2d(0, 0) }, |
| 89 { gfx::Vector2dF(-0.3f, -0.3f), -gfx::Vector2dF(0.3f, 0.3f) }, |
| 90 { gfx::Vector2dF(0.3f, 0.3f), -gfx::Vector2dF(-0.3f, -0.3f) }, |
| 91 { gfx::Vector2dF(-0.3f, 0.3f), -gfx::Vector2dF(0.3f, -0.3f) }, |
| 92 { gfx::Vector2dF(0.3f, -0.3f), -gfx::Vector2dF(-0.3f, 0.3f) } |
| 93 }; |
| 94 |
| 95 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) |
| 96 EXPECT_EQ(float_tests[i].expected.ToString(), |
| 97 float_tests[i].actual.ToString()); |
| 98 } |
| 99 |
| 100 TEST(Vector2dTest, Scale) { |
| 101 float double_values[][4] = { |
| 102 { 4.5f, 1.2f, 3.3f, 5.6f }, |
| 103 { 4.5f, -1.2f, 3.3f, 5.6f }, |
| 104 { 4.5f, 1.2f, 3.3f, -5.6f }, |
| 105 { 4.5f, 1.2f, -3.3f, -5.6f }, |
| 106 { -4.5f, 1.2f, 3.3f, 5.6f }, |
| 107 { -4.5f, 1.2f, 0, 5.6f }, |
| 108 { -4.5f, 1.2f, 3.3f, 0 }, |
| 109 { 4.5f, 0, 3.3f, 5.6f }, |
| 110 { 0, 1.2f, 3.3f, 5.6f } |
| 111 }; |
| 112 |
| 113 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(double_values); ++i) { |
| 114 gfx::Vector2dF v(double_values[i][0], double_values[i][1]); |
| 115 v.Scale(double_values[i][2], double_values[i][3]); |
| 116 EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]); |
| 117 EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]); |
| 118 } |
| 119 |
| 120 float single_values[][3] = { |
| 121 { 4.5f, 1.2f, 3.3f }, |
| 122 { 4.5f, -1.2f, 3.3f }, |
| 123 { 4.5f, 1.2f, 3.3f }, |
| 124 { 4.5f, 1.2f, -3.3f }, |
| 125 { -4.5f, 1.2f, 3.3f }, |
| 126 { -4.5f, 1.2f, 0 }, |
| 127 { -4.5f, 1.2f, 3.3f }, |
| 128 { 4.5f, 0, 3.3f }, |
| 129 { 0, 1.2f, 3.3f } |
| 130 }; |
| 131 |
| 132 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(single_values); ++i) { |
| 133 gfx::Vector2dF v(single_values[i][0], single_values[i][1]); |
| 134 v.Scale(single_values[i][2]); |
| 135 EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]); |
| 136 EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]); |
| 137 } |
| 138 } |
| 139 |
| 140 TEST(Vector2dTest, Length) { |
| 141 int int_values[][2] = { |
| 142 { 0, 0 }, |
| 143 { 10, 20 }, |
| 144 { 20, 10 }, |
| 145 { -10, -20 }, |
| 146 { -20, 10 }, |
| 147 { 10, -20 }, |
| 148 }; |
| 149 |
| 150 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_values); ++i) { |
| 151 int v0 = int_values[i][0]; |
| 152 int v1 = int_values[i][1]; |
| 153 double length_squared = |
| 154 static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1; |
| 155 double length = std::sqrt(length_squared); |
| 156 gfx::Vector2d vector(v0, v1); |
| 157 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared()); |
| 158 EXPECT_EQ(static_cast<float>(length), vector.Length()); |
| 159 } |
| 160 |
| 161 float float_values[][2] = { |
| 162 { 0, 0 }, |
| 163 { 10.5f, 20.5f }, |
| 164 { 20.5f, 10.5f }, |
| 165 { -10.5f, -20.5f }, |
| 166 { -20.5f, 10.5f }, |
| 167 { 10.5f, -20.5f }, |
| 168 // A large vector that fails if the Length function doesn't use |
| 169 // double precision internally. |
| 170 { 1236278317862780234892374893213178027.12122348904204230f, |
| 171 335890352589839028212313231225425134332.38123f }, |
| 172 }; |
| 173 |
| 174 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_values); ++i) { |
| 175 double v0 = float_values[i][0]; |
| 176 double v1 = float_values[i][1]; |
| 177 double length_squared = |
| 178 static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1; |
| 179 double length = std::sqrt(length_squared); |
| 180 gfx::Vector2dF vector(v0, v1); |
| 181 EXPECT_EQ(length_squared, vector.LengthSquared()); |
| 182 EXPECT_EQ(static_cast<float>(length), vector.Length()); |
| 183 } |
| 184 } |
| 185 |
| 186 } // namespace ui |
OLD | NEW |