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 TestNonOverlappingTimeSpanSet extends TestCase {
035: public void test_notss() {
036: class X implements TimeSpan {
037: long start;
038: long end;
039:
040: public X(long x) {
041: start = x;
042: end = x + 3;
043: }
044:
045: public long getStartTime() {
046: return start;
047: }
048:
049: public long getEndTime() {
050: return end;
051: }
052:
053: public String toString() {
054: return "{" + start + "-" + end + "}";
055: }
056: }
057:
058: class Fill implements NewTimeSpan, Cloneable {
059: long start = TimeSpan.MIN_VALUE;
060: long end = TimeSpan.MAX_VALUE;
061:
062: public Fill() {
063: }
064:
065: public long getStartTime() {
066: return start;
067: }
068:
069: public long getEndTime() {
070: return end;
071: }
072:
073: public void setTimeSpan(long s, long e) {
074: start = s;
075: end = e;
076: }
077:
078: public Object clone() {
079: Fill fill = new Fill();
080: fill.setTimeSpan(getStartTime(), getEndTime());
081: return fill;
082: }
083:
084: public String toString() {
085: return "<" + start + "-" + end + ">";
086: }
087: }
088:
089: NonOverlappingTimeSpanSet notss = new NonOverlappingTimeSpanSet();
090:
091: for (int i = 1; i < 25; i += 5) {
092: notss.add(new X(i));
093: }
094:
095: assertEquals("initial value",
096: "[{1-4}, {6-9}, {11-14}, {16-19}, {21-24}]", str(notss));
097:
098: assertEquals("intersects(0)", "null", str(notss.intersects(0)));
099: assertEquals("intersects(4)", "null", str(notss.intersects(4)));
100: assertEquals("intersects(6)", "{6-9}", str(notss.intersects(6)));
101: assertEquals("intersects(7)", "{6-9}", str(notss.intersects(7)));
102: assertEquals("intersects(9)", "null", str(notss.intersects(9)));
103: assertEquals("intersects(10)", "null",
104: str(notss.intersects(10)));
105: assertEquals("intersects(-1)", "null",
106: str(notss.intersects(-1)));
107: assertEquals("intersects(1000)", "null", str(notss
108: .intersects(1000)));
109:
110: NonOverlappingTimeSpanSet notssClone = new NonOverlappingTimeSpanSet(
111: notss);
112: assertEquals("[{1-4}, {6-9}, {11-14}, {16-19}, {21-24}]",
113: str(notssClone));
114:
115: NonOverlappingTimeSpanSet notssEmpty = new NonOverlappingTimeSpanSet(
116: new ArrayList());
117: assertEquals("[]", str(notssEmpty));
118:
119: NonOverlappingTimeSpanSet fs = notss.fill(new Fill());
120: assertEquals("filled intersects(0)",
121: "<-2305843009213693951-1>", str(fs.intersects(0)));
122: assertEquals("filled intersects(1)", "{1-4}", str(fs
123: .intersects(1)));
124: assertEquals("filled intersects(2)", "{1-4}", str(fs
125: .intersects(2)));
126: assertEquals("filled intersects(4)", "<4-6>", str(fs
127: .intersects(4)));
128: assertEquals("filled intersects(5)", "<4-6>", str(fs
129: .intersects(5)));
130: assertEquals("filled intersects(6)", "{6-9}", str(fs
131: .intersects(6)));
132: assertEquals("filled intersects(24)",
133: "<24-2305843009213693951>", str(fs.intersects(24)));
134: assertEquals("filled intersects(1000)",
135: "<24-2305843009213693951>", str(fs.intersects(1000)));
136: }
137:
138: private String str(Object o) {
139: return (o == null) ? "null" : o.toString();
140: }
141: }
|