/*
 * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8205592
 * @summary Verify {double, float}Value methods work
 * @library /test/lib
 * @key randomness
 * @build jdk.test.lib.RandomFactory
 * @run main DoubleFloatValueTests
 */

import jdk.test.lib.RandomFactory;

import java.math.BigDecimal;
import java.util.Random;

public class DoubleFloatValueTests {
    private static final BigDecimal HALF = BigDecimal.valueOf(5, 1);
    private static final BigDecimal EPS = BigDecimal.valueOf(1, 10_000);

    private static BigDecimal nextHalfUp(double v) {
        BigDecimal bv = new BigDecimal(v);
        BigDecimal ulp = new BigDecimal(Math.ulp(v));
        return bv.add(ulp.multiply(HALF));
    }

    private static BigDecimal nextHalfDown(double v) {
        BigDecimal bv = new BigDecimal(v);
        BigDecimal ulp = new BigDecimal(v - Math.nextDown(v));
        return bv.subtract(ulp.multiply(HALF));
    }

    private static BigDecimal nextHalfUp(float v) {
        BigDecimal bv = new BigDecimal(v);
        BigDecimal ulp = new BigDecimal(Math.ulp(v));
        return bv.add(ulp.multiply(HALF));
    }

    private static BigDecimal nextHalfDown(float v) {
        BigDecimal bv = new BigDecimal(v);
        BigDecimal ulp = new BigDecimal(v - Math.nextDown(v));
        return bv.subtract(ulp.multiply(HALF));
    }

    private static String toDecHexString(double v) {
        return v + " (" + Double.toHexString(v) + ")";
    }

    private static String toDecHexString(float v) {
        return v + " (" + Float.toHexString(v) + ")";
    }

    private static void checkDouble(BigDecimal bd, double exp) {
        double res = bd.doubleValue();
        if (exp != res ) {
            String message = "Bad conversion: got " + toDecHexString(res) +
                    ", expected " + toDecHexString(exp);
            throw new RuntimeException(message);
        }
    }

    private static void checkFloat(BigDecimal bv, float exp) {
        float res = bv.floatValue();
        if (exp != res ) {
            String message = "Bad conversion: got " + toDecHexString(res) +
                    ", expected " + toDecHexString(exp);
            throw new RuntimeException(message);
        }
    }

    private static boolean isOdd(int n) {
        return (n & 0x1) != 0;
    }

    private static void testDoubleValueNearMinValue() {
        for (int n = 0; n < 100; ++n) {
            BigDecimal b = nextHalfUp(n * Double.MIN_VALUE);
            checkDouble(b, ((n + 1) / 2 * 2) * Double.MIN_VALUE);
            checkDouble(b.subtract(EPS), n * Double.MIN_VALUE);
            checkDouble(b.add(EPS), (n + 1) * Double.MIN_VALUE);
        }
    }

    private static void testFloatValueNearMinValue() {
        for (int n = 0; n < 100; ++n) {
            BigDecimal b = nextHalfUp(n * Float.MIN_VALUE);
            checkFloat(b, ((n + 1) / 2 * 2) * Float.MIN_VALUE);
            checkFloat(b.subtract(EPS), n * Float.MIN_VALUE);
            checkFloat(b.add(EPS), (n + 1) * Float.MIN_VALUE);
        }
    }

    private static void testDoubleValueNearMinNormal() {
        double v = Double.MIN_NORMAL;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfDown(v);
            checkDouble(bv, isOdd(n) ? Math.nextDown(v) : v);
            checkDouble(bv.subtract(EPS), Math.nextDown(v));
            checkDouble(bv.add(EPS), v);
            v = Math.nextDown(v);
        }
        v = Double.MIN_NORMAL;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfUp(v);
            checkDouble(bv, isOdd(n) ? Math.nextUp(v) : v);
            checkDouble(bv.subtract(EPS), v);
            checkDouble(bv.add(EPS), Math.nextUp(v));
            v = Math.nextUp(v);
        }
    }

    private static void testFloatValueNearMinNormal() {
        float v = Float.MIN_NORMAL;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfDown(v);
            checkFloat(bv, isOdd(n) ? Math.nextDown(v) : v);
            checkFloat(bv.subtract(EPS), Math.nextDown(v));
            checkFloat(bv.add(EPS), v);
            v = Math.nextDown(v);
        }
        v = Float.MIN_NORMAL;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfUp(v);
            checkFloat(bv, isOdd(n) ? Math.nextUp(v) : v);
            checkFloat(bv.subtract(EPS), v);
            checkFloat(bv.add(EPS), Math.nextUp(v));
            v = Math.nextUp(v);
        }
    }

    private static void testDoubleValueNearMaxValue() {
        double v = Double.MAX_VALUE;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfDown(v);
            checkDouble(bv, isOdd(n) ? v : Math.nextDown(v));
            checkDouble(bv.subtract(EPS), Math.nextDown(v));
            checkDouble(bv.add(EPS), v);
            v = Math.nextDown(v);
        }
        BigDecimal bv = nextHalfUp(Double.MAX_VALUE);
        checkDouble(bv, Double.POSITIVE_INFINITY);
        checkDouble(bv.subtract(EPS), Double.MAX_VALUE);
        checkDouble(bv.add(EPS), Double.POSITIVE_INFINITY);
    }

    private static void testFloatValueNearMaxValue() {
        float v = Float.MAX_VALUE;
        for (int n = 0; n < 100; ++n) {
            BigDecimal bv = nextHalfDown(v);
            checkFloat(bv, isOdd(n) ? v : Math.nextDown(v));
            checkFloat(bv.subtract(EPS), Math.nextDown(v));
            checkFloat(bv.add(EPS), v);
            v = Math.nextDown(v);
        }
        BigDecimal bv = nextHalfUp(Float.MAX_VALUE);
        checkFloat(bv, Float.POSITIVE_INFINITY);
        checkFloat(bv.subtract(EPS), Float.MAX_VALUE);
        checkFloat(bv.add(EPS), Float.POSITIVE_INFINITY);
    }

    private static void testDoubleValueRandom() {
        Random r = RandomFactory.getRandom();
        for (int i = 0; i < 10_000; ++i) {
            double v = r.nextDouble(-Double.MAX_VALUE, Double.MAX_VALUE);
            checkDouble(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            double v = r.nextDouble(-1e9, 1e9);
            checkDouble(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            double v = r.nextDouble(-1e6, 1e6);
            checkDouble(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            double v = r.nextDouble(-1e-6, 1e-6);
            checkDouble(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            double v = r.nextDouble(-1e-9, 1e-9);
            checkDouble(new BigDecimal(v), v);
        }
    }

    private static void testFloatValueRandom() {
        Random r = RandomFactory.getRandom();
        for (int i = 0; i < 10_000; ++i) {
            float v = r.nextFloat(-Float.MAX_VALUE, Float.MAX_VALUE);
            checkFloat(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            float v = r.nextFloat(-1e9f, 1e9f);
            checkFloat(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            float v = r.nextFloat(-1e6f, 1e6f);
            checkFloat(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            float v = r.nextFloat(-1e-6f, 1e-6f);
            checkFloat(new BigDecimal(v), v);
        }
        for (int i = 0; i < 10_000; ++i) {
            float v = r.nextFloat(-1e-9f, 1e-9f);
            checkFloat(new BigDecimal(v), v);
        }
    }

    private static void testDoubleValueExtremes() {
        checkDouble(BigDecimal.valueOf(1, 1000), 0.0);
        checkDouble(BigDecimal.valueOf(-1, 1000), -0.0);
        checkDouble(BigDecimal.valueOf(1, -1000), Double.POSITIVE_INFINITY);
        checkDouble(BigDecimal.valueOf(-1, -1000), -Double.POSITIVE_INFINITY);
    }

    private static void testFloatValueExtremes() {
        checkFloat(BigDecimal.valueOf(1, 1000), 0.0f);
        checkFloat(BigDecimal.valueOf(-1, 1000), -0.0f);
        checkFloat(BigDecimal.valueOf(1, -1000), Float.POSITIVE_INFINITY);
        checkFloat(BigDecimal.valueOf(-1, -1000), -Float.POSITIVE_INFINITY);
    }

    public static void main(String[] args) {
        testDoubleValueNearMinValue();
        testDoubleValueNearMinNormal();
        testDoubleValueNearMaxValue();
        testDoubleValueRandom();
        testDoubleValueExtremes();

        testFloatValueNearMinValue();
        testFloatValueNearMinNormal();
        testFloatValueNearMaxValue();
        testFloatValueRandom();
        testFloatValueExtremes();
    }

}
