001    package serp.bytecode;
002    
003    import junit.framework.*;
004    import junit.textui.*;
005    
006    /**
007     * Tests the {@link ArrayStoreInstruction} type.
008     *
009     * @author Abe White
010     */
011    public class TestArrayStoreInstruction extends TestCase {
012        private Code _code = new Code();
013    
014        public TestArrayStoreInstruction(String test) {
015            super(test);
016        }
017    
018        /**
019         * Test that the instruction initializes correctly when generated.
020         */
021        public void testIniitalize() {
022            assertEquals(Constants.NOP, _code.xastore().getOpcode());
023            assertEquals(Constants.IASTORE, _code.iastore().getOpcode());
024            assertEquals(Constants.LASTORE, _code.lastore().getOpcode());
025            assertEquals(Constants.FASTORE, _code.fastore().getOpcode());
026            assertEquals(Constants.DASTORE, _code.dastore().getOpcode());
027            assertEquals(Constants.AASTORE, _code.aastore().getOpcode());
028            assertEquals(Constants.BASTORE, _code.bastore().getOpcode());
029            assertEquals(Constants.CASTORE, _code.castore().getOpcode());
030            assertEquals(Constants.SASTORE, _code.sastore().getOpcode());
031        }
032    
033        /**
034         * Test the the instruction returns its type correctly.
035         */
036        public void testGetType() {
037            assertNull(_code.xastore().getType());
038            assertEquals(int.class, _code.iastore().getType());
039            assertEquals(long.class, _code.lastore().getType());
040            assertEquals(float.class, _code.fastore().getType());
041            assertEquals(double.class, _code.dastore().getType());
042            assertEquals(Object.class, _code.aastore().getType());
043            assertEquals(byte.class, _code.bastore().getType());
044            assertEquals(char.class, _code.castore().getType());
045            assertEquals(short.class, _code.sastore().getType());
046        }
047    
048        /**
049         * Test that the opcode morphs correctly with type changes.
050         */
051        public void testOpcodeMorph() {
052            ArrayStoreInstruction ins = _code.xastore();
053            assertEquals(Constants.NOP, ins.getOpcode());
054            assertEquals(Constants.NOP, ins.setType((String) null).getOpcode());
055            assertEquals(Constants.NOP, ins.setType((BCClass) null).getOpcode());
056            assertEquals(Constants.NOP, ins.setType((Class) null).getOpcode());
057    
058            assertEquals(Constants.IASTORE, ins.setType(int.class).getOpcode());
059            assertEquals(Constants.NOP, ins.setType((String) null).getOpcode());
060            assertEquals(Constants.LASTORE, ins.setType(long.class).getOpcode());
061            assertEquals(Constants.FASTORE, ins.setType(float.class).getOpcode());
062            assertEquals(Constants.DASTORE, ins.setType(double.class).getOpcode());
063            assertEquals(Constants.AASTORE, ins.setType(Object.class).getOpcode());
064            assertEquals(Constants.BASTORE, ins.setType(byte.class).getOpcode());
065            assertEquals(Constants.CASTORE, ins.setType(char.class).getOpcode());
066            assertEquals(Constants.SASTORE, ins.setType(short.class).getOpcode());
067            assertEquals(Constants.IASTORE, ins.setType(void.class).getOpcode());
068            assertEquals(Constants.AASTORE, ins.setType(String.class).getOpcode());
069            assertEquals(Constants.IASTORE, ins.setType(boolean.class).getOpcode());
070        }
071    
072        public static Test suite() {
073            return new TestSuite(TestArrayStoreInstruction.class);
074        }
075    
076        public static void main(String[] args) {
077            TestRunner.run(suite());
078        }
079    }