001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.*;
030:
031: import junit.framework.TestCase;
032: import junit.framework.*;
033:
034: public class TestTimeSpanSet extends TestCase {
035: private class X implements TimeSpan {
036: long start;
037: long end;
038: String text;
039:
040: public X(long x, String t) {
041: start = x;
042: end = x + 1;
043: text = t;
044: }
045:
046: public long getStartTime() {
047: return start;
048: }
049:
050: public long getEndTime() {
051: return end;
052: }
053:
054: public void setTimeSpan(long s, long e) {
055: start = s;
056: end = e;
057: }
058:
059: public String getText() {
060: return text;
061: }
062:
063: public void setText(String t) {
064: text = t;
065: }
066:
067: public String toString() {
068: return text + "/" + start + "-" + end;
069: }
070: }
071:
072: public void test_TSS() {
073: TimeSpanSet tss = new TimeSpanSet();
074:
075: assertTrue(tss.add(new X(TimeSpan.MIN_VALUE, "milk")));
076: assertTrue(tss.add(new X(TimeSpan.MIN_VALUE, "dark")));
077:
078: X forever = new X(TimeSpan.MIN_VALUE, "bittersweet");
079: forever.setTimeSpan(forever.getStartTime(), TimeSpan.MAX_VALUE);
080: assertTrue(tss.add(forever));
081:
082: X longTime = new X(TimeSpan.MIN_VALUE, "semisweet");
083: longTime.setTimeSpan(forever.getStartTime() + TimeSpan.EPSILON,
084: TimeSpan.MAX_VALUE);
085: assertTrue(tss.add(longTime));
086:
087: X midEpoch3 = new X(TimeSpan.MIN_VALUE, "hazelnut");
088: midEpoch3.setTimeSpan(midEpoch3.getStartTime()
089: + TimeSpan.EPSILON, 10);
090: assertTrue(tss.add(midEpoch3));
091:
092: X midEpoch1 = new X(9, "truffle");
093: midEpoch1.setTimeSpan(midEpoch1.getStartTime(),
094: TimeSpan.MAX_VALUE);
095: assertTrue(tss.add(midEpoch1));
096:
097: X midEpoch2 = new X(9, "caramel");
098: midEpoch2.setTimeSpan(midEpoch2.getStartTime(),
099: TimeSpan.MAX_VALUE);
100: assertTrue(tss.add(midEpoch2));
101:
102: for (int i = 3; i < 16; i += 2) {
103: assertTrue(tss.add(new X(i, Long.toString(i))));
104: }
105:
106: int[] expected = new int[] { 5, 5, 5, 5, 6, 6, 7, 7, 8, 8, 11,
107: 11, 12, 12, 13, 13, 14, 14, 14, 14 };
108:
109: for (int i = 0; i < 20; i++) {
110: assertTrue("search(" + i + "," + (i + 1) + ")",
111: expected[i] == tss.search(i, i + 1));
112: }
113: }
114: }
|