From cae5202be0bc915e46c4b96d8c16d92aa655a3e7 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 10 Nov 2008 17:07:15 -0700 Subject: [PATCH] split Misc.java into four separate files --- test/Arrays.java | 53 +++++++++ test/Integers.java | 62 ++++++++++ test/Longs.java | 192 ++++++++++++++++++++++++++++++ test/Misc.java | 285 --------------------------------------------- 4 files changed, 307 insertions(+), 285 deletions(-) create mode 100644 test/Arrays.java create mode 100644 test/Integers.java create mode 100644 test/Longs.java diff --git a/test/Arrays.java b/test/Arrays.java new file mode 100644 index 0000000000..b66c3177ac --- /dev/null +++ b/test/Arrays.java @@ -0,0 +1,53 @@ +public class Arrays { + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } + + public static void main(String[] args) { + { int[] array = new int[0]; + Exception exception = null; + try { + int x = array[0]; + } catch (ArrayIndexOutOfBoundsException e) { + exception = e; + } + + expect(exception != null); + } + + { int[] array = new int[3]; + int i = 0; + array[i++] = 1; + array[i++] = 2; + array[i++] = 3; + + expect(array[--i] == 3); + expect(array[--i] == 2); + expect(array[--i] == 1); + } + + { Object[][] array = new Object[1][1]; + expect(array.length == 1); + expect(array[0].length == 1); + } + + { int j = 0; + byte[] decodeTable = new byte[256]; + for (int i = 'A'; i <= 'Z'; ++i) decodeTable[i] = (byte) j++; + for (int i = 'a'; i <= 'z'; ++i) decodeTable[i] = (byte) j++; + for (int i = '0'; i <= '9'; ++i) decodeTable[i] = (byte) j++; + decodeTable['+'] = (byte) j++; + decodeTable['/'] = (byte) j++; + decodeTable['='] = 0; + + expect(decodeTable['a'] != 0); + } + + { boolean p = true; + int[] array = new int[] { 1, 2 }; + expect(array[0] == array[p ? 0 : 1]); + p = false; + expect(array[1] == array[p ? 0 : 1]); + } + } +} diff --git a/test/Integers.java b/test/Integers.java new file mode 100644 index 0000000000..ef9202cd85 --- /dev/null +++ b/test/Integers.java @@ -0,0 +1,62 @@ +public class Integers { + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } + + public static void main(String[] args) { + { int a = 2; + int b = 2; + int c = a + b; + } + + { int a = -5; + int b = 2; + expect(a >> b == -5 >> 2); + expect(a >>> b == -5 >>> 2); + expect(a << b == -5 << 2); + expect(a + b == -5 + 2); + expect(a - b == -5 - 2); + expect(a * b == -5 * 2); + expect(a / b == -5 / 2); + expect(a % b == -5 % 2); + expect((a & b) == (-5 & 2)); + expect((a | b) == (-5 | 2)); + expect((a ^ b) == (-5 ^ 2)); + expect(-a == 5); + expect(~a == ~-5); + + a = 5; + b = 2; + expect(a >> b == 5 >> 2); + expect(a >>> b == 5 >>> 2); + expect(a << b == 5 << 2); + expect(a + b == 5 + 2); + expect(a - b == 5 - 2); + expect(a * b == 5 * 2); + expect(a / b == 5 / 2); + expect(a % b == 5 % 2); + expect((a & b) == (5 & 2)); + expect((a | b) == (5 | 2)); + expect((a ^ b) == (5 ^ 2)); + expect(-a == -5); + expect(~a == ~5); + } + + { int get_buffer = 2144642881; + int bits_left = 30; + int l = 9; + int code = (((get_buffer >> (bits_left -= (l)))) & ((1<<(l))-1)); + expect(code == 510); + } + + { int width = 8; + int height = 8; + int depth = 24; + int scanlinePad = 4; + + int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) + / scanlinePad * scanlinePad; + expect(bytesPerLine == 24); + } + } +} diff --git a/test/Longs.java b/test/Longs.java new file mode 100644 index 0000000000..c24ae1f340 --- /dev/null +++ b/test/Longs.java @@ -0,0 +1,192 @@ +public class Longs { + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } + + public static void putInt(int val, byte[] dst, int offset) { + System.out.println("put " + val); + dst[offset] = (byte)((val >> 24) & 0xff); + dst[offset+1] = (byte)((val >> 16) & 0xff); + dst[offset+2] = (byte)((val >> 8) & 0xff); + dst[offset+3] = (byte)((val ) & 0xff); + } + + public static void putLong(long val, byte[] dst, int offset) { + putInt((int)(val >> 32), dst, offset); + putInt((int)val, dst, offset + 4); + } + + private static long roundUp(long a, long b) { + a += b - 1L; + return a - (a % b); + } + + public static void main(String[] args) { + { long foo = 25214903884L; + int radix = 10; + expect(foo > 0); + foo /= radix; + expect(foo > 0); + } + + expect(roundUp(156, 2) == 156); + + expect(Long.parseLong("25214903884") == 25214903884L); + + expect(Long.parseLong("-9223372036854775808") == -9223372036854775808L); + + expect(String.valueOf(25214903884L).equals("25214903884")); + + expect(String.valueOf(-9223372036854775808L).equals + ("-9223372036854775808")); + + { long a = -5; + long b = 2; + expect(a >> b == -5L >> 2); + expect(a >>> b == -5L >>> 2); + expect(a << b == -5L << 2); + expect(a + b == -5L + 2L); + expect(a - b == -5L - 2L); + expect(a * b == -5L * 2L); + expect(a / b == -5L / 2L); + expect(a % b == -5L % 2L); + expect((a & b) == (-5L & 2L)); + expect((a | b) == (-5L | 2L)); + expect((a ^ b) == (-5L ^ 2L)); + expect(-a == 5L); + expect(~a == ~-5L); + + a = 5; + b = 2; + expect(a >> b == 5L >> 2); + expect(a >>> b == 5L >>> 2); + expect(a << b == 5L << 2); + expect(a + b == 5L + 2L); + expect(a - b == 5L - 2L); + expect(a * b == 5L * 2L); + expect(a / b == 5L / 2L); + expect(a % b == 5L % 2L); + expect((a & b) == (5L & 2L)); + expect((a | b) == (5L | 2L)); + expect((a ^ b) == (5L ^ 2L)); + expect(-a == -5L); + expect(~a == ~5L); + } + + { long a = -25214903884L; + long b = 2; + expect(a >> b == -25214903884L >> 2); + expect(a >>> b == -25214903884L >>> 2); + expect(a << b == -25214903884L << 2); + expect(a + b == -25214903884L + 2L); + expect(a - b == -25214903884L - 2L); + expect(a * b == -25214903884L * 2L); + expect(a / b == -25214903884L / 2L); + expect(a % b == -25214903884L % 2L); + expect((a & b) == (-25214903884L & 2L)); + expect((a | b) == (-25214903884L | 2L)); + expect((a ^ b) == (-25214903884L ^ 2L)); + expect(-a == 25214903884L); + expect(~a == ~-25214903884L); + + a = 25214903884L; + b = 2; + expect(a >> b == 25214903884L >> 2); + expect(a >>> b == 25214903884L >>> 2); + expect(a << b == 25214903884L << 2); + expect(a + b == 25214903884L + 2L); + expect(a - b == 25214903884L - 2L); + expect(a * b == 25214903884L * 2L); + expect(a / b == 25214903884L / 2L); + expect(a % b == 25214903884L % 2L); + expect((a & b) == (25214903884L & 2L)); + expect((a | b) == (25214903884L | 2L)); + expect((a ^ b) == (25214903884L ^ 2L)); + expect(-a == -25214903884L); + expect(~a == ~25214903884L); + } + + { long b = 2; + expect((-25214903884L) >> b == -25214903884L >> 2); + expect((-25214903884L) >>> b == -25214903884L >>> 2); + expect((-25214903884L) << b == -25214903884L << 2); + expect((-25214903884L) + b == -25214903884L + 2L); + expect((-25214903884L) - b == -25214903884L - 2L); + expect((-25214903884L) * b == -25214903884L * 2L); + expect((-25214903884L) / b == -25214903884L / 2L); + expect((-25214903884L) % b == -25214903884L % 2L); + expect(((-25214903884L) & b) == (-25214903884L & 2L)); + expect(((-25214903884L) | b) == (-25214903884L | 2L)); + expect(((-25214903884L) ^ b) == (-25214903884L ^ 2L)); + + b = 2; + expect(25214903884L >> b == 25214903884L >> 2); + expect(25214903884L >>> b == 25214903884L >>> 2); + expect(25214903884L << b == 25214903884L << 2); + expect(25214903884L + b == 25214903884L + 2L); + expect(25214903884L - b == 25214903884L - 2L); + expect(25214903884L * b == 25214903884L * 2L); + expect(25214903884L / b == 25214903884L / 2L); + expect(25214903884L % b == 25214903884L % 2L); + expect((25214903884L & b) == (25214903884L & 2L)); + expect((25214903884L | b) == (25214903884L | 2L)); + expect((25214903884L ^ b) == (25214903884L ^ 2L)); + } + + { long a = 2L; + expect(a + (-25214903884L) == 2L + (-25214903884L)); + expect(a - (-25214903884L) == 2L - (-25214903884L)); + expect(a * (-25214903884L) == 2L * (-25214903884L)); + expect(a / (-25214903884L) == 2L / (-25214903884L)); + expect(a % (-25214903884L) == 2L % (-25214903884L)); + expect((a & (-25214903884L)) == (2L & (-25214903884L))); + expect((a | (-25214903884L)) == (2L | (-25214903884L))); + expect((a ^ (-25214903884L)) == (2L ^ (-25214903884L))); + + a = 2L; + expect(a + 25214903884L == 2L + 25214903884L); + expect(a - 25214903884L == 2L - 25214903884L); + expect(a * 25214903884L == 2L * 25214903884L); + expect(a / 25214903884L == 2L / 25214903884L); + expect(a % 25214903884L == 2L % 25214903884L); + expect((a & 25214903884L) == (2L & 25214903884L)); + expect((a | 25214903884L) == (2L | 25214903884L)); + expect((a ^ 25214903884L) == (2L ^ 25214903884L)); + } + + { long x = 231; + expect((x >> 32) == 0); + expect((x >>> 32) == 0); + expect((x << 32) == 992137445376L); + + int shift = 32; + expect((x >> shift) == 0); + expect((x >>> shift) == 0); + expect((x << shift) == 992137445376L); + + long y = -231; + expect((y >> 32) == 0xffffffffffffffffL); + expect((y >>> 32) == 0xffffffffL); + } + + expect(Long.valueOf(231L) == 231L); + + { byte[] array = new byte[8]; + putLong(231, array, 0); + expect((array[0] & 0xff) == 0); + expect((array[1] & 0xff) == 0); + expect((array[2] & 0xff) == 0); + expect((array[3] & 0xff) == 0); + expect((array[4] & 0xff) == 0); + expect((array[5] & 0xff) == 0); + expect((array[6] & 0xff) == 0); + expect((array[7] & 0xff) == 231); + } + + java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(8); + buffer.putLong(231); + buffer.flip(); + expect(buffer.getLong() == 231); + } + +} diff --git a/test/Misc.java b/test/Misc.java index be83804e78..f5676e581f 100644 --- a/test/Misc.java +++ b/test/Misc.java @@ -49,19 +49,6 @@ public class Misc { } } - public static void putInt(int val, byte[] dst, int offset) { - System.out.println("put " + val); - dst[offset] = (byte)((val >> 24) & 0xff); - dst[offset+1] = (byte)((val >> 16) & 0xff); - dst[offset+2] = (byte)((val >> 8) & 0xff); - dst[offset+3] = (byte)((val ) & 0xff); - } - - public static void putLong(long val, byte[] dst, int offset) { - putInt((int)(val >> 32), dst, offset); - putInt((int)val, dst, offset + 4); - } - public String toString() { return super.toString(); } @@ -89,252 +76,19 @@ public class Misc { return a + b + c; } - private static long roundUp(long a, long b) { - a += b - 1L; - return a - (a % b); - } - public static void main(String[] args) { - { long foo = 25214903884L; - int radix = 10; - expect(foo > 0); - foo /= radix; - expect(foo > 0); - } - - expect(Long.parseLong("25214903884") == 25214903884L); - - expect(Long.parseLong("-9223372036854775808") == -9223372036854775808L); - - expect(String.valueOf(25214903884L).equals("25214903884")); - - expect(String.valueOf(-9223372036854775808L).equals - ("-9223372036854775808")); - - { boolean p = true; - int[] array = new int[] { 1, 2 }; - expect(array[0] == array[p ? 0 : 1]); - p = false; - expect(array[1] == array[p ? 0 : 1]); - } - - expect(roundUp(156, 2) == 156); - { Foo foo = new Foo(); int x = foo.a + foo.b + foo.c; bar(foo.a, foo.b, foo.c); } - { int get_buffer = 2144642881; - int bits_left = 30; - int l = 9; - int code = (((get_buffer >> (bits_left -= (l)))) & ((1<<(l))-1)); - expect(code == 510); - } - - { int width = 8; - int height = 8; - int depth = 24; - int scanlinePad = 4; - - int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) - / scanlinePad * scanlinePad; - expect(bytesPerLine == 24); - } - - { int a = -5; - int b = 2; - expect(a >> b == -5 >> 2); - expect(a >>> b == -5 >>> 2); - expect(a << b == -5 << 2); - expect(a + b == -5 + 2); - expect(a - b == -5 - 2); - expect(a * b == -5 * 2); - expect(a / b == -5 / 2); - expect(a % b == -5 % 2); - expect((a & b) == (-5 & 2)); - expect((a | b) == (-5 | 2)); - expect((a ^ b) == (-5 ^ 2)); - expect(-a == 5); - expect(~a == ~-5); - - a = 5; - b = 2; - expect(a >> b == 5 >> 2); - expect(a >>> b == 5 >>> 2); - expect(a << b == 5 << 2); - expect(a + b == 5 + 2); - expect(a - b == 5 - 2); - expect(a * b == 5 * 2); - expect(a / b == 5 / 2); - expect(a % b == 5 % 2); - expect((a & b) == (5 & 2)); - expect((a | b) == (5 | 2)); - expect((a ^ b) == (5 ^ 2)); - expect(-a == -5); - expect(~a == ~5); - } - - { long a = -5; - long b = 2; - expect(a >> b == -5L >> 2); - expect(a >>> b == -5L >>> 2); - expect(a << b == -5L << 2); - expect(a + b == -5L + 2L); - expect(a - b == -5L - 2L); - expect(a * b == -5L * 2L); - expect(a / b == -5L / 2L); - expect(a % b == -5L % 2L); - expect((a & b) == (-5L & 2L)); - expect((a | b) == (-5L | 2L)); - expect((a ^ b) == (-5L ^ 2L)); - expect(-a == 5L); - expect(~a == ~-5L); - - a = 5; - b = 2; - expect(a >> b == 5L >> 2); - expect(a >>> b == 5L >>> 2); - expect(a << b == 5L << 2); - expect(a + b == 5L + 2L); - expect(a - b == 5L - 2L); - expect(a * b == 5L * 2L); - expect(a / b == 5L / 2L); - expect(a % b == 5L % 2L); - expect((a & b) == (5L & 2L)); - expect((a | b) == (5L | 2L)); - expect((a ^ b) == (5L ^ 2L)); - expect(-a == -5L); - expect(~a == ~5L); - } - - { long a = -25214903884L; - long b = 2; - expect(a >> b == -25214903884L >> 2); - expect(a >>> b == -25214903884L >>> 2); - expect(a << b == -25214903884L << 2); - expect(a + b == -25214903884L + 2L); - expect(a - b == -25214903884L - 2L); - expect(a * b == -25214903884L * 2L); - expect(a / b == -25214903884L / 2L); - expect(a % b == -25214903884L % 2L); - expect((a & b) == (-25214903884L & 2L)); - expect((a | b) == (-25214903884L | 2L)); - expect((a ^ b) == (-25214903884L ^ 2L)); - expect(-a == 25214903884L); - expect(~a == ~-25214903884L); - - a = 25214903884L; - b = 2; - expect(a >> b == 25214903884L >> 2); - expect(a >>> b == 25214903884L >>> 2); - expect(a << b == 25214903884L << 2); - expect(a + b == 25214903884L + 2L); - expect(a - b == 25214903884L - 2L); - expect(a * b == 25214903884L * 2L); - expect(a / b == 25214903884L / 2L); - expect(a % b == 25214903884L % 2L); - expect((a & b) == (25214903884L & 2L)); - expect((a | b) == (25214903884L | 2L)); - expect((a ^ b) == (25214903884L ^ 2L)); - expect(-a == -25214903884L); - expect(~a == ~25214903884L); - } - - { long b = 2; - expect((-25214903884L) >> b == -25214903884L >> 2); - expect((-25214903884L) >>> b == -25214903884L >>> 2); - expect((-25214903884L) << b == -25214903884L << 2); - expect((-25214903884L) + b == -25214903884L + 2L); - expect((-25214903884L) - b == -25214903884L - 2L); - expect((-25214903884L) * b == -25214903884L * 2L); - expect((-25214903884L) / b == -25214903884L / 2L); - expect((-25214903884L) % b == -25214903884L % 2L); - expect(((-25214903884L) & b) == (-25214903884L & 2L)); - expect(((-25214903884L) | b) == (-25214903884L | 2L)); - expect(((-25214903884L) ^ b) == (-25214903884L ^ 2L)); - - b = 2; - expect(25214903884L >> b == 25214903884L >> 2); - expect(25214903884L >>> b == 25214903884L >>> 2); - expect(25214903884L << b == 25214903884L << 2); - expect(25214903884L + b == 25214903884L + 2L); - expect(25214903884L - b == 25214903884L - 2L); - expect(25214903884L * b == 25214903884L * 2L); - expect(25214903884L / b == 25214903884L / 2L); - expect(25214903884L % b == 25214903884L % 2L); - expect((25214903884L & b) == (25214903884L & 2L)); - expect((25214903884L | b) == (25214903884L | 2L)); - expect((25214903884L ^ b) == (25214903884L ^ 2L)); - } - - { long a = 2L; - expect(a + (-25214903884L) == 2L + (-25214903884L)); - expect(a - (-25214903884L) == 2L - (-25214903884L)); - expect(a * (-25214903884L) == 2L * (-25214903884L)); - expect(a / (-25214903884L) == 2L / (-25214903884L)); - expect(a % (-25214903884L) == 2L % (-25214903884L)); - expect((a & (-25214903884L)) == (2L & (-25214903884L))); - expect((a | (-25214903884L)) == (2L | (-25214903884L))); - expect((a ^ (-25214903884L)) == (2L ^ (-25214903884L))); - - a = 2L; - expect(a + 25214903884L == 2L + 25214903884L); - expect(a - 25214903884L == 2L - 25214903884L); - expect(a * 25214903884L == 2L * 25214903884L); - expect(a / 25214903884L == 2L / 25214903884L); - expect(a % 25214903884L == 2L % 25214903884L); - expect((a & 25214903884L) == (2L & 25214903884L)); - expect((a | 25214903884L) == (2L | 25214903884L)); - expect((a ^ 25214903884L) == (2L ^ 25214903884L)); - } - byte2 = 0; expect(byte2 == 0); - expect(Long.valueOf(231L) == 231L); - - { long x = 231; - expect((x >> 32) == 0); - expect((x >>> 32) == 0); - expect((x << 32) == 992137445376L); - - int shift = 32; - expect((x >> shift) == 0); - expect((x >>> shift) == 0); - expect((x << shift) == 992137445376L); - - long y = -231; - expect((y >> 32) == 0xffffffffffffffffL); - expect((y >>> 32) == 0xffffffffL); - } - - { byte[] array = new byte[8]; - putLong(231, array, 0); - expect((array[0] & 0xff) == 0); - expect((array[1] & 0xff) == 0); - expect((array[2] & 0xff) == 0); - expect((array[3] & 0xff) == 0); - expect((array[4] & 0xff) == 0); - expect((array[5] & 0xff) == 0); - expect((array[6] & 0xff) == 0); - expect((array[7] & 0xff) == 231); - } - - java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(8); - buffer.putLong(231); - buffer.flip(); - expect(buffer.getLong() == 231); - boolean v = Boolean.valueOf("true"); ClassLoader.getSystemClassLoader().toString(); - { int a = 2; - int b = 2; - int c = a + b; - } - { Misc m = new Misc(); m.toString(); @@ -370,33 +124,6 @@ public class Misc { expect(zip() == 47); expect(zup() == 47); - { int[] array = new int[0]; - Exception exception = null; - try { - int x = array[0]; - } catch (ArrayIndexOutOfBoundsException e) { - exception = e; - } - - expect(exception != null); - } - - { int[] array = new int[3]; - int i = 0; - array[i++] = 1; - array[i++] = 2; - array[i++] = 3; - - expect(array[--i] == 3); - expect(array[--i] == 2); - expect(array[--i] == 1); - } - - { Object[][] array = new Object[1][1]; - expect(array.length == 1); - expect(array[0].length == 1); - } - { Object a = new Object(); Object b = new Object(); @@ -416,17 +143,5 @@ public class Misc { foo.array = new int[3]; foo.a = (foo.a + 1) % foo.array.length; } - - { int j = 0; - byte[] decodeTable = new byte[256]; - for (int i = 'A'; i <= 'Z'; ++i) decodeTable[i] = (byte) j++; - for (int i = 'a'; i <= 'z'; ++i) decodeTable[i] = (byte) j++; - for (int i = '0'; i <= '9'; ++i) decodeTable[i] = (byte) j++; - decodeTable['+'] = (byte) j++; - decodeTable['/'] = (byte) j++; - decodeTable['='] = 0; - - expect(decodeTable['a'] != 0); - } } }