chromium/v8/test/unittests/base/ieee754-unittest.cc

// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/base/ieee754.h"

#include <limits>

#include "src/base/overflowing-math.h"
#include "testing/gmock-support.h"

BitEq;
IsNaN;

namespace v8 {
namespace base {
namespace ieee754 {

namespace {

double const kE =;
double const kPI =;
double const kTwo120 =;
double const kInfinity =;
double const kQNaN =;
double const kSNaN =;

}  // namespace

TEST(Ieee754, Acos) {}

TEST(Ieee754, Acosh) {}

TEST(Ieee754, Asin) {}

TEST(Ieee754, Asinh) {}

TEST(Ieee754, Atan) {}

TEST(Ieee754, Atan2) {}

TEST(Ieee754, Atanh) {}

#if defined(V8_USE_LIBM_TRIG_FUNCTIONS)
TEST(Ieee754, LibmCos) {}

TEST(Ieee754, LibmSin) {}

TEST(Ieee754, FdlibmCos) {}

TEST(Ieee754, FdlibmSin) {}

#else

TEST(Ieee754, Cos) {
  // Test values mentioned in the ECMAScript spec.
  EXPECT_THAT(cos(kQNaN), IsNaN());
  EXPECT_THAT(cos(kSNaN), IsNaN());
  EXPECT_THAT(cos(kInfinity), IsNaN());
  EXPECT_THAT(cos(-kInfinity), IsNaN());

  // Tests for cos for |x| < pi/4
  EXPECT_EQ(1.0, 1 / cos(-0.0));
  EXPECT_EQ(1.0, 1 / cos(0.0));
  // cos(x) = 1 for |x| < 2^-27
  EXPECT_EQ(1, cos(2.3283064365386963e-10));
  EXPECT_EQ(1, cos(-2.3283064365386963e-10));
  // Test KERNELCOS for |x| < 0.3.
  // cos(pi/20) = sqrt(sqrt(2)*sqrt(sqrt(5)+5)+4)/2^(3/2)
  EXPECT_EQ(0.9876883405951378, cos(0.15707963267948966));
  // Test KERNELCOS for x ~= 0.78125
  EXPECT_EQ(0.7100335477927638, cos(0.7812504768371582));
  EXPECT_EQ(0.7100338835660797, cos(0.78125));
  // Test KERNELCOS for |x| > 0.3.
  // cos(pi/8) = sqrt(sqrt(2)+1)/2^(3/4)
  EXPECT_EQ(0.9238795325112867, cos(0.39269908169872414));
  // Test KERNELTAN for |x| < 0.67434.
  EXPECT_EQ(0.9238795325112867, cos(-0.39269908169872414));

  // Tests for cos.
  EXPECT_EQ(1, cos(3.725290298461914e-9));
  // Cover different code paths in KERNELCOS.
  EXPECT_EQ(0.9689124217106447, cos(0.25));
  EXPECT_EQ(0.8775825618903728, cos(0.5));
  EXPECT_EQ(0.7073882691671998, cos(0.785));
  // Test that cos(Math.PI/2) != 0 since Math.PI is not exact.
  EXPECT_EQ(6.123233995736766e-17, cos(1.5707963267948966));
  // Test cos for various phases.
  EXPECT_EQ(0.7071067811865474, cos(7.0 / 4 * kPI));
  EXPECT_EQ(0.7071067811865477, cos(9.0 / 4 * kPI));
  EXPECT_EQ(-0.7071067811865467, cos(11.0 / 4 * kPI));
  EXPECT_EQ(-0.7071067811865471, cos(13.0 / 4 * kPI));
  EXPECT_EQ(0.9367521275331447, cos(1000000.0));
  EXPECT_EQ(-3.435757038074824e-12, cos(1048575.0 / 2 * kPI));

  // Test Hayne-Panek reduction.
  EXPECT_EQ(-0.9258790228548379e0, cos(kTwo120));
  EXPECT_EQ(-0.9258790228548379e0, cos(-kTwo120));
}

TEST(Ieee754, Sin) {
  // Test values mentioned in the ECMAScript spec.
  EXPECT_THAT(sin(kQNaN), IsNaN());
  EXPECT_THAT(sin(kSNaN), IsNaN());
  EXPECT_THAT(sin(kInfinity), IsNaN());
  EXPECT_THAT(sin(-kInfinity), IsNaN());

  // Tests for sin for |x| < pi/4
  EXPECT_EQ(-kInfinity, Divide(1.0, sin(-0.0)));
  EXPECT_EQ(kInfinity, Divide(1.0, sin(0.0)));
  // sin(x) = x for x < 2^-27
  EXPECT_EQ(2.3283064365386963e-10, sin(2.3283064365386963e-10));
  EXPECT_EQ(-2.3283064365386963e-10, sin(-2.3283064365386963e-10));
  // sin(pi/8) = sqrt(sqrt(2)-1)/2^(3/4)
  EXPECT_EQ(0.3826834323650898, sin(0.39269908169872414));
  EXPECT_EQ(-0.3826834323650898, sin(-0.39269908169872414));

  // Tests for sin.
  EXPECT_EQ(0.479425538604203, sin(0.5));
  EXPECT_EQ(-0.479425538604203, sin(-0.5));
  EXPECT_EQ(1, sin(kPI / 2.0));
  EXPECT_EQ(-1, sin(-kPI / 2.0));
  // Test that sin(Math.PI) != 0 since Math.PI is not exact.
  EXPECT_EQ(1.2246467991473532e-16, sin(kPI));
  EXPECT_EQ(-7.047032979958965e-14, sin(2200.0 * kPI));
  // Test sin for various phases.
  EXPECT_EQ(-0.7071067811865477, sin(7.0 / 4.0 * kPI));
  EXPECT_EQ(0.7071067811865474, sin(9.0 / 4.0 * kPI));
  EXPECT_EQ(0.7071067811865483, sin(11.0 / 4.0 * kPI));
  EXPECT_EQ(-0.7071067811865479, sin(13.0 / 4.0 * kPI));
  EXPECT_EQ(-3.2103381051568376e-11, sin(1048576.0 / 4 * kPI));

  // Test Hayne-Panek reduction.
  EXPECT_EQ(0.377820109360752e0, sin(kTwo120));
  EXPECT_EQ(-0.377820109360752e0, sin(-kTwo120));
}

#endif

TEST(Ieee754, Cosh) {}

TEST(Ieee754, Exp) {}

TEST(Ieee754, Expm1) {}

TEST(Ieee754, Log) {}

TEST(Ieee754, Log1p) {}

TEST(Ieee754, Log2) {}

TEST(Ieee754, Log10) {}

TEST(Ieee754, Cbrt) {}

TEST(Ieee754, Sinh) {}

TEST(Ieee754, Tan) {}

TEST(Ieee754, Tanh) {}

}  // namespace ieee754
}  // namespace base
}  // namespace v8