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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/geometry/vector2d.h"
#include <stddef.h>
#include <cmath>
#include <limits>
#include "testing/gtest/include/gtest/gtest.h"
namespace gfx {
TEST(Vector2dTest, IsZero) {
EXPECT_TRUE(Vector2d().IsZero());
EXPECT_TRUE(Vector2d(0, 0).IsZero());
EXPECT_FALSE(Vector2d(1, 0).IsZero());
EXPECT_FALSE(Vector2d(0, -2).IsZero());
EXPECT_FALSE(Vector2d(1, -2).IsZero());
}
TEST(Vector2dTest, Add) {
Vector2d i1(3, 5);
Vector2d i2(4, -1);
EXPECT_EQ(Vector2d(3, 5), i1 + Vector2d());
EXPECT_EQ(Vector2d(3 + 4, 5 - 1), i1 + i2);
EXPECT_EQ(Vector2d(3 - 4, 5 + 1), i1 - i2);
}
TEST(Vector2dTest, Negative) {
EXPECT_EQ(Vector2d(0, 0), -Vector2d(0, 0));
EXPECT_EQ(Vector2d(-3, -3), -Vector2d(3, 3));
EXPECT_EQ(Vector2d(3, 3), -Vector2d(-3, -3));
EXPECT_EQ(Vector2d(-3, 3), -Vector2d(3, -3));
EXPECT_EQ(Vector2d(3, -3), -Vector2d(-3, 3));
}
TEST(Vector2dTest, Length) {
int values[][2] = {
{0, 0}, {10, 20}, {20, 10}, {-10, -20}, {-20, 10}, {10, -20},
};
for (auto& value : values) {
int v0 = value[0];
int v1 = value[1];
double length_squared =
static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1;
double length = std::sqrt(length_squared);
Vector2d vector(v0, v1);
EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
EXPECT_EQ(static_cast<float>(length), vector.Length());
}
}
TEST(Vector2dTest, SetToMinMax) {
Vector2d a;
a = Vector2d(3, 5);
EXPECT_EQ(Vector2d(3, 5), a);
a.SetToMax(Vector2d(2, 4));
EXPECT_EQ(Vector2d(3, 5), a);
a.SetToMax(Vector2d(3, 5));
EXPECT_EQ(Vector2d(3, 5), a);
a.SetToMax(Vector2d(4, 2));
EXPECT_EQ(Vector2d(4, 5), a);
a.SetToMax(Vector2d(8, 10));
EXPECT_EQ(Vector2d(8, 10), a);
a.SetToMin(Vector2d(9, 11));
EXPECT_EQ(Vector2d(8, 10), a);
a.SetToMin(Vector2d(8, 10));
EXPECT_EQ(Vector2d(8, 10), a);
a.SetToMin(Vector2d(11, 9));
EXPECT_EQ(Vector2d(8, 9), a);
a.SetToMin(Vector2d(7, 11));
EXPECT_EQ(Vector2d(7, 9), a);
a.SetToMin(Vector2d(3, 5));
EXPECT_EQ(Vector2d(3, 5), a);
}
TEST(Vector2dTest, IntegerOverflow) {
int int_max = std::numeric_limits<int>::max();
int int_min = std::numeric_limits<int>::min();
Vector2d max_vector(int_max, int_max);
Vector2d min_vector(int_min, int_min);
Vector2d test;
test = Vector2d();
test += Vector2d(int_max, int_max);
EXPECT_EQ(test, max_vector);
test = Vector2d();
test += Vector2d(int_min, int_min);
EXPECT_EQ(test, min_vector);
test = Vector2d(10, 20);
test += Vector2d(int_max, int_max);
EXPECT_EQ(test, max_vector);
test = Vector2d(-10, -20);
test += Vector2d(int_min, int_min);
EXPECT_EQ(test, min_vector);
test = Vector2d();
test -= Vector2d(int_max, int_max);
EXPECT_EQ(test, Vector2d(-int_max, -int_max));
test = Vector2d();
test -= Vector2d(int_min, int_min);
EXPECT_EQ(test, max_vector);
test = Vector2d(10, 20);
test -= Vector2d(int_min, int_min);
EXPECT_EQ(test, max_vector);
test = Vector2d(-10, -20);
test -= Vector2d(int_max, int_max);
EXPECT_EQ(test, min_vector);
test = Vector2d();
test -= Vector2d(int_min, int_min);
EXPECT_EQ(test, max_vector);
test = -Vector2d(int_min, int_min);
EXPECT_EQ(test, max_vector);
}
TEST(Vector2dTest, Transpose) {
gfx::Vector2d v(1, -2);
EXPECT_EQ(gfx::Vector2d(-2, 1), TransposeVector2d(v));
v.Transpose();
EXPECT_EQ(gfx::Vector2d(-2, 1), v);
}
} // namespace gfx
|