176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)/*
276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * Copyright (C) 2014 Google Inc. All rights reserved.
376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) *
476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * modification, are permitted provided that the following conditions
676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * are met:
776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * 1.  Redistributions of source code must retain the above copyright
876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) *     notice, this list of conditions and the following disclaimer.
976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * 2.  Redistributions in binary form must reproduce the above copyright
1076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) *     notice, this list of conditions and the following disclaimer in the
1176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) *     documentation and/or other materials provided with the distribution.
1276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) *
1376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
1476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
1776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles) */
2476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
2576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)#include "config.h"
2676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)#include "platform/geometry/FloatBoxTestHelpers.h"
2776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
2876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)#include "platform/geometry/FloatBox.h"
2976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)const static float kTestEpsilon = 1e-6;
3076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
3176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)void WebCore::PrintTo(const FloatBox& box, ::std::ostream* os)
3276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles){
3376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    *os << "FloatBox("
3476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.x() << ", "
3576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.y() << ", "
3676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.z() << ", "
3776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.width() << ", "
3876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.height() << ", "
3976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        << box.depth() << ")";
4076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)}
4176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
4276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)bool WebCore::FloatBoxTest::ApproximatelyEqual(const float& a, const float& b)
4376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles){
4476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    float absA = ::fabs(a);
4576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    float absB = ::fabs(b);
4676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    float absErr = ::fabs(a - b);
4776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    if (a == b)
4876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        return true;
4976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
5076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    if (a == 0 || b == 0 || absErr < std::numeric_limits<float>::min())
5176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        return absErr < (kTestEpsilon * std::numeric_limits<float>::min());
5276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
5376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    return ((absErr / (absA + absB)) < kTestEpsilon);
5476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)}
5576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
5676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)bool WebCore::FloatBoxTest::ApproximatelyEqual(const FloatBox& a, const FloatBox& b)
5776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles){
5876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    if (!ApproximatelyEqual(a.x(), b.x())
5976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        || !ApproximatelyEqual(a.y(), b.y())
6076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        || !ApproximatelyEqual(a.z(), b.z())
6176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        || !ApproximatelyEqual(a.width(), b.width())
6276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        || !ApproximatelyEqual(a.height(), b.height())
6376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        || !ApproximatelyEqual(a.depth(), b.depth())) {
6476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        return false;
6576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    }
6676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    return true;
6776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)}
6876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
6976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)::testing::AssertionResult WebCore::FloatBoxTest::AssertAlmostEqual(const char* m_expr, const char* n_expr, const FloatBox& m, const FloatBox& n)
7076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles){
7176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    if (!ApproximatelyEqual(m, n)) {
7276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        return ::testing::AssertionFailure() << "       Value of:" << n_expr << std::endl
7376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "         Actual:" << testing::PrintToString(n) << std::endl
7476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "Expected Approx:" << m_expr << std::endl
7576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "       Which is:" << ::testing::PrintToString(m);
7676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    }
7776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    return ::testing::AssertionSuccess();
7876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)}
7976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
8076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)::testing::AssertionResult WebCore::FloatBoxTest::AssertContains(const char* m_expr, const char* n_expr, const FloatBox& m, const FloatBox& n)
8176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles){
8276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    FloatBox newM = m;
8376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    newM.expandTo(n);
8476c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    if (!ApproximatelyEqual(m, newM)) {
8576c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)        return ::testing::AssertionFailure() << "        Value of:" << n_expr << std::endl
8676c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "          Actual:" << testing::PrintToString(n) << std::endl
8776c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "Not Contained in:" << m_expr << std::endl
8876c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)            << "        Which is:" << ::testing::PrintToString(m);
8976c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    }
9076c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)    return ::testing::AssertionSuccess();
9176c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)}
9276c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
9376c265b59aa821ccbf8c75ab2bb0d036e97d2956Torne (Richard Coles)
94