/*
 * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 * @test
 * @bug 1234566
 * @summary Verify compareTo works for a range of arguments
 * @author Joseph D. Darcy
 */

import java.math.*;
import static java.math.BigInteger.*;

public class CompareToTests {
    
    public static void main(String... argv) {
	int failures = 0;

	BigInteger[] specialValues = {
	    new BigInteger("-9223372036854775809"), // Long.MIN_VALUE - 1
	    valueOf(Long.MIN_VALUE),
	    valueOf(-2147483649L), // Integer.MIN_VALUE - 1
	    valueOf(Integer.MIN_VALUE),
	    valueOf(-2),
	    valueOf(-1),
	    valueOf(0),
	    valueOf(1),
	    valueOf(2),
	    valueOf(Integer.MAX_VALUE),
	    valueOf(2147483648L), // Integer.MAX_VALUE + 1
	    valueOf(Long.MAX_VALUE),
	    new BigInteger("9223372036854775808"), // Long.MAX_VALUE +1
	};
	
	for (int i = 0; i < specialValues.length; i++) {
	    BigInteger rhs = specialValues[i];
	    
	    failures += compare(rhs, rhs, 0); // Self-compare

	    for (int j = i+1; j < specialValues.length; j++) {
		BigInteger lhs = specialValues[j];
		failures += compare(rhs, lhs, -1);
		failures += compare(lhs, rhs,  1);
	    }

	}

	if (failures > 0) {
	    System.err.printf("%d failures encountered.", failures);
	    throw new RuntimeException();
	}
    }

    private static int compare(BigInteger rhs,  BigInteger lhs, int expected) {
	int compareResult = rhs.compareTo(lhs);
	if (compareResult != expected) {
	    System.err.printf("Bad compareTo on %s.compareTo(%s) => %d, expected %d%n", 
			      rhs, lhs, compareResult, expected);
	    return 1;
	} else
	    return 0;
    }
}
