3.4 Testing Code Examples

Code examples appear throughout this chapter. Unfortunately, we haven't really discussed how to do output, so it will be a little tricky to actually try out any of these code examples. Output involves some mucking about with method calls and sometimes with creating new objects and other such complicated things that are better left for a later chapter.

The good news is that you can use Java to assemble a test harness for these code samples. You can wrap the test code up in a small, easy-to-write class, then use Java to perform the input and output. For example, here is some code to answer the question, "What do you get if you multiply 6 by 9?"

 bipush 6         ; Push 6 bipush 9         ; Push 9 imul             ; Result is 54 

By itself, this code is not executable, because it's not part of a method. To make it executable, build a class:

 .class Test                    ; Create a class Test .method static run()I          ; Add a static method to the class    ; This is the code example:    bipush 6                    ; Push 6    bipush 9                    ; Push 9    imul                        ; Result is 54    ireturn                     ; Return the result .end method 

The ireturn instruction is added to the end so that it returns the result. This is a complete class with a method you can call. You can run the Oolong assembler on this file, and it will produce the file Test.class containing the class Test. (See appendix B for more about using the Oolong assembler.)

If you want to see if the code sample actually does what the book claims that it does, you can write a Java class to test it:

 class RunTest {    public static void main(String a[])    {        System.out.println("The result is: " + Test.run());    } } 

The RunTest class works just like any other Java class; its meaning should be readily apparent. If you run this class through a Java compiler and execute it, the system should print

 The result is: 54 

A similar test harness can be assembled for other types besides int. Use freturn for floats, lreturn for longs, and dreturn for doubles. You must also change the return type in the type descriptor for run. Use F for floats, D for doubles, and J for longs. This last is not entirely intuitive just memorize it.

For example, to rewrite the test to use doubles instead of ints:

 .class Test .method static run()D ldc2_w 6.0D           ; Push the number 6 as a double ldc2_w 9.0D           ; Push the number 9 as a double dmul                  ; Multiply to get 54.0 dreturn .end method 

Here, the descriptor of the method is run()D instead of run()I, because the return type is a double rather than an int. For the same reason, the last instruction is dreturn instead of ireturn.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net