harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mloe...@apache.org
Subject svn commit: r431314 - in /incubator/harmony/enhanced/classlib/trunk/modules/sound/src: main/java/javax/sound/midi/ test/java/org/apache/harmony/sound/tests/javax/sound/midi/
Date Mon, 14 Aug 2006 09:35:16 GMT
Author: mloenko
Date: Mon Aug 14 02:35:16 2006
New Revision: 431314

URL: http://svn.apache.org/viewvc?rev=431314&view=rev
Log:
applied patch for HARMONY-1160
[classlib][sound] Classes Sequence and Track and tests for its

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/SequenceTest.java
    incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/TrackTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Sequence.java
    incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Track.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Sequence.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Sequence.java?rev=431314&r1=431313&r2=431314&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Sequence.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Sequence.java
Mon Aug 14 02:35:16 2006
@@ -34,54 +34,106 @@
     protected int resolution;
 
     protected Vector<Track> tracks;
+    
+    private Vector<Patch> patches;
 
     public Sequence(float divisionType, int resolution) throws InvalidMidiDataException {
-        //TODO
+        if (divisionType != Sequence.PPQ &&
+                divisionType != Sequence.SMPTE_24 &&
+                divisionType != Sequence.SMPTE_25 &&
+                divisionType != Sequence.SMPTE_30 &&
+                divisionType != Sequence.SMPTE_30DROP ) {
+            throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
     
+        }
+        this.divisionType = divisionType;
+        this.resolution = resolution;
+        this.tracks = new Vector<Track>();
+        this.patches = new Vector<Patch>();
+        
     }
 
     public Sequence(float divisionType, int resolution, int numTracks)
             throws InvalidMidiDataException {
-        //TODO
+        if (divisionType != Sequence.PPQ &&
+                divisionType != Sequence.SMPTE_24 &&
+                divisionType != Sequence.SMPTE_25 &&
+                divisionType != Sequence.SMPTE_30 &&
+                divisionType != Sequence.SMPTE_30DROP ) {
+            throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
     
+        }
+        this.divisionType = divisionType;
+        this.resolution = resolution;
+        this.patches = new Vector<Patch>();
+        this.tracks = new Vector<Track>();
+        if (numTracks > 0) {
+            for (int i = 0; i < numTracks; i++) {
+                tracks.add(new Track());
+            }
+        }
     }
 
     public Track createTrack() {
-        //TODO
-        return null;
+        /*
+         * new Tracks accrue to the end of vector
+         */
+        Track tr = new Track();
+        tracks.add(tr);
+        return tr;
     }
 
     public boolean deleteTrack(Track track) {
-        //TODO
-        return false;
+        return tracks.remove(track);
     }
 
     public float getDivisionType() {
-        //TODO
-        return 1.0f;
+        return divisionType;
     }
 
     public long getMicrosecondLength() {
-        //TODO
-        return 1L;
+        float divisionType;
+        if (this.divisionType == 0.0f) {
+            divisionType = 2;
+        } else {
+            divisionType = this.divisionType;
+        }
+        return (long) (1000000.0 * getTickLength() / 
+                (divisionType * this.resolution * 1.0f));
     }
 
     public Patch[] getPatchList() {
-        //TODO
-        return null;
+        //FIXME
+        /*
+         * I don't understand how to works this method, and so
+         * I simply return an empty array. 'patches' initializes
+         * in the constructor as empty vector 
+         */
+        Patch[] patch = new Patch[patches.size()];
+        patches.toArray(patch);
+        return patch;
     }
 
     public int getResolution() {
-        //TODO
-        return 1;
+        return resolution;
     }
 
     public long getTickLength() {
-        //TODO
-        return 1L;
+        /*
+         * this method return the biggest value of tick of 
+         * all tracks contain in the Sequence
+         */
+        long maxTick = 0;
+        for (int i = 0; i < tracks.size(); i++) {
+            if (maxTick < tracks.get(i).ticks()) {
+                maxTick = tracks.get(i).ticks();
+            }
+        }
+        return maxTick;
     }
 
     public Track[] getTracks() {
-        //TODO
-        return null;
+        Track[] track = new Track[tracks.size()];
+        tracks.toArray(track);
+        return track;
     }
 
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Track.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Track.java?rev=431314&r1=431313&r2=431314&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Track.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/javax/sound/midi/Track.java
Mon Aug 14 02:35:16 2006
@@ -16,29 +16,148 @@
 
 package javax.sound.midi;
 
+import java.util.ArrayList;
+
 public class Track {
+    private ArrayList<MidiEvent> events; //vector of events contain in the Track
+    
+    private MidiEvent badEvent; //variable to save event which I try to add
+                                //to empty Track; see description below
+    
+    private long tick; 
+    
+    Track() {
+        /*
+         * create an empty Track; new Track must contain only meta-event End of Track.
+         * MetaMessage with MetaMessage.data contains -1, 47 and 0 is meta-event
+         */
+        events = new ArrayList<MidiEvent>();
+        events.add(new MidiEvent(new MetaMessage(new byte[] {-1, 47, 0}), 0));
+    }
     public boolean add(MidiEvent event) {
-        //TODO
-        return false;
+        //FIXME
+        /*
+         * Some words about badEvent.
+         * When I write tests, I find following situation in the RI:
+         * if I want to add to empty Track new event that is not meta-event End of Track,
+         * I catch exception ArrayIndexOutOfBoundsException with meaning -1, and my
+         * event doesn't add to Track, but meta-event adds. If I try to add the same 
+         * event after it, method Track.add(MidiEvent) return 'false', and my event
+         * doesn't add again. So, I want to delete this event and use method 
+         * Track.remove(MidiEvent) for it, but it return 'false' too! And only after
+         * this "shamanism" I can add this event to Track normally. 
+         * And only for this situation I use variable badEvent. 
+         * 
+         * See test org.apache.harmony.sound.tests.javax.sound.midi.TrackTest 
+         * for more details
+         */
+        
+        /*
+         * if event equals null or badEvent, this method return 'false'
+         */
+        if (event == null || event == badEvent) {
+            return false;
+        }
+        /*
+         * If event equals meta-event End of Track and Track in this moment
+         * doesn't contain some events, i.e. Track.size() return 0, this 
+         * event accrue to Track; 
+         * if Track is not empty, but it doesn't contain meta-event, 
+         * this event accrue to the end of Track;
+         * in any case addition of this meta-event is successful, this method 
+         * return 'true' even if meta-event End of Track already contains in the Track
+         */
+        if (event.getMessage().getMessage()[0] == -1 &&
+                event.getMessage().getMessage()[1] == 47 &&
+                event.getMessage().getMessage()[2] == 0 ) {
+            if (events.size() == 0) {
+                return events.add(event);
+            } else {
+                byte[] bt = events.get(events.size() - 1).getMessage().getMessage();
+                if ((bt[0] != -1) && (bt[1] != 47) && (bt[2] != 0)) {
+                    return events.add(event);
+                }
+            }         
+            return true;
+        }
+        /*
+         * after use method Track.add(MidiEvent) Track must contain meta-event
+         * End of Track; so, at first I add this event to Track if it doesn't 
+         * contain meta-event and parameter 'event' is not meta-event
+         */
+        if (events.size() == 0) {
+            events.add(new MidiEvent(new MetaMessage(new byte[] {-1, 47, 0}), 0));
+            badEvent = event;
+            throw new ArrayIndexOutOfBoundsException("-1");
+        } else {
+            byte[] bt = events.get(events.size() - 1).getMessage().getMessage();
+            if ((bt[0] != -1) && (bt[1] != 47) && (bt[2] != 0)) {
+                events.add(new MidiEvent(new MetaMessage(new byte[] {-1, 47, 0}), 0));
+            }
+        }
+        
+        if (events.contains(event)) {
+            return false;
+        } 
+        
+        /*
+         * events in the Track must take up position in ascending ticks
+         */
+        if (events.size() == 1) {
+            events.add(0, event);
+        }
+        for (int i = 0; i < events.size() - 1; i++ ) {
+            if (events.get(i).getTick() <= event.getTick()) {
+                continue;
+            } else {
+                events.add(i, event);
+                break;
+            }
+        }
+        /*
+         * method Track.ticks() return the biggest value of tick of all events
+         * and save it even I remove event with the biggest values of tick
+         */
+        if (tick < event.getTick()) {
+            tick = event.getTick();
+        }
+        return true;           
     }
 
     public MidiEvent get(int index) throws ArrayIndexOutOfBoundsException {
-        //TODO
-        return null;
+        if (index < 0 || index >= events.size()) {
+            throw new ArrayIndexOutOfBoundsException("Index: " + index + ", Size: " + 
+                    events.size());
+        }
+        return events.get(index);
     }
 
     public boolean remove(MidiEvent event) {
-        //TODO
+        /*
+         * if I remove event that equals badEvent, I "delete" badEvent
+         */
+        if (event == badEvent) {
+            badEvent = null;
+            return false;
+        }
+        /*
+         * method Track.ticks() always return the biggest value that ever has been
+         * in the Track; so only when Track is empty Track.ticks() return 0 
+         */
+        if (events.remove(event)) {
+            if (events.size() == 0) {
+                tick = 0;
+            }
+            return true;
+        }
         return false;
     }
 
     public int size() {
-        //TODO
-        return 1;
+        return events.size();
     }
 
     public long ticks() {
-        //TODO
-        return 1L;
+        return tick;
     }
 }

Added: incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/SequenceTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/SequenceTest.java?rev=431314&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/SequenceTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/SequenceTest.java
Mon Aug 14 02:35:16 2006
@@ -0,0 +1,322 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.sound.tests.javax.sound.midi;
+
+import javax.sound.midi.InvalidMidiDataException;
+import javax.sound.midi.MidiEvent;
+import javax.sound.midi.MidiMessage;
+import javax.sound.midi.Sequence;
+import javax.sound.midi.Track;
+
+import org.apache.harmony.sound.tests.javax.sound.midi.TrackTest.MidiMessage1;
+
+import junit.framework.TestCase;
+
+public class SequenceTest extends TestCase {
+    /**
+     * test constants
+     */
+    public void test_constants() {
+        assertEquals(0.0f, Sequence.PPQ);
+        assertEquals(24.0f, Sequence.SMPTE_24);
+        assertEquals(25.0f, Sequence.SMPTE_25);
+        assertEquals(30.0f, Sequence.SMPTE_30);
+        assertEquals(29.969999313354492f, Sequence.SMPTE_30DROP);
+    }
+    /**
+     * test constructor Sequence(float, int)
+     */
+    public void test_constructor1() throws Exception {
+        
+        Sequence seq0 = new Sequence(Sequence.PPQ, 10, 2);
+        seq0.getTracks();
+        
+        Sequence seq1 = new Sequence(Sequence.PPQ, 10);
+        Sequence seq2 = new Sequence(Sequence.SMPTE_24, -10);
+        Sequence seq3 = new Sequence(Sequence.SMPTE_25, 9854);
+        Sequence seq4 = new Sequence(Sequence.SMPTE_30, -82534);
+        Sequence seq5 = new Sequence(Sequence.SMPTE_30DROP, 0);
+        
+        try {
+            Sequence test = new Sequence(32.0f, 16);
+            fail("InvalidMidiDataException expected");
+        } catch (InvalidMidiDataException e) {}
+        
+        assertEquals(24.0f, seq2.getDivisionType());
+        assertEquals(0, seq2.getMicrosecondLength());
+        assertEquals(-10, seq2.getResolution());
+        assertEquals(0, seq2.getTickLength());
+        assertEquals(0, seq2.getPatchList().length);
+        assertEquals(0, seq2.getTracks().length);
+    }
+    
+    /**
+     * test constructor Sequence(float, int, int)
+     */
+    public void test_constructor2() throws Exception {
+        Sequence seq1 = new Sequence(Sequence.PPQ, 10, 0);
+        Sequence seq2 = new Sequence(Sequence.SMPTE_24, -10, 8762);
+        Sequence seq3 = new Sequence(Sequence.SMPTE_25, 9854, -18);
+        Sequence seq4 = new Sequence(Sequence.SMPTE_30, -82534, 34);
+        Sequence seq5 = new Sequence(Sequence.SMPTE_30DROP, 0);
+        
+        try {
+            Sequence test = new Sequence(-9.1f, 16, 54);
+            fail("InvalidMidiDataException expected");
+        } catch (InvalidMidiDataException e) {}
+        
+        assertEquals(25.0f, seq3.getDivisionType());
+        assertEquals(0, seq3.getMicrosecondLength());
+        assertEquals(9854, seq3.getResolution());
+        assertEquals(0, seq3.getTickLength());
+        assertEquals(0, seq3.getPatchList().length);
+        assertEquals(8762, seq2.getTracks().length); //seq2
+        assertEquals(0, seq3.getTracks().length);
+        assertEquals(0, seq5.getTracks().length); //seq5
+        
+        assertEquals(1, seq2.getTracks()[0].size());
+        assertEquals(0, seq2.getTracks()[0].ticks());
+        
+        assertEquals(0, seq2.getTracks()[345].get(0).getTick());
+        
+        MidiMessage mes = seq2.getTracks()[345].get(0).getMessage();
+        assertEquals(3, mes.getLength());
+        assertEquals(255, mes.getStatus());
+        assertEquals(3, mes.getMessage().length);
+        assertEquals(-1, mes.getMessage()[0]);
+        assertEquals(47, mes.getMessage()[1]);
+        assertEquals(0, mes.getMessage()[2]);
+    }
+    
+    /**
+     * test method createTrack()
+     */
+    public void test_createTrack() throws Exception {
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        assertEquals(9, seq.getTracks().length);
+
+        Track tr = seq.createTrack();
+        Track tr1 = seq.createTrack();
+        /*
+         * returned Track is the same that create with constructor,
+         * but size of vector increase
+         */
+        assertEquals(1, tr.size());
+        assertEquals(3, tr.get(0).getMessage().getMessage().length);
+        assertEquals(-1, tr.get(0).getMessage().getMessage()[0]);
+        assertEquals(47, tr.get(0).getMessage().getMessage()[1]);
+        assertEquals(0, tr.get(0).getMessage().getMessage()[2]);
+        
+        assertEquals(1, tr1.size());
+        assertEquals(3, tr1.get(0).getMessage().getMessage().length);
+        assertEquals(-1, tr1.get(0).getMessage().getMessage()[0]);
+        assertEquals(47, tr1.get(0).getMessage().getMessage()[1]);
+        assertEquals(0, tr1.get(0).getMessage().getMessage()[2]);
+        
+        assertEquals(11, seq.getTracks().length);
+        
+        /*
+         * new Tracks accrue to the end of vector
+         */
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 3L);
+        tr1.add(event2);
+        assertEquals(23, seq.getTracks()[10].get(0).getMessage().getMessage()[0]);
+    }
+    
+    /**
+     * test method deleteTrack(Track)
+     * @throws Exception
+     */
+    public void test_deleteTrack() throws Exception {
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        Track tr1 = seq.createTrack();
+        assertEquals(11, seq.getTracks().length);
+        
+        tr.add(new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), 10));
+        assertTrue(seq.deleteTrack(tr));
+        assertFalse(seq.deleteTrack(tr));
+        assertEquals(10, seq.getTracks().length);
+        
+        tr1 = null;
+        assertFalse(seq.deleteTrack(tr1));
+    }
+    
+    /**
+     * test method getMicrosecondLength()
+     *
+     */
+    public void test_getMicrosecondLength() throws Exception {
+        Sequence seq = new Sequence(Sequence.PPQ, 15, 2);
+        Track tr = seq.createTrack();
+        MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), -10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 3L);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
+        
+        /*------- Sequence.PPQ -------*/
+        tr.add(event2);     
+        assertEquals(100000, seq.getMicrosecondLength());
+        Track tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(200000, seq.getMicrosecondLength());
+        seq.deleteTrack(tr1);
+        assertEquals(100000, seq.getMicrosecondLength());
+        seq.deleteTrack(tr);
+        assertEquals(0, seq.getMicrosecondLength());
+        
+        /*------- Sequence.SMPTE_24 -------*/
+        event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0L);
+        seq = new Sequence(Sequence.SMPTE_24, 17, 2);
+        tr = seq.createTrack();
+        tr.add(event2);     
+        assertEquals(0, seq.getMicrosecondLength());
+        tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(14705, seq.getMicrosecondLength());
+        seq.deleteTrack(tr1);
+        assertEquals(0, seq.getMicrosecondLength());
+        
+        /*------- Sequence.SMPTE_25 -------*/
+        event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 3L);
+        event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 97913L);
+        seq = new Sequence(Sequence.SMPTE_25, -5, 2);
+        tr = seq.createTrack();
+        tr.add(event2);     
+        assertEquals(-24000, seq.getMicrosecondLength());
+        tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(-783304000, seq.getMicrosecondLength());
+        seq.deleteTrack(tr1);
+        assertEquals(-24000, seq.getMicrosecondLength());
+
+        /*------- Sequence.SMPTE_30 -------*/
+        seq = new Sequence(Sequence.SMPTE_30, 0, 2);
+        tr = seq.createTrack();
+        tr.add(event2);     
+        assertEquals(9223372036854775807L, seq.getMicrosecondLength());
+        tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(9223372036854775807L, seq.getMicrosecondLength());
+        seq.deleteTrack(tr1);
+        assertEquals(9223372036854775807L, seq.getMicrosecondLength());
+        
+        /*------- Sequence.SMPTE_30DROP -------*/
+        seq = new Sequence(Sequence.SMPTE_30DROP, 17, 2);
+        tr = seq.createTrack();
+        tr.add(event2);     
+        assertEquals(5888L, seq.getMicrosecondLength());
+        tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(192178456L, seq.getMicrosecondLength());
+        seq.deleteTrack(tr1);
+        assertEquals(5888L, seq.getMicrosecondLength());
+    }
+    
+    /**
+     * test method getPatchList()
+     *
+     */
+    public void test_getPatchList() throws Exception {
+        //TODO
+        /*
+         * I don't understand how this method works
+         */
+        Sequence seq = new Sequence(Sequence.PPQ, 987, 2);
+        Track tr = seq.createTrack();
+        MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3, 34, -98, -27}),
-10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4, 78, -12,
5}), 3L);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1, 87, 9,
8, -2}), 6L);
+        tr.add(event1);
+        tr.add(event3);
+        Track tr1 = seq.createTrack();
+        tr1.add(event2);
+        assertEquals(0, seq.getPatchList().length);
+    }
+    
+    /**
+     * test method getTickLength()
+     *
+     */
+    public void test_getTickLength() throws Exception {
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), -10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 2L);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
+        tr.add(event1);
+        tr.add(event2);
+        
+        assertEquals(2, seq.getTickLength());
+        Track tr1 = seq.createTrack();
+        tr1.add(event1);
+        tr1.add(event2);
+        tr1.add(event3);
+        assertEquals(6, seq.getTickLength());
+        seq.deleteTrack(tr1);
+        assertEquals(2, seq.getTickLength());
+        seq.deleteTrack(tr);
+        assertEquals(0, seq.getTickLength());
+    }
+    
+    /**
+     * test method getTracks()
+     *
+     */
+    public void test_getTracks() throws Exception {
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        Track tr1 = seq.createTrack();
+        
+        Track[] tracks = seq.getTracks();
+        assertEquals(11, tracks.length);
+        
+        /*
+         * actions with array doesn't influence on 
+         * initial data...
+         */
+        assertEquals(tr1, tracks[10]);
+        tracks[10] = null;
+        assertTrue(seq.deleteTrack(tr1));
+        assertTrue(seq.deleteTrack(tracks[9]));
+        /*
+         * ...and action with initial data doesn't 
+         * influence on array
+         */
+        assertEquals(11, tracks.length);
+    }
+    
+    
+    static public class MidiMessage1 extends MidiMessage {
+        MidiMessage1(byte[] data) {
+            super(data);
+        }
+        
+        public Object clone() {
+            return null;
+        }
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/TrackTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/TrackTest.java?rev=431314&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/TrackTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/sound/src/test/java/org/apache/harmony/sound/tests/javax/sound/midi/TrackTest.java
Mon Aug 14 02:35:16 2006
@@ -0,0 +1,287 @@
+/*
+ *  Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.sound.tests.javax.sound.midi;
+
+import javax.sound.midi.MidiEvent;
+import javax.sound.midi.MidiMessage;
+import javax.sound.midi.Sequence;
+import javax.sound.midi.Track;
+
+import junit.framework.TestCase;
+
+public class TrackTest extends TestCase {
+    /**
+     * test method add()
+     * @throws Exception
+     */
+    public void test_add() throws Exception {
+        /*
+         * create an empty Track
+         */
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        assertEquals(1, tr.size());
+        assertEquals(0, tr.ticks());
+        
+        byte[] bt = new byte[] {1, 2, 3};
+        MidiMessage1 message = new MidiMessage1(bt);
+        MidiEvent event = new MidiEvent(message, 10L);
+        
+        assertTrue(tr.add(event));
+        assertFalse(tr.add(event));
+        bt[0] = 10;
+        bt[1] = 20;
+        assertFalse(tr.add(event));
+        /*
+         * values change
+         */
+        assertEquals(10, tr.get(0).getMessage().getMessage()[0]);
+        assertEquals(20, tr.get(0).getMessage().getMessage()[1]);
+        assertEquals(3, tr.get(0).getMessage().getMessage()[2]);
+        
+        /*
+         * if event is equals null, so it doesn't add to Track, i.e. 
+         * method add(MidiEvent) return 'false'
+         */
+        assertFalse(tr.add(null));
+        
+        /*
+         * In the first place, the MidiMessage with the array MidiMessage.data contains 
+         * -1, 47 and 0 is the meta-event End of Track, and so it 
+         * accrue to Track always, even it contains meta-event already, i.e. method
+         * add(MidiEvent) always return 'true' in this case, but size of
+         * Track doesn't increase.
+         * 
+         * In the second place, other events accrue to Track taking
+         * into account value of tick of MidiEvent, i.e. the MidiEvent
+         * with the smallest value of tick will be the first in the Track
+         * and so on and the MidiEvent with the biggest value of tick
+         * will be the last but one; the last is the meta-event End of Track
+         * 
+         * If values of ticks of two or more events are equals, they add 
+         * to Track on the basis of addition, i.e. if any event adds to Track,
+         * it will be after events with the same value of tick
+         */
+        assertTrue(tr.add(new MidiEvent(new MidiMessage1(new byte[] {-1, 47, 0}), -20)));
+        assertTrue(tr.add(new MidiEvent(new MidiMessage1(new byte[] {-1, 47, 0}), 0)));
+        
+        assertTrue(tr.add(new MidiEvent(new MidiMessage1(new byte[] {-23, 92, 89}), 8)));
+        assertTrue(tr.add(new MidiEvent(new MidiMessage1(new byte[] {23, 2, -9}), 8)));
+        
+        assertEquals(-23, tr.get(0).getMessage().getMessage()[0]);
+        assertEquals(23, tr.get(1).getMessage().getMessage()[0]);
+        assertEquals(10, tr.get(2).getMessage().getMessage()[0]);
+        assertEquals(-1, tr.get(3).getMessage().getMessage()[0]);
+    }
+
+    /**
+     * test method get(int)
+     * @throws Exception
+     */
+    public void test_get() throws Exception {
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        
+        /*
+         * numeration of events begin with 0, and so the first 
+         * event gets with index 0, the second - with index 1 
+         * and so on
+         */
+        try {
+            tr.get(-1);
+            fail("ArrayIndexOutOfBoundsException expected");
+        } catch (ArrayIndexOutOfBoundsException e) {}
+        
+        try {
+            tr.get(1);
+            fail("ArrayIndexOutOfBoundsException expected");
+        } catch (ArrayIndexOutOfBoundsException e) {}
+    }
+
+    /**
+     * test method remove(MidiEvent)
+     * @throws Exception
+     */
+    public void test_remove() throws Exception {
+        /*
+         * create an empty Track
+         */
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        
+        byte[] bt = new byte[] {1, 2, 3};
+        MidiMessage1 message = new MidiMessage1(bt);
+        MidiEvent event1 = new MidiEvent(message, 10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
+        tr.add(event1);
+        tr.add(event2);
+        tr.add(event3);
+        assertEquals(4, tr.size());
+        
+        assertTrue(tr.remove(event3));
+        assertFalse(tr.remove(event3));
+        assertEquals(3, tr.size());
+        
+        assertFalse(tr.remove(new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0)));
+        assertTrue(tr.remove(event2));
+        
+        assertFalse(tr.remove(null));
+        
+        /*
+         * I can remove meta-event End of Track
+         */
+        MidiEvent event4 = tr.get(1);
+        assertTrue(tr.remove(event4));
+        
+        assertTrue(tr.add(event4));
+        assertTrue(tr.remove(event4));
+        
+        assertTrue(tr.remove(event1));
+        
+        /*
+         * If I remove meta-event End of Track and I have some
+         * events in Track still, and I try to add new event, it's 
+         * all right, it adds, and so meta-event End of Track adds too;
+         * if Track doesn't contain any events, i.e. method size() 
+         * return 0, if I try to add meta-event End of Track, it's
+         * all right, but if I try to add not meta-event, I have some problem.
+         * In the first place, meta-event adds to the Track, but event
+         * I want to add doesn't add, i.e. size() return 1. But after it
+         * I use method add(MidiEvent), and 
+         * method add(<the_same_event_I_want_to_add>) return 'false'!!!
+         * And method remove(<the_same_event_I_want_to_add>) return 
+         * 'false' too! And only now method add(<the_same_event_I_want_to_add>)
+         * return 'true', and this event adds to Track;
+         * Other events accrue to Track correctly.
+         */
+        assertEquals(0, tr.size());
+        
+        assertTrue(tr.add(event4)); //add meta-event to empty track; it's OK
+        
+        assertTrue(tr.add(event1)); //add some event
+        assertEquals(2, tr.size());
+        assertTrue(tr.remove(event4)); //remove meta-event End of Track
+        assertTrue(tr.add(event2)); //add some event...
+        assertEquals(3, tr.size()); //...and size of Track is 3; meta-event 
+                                    //was added
+        assertTrue(tr.remove(tr.get(2)));
+        assertTrue(tr.remove(event2));
+        assertTrue(tr.remove(event1)); //now Track is empty
+        try {
+            tr.add(event2); //add some event; I catch exception
+            fail("ArrayIndexOutOfBoundsException expected");
+        } catch (ArrayIndexOutOfBoundsException e) {}
+        assertEquals(1, tr.size()); //size of Track is 1; it contain meta-event
+        assertTrue(tr.add(event1)); //I can add event1,...
+        assertFalse(tr.add(event2)); //...but I can't add event2...
+        assertFalse(tr.remove(event2)); //...and remove it too!
+        assertTrue(tr.add(event2)); //but now it's all right!
+        assertEquals(3, tr.size()); //Track contain two my events and meta-event
+    }
+
+    /**
+     * test method size()
+     * @throws Exception
+     */
+    public void test_size() throws Exception {
+        /*
+         * create an empty Track
+         */
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        assertEquals(1, tr.size());
+        MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), 10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 0);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
+        tr.add(event1);
+        tr.add(event2); 
+        tr.add(event3);
+        assertEquals(4, tr.size());
+        tr.add(event1); //false; it contains already
+        assertEquals(4, tr.size());
+        tr.remove(event1);
+        tr.remove(event2);
+        assertEquals(2, tr.size());
+        tr.remove(event3);
+        tr.remove(tr.get(0));
+        assertEquals(0, tr.size());
+        tr.add(new MidiEvent(new MidiMessage1(new byte[] {-1, 47, 0}), 6L));
+        assertEquals(1, tr.size());
+        tr.remove(tr.get(0));
+        assertEquals(0, tr.size());
+        try {
+            tr.add(event1);
+            fail("ArrayIndexOutOfBoundsException expected");
+        } catch (ArrayIndexOutOfBoundsException e) {}
+        assertEquals(1, tr.size());
+    }
+
+    /**
+     * test method ticks()
+     * @throws Exception
+     */
+    public void test_ticks() throws Exception {
+        /*
+         * create an empty Track
+         */
+        Sequence seq = new Sequence(Sequence.SMPTE_24, 67, 9);
+        Track tr = seq.createTrack();
+        assertEquals(0, tr.ticks());
+        MidiEvent event1 = new MidiEvent(new MidiMessage1(new byte[] {1, 2, 3}), -10L);
+        MidiEvent event2 = new MidiEvent(new MidiMessage1(new byte[] {23, -16, 4}), 2L);
+        MidiEvent event3 = new MidiEvent(new MidiMessage1(new byte[] {3, -67, -1}), 6L);
+        /*
+         * values of Track.ticks() only positive;
+         * value of Track.ticks() is the biggest value of ticks of events that
+         * contain in the Track; 
+         * if I remove event with the biggest value of 
+         * tick, value that return method Track.ticks() will be the same
+         */
+        tr.add(event1);
+        assertEquals(0, tr.ticks());
+        tr.add(event2);
+        assertEquals(2, tr.ticks());
+        tr.add(event3);
+        assertEquals(6, tr.ticks());
+        tr.remove(event3); //I remove event with the biggest tick,...
+        assertEquals(6, tr.ticks()); //...but value that return method Track.ticks() the
same
+        tr.remove(event2);
+        tr.remove(event1);
+        assertEquals(6, tr.ticks()); //and even now...
+        tr.add(event2);
+        assertEquals(6, tr.ticks()); //and now...
+        tr.remove(tr.get(1));
+        tr.remove(event2);
+        assertEquals(0, tr.size()); //Track is empty
+        assertEquals(0, tr.ticks()); //Track is empty, value that return Track.ticks() equals
0
+    }
+    
+    /**
+     * subsidiary class to use abstract class MidiMessage
+     * 
+     */
+    static public class MidiMessage1 extends MidiMessage {
+        MidiMessage1(byte[] data) {
+            super(data);
+        }
+        
+        public Object clone() {
+            return null;
+        }
+    }
+}



Mime
View raw message