/*
 * 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.
 */
package compiler.intrinsics;

import compiler.lib.ir_framework.*;
import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;

/*
 * @test
 * @key randomness
 * @bug 8283726
 * @requires os.arch=="amd64" | os.arch=="x86_64"
 * @summary Test the intrinsics implementation of Integer/Long::compareUnsigned
 * @library /test/lib /
 * @run driver compiler.intrinsics.TestCompareUnsigned
 */
public class TestCompareUnsigned {
    static final int TRUE_VALUE = 10;
    static final int FALSE_VALUE = 4;

    public static void main(String[] args) {
        var test = new TestFramework(TestCompareUnsigned.class);
        test.setDefaultWarmup(1);
        test.start();
    }

    static int expectedResult(int x, int y) {
        return Integer.compare(x + Integer.MIN_VALUE, y + Integer.MIN_VALUE);
    }

    static int expectedResult(long x, long y) {
        return Long.compare(x + Long.MIN_VALUE, y + Long.MIN_VALUE);
    }

    @Test
    @IR(failOn = {IRNode.CMP_U3})
    @IR(counts = {IRNode.CMP_U, "1"})
    public int lessThanInt(int x, int y) {
        return Integer.compareUnsigned(x, y) < 0 ? TRUE_VALUE : FALSE_VALUE;
    }

    @Test
    @IR(failOn = {IRNode.CMP_UL3})
    @IR(counts = {IRNode.CMP_UL, "1"})
    public int lessThanLong(long x, long y) {
        return Long.compareUnsigned(x, y) < 0 ? TRUE_VALUE : FALSE_VALUE;
    }

    @Test
    @IR(counts = {IRNode.CMP_U3, "1"})
    public int compareInt(int x, int y) {
        return Integer.compareUnsigned(x, y);
    }

    @Test
    @IR(counts = {IRNode.CMP_UL3, "1"})
    public int compareLong(long x, long y) {
        return Long.compareUnsigned(x, y);
    }

    @Run(test = {"lessThanInt", "lessThanLong", "compareInt", "compareLong"})
    public void runTests() {
        var random = Utils.getRandomInstance();
        for (int i = 0; i < 1000; i++) {
            int x = random.nextInt();
            int y = random.nextInt();
            Asserts.assertEquals(lessThanInt(x, x), FALSE_VALUE);
            Asserts.assertEquals(compareInt(x, x), 0);
            Asserts.assertEquals(lessThanInt(x, y), expectedResult(x, y) < 0 ? TRUE_VALUE : FALSE_VALUE);
            Asserts.assertEquals(compareInt(x, y), expectedResult(x, y));
        }
        for (int i = 0; i < 1000; i++) {
            long x = random.nextLong();
            long y = random.nextLong();
            Asserts.assertEquals(lessThanLong(x, x), FALSE_VALUE);
            Asserts.assertEquals(compareLong(x, x), 0);
            Asserts.assertEquals(lessThanLong(x, y), expectedResult(x, y) < 0 ? TRUE_VALUE : FALSE_VALUE);
            Asserts.assertEquals(compareLong(x, y), expectedResult(x, y));
        }
    }
}