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 <stddef.h>
6
7 #include "base/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/geometry/point3_f.h"
10
11 namespace gfx {
12
TEST(Point3Test,VectorArithmetic)13 TEST(Point3Test, VectorArithmetic) {
14 gfx::Point3F a(1.6f, 5.1f, 3.2f);
15 gfx::Vector3dF v1(3.1f, -3.2f, 9.3f);
16 gfx::Vector3dF v2(-8.1f, 1.2f, 3.3f);
17
18 static const struct {
19 gfx::Point3F expected;
20 gfx::Point3F actual;
21 } tests[] = {
22 { gfx::Point3F(4.7f, 1.9f, 12.5f), a + v1 },
23 { gfx::Point3F(-1.5f, 8.3f, -6.1f), a - v1 },
24 { a, a - v1 + v1 },
25 { a, a + v1 - v1 },
26 { a, a + gfx::Vector3dF() },
27 { gfx::Point3F(12.8f, 0.7f, 9.2f), a + v1 - v2 },
28 { gfx::Point3F(-9.6f, 9.5f, -2.8f), a - v1 + v2 }
29 };
30
31 for (size_t i = 0; i < arraysize(tests); ++i)
32 EXPECT_EQ(tests[i].expected.ToString(),
33 tests[i].actual.ToString());
34
35 a += v1;
36 EXPECT_EQ(Point3F(4.7f, 1.9f, 12.5f).ToString(), a.ToString());
37
38 a -= v2;
39 EXPECT_EQ(Point3F(12.8f, 0.7f, 9.2f).ToString(), a.ToString());
40 }
41
TEST(Point3Test,VectorFromPoints)42 TEST(Point3Test, VectorFromPoints) {
43 gfx::Point3F a(1.6f, 5.2f, 3.2f);
44 gfx::Vector3dF v1(3.1f, -3.2f, 9.3f);
45
46 gfx::Point3F b(a + v1);
47 EXPECT_EQ((b - a).ToString(), v1.ToString());
48 }
49
TEST(Point3Test,Scale)50 TEST(Point3Test, Scale) {
51 EXPECT_EQ(Point3F().ToString(), ScalePoint(Point3F(), 2.f).ToString());
52 EXPECT_EQ(Point3F().ToString(),
53 ScalePoint(Point3F(), 2.f, 2.f, 2.f).ToString());
54
55 EXPECT_EQ(Point3F(2.f, -2.f, 4.f).ToString(),
56 ScalePoint(Point3F(1.f, -1.f, 2.f), 2.f).ToString());
57 EXPECT_EQ(Point3F(2.f, -3.f, 8.f).ToString(),
58 ScalePoint(Point3F(1.f, -1.f, 2.f), 2.f, 3.f, 4.f).ToString());
59
60 Point3F zero;
61 zero.Scale(2.f);
62 zero.Scale(6.f, 3.f, 1.5f);
63 EXPECT_EQ(Point3F().ToString(), zero.ToString());
64
65 Point3F point(1.f, -1.f, 2.f);
66 point.Scale(2.f);
67 point.Scale(6.f, 3.f, 1.5f);
68 EXPECT_EQ(Point3F(12.f, -6.f, 6.f).ToString(), point.ToString());
69 }
70
71 } // namespace gfx
72