TestPoint.cpp (2677B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 #include <cmath> 9 10 #include "BasePoint.h" 11 #include "Units.h" 12 #include "Point.h" 13 14 using mozilla::CSSCoord; 15 using mozilla::CSSIntCoord; 16 using mozilla::CSSIntPoint; 17 using mozilla::CSSPixel; 18 using mozilla::gfx::CoordTyped; 19 using mozilla::gfx::FloatType_t; 20 using mozilla::gfx::IntCoordTyped; 21 using mozilla::gfx::IntPointTyped; 22 using mozilla::gfx::PointTyped; 23 using mozilla::gfx::UnknownUnits; 24 25 TEST(Gfx, TestCSSIntPointLength) 26 { 27 CSSIntPoint testPnt(1, 1); 28 float res = testPnt.Length(); 29 30 float epsilon = 0.001f; 31 constexpr float sqrtOfTwo = 1.414; 32 float diff = std::abs(res - sqrtOfTwo); 33 EXPECT_LT(diff, epsilon); 34 } 35 36 TEST(Gfx, TestPointAddition) 37 { 38 PointTyped<CSSPixel> a, b; 39 a.x = 2; 40 a.y = 2; 41 b.x = 5; 42 b.y = -5; 43 44 a += b; 45 46 EXPECT_EQ(a.x, 7.f); 47 EXPECT_EQ(a.y, -3.f); 48 } 49 50 TEST(Gfx, TestPointSubtraction) 51 { 52 PointTyped<CSSPixel> a, b; 53 a.x = 2; 54 a.y = 2; 55 b.x = 5; 56 b.y = -5; 57 58 a -= b; 59 60 EXPECT_EQ(a.x, -3.f); 61 EXPECT_EQ(a.y, 7.f); 62 } 63 64 TEST(Gfx, TestPointRoundToMultiple) 65 { 66 const int32_t roundTo = 2; 67 68 IntPointTyped<CSSPixel> p(478, -394); 69 EXPECT_EQ(p.RoundedToMultiple(roundTo), p); 70 71 IntPointTyped<CSSPixel> p2(478, 393); 72 EXPECT_NE(p2.RoundedToMultiple(roundTo), p2); 73 } 74 75 TEST(Gfx, TestFloatTypeMeta) 76 { 77 // TODO: Should int64_t's FloatType be double instead? 78 static_assert(std::is_same_v<FloatType_t<int64_t>, float>, 79 "The FloatType of an integer type should be float"); 80 static_assert(std::is_same_v<FloatType_t<int32_t>, float>, 81 "The FloatType of an integer type should be float"); 82 static_assert( 83 std::is_same_v<FloatType_t<float>, float>, 84 "The FloatType of a floating-point type should be the given type"); 85 static_assert( 86 std::is_same_v<FloatType_t<double>, double>, 87 "The FloatType of a floating-point type should be the given type"); 88 static_assert( 89 std::is_same_v<FloatType_t<IntCoordTyped<UnknownUnits, int32_t>>, 90 CoordTyped<UnknownUnits, float>>, 91 "The FloatType of an IntCoordTyped<Units, Rep> should be " 92 "CoordTyped<Units, float>"); 93 static_assert( 94 std::is_same_v<FloatType_t<CoordTyped<UnknownUnits, long double>>, 95 CoordTyped<UnknownUnits, long double>>, 96 "The FloatType of a CoordTyped<Units, Rep> should be " 97 "CoordTyped<Units, Rep>"); 98 }