Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2012 Intel Corporation
      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''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 
     28 #include "wtf/MathExtras.h"
     29 #include <gtest/gtest.h>
     30 
     31 namespace {
     32 
     33 TEST(WTF, Lrint)
     34 {
     35     EXPECT_EQ(lrint(-7.5), -8);
     36     EXPECT_EQ(lrint(-8.5), -8);
     37     EXPECT_EQ(lrint(-0.5), 0);
     38     EXPECT_EQ(lrint(0.5), 0);
     39     EXPECT_EQ(lrint(-0.5), 0);
     40     EXPECT_EQ(lrint(1.3), 1);
     41     EXPECT_EQ(lrint(1.7), 2);
     42     EXPECT_EQ(lrint(0), 0);
     43     EXPECT_EQ(lrint(-0), 0);
     44     if (sizeof(long int) == 8) {
     45         // Largest double number with 0.5 precision and one halfway rounding case below.
     46         EXPECT_EQ(lrint(pow(2.0, 52) - 0.5), pow(2.0, 52));
     47         EXPECT_EQ(lrint(pow(2.0, 52) - 1.5), pow(2.0, 52) - 2);
     48         // Smallest double number with 0.5 precision and one halfway rounding case above.
     49         EXPECT_EQ(lrint(-pow(2.0, 52) + 0.5), -pow(2.0, 52));
     50         EXPECT_EQ(lrint(-pow(2.0, 52) + 1.5), -pow(2.0, 52) + 2);
     51     }
     52 }
     53 
     54 TEST(WTF, clampToIntLong)
     55 {
     56     if (sizeof(long) == sizeof(int))
     57         return;
     58 
     59     long maxInt = std::numeric_limits<int>::max();
     60     long minInt = std::numeric_limits<int>::min();
     61     long overflowInt = maxInt + 1;
     62     long underflowInt = minInt - 1;
     63 
     64     EXPECT_GT(overflowInt, maxInt);
     65     EXPECT_LT(underflowInt, minInt);
     66 
     67     EXPECT_EQ(clampTo<int>(maxInt), maxInt);
     68     EXPECT_EQ(clampTo<int>(minInt), minInt);
     69 
     70     EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
     71     EXPECT_EQ(clampTo<int>(underflowInt), minInt);
     72 }
     73 
     74 TEST(WTF, clampToIntLongLong)
     75 {
     76     long long maxInt = std::numeric_limits<int>::max();
     77     long long minInt = std::numeric_limits<int>::min();
     78     long long overflowInt = maxInt + 1;
     79     long long underflowInt = minInt - 1;
     80 
     81     EXPECT_GT(overflowInt, maxInt);
     82     EXPECT_LT(underflowInt, minInt);
     83 
     84     EXPECT_EQ(clampTo<int>(maxInt), maxInt);
     85     EXPECT_EQ(clampTo<int>(minInt), minInt);
     86 
     87     EXPECT_EQ(clampTo<int>(overflowInt), maxInt);
     88     EXPECT_EQ(clampTo<int>(underflowInt), minInt);
     89 }
     90 
     91 TEST(WTF, clampToIntegerFloat)
     92 {
     93     // This test is inaccurate as floats will round the min / max integer
     94     // due to the narrow mantissa. However it will properly checks within
     95     // (close to the extreme) and outside the integer range.
     96     float maxInt = std::numeric_limits<int>::max();
     97     float minInt = std::numeric_limits<int>::min();
     98     float overflowInt = maxInt * 1.1;
     99     float underflowInt = minInt * 1.1;
    100 
    101     EXPECT_GT(overflowInt, maxInt);
    102     EXPECT_LT(underflowInt, minInt);
    103 
    104     // If maxInt == 2^31 - 1 (ie on I32 architecture), the closest float used to represent it is 2^31.
    105     EXPECT_NEAR(clampToInteger(maxInt), maxInt, 1);
    106     EXPECT_EQ(clampToInteger(minInt), minInt);
    107 
    108     EXPECT_NEAR(clampToInteger(overflowInt), maxInt, 1);
    109     EXPECT_EQ(clampToInteger(underflowInt), minInt);
    110 }
    111 
    112 TEST(WTF, clampToIntegerDouble)
    113 {
    114     double maxInt = std::numeric_limits<int>::max();
    115     double minInt = std::numeric_limits<int>::min();
    116     double overflowInt = maxInt + 1;
    117     double underflowInt = minInt - 1;
    118 
    119     EXPECT_GT(overflowInt, maxInt);
    120     EXPECT_LT(underflowInt, minInt);
    121 
    122     EXPECT_EQ(clampToInteger(maxInt), maxInt);
    123     EXPECT_EQ(clampToInteger(minInt), minInt);
    124 
    125     EXPECT_EQ(clampToInteger(overflowInt), maxInt);
    126     EXPECT_EQ(clampToInteger(underflowInt), minInt);
    127 }
    128 
    129 TEST(WTF, clampToFloat)
    130 {
    131     double maxFloat = std::numeric_limits<float>::max();
    132     double minFloat = -maxFloat;
    133     double overflowFloat = maxFloat * 1.1;
    134     double underflowFloat = minFloat * 1.1;
    135 
    136     EXPECT_GT(overflowFloat, maxFloat);
    137     EXPECT_LT(underflowFloat, minFloat);
    138 
    139     EXPECT_EQ(clampToFloat(maxFloat), maxFloat);
    140     EXPECT_EQ(clampToFloat(minFloat), minFloat);
    141 
    142     EXPECT_EQ(clampToFloat(overflowFloat), maxFloat);
    143     EXPECT_EQ(clampToFloat(underflowFloat), minFloat);
    144 
    145     EXPECT_EQ(clampToFloat(std::numeric_limits<float>::infinity()), maxFloat);
    146     EXPECT_EQ(clampToFloat(-std::numeric_limits<float>::infinity()), minFloat);
    147 }
    148 
    149 TEST(WTF, clampToUnsignedLong)
    150 {
    151     if (sizeof(unsigned long) == sizeof(unsigned))
    152         return;
    153 
    154     unsigned long maxUnsigned = std::numeric_limits<unsigned>::max();
    155     unsigned long overflowUnsigned = maxUnsigned + 1;
    156 
    157     EXPECT_GT(overflowUnsigned, maxUnsigned);
    158 
    159     EXPECT_EQ(clampTo<unsigned>(maxUnsigned), maxUnsigned);
    160 
    161     EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
    162     EXPECT_EQ(clampTo<unsigned>(-1), 0u);
    163 }
    164 
    165 TEST(WTF, clampToUnsignedLongLong)
    166 {
    167     unsigned long long maxUnsigned = std::numeric_limits<unsigned>::max();
    168     unsigned long long overflowUnsigned = maxUnsigned + 1;
    169 
    170     EXPECT_GT(overflowUnsigned, maxUnsigned);
    171 
    172     EXPECT_EQ(clampTo<unsigned>(maxUnsigned), maxUnsigned);
    173 
    174     EXPECT_EQ(clampTo<unsigned>(overflowUnsigned), maxUnsigned);
    175     EXPECT_EQ(clampTo<unsigned>(-1), 0u);
    176 }
    177 
    178 } // namespace
    179