Path: blob/master/test/jdk/java/lang/IntegralPrimitiveToString.java
41145 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.math.BigInteger;27import java.util.ArrayList;28import java.util.Iterator;29import java.util.Arrays;30import java.util.List;31import java.util.function.LongFunction;32import java.util.function.Function;3334import static org.testng.Assert.assertEquals;3536/**37* @test38* @run testng IntegralPrimitiveToString39* @summary test string conversions for primitive integral types.40* @author Mike Duigou41*/42public class IntegralPrimitiveToString {4344@Test(dataProvider="numbers")45public <N extends Number> void testToString(String description,46Function<N, BigInteger> converter,47Function<N, BigInteger> unsignedConverter,48N[] values,49Stringifier<N>[] stringifiers) {50System.out.printf("%s : conversions: %d values: %d\n", description, stringifiers.length, values.length);51for( N value : values) {52BigInteger asBigInt = converter.apply(value);53BigInteger asUnsignedBigInt = unsignedConverter.apply(value);54for(Stringifier<N> stringifier : stringifiers) {55stringifier.assertMatchingToString(value, asBigInt, asUnsignedBigInt, description);56}57}58}5960static class Stringifier<N extends Number> {61final boolean signed;62final int radix;63final Function<N,String> toString;64Stringifier(boolean signed, int radix, Function<N,String> toString) {65this.signed = signed;66this.radix = radix;67this.toString = toString;68}6970public void assertMatchingToString(N value, BigInteger asSigned, BigInteger asUnsigned, String description) {71String expected = signed72? asSigned.toString(radix)73: asUnsigned.toString(radix);7475String actual = toString.apply(value);7677assertEquals(actual, expected, description + " conversion should be the same");78}79}8081@DataProvider(name="numbers", parallel=true)82public Iterator<Object[]> testSetProvider() {8384return Arrays.asList(85new Object[] { "Byte",86(Function<Byte,BigInteger>) b -> BigInteger.valueOf((long) b),87(Function<Byte,BigInteger>) b -> BigInteger.valueOf(Integer.toUnsignedLong((byte) b)),88numberProvider((LongFunction<Byte>) l -> Byte.valueOf((byte) l), Byte.SIZE),89new Stringifier[] {90new Stringifier<Byte>(true, 10, b -> b.toString()),91new Stringifier<Byte>(true, 10, b -> Byte.toString(b))92}93},94new Object[] { "Short",95(Function<Short,BigInteger>) s -> BigInteger.valueOf((long) s),96(Function<Short,BigInteger>) s -> BigInteger.valueOf(Integer.toUnsignedLong((short) s)),97numberProvider((LongFunction<Short>) l -> Short.valueOf((short) l), Short.SIZE),98new Stringifier[] {99new Stringifier<Short>(true, 10, s -> s.toString()),100new Stringifier<Short>(true, 10, s -> Short.toString( s))101}102},103new Object[] { "Integer",104(Function<Integer,BigInteger>) i -> BigInteger.valueOf((long) i),105(Function<Integer,BigInteger>) i -> BigInteger.valueOf(Integer.toUnsignedLong(i)),106numberProvider((LongFunction<Integer>) l -> Integer.valueOf((int) l), Integer.SIZE),107new Stringifier[] {108new Stringifier<Integer>(true, 10, i -> i.toString()),109new Stringifier<Integer>(true, 10, i -> Integer.toString(i)),110new Stringifier<Integer>(false, 2, Integer::toBinaryString),111new Stringifier<Integer>(false, 16, Integer::toHexString),112new Stringifier<Integer>(false, 8, Integer::toOctalString),113new Stringifier<Integer>(true, 2, i -> Integer.toString(i, 2)),114new Stringifier<Integer>(true, 8, i -> Integer.toString(i, 8)),115new Stringifier<Integer>(true, 10, i -> Integer.toString(i, 10)),116new Stringifier<Integer>(true, 16, i -> Integer.toString(i, 16)),117new Stringifier<Integer>(true, Character.MAX_RADIX, i -> Integer.toString(i, Character.MAX_RADIX)),118new Stringifier<Integer>(false, 10, i -> Integer.toUnsignedString(i)),119new Stringifier<Integer>(false, 2, i -> Integer.toUnsignedString(i, 2)),120new Stringifier<Integer>(false, 8, i -> Integer.toUnsignedString(i, 8)),121new Stringifier<Integer>(false, 10, i -> Integer.toUnsignedString(i, 10)),122new Stringifier<Integer>(false, 16, i -> Integer.toUnsignedString(i, 16)),123new Stringifier<Integer>(false, Character.MAX_RADIX, i -> Integer.toUnsignedString(i, Character.MAX_RADIX))124}125},126new Object[] { "Long",127(Function<Long, BigInteger>) BigInteger::valueOf,128(Function<Long, BigInteger>) l -> {129if (l >= 0) {130return BigInteger.valueOf((long) l);131} else {132int upper = (int)(l >>> 32);133int lower = (int) (long) l;134135// return (upper << 32) + lower136return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32).137add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));138}139},140numberProvider((LongFunction<Long>) Long::valueOf, Long.SIZE),141new Stringifier[] {142new Stringifier<Long>(true, 10, l -> l.toString()),143new Stringifier<Long>(true, 10, l -> Long.toString(l)),144new Stringifier<Long>(false, 2, Long::toBinaryString),145new Stringifier<Long>(false, 16, Long::toHexString),146new Stringifier<Long>(false, 8, Long::toOctalString),147new Stringifier<Long>(true, 2, l -> Long.toString(l, 2)),148new Stringifier<Long>(true, 8, l -> Long.toString(l, 8)),149new Stringifier<Long>(true, 10, l -> Long.toString(l, 10)),150new Stringifier<Long>(true, 16, l -> Long.toString(l, 16)),151new Stringifier<Long>(true, Character.MAX_RADIX, l -> Long.toString(l, Character.MAX_RADIX)),152new Stringifier<Long>(false, 10, Long::toUnsignedString),153new Stringifier<Long>(false, 2, l -> Long.toUnsignedString(l, 2)),154new Stringifier<Long>(false, 8, l-> Long.toUnsignedString(l, 8)),155new Stringifier<Long>(false, 10, l -> Long.toUnsignedString(l, 10)),156new Stringifier<Long>(false, 16, l -> Long.toUnsignedString(l, 16)),157new Stringifier<Long>(false, Character.MAX_RADIX, l -> Long.toUnsignedString(l, Character.MAX_RADIX))158}159}160).iterator();161}162private static final long[] SOME_PRIMES = {1633L, 5L, 7L, 11L, 13L, 17L, 19L, 23L, 29L, 31L, 37L, 41L, 43L, 47L, 53L,16459L, 61L, 71L, 73L, 79L, 83L, 89L, 97L, 101L, 103L, 107L, 109L, 113L,1655953L, 5981L, 5987L, 6007L, 6011L, 6029L, 6037L, 6043L, 6047L, 6053L,16616369L, 16381L, 16411L, 32749L, 32771L, 65521L, 65537L,167(long) Integer.MAX_VALUE };168169public <N extends Number> N[] numberProvider(LongFunction<N> boxer, int bits, N... extras) {170List<N> numbers = new ArrayList<>();171172for(int bitmag = 0; bitmag < bits; bitmag++) {173long value = 1L << bitmag;174numbers.add(boxer.apply(value));175numbers.add(boxer.apply(value - 1));176numbers.add(boxer.apply(value + 1));177numbers.add(boxer.apply(-value));178for(int divisor = 0; divisor < SOME_PRIMES.length && value < SOME_PRIMES[divisor]; divisor++) {179numbers.add(boxer.apply(value - SOME_PRIMES[divisor]));180numbers.add(boxer.apply(value + SOME_PRIMES[divisor]));181numbers.add(boxer.apply(value * SOME_PRIMES[divisor]));182numbers.add(boxer.apply(value / SOME_PRIMES[divisor]));183numbers.add(boxer.apply(value | SOME_PRIMES[divisor]));184numbers.add(boxer.apply(value & SOME_PRIMES[divisor]));185numbers.add(boxer.apply(value ^ SOME_PRIMES[divisor]));186}187}188189numbers.addAll(Arrays.asList(extras));190191return (N[]) numbers.toArray(new Number[numbers.size()]);192}193}194195196