exerciseopcodes.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,444 行 · 第 1/5 页

JAVA
1,444
字号
/* * @(#)ExerciseOpcodes.java	1.17 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program 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 program 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 at /legal/license.txt).    *    * 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 or visit www.sun.com if you need additional   * information or have any questions.  * *//*    NOTE: This module compiles some methods which are specifically designed    to operate on some bytecodes.  And compares the compiled method against    the expected behavior as determined by an interpreted version.*/public class ExerciseOpcodes{    static boolean verbose = false;    static int totalTests = 0;    static int totalFailures = 0;    static void dumpValue(boolean value) {        System.out.println("   boolean value: " + value);    }    static void dumpValue(int value) {        System.out.println("   int value: " + value);    }    static void dumpValue(long value) {        System.out.println("   long value: " + value);    }    static void dumpValue(float value) {        System.out.println("   float value: " + value + " (0x" +            Integer.toHexString(Float.floatToIntBits(value)) + ")");    }    static void dumpValue(double value) {        System.out.println("   double value: " + value + " (0x" +            Long.toHexString(Double.doubleToLongBits(value)) + ")");    }    static boolean    reportPassIf(String testName, boolean success) {        if (!success && !verbose) {            System.out.println((success ? "PASSED" : "FAILED") + " Test " +                               testName);        }        totalTests++;        if (!success) {            totalFailures++;        }        return success;    }    static boolean    reportPassIf(String testName, boolean actual, boolean expected) {        boolean success = (actual == expected);        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected);            System.out.println("   Actual = " + actual);        }        return success;    }    static boolean    reportPassIf(String testName, int actual, int expected) {        boolean success = (actual == expected);        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected);            System.out.println("   Actual = " + actual);        }        return success;    }    static boolean    reportPassIf(String testName, long actual, long expected) {        boolean success = (actual == expected);        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected);            System.out.println("   Actual = " + actual);        }        return success;    }    static boolean    reportPassIf(String testName, float actual, float expected) {        boolean success = (actual == expected);        if (!success) {            // One possibility of why the comparison is if both are NaNs.            // Check for this case explicitly if necessary:            if (Float.isNaN(actual) && Float.isNaN(expected)) {                success = true;            }        }        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected + " (0x" +                Integer.toHexString(Float.floatToIntBits(expected)) + ")");            System.out.println("   Actual = " + actual + " (0x" +                Integer.toHexString(Float.floatToIntBits(actual)) + ")");        }        return success;    }    static boolean    reportPassIf(String testName, double actual, double expected) {        boolean success = (actual == expected);        if (!success) {            // One possibility of why the comparison is if both are NaNs.            // Check for this case explicitly if necessary:            if (Double.isNaN(actual) && Double.isNaN(expected)) {                success = true;            }        }        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected);            System.out.println("   Actual = " + actual);        }        return success;    }    static boolean    reportPassIf(String testName, Object actual, Object expected) {        boolean success = (actual == expected);        reportPassIf(testName, success);        if (!success) {            System.out.println("   Expected = " + expected);            System.out.println("   Actual = " + actual);        }        return success;    }    public static void main(String[] args) {        String[] compileItems = {            "ExerciseReturnOpcodes",            "ExerciseIntOpcodes",            "ExerciseLongOpcodes",            "ExerciseFloatOpcodes",            "ExerciseDoubleOpcodes",            "ExerciseArrayOpcodes",        };        // Do interpreted run to take care of clinit:        //System.err.println("Compiling named classes...\n");        //exerciseOpcodes();        /*         * Compile the associated classes         */        System.out.println("Compiling named classes...\n");        CompilerTest.main(compileItems);        totalTests = 0;        totalFailures = 0;        /*         * Run the tests:         */        System.out.println("Exercise opcodes:\n");        exerciseOpcodes();        exerciseIDivOpcodes();        exerciseIRemOpcodes();        exerciseIMulOpcodes();        // Report the total number of failures:        System.out.println("Tests ran: " + totalTests + ", failures: " + totalFailures);    }    static final int dividends[] = {         0, 1, 2, 3, 4, 5, 6, 7, 9, 10,        15, 23, 28, 69, 100, 127, 168, 378,        500, 1029, 2048, 2056, 4096, 4230,        8192, 8392, 16384, 16397, 32768, 32775,        65536, 65537, 131072, 131085, 262144, 262174,        524288, 524293, 1048576, 1048579,        2097152, 2097157, 4194304, 4194309,        8388608, 8388609, 16777216, 16777217,        33554432, 33554436, 67108864, 67108868,        134217728, 134217729, 268435456, 268435457,        536870912, 536870913, 1073741824, 1073741825,        2147483647,         17, 100, 125, 1027, 5612712, 0x7fffffff,        -1, -2, -3, -4, -5, -6, -7, -9, -10,        -15, -23, -28, -69, -100, -127, -168, -378,        -500, -1029, -2048, -2056, -4096, -4230,        -8192, -8392, -16384, -16397, -32768, -32775,        -65536, -65537, -131072, -131085, -262144, -262174,        -524288, -524293, -1048576, -1048579,        -2097152, -2097157, -4194304, -4194309,        -8388608, -8388609, -16777216, -16777217,        -33554432, -33554436, -67108864, -67108868,        -134217728, -134217729, -268435456, -268435457,        -536870912, -536870913, -1073741824, -1073741825,        -2147483647, -2147483648,        -17, -100, -125, -1027, -5612712, -0x7fffffff,    };    static void exerciseOpcodes() {        // Exercise the Return opcodes:        System.out.println("Testing Return Opcodes:");        ExerciseReturnOpcodes er = new ExerciseReturnOpcodes();        {            Object o = new Object();            er.exerciseReturn(o);            reportPassIf("exerciseReturn", true);        }        {            int value = er.exerciseIReturn(5);            reportPassIf("exerciseIReturn", value, 5);        }        {            float value = er.exerciseFReturn(5.0f);            reportPassIf("exerciseFReturn", value, 5.0f);        }        {            long value = er.exerciseLReturn(5l);            reportPassIf("exerciseLReturn", value, 5l);        }        {            double value = er.exerciseDReturn(5.0d);            reportPassIf("exerciseDReturn", value, 5.0d);        }        {            Object o = new Object();            Object value;            value = er.exerciseAReturn(o);            reportPassIf("exerciseAReturn(o)", value, o);            value = er.exerciseAReturn(null);            reportPassIf("exerciseAReturn(null)", value, null);        }        if (verbose) {            System.out.println("");        }        // Exercise the Int opcodes:        System.out.println("Testing Int Opcodes:");        ExerciseIntOpcodes ei = new ExerciseIntOpcodes();        {            byte value = ei.exerciseI2B(5);            reportPassIf("exerciseI2B(5)", value, 5);        }        {            char value = ei.exerciseI2C((int)'A');            reportPassIf("exerciseI2C(5)", value, 'A');        }        {            short value = ei.exerciseI2S(5);            reportPassIf("exerciseI2S(5)", value, 5);        }        {            double value = ei.exerciseI2D(5);            reportPassIf("exerciseI2D(5)", value, 5.0d);        }        {            float value = ei.exerciseI2F(5);            reportPassIf("exerciseI2F(5)", value, 5.0f);        }        {            long value = ei.exerciseI2L(5);            reportPassIf("exerciseI2L(5)", value, 5l);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?