1 /* 2 * Copyright (C) 2014 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include "config.h" 26 #include "platform/geometry/FloatBoxTestHelpers.h" 27 28 #include "platform/geometry/FloatBox.h" 29 const static float kTestEpsilon = 1e-6; 30 31 void WebCore::PrintTo(const FloatBox& box, ::std::ostream* os) 32 { 33 *os << "FloatBox(" 34 << box.x() << ", " 35 << box.y() << ", " 36 << box.z() << ", " 37 << box.width() << ", " 38 << box.height() << ", " 39 << box.depth() << ")"; 40 } 41 42 bool WebCore::FloatBoxTest::ApproximatelyEqual(const float& a, const float& b) 43 { 44 float absA = ::fabs(a); 45 float absB = ::fabs(b); 46 float absErr = ::fabs(a - b); 47 if (a == b) 48 return true; 49 50 if (a == 0 || b == 0 || absErr < std::numeric_limits<float>::min()) 51 return absErr < (kTestEpsilon * std::numeric_limits<float>::min()); 52 53 return ((absErr / (absA + absB)) < kTestEpsilon); 54 } 55 56 bool WebCore::FloatBoxTest::ApproximatelyEqual(const FloatBox& a, const FloatBox& b) 57 { 58 if (!ApproximatelyEqual(a.x(), b.x()) 59 || !ApproximatelyEqual(a.y(), b.y()) 60 || !ApproximatelyEqual(a.z(), b.z()) 61 || !ApproximatelyEqual(a.width(), b.width()) 62 || !ApproximatelyEqual(a.height(), b.height()) 63 || !ApproximatelyEqual(a.depth(), b.depth())) { 64 return false; 65 } 66 return true; 67 } 68 69 ::testing::AssertionResult WebCore::FloatBoxTest::AssertAlmostEqual(const char* m_expr, const char* n_expr, const FloatBox& m, const FloatBox& n) 70 { 71 if (!ApproximatelyEqual(m, n)) { 72 return ::testing::AssertionFailure() << " Value of:" << n_expr << std::endl 73 << " Actual:" << testing::PrintToString(n) << std::endl 74 << "Expected Approx:" << m_expr << std::endl 75 << " Which is:" << ::testing::PrintToString(m); 76 } 77 return ::testing::AssertionSuccess(); 78 } 79 80 ::testing::AssertionResult WebCore::FloatBoxTest::AssertContains(const char* m_expr, const char* n_expr, const FloatBox& m, const FloatBox& n) 81 { 82 FloatBox newM = m; 83 newM.expandTo(n); 84 if (!ApproximatelyEqual(m, newM)) { 85 return ::testing::AssertionFailure() << " Value of:" << n_expr << std::endl 86 << " Actual:" << testing::PrintToString(n) << std::endl 87 << "Not Contained in:" << m_expr << std::endl 88 << " Which is:" << ::testing::PrintToString(m); 89 } 90 return ::testing::AssertionSuccess(); 91 } 92 93 94