brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · 97f4a24 Raw
85 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// <complex>10 11// template<class T>12//   complex<T>13//   __sqr(const complex<T>& x);14 15#include <complex>16#include <cassert>17 18#include "test_macros.h"19 20template <class T>21void22test()23{24    const T tolerance = std::is_same<T, float>::value ? 1.e-6 : 1.e-14;25 26    typedef std::complex<T> cplx;27    struct test_case28    {29        cplx value;30        cplx expected;31    };32 33    const test_case cases[] = {34        {cplx( 0,  0), cplx( 0,  0)},35        {cplx( 1,  0), cplx( 1,  0)},36        {cplx( 2,  0), cplx( 4,  0)},37        {cplx(-1,  0), cplx( 1,  0)},38        {cplx( 0,  1), cplx(-1,  0)},39        {cplx( 0,  2), cplx(-4,  0)},40        {cplx( 0, -1), cplx(-1,  0)},41        {cplx( 1,  1), cplx( 0,  2)},42        {cplx( 1, -1), cplx( 0, -2)},43        {cplx(-1, -1), cplx( 0,  2)},44        {cplx(0.5, 0), cplx(0.25, 0)},45    };46 47    const unsigned num_cases = sizeof(cases) / sizeof(test_case);48    for (unsigned i = 0; i < num_cases; ++i)49    {50        const test_case& test = cases[i];51        const std::complex<T> actual = std::__sqr(test.value);52        assert(std::abs(actual.real() - test.expected.real()) < tolerance);53        assert(std::abs(actual.imag() - test.expected.imag()) < tolerance);54    }55 56    const cplx nan1 = std::__sqr(cplx(NAN, 0));57    assert(std::isnan(nan1.real()));58    assert(std::isnan(nan1.imag()));59 60    const cplx nan2 = std::__sqr(cplx(0, NAN));61    assert(std::isnan(nan2.real()));62    assert(std::isnan(nan2.imag()));63 64    const cplx nan3 = std::__sqr(cplx(NAN, NAN));65    assert(std::isnan(nan3.real()));66    assert(std::isnan(nan3.imag()));67 68    const cplx inf1 = std::__sqr(cplx(INFINITY, 0));69    assert(std::isinf(inf1.real()));70    assert(inf1.real() > 0);71 72    const cplx inf2 = std::__sqr(cplx(0, INFINITY));73    assert(std::isinf(inf2.real()));74    assert(inf2.real() < 0);75}76 77int main(int, char**)78{79    test<float>();80    test<double>();81    test<long double>();82 83  return 0;84}85