📄 segmentedtimelinetests.java
字号:
//////////////////////////////////////////////////////////////////////////
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMsSegment() {
verifyOneSegment(msTimeline);
}
/**
* Tests one segment of the ms timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMs2Segment() {
verifyOneSegment(ms2Timeline);
}
/**
* Tests one segment of the Monday through Friday timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testMondayThroughFridaySegment() {
verifyOneSegment(mondayFridayTimeline);
}
/**
* Tests one segment of the Fifteen timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
*/
public void testFifteenMinSegment() {
verifyOneSegment(fifteenMinTimeline);
}
/**
* Tests one segment of the Monday through Friday timeline. Internal indices
* inside one segment as well as adjacent segments are verified.
* @param timeline the timeline to use for verifications.
*/
public void verifyOneSegment(SegmentedTimeline timeline) {
for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
testCycle += TEST_CYCLE_INC) {
// get two consecutive segments for various tests
SegmentedTimeline.Segment segment1 =
timeline.getSegment(monday.getTime().getTime() + testCycle);
SegmentedTimeline.Segment segment2 =
timeline.getSegment(segment1.getSegmentEnd() + 1);
// verify segments are consecutive and correct
assertEquals(segment1.getSegmentNumber() + 1, segment2.getSegmentNumber());
assertEquals(segment1.getSegmentEnd() + 1, segment2.getSegmentStart());
assertEquals(segment1.getSegmentStart() + timeline.getSegmentSize() - 1,
segment1.getSegmentEnd());
assertEquals(segment1.getSegmentStart() + timeline.getSegmentSize(),
segment2.getSegmentStart());
assertEquals(segment1.getSegmentEnd() + timeline.getSegmentSize(),
segment2.getSegmentEnd());
// verify various indices inside a segment are the same segment
long delta;
if (timeline.getSegmentSize() > 1000000) {
delta = timeline.getSegmentSize() / 10000;
}
else if (timeline.getSegmentSize() > 100000) {
delta = timeline.getSegmentSize() / 1000;
}
else if (timeline.getSegmentSize() > 10000) {
delta = timeline.getSegmentSize() / 100;
}
else if (timeline.getSegmentSize() > 1000) {
delta = timeline.getSegmentSize() / 10;
}
else if (timeline.getSegmentSize() > 100) {
delta = timeline.getSegmentSize() / 5;
}
else {
delta = 1;
}
long start = segment1.getSegmentStart() + delta;
long end = segment1.getSegmentStart() + timeline.getSegmentSize() - 1;
SegmentedTimeline.Segment lastSeg = timeline.getSegment(segment1.
getSegmentStart());
SegmentedTimeline.Segment seg;
for (long i = start; i < end; i += delta) {
seg = timeline.getSegment(i);
assertEquals(lastSeg.getSegmentNumber(), seg.getSegmentNumber());
assertEquals(lastSeg.getSegmentStart(), seg.getSegmentStart());
assertEquals(lastSeg.getSegmentEnd(), seg.getSegmentEnd());
assertTrue(lastSeg.getIndex() < seg.getIndex());
lastSeg = seg;
}
// try next segment
seg = timeline.getSegment(end + 1);
assertEquals(segment2.getSegmentNumber(), seg.getSegmentNumber());
assertEquals(segment2.getSegmentStart(), seg.getSegmentStart());
assertEquals(segment2.getSegmentEnd(), seg.getSegmentEnd());
}
}
//////////////////////////////////////////////////////////////////////////
// test inc methods
//////////////////////////////////////////////////////////////////////////
/**
* Tests the inc methods on the msTimeline.
*/
public void testMsInc() {
verifyInc(msTimeline);
}
/**
* Tests the inc methods on the msTimeline.
*/
public void testMs2Inc() {
verifyInc(ms2Timeline);
}
/**
* Tests the inc methods on the Monday through Friday timeline.
*/
public void testMondayThroughFridayInc() {
verifyInc(mondayFridayTimeline);
}
/**
* Tests the inc methods on the Fifteen minute timeline.
*/
public void testFifteenMinInc() {
verifyInc(fifteenMinTimeline);
}
/**
* Tests the inc methods.
* @param timeline the timeline to use for verifications.
*/
public void verifyInc(SegmentedTimeline timeline) {
for (long testCycle = TEST_CYCLE_START; testCycle < TEST_CYCLE_END;
testCycle += TEST_CYCLE_INC) {
long m = timeline.getSegmentSize();
SegmentedTimeline.Segment segment = timeline.getSegment(testCycle);
SegmentedTimeline.Segment seg1 = segment.copy();
for (int i = 0; i < 1000; i++) {
// test inc() method
SegmentedTimeline.Segment seg2 = seg1.copy();
seg2.inc();
if ((seg1.getSegmentEnd() + 1) != seg2.getSegmentStart()) {
// logically consecutive segments non-physically consecutive
// (with non-contained time in between)
assertTrue(!timeline.containsDomainRange(seg1.getSegmentEnd() + 1,
seg2.getSegmentStart() - 1));
assertEquals(0, (seg2.getSegmentStart() - seg1.getSegmentStart()) % m);
assertEquals(0, (seg2.getSegmentEnd() - seg1.getSegmentEnd()) % m);
assertEquals(0, (seg2.getIndex() - seg1.getIndex()) % m);
}
else {
// physically consecutive
assertEquals(seg1.getSegmentStart() + m,
seg2.getSegmentStart());
assertEquals(seg1.getSegmentEnd() + m, seg2.getSegmentEnd());
assertEquals(seg1.getIndex() + m, seg2.getIndex());
}
// test inc(n) method
SegmentedTimeline.Segment seg3 = seg1.copy();
SegmentedTimeline.Segment seg4 = seg1.copy();
for (int j = 0; j < i; j++) {
seg3.inc();
}
seg4.inc(i);
assertEquals(seg3.getSegmentStart(), seg4.getSegmentStart());
assertEquals(seg3.getSegmentEnd(), seg4.getSegmentEnd());
assertEquals(seg3.getIndex(), seg4.getIndex());
// go to another segment to continue test
seg1.inc();
}
}
}
//////////////////////////////////////////////////////////////////////////
// main include and excluded segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests that the msTimeline's included and excluded
* segments are being calculated correctly.
*/
public void testMsInclucedAndExcludedSegments() {
verifyInclucedAndExcludedSegments(msTimeline, 0);
}
/**
* Tests that the ms2Timeline's included and excluded
* segments are being calculated correctly.
*/
public void testMs2InclucedAndExcludedSegments() {
verifyInclucedAndExcludedSegments(ms2Timeline, 1);
}
/**
* Tests that the Monday through Friday timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testMondayThroughFridayInclucedAndExcludedSegments() {
verifyInclucedAndExcludedSegments(mondayFridayTimeline, monday.getTime().getTime());
}
/**
* Tests that the Fifteen-Min timeline's included and excluded
* segments are being calculated correctly. The test is performed starting
* on the first monday after 1/1/2000 and for five years.
*/
public void testFifteenMinInclucedAndExcludedSegments() {
verifyInclucedAndExcludedSegments(fifteenMinTimeline, monday9am.getTime().getTime());
}
/**
* Tests that a timeline's included and excluded segments are being calculated
* correctly.
* @param timeline the timeline to varify
* @param n the first segment number to start verifying
*/
public void verifyInclucedAndExcludedSegments(SegmentedTimeline timeline, long n) {
// clear any exceptions in this timeline
timeline.getExceptionSegments().clear();
// test some included and excluded segments
SegmentedTimeline.Segment segment = timeline.getSegment(n);
for (int i = 0; i < 1000; i++) {
int d = (i % timeline.getSegmentsGroup());
if (d < timeline.getSegmentsIncluded()) {
// should be an included segment
assertTrue(segment.inIncludeSegments());
assertTrue(!segment.inExcludeSegments());
assertTrue(!segment.inExceptionSegments());
}
else {
// should be an excluded segment
assertTrue(!segment.inIncludeSegments());
assertTrue(segment.inExcludeSegments());
assertTrue(!segment.inExceptionSegments());
}
segment.inc();
}
}
//////////////////////////////////////////////////////////////////////////
// test exception segments
//////////////////////////////////////////////////////////////////////////
/**
* Tests methods related to exceptions methods in the msTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMsExceptionSegments() throws ParseException {
verifyExceptionSegments(msTimeline, MS_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the ms2BaseTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMs2BaseTimelineExceptionSegments() throws ParseException {
verifyExceptionSegments(ms2BaseTimeline, MS2_BASE_TIMELINE_EXCEPTIONS, NUMBER_FORMAT);
}
/**
* Tests methods related to exceptions methods in the mondayFridayTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testMondayThoughFridayExceptionSegments() throws ParseException {
verifyExceptionSegments(mondayFridayTimeline, US_HOLIDAYS, DATE_FORMAT);
}
/**
* Tests methods related to exceptions methods in the fifteenMinTimeline.
*
* @throws ParseException if there is a parsing error.
*/
public void testFifteenMinExceptionSegments() throws ParseException {
verifyExceptionSegments(fifteenMinTimeline, FIFTEEN_MIN_EXCEPTIONS, DATE_TIME_FORMAT);
}
/**
* Tests methods related to adding exceptions.
*
* @param timeline the timeline to verify
* @param exceptionString array of Strings that represent the exceptions
* @param fmt Format object that can parse the exceptionString strings
*
* @throws ParseException if there is a parsing error.
*/
public void verifyExceptionSegments(SegmentedTimeline timeline,
String[] exceptionString,
Format fmt)
throws ParseException {
// fill in the exceptions
long[] exception =
verifyFillInExceptions(timeline, exceptionString, fmt);
int m = exception.length;
// verify list of exceptions
assertEquals(exception.length, timeline.getExceptionSegments().size());
SegmentedTimeline.Segment lastSegment = timeline.getSegment(exception[m - 1]);
for (int i = 0; i < m; i++) {
SegmentedTimeline.Segment segment = timeline.getSegment(exception[i]);
assertTrue(segment.inExceptionSegments());
// include current exception and last one
assertEquals(m - i, timeline.getExceptionSegmentCount(
segment.getSegmentStart(), lastSegment.getSegmentEnd()));
// exclude current exception and last one
assertEquals(Math.max(0, m - i - 2), timeline.getExceptionSegmentCount(
exception[i] + 1, exception[m - 1] - 1));
}
}
//////////////////////////////////////////////////////////////////////////
// test timeline translations
//////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -