Calendar Redesign including addition of Chinese Calendar

X-SVN-Rev: 2942
This commit is contained in:
Alan Liu 2000-11-18 00:17:58 +00:00
parent 396af9063d
commit 1a3d3875f3
20 changed files with 2016 additions and 390 deletions

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/TestAll.java,v $
* $Date: 2000/10/06 21:42:38 $
* $Revision: 1.4 $
* $Date: 2000/11/18 00:17:43 $
* $Revision: 1.5 $
*
*****************************************************************************************
*/
@ -86,7 +86,8 @@ public class TestAll extends TestFmwk {
new com.ibm.test.calendar.CompatibilityTest(),
new com.ibm.test.calendar.HebrewTest(),
new com.ibm.test.calendar.IBMCalendarTest(),
new com.ibm.test.calendar.IslamicTest()
new com.ibm.test.calendar.IslamicTest(),
new com.ibm.test.calendar.ChineseTest()
});
}

View File

@ -18,6 +18,15 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
new CalendarRegression().run(args);
}
static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
/*
Synopsis: java.sql.Timestamp constructor works wrong on Windows 95
@ -48,12 +57,18 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear();
cal.set(1900, 15, 5, 5, 8, 13);
if (cal.get(Calendar.HOUR) != 5) {
logln(zone.getID() + " " +
//zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
logln("Fail: " + zone.getID() + " " +
zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
bad = true;
} else if (false) { // Change to true to debug
logln("OK: " + zone.getID() + " " +
zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
}
}
if (bad) errln("TimeZone problems with GC");
@ -650,6 +665,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
Date d01 = new Date(97, Calendar.MARCH, 15, 12, 00, 56);
Date d10 = new Date(97, Calendar.MARCH, 15, 12, 34, 00);
Date d11 = new Date(97, Calendar.MARCH, 15, 12, 34, 56);
Date dM = new Date(97, Calendar.JANUARY, 15, 12, 34, 56);
Date epoch = new Date(70, Calendar.JANUARY, 1);
Calendar cal = Calendar.getInstance();
@ -657,8 +673,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear( Calendar.MINUTE );
logln(cal.getTime().toString());
if (!cal.getTime().equals(d01))
errln("Fail: clear(MINUTE) broken");
if (!cal.getTime().equals(d01)) {
errln("Fail: " + d11 + " clear(MINUTE) => expect " +
d01 + ", got " + cal.getTime());
}
cal.set( Calendar.SECOND, 0 );
logln(cal.getTime().toString());
@ -679,7 +697,15 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear();
logln(cal.getTime().toString());
if (!cal.getTime().equals(epoch))
errln("Fail: clear() broken Want " + epoch);
errln("Fail: after clear() expect " + epoch + ", got " + cal.getTime());
cal.setTime(d11);
cal.clear( Calendar.MONTH );
logln(cal.getTime().toString());
if (!cal.getTime().equals(dM)) {
errln("Fail: " + d11 + " clear(MONTH) => expect " +
dM + ", got " + cal.getTime());
}
}
public void Test4114578() {
@ -891,17 +917,20 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
* other of these will wrap. We've modified the test given in the bug
* report to therefore only check the behavior of a calendar with a zero raw
* offset zone.
*
* NEWCAL We no longer support dates before Calendar.MIN_DATE or after
* Calendar.MAX_DATE. Modify this test.
*/
public void Test4145158() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTime(new Date(Long.MIN_VALUE));
calendar.setTime(Calendar.MIN_DATE);
int year1 = calendar.get(Calendar.YEAR);
int era1 = calendar.get(Calendar.ERA);
calendar.setTime(new Date(Long.MAX_VALUE));
calendar.setTime(Calendar.MAX_DATE);
int year2 = calendar.get(Calendar.YEAR);
int era2 = calendar.get(Calendar.ERA);
@ -916,7 +945,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
public void Test4145983() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
Date[] DATES = { new Date(Long.MAX_VALUE), new Date(Long.MIN_VALUE) };
Date[] DATES = { Calendar.MAX_DATE, Calendar.MIN_DATE };
for (int i=0; i<DATES.length; ++i) {
calendar.setTime(DATES[i]);
int year = calendar.get(Calendar.YEAR);
@ -935,29 +964,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
* field. - liu 6/29/98
*/
public void Test4147269() {
final String[] fieldName = {
"ERA",
"YEAR",
"MONTH",
"WEEK_OF_YEAR",
"WEEK_OF_MONTH",
"DAY_OF_MONTH",
"DAY_OF_YEAR",
"DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH",
"AM_PM",
"HOUR",
"HOUR_OF_DAY",
"MINUTE",
"SECOND",
"MILLISECOND",
"ZONE_OFFSET",
"DST_OFFSET"
};
GregorianCalendar calendar = new GregorianCalendar();
calendar.setLenient(false);
Date date = new Date(1996-1900, Calendar.JANUARY, 3); // Arbitrary date
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
for (int field = 0; field < calendar.getFieldCount(); field++) {
calendar.setTime(date);
// Note: In the bug report, getActualMaximum() was called instead
// of getMaximum() -- this was an error. The validation code doesn't
@ -969,7 +979,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
calendar.getTime(); // Force time computation
// We expect an exception to be thrown. If we fall through
// to the next line, then we have a bug.
errln("Test failed with field " + fieldName[field] +
errln("Test failed with field " + FIELD_NAME[field] +
", date before: " + date +
", date after: " + calendar.getTime() +
", value: " + value + " (max = " + max +")");
@ -990,22 +1000,22 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
GregorianCalendar calendar = new GregorianCalendar(zones[i]);
// Make sure extreme values don't wrap around
calendar.setTime(new Date(Long.MIN_VALUE));
calendar.setTime(Calendar.MIN_DATE);
if (calendar.get(Calendar.ERA) != GregorianCalendar.BC) {
errln("Fail: Date(Long.MIN_VALUE) has an AD year");
errln("Fail: Calendar.MIN_DATE has an AD year");
}
calendar.setTime(new Date(Long.MAX_VALUE));
calendar.setTime(Calendar.MAX_DATE);
if (calendar.get(Calendar.ERA) != GregorianCalendar.AD) {
errln("Fail: Date(Long.MAX_VALUE) has a BC year");
errln("Fail: Calendar.MAX_DATE has a BC year");
}
calendar.setGregorianChange(new Date(Long.MAX_VALUE));
calendar.setGregorianChange(Calendar.MAX_DATE);
// to obtain a pure Julian calendar
boolean is100Leap = calendar.isLeapYear(100);
if (!is100Leap) {
errln("test failed with zone " + zones[i].getID());
errln(" cutover date is Date(Long.MAX_VALUE)");
errln(" cutover date is Calendar.MAX_DATE");
errln(" isLeapYear(100) returns: " + is100Leap);
}
}
@ -1134,9 +1144,9 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
for (int j=0; j<calendars.length; ++j) {
GregorianCalendar calendar = calendars[j];
if (k == 1) {
calendar.setGregorianChange(new Date(Long.MIN_VALUE));
calendar.setGregorianChange(Calendar.MIN_DATE);
} else if (k == 2) {
calendar.setGregorianChange(new Date(Long.MAX_VALUE));
calendar.setGregorianChange(Calendar.MAX_DATE);
}
format.setCalendar((Calendar)calendar.clone());
@ -1161,7 +1171,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
if (valid && newYear != years[i]) {
errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
} else if (!valid && newYear == years[i]) {
errln(" FAIL: " + newYear + " should be invalid");
// We no longer require strict year maxima. That is, the calendar
// algorithm may work for values > the stated maximum.
//errln(" FAIL: " + newYear + " should be invalid");
logln(" Note: " + newYear + " > maximum, but still valid");
}
}
}
@ -1177,12 +1190,6 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
{ 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 },
{ 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 }
};
String[] fieldNames = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DATE",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH",
"AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND",
"ZONE_OFFSET", "DST_OFFSET"
};
int limit = 40;
GregorianCalendar cal = new GregorianCalendar();
@ -1192,11 +1199,11 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
if (cal.getTime().getTime() != 0) {
errln("Hour rolling broken");
}
for (int op=0; op<2; ++op) {
logln("Testing GregorianCalendar " +
(op==0 ? "add" : "roll"));
for (int field=0; field < Calendar.FIELD_COUNT; ++field) {
for (int field=0; field < cal.getFieldCount(); ++field) {
if (field != Calendar.ZONE_OFFSET &&
field != Calendar.DST_OFFSET) {
for (int j=0; j<fieldsList.length; ++j) {
@ -1220,7 +1227,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.get(Calendar.SECOND) != fields[5] ||
cal.get(Calendar.MILLISECOND) != fields[6]) {
errln("Field " + field +
" (" + fieldNames[field] +
" (" + FIELD_NAME[field] +
") FAIL, expected " +
fields[0] +
"/" + (fields[1] + 1) +
@ -1240,7 +1247,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.set(fields[0], fields[1], fields[2],
fields[3], fields[4], fields[5]);
cal.set(Calendar.MILLISECOND, fields[6]);
errln(cal.get(Calendar.YEAR) +
logln("Start date: " + cal.get(Calendar.YEAR) +
"/" + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) +
" " + cal.get(Calendar.HOUR_OF_DAY) +
@ -1257,8 +1264,8 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
long t = cal.getTime().getTime();
long delta = t - prev;
prev = t;
errln((op == 0 ? "add" : "roll") +
(i < limit ? " +> " : " -> ") +
logln((op == 0 ? "add(" : "roll(") + FIELD_NAME[field] +
(i < limit ? ", +1) => " : ", -1) => ") +
cal.get(Calendar.YEAR) +
"/" + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) +
@ -1266,7 +1273,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
":" + cal.get(Calendar.MINUTE) +
":" + cal.get(Calendar.SECOND) +
"." + cal.get(Calendar.MILLISECOND) +
" d=" + delta);
" delta=" + delta + " ms");
}
}
}
@ -1274,7 +1281,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
}
}
}
public void Test4174361() {
GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
@ -1577,6 +1584,77 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
}
}
}
/**
* Set behavior of DST_OFFSET field. ICU4J Jitterbug 9.
*/
public void TestJ9() {
int HOURS = 60*60*1000;
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("PST"),
Locale.US);
final int END_FIELDS = 0x1234;
int[] DATA = {
// With no explicit ZONE/DST expect 12:00 am
Calendar.MONTH, Calendar.JUNE,
END_FIELDS,
0, 0, // expected hour, min
// Normal ZONE/DST for June 1 Pacific is 8:00/1:00
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
Calendar.DST_OFFSET, HOURS,
END_FIELDS,
0, 0, // expected hour, min
// With ZONE/DST of 8:00/0:30 expect time of 12:30 am
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
Calendar.DST_OFFSET, HOURS/2,
END_FIELDS,
0, 30, // expected hour, min
// With ZONE/DST of 8:00/UNSET expect time of 1:00 am
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
END_FIELDS,
1, 0, // expected hour, min
// With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before)
Calendar.MONTH, Calendar.JUNE,
Calendar.DST_OFFSET, HOURS/2,
END_FIELDS,
16, 30, // expected hour, min
};
for (int i=0; i<DATA.length; ) {
int start = i;
cal.clear();
// Set fields
while (DATA[i] != END_FIELDS) {
cal.set(DATA[i++], DATA[i++]);
}
++i; // skip over END_FIELDS
// Get hour/minute
int h = cal.get(Calendar.HOUR_OF_DAY);
int m = cal.get(Calendar.MINUTE);
// Check
if (h != DATA[i] || m != DATA[i+1]) {
errln("Fail: expected " + DATA[i] + ":" + DATA[i+1] +
", got " + h + ":" + m + " after:");
while (DATA[start] != END_FIELDS) {
logln("set(" + FIELD_NAME[DATA[start++]] +
", " + DATA[start++] + ");");
}
}
i += 2; // skip over expected hour, min
}
}
}
//eof

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/CalendarTest.java,v $
* $Date: 2000/10/17 20:55:09 $
* $Revision: 1.6 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.7 $
*
*****************************************************************************************
*/
@ -18,6 +18,8 @@ import com.ibm.text.DateFormat;
import com.ibm.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Hashtable;
import java.util.Enumeration;
import com.ibm.util.*;
/**
@ -50,8 +52,20 @@ public class CalendarTest extends TestFmwk {
public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");
final String pattern = "E, MM/dd/yyyy G HH:mm:ss.S z";
private static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
public static final String fieldName(int f) {
return (f>=0 && f<FIELD_NAME.length) ?
FIELD_NAME[f] : ("<Field " + f + ">");
}
/**
* Iterates through a list of calendar <code>TestCase</code> objects and
* makes sure that the time-to-fields and fields-to-time calculations work
@ -64,18 +78,25 @@ public class CalendarTest extends TestFmwk {
// Get a format to use for printing dates in the calendar system we're testing
DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault());
// TODO Fix this to include the ERA ("G") again once ChineseDateFormat
// is implemented - Liu
final String pattern = "E, MM/dd/yyyy HH:mm:ss.S z";
((SimpleDateFormat)format).applyPattern(pattern);
DateFormat testFmt = (DateFormat)format.clone();
// This format is used for pringing Gregorian dates. This one is easier
// This format is used for printing Gregorian dates.
DateFormat gregFormat = new SimpleDateFormat(pattern);
gregFormat.setTimeZone(UTC);
GregorianCalendar pureGreg = new GregorianCalendar(UTC);
pureGreg.setGregorianChange(Calendar.MIN_DATE);
DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G");
pureGregFmt.setCalendar(pureGreg);
// Now iterate through the test cases and see what happens
for (int i = 0; i < cases.length; i++)
{
TestCase test = cases[i];
testFmt.setCalendar(test);
//
// First we want to make sure that the millis -> fields calculation works
@ -84,27 +105,24 @@ public class CalendarTest extends TestFmwk {
// that they're the same as the ones in the testcase
//
test.applyTime(cal);
if (!test.fieldsEqual(cal)) {
errln("ERROR: millis --> fields calculation incorrect for "
+ gregFormat.format(test.getTime()));
logln(" expected " + testFmt.format(test.getTime()));
logln(" got " + format.format(cal.getTime()) );
if (!test.fieldsEqual(cal, this)) {
errln("Fail: (millis=>fields) " +
gregFormat.format(test.getTime()) + " => " +
format.format(cal.getTime()) +
", expected " + test);
}
else {
//
// If that was OK, check the fields -> millis calculation
// test.applyFields will set all of the calendar's fields to
// match those in the test case.
//
cal.setTime(new Date(0));
test.applyFields(cal);
if (!test.equals(cal)) {
errln("ERROR: fields --> millis calculation incorrect for "
+ testFmt.format(test.getTime()));
logln(" expected " + test.getTime().getTime());
logln(" got " + cal.getTime().getTime() );
}
//
// If that was OK, check the fields -> millis calculation
// test.applyFields will set all of the calendar's fields to
// match those in the test case.
//
cal.clear();
test.applyFields(cal);
if (!test.equals(cal)) {
errln("Fail: (fields=>millis) " + test + " => " +
pureGregFmt.format(cal.getTime()) +
", expected " + pureGregFmt.format(test.getTime()));
}
}
}
@ -144,7 +162,6 @@ public class CalendarTest extends TestFmwk {
cal.clear();
cal.set(test[0], test[1], test[2]);
if (roll) {
cal.roll(test[3], test[4]);
} else {
@ -155,13 +172,158 @@ public class CalendarTest extends TestFmwk {
|| cal.get(DATE) != test[7])
{
errln("Error " + name + " "+ ymdToString(test[0], test[1], test[2])
+ " field " + test[3] + " by " + test[4]
+ " " + FIELD_NAME[test[3]] + " by " + test[4]
+ ": expected " + ymdToString(test[5], test[6], test[7])
+ ", got " + ymdToString(cal.get(YEAR), cal.get(MONTH), cal.get(DATE)));
+ ", got " + ymdToString(cal));
}
}
}
/**
* Test the functions getXxxMinimum() and getXxxMaximum() by marching a
* test calendar 'cal' through 'numberOfDays' sequential days starting
* with 'startDate'. For each date, read a field value along with its
* reported actual minimum and actual maximum. These values are
* checked against one another as well as against getMinimum(),
* getGreatestMinimum(), getLeastMaximum(), and getMaximum(). We
* expect to see:
*
* 1. minimum <= actualMinimum <= greatestMinimum <=
* leastMaximum <= actualMaximum <= maximum
*
* 2. actualMinimum <= value <= actualMaximum
*
* Note: In addition to outright failures, this test reports some
* results as warnings. These are not generally of concern, but they
* should be evaluated by a human. To see these, run this test in
* verbose mode.
* @param cal the calendar to be tested
* @param fieldsToTest an array of field values to be tested, e.g., new
* int[] { Calendar.MONTH, Calendar.DAY_OF_MONTH }. It only makes
* sense to test the day fields; the time fields are not tested by this
* method. If null, then test all standard fields.
* @param startDate the first date to test
* @param testDuration if positive, the number of days to be tested.
* If negative, the number of seconds to run the test.
*/
protected void doLimitsTest(Calendar cal, int[] fieldsToTest,
Date startDate, int testDuration) {
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(startDate);
logln("Start: " + startDate);
if (fieldsToTest == null) {
fieldsToTest = new int[] {
Calendar.ERA, Calendar.YEAR, Calendar.MONTH,
Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR,
Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.YEAR_WOY,
Calendar.EXTENDED_YEAR
};
}
// Keep a record of minima and maxima that we actually see.
// These are kept in an array of arrays of hashes.
Hashtable[][] limits = new Hashtable[fieldsToTest.length][2];
Object nub = new Object(); // Meaningless placeholder
// This test can run for a long time; show progress.
long millis = System.currentTimeMillis();
long mark = millis + 5000; // 5 sec
millis -= testDuration * 1000; // stop time if testDuration<0
for (int i=0;
testDuration>0 ? i<testDuration
: System.currentTimeMillis()<millis;
++i) {
if (System.currentTimeMillis() >= mark) {
logln("(" + i + " days)");
mark += 5000; // 5 sec
}
cal.setTimeInMillis(greg.getTimeInMillis());
for (int j=0; j<fieldsToTest.length; ++j) {
int f = fieldsToTest[j];
int v = cal.get(f);
int minActual = cal.getActualMinimum(f);
int maxActual = cal.getActualMaximum(f);
int minLow = cal.getMinimum(f);
int minHigh = cal.getGreatestMinimum(f);
int maxLow = cal.getLeastMaximum(f);
int maxHigh = cal.getMaximum(f);
// Fetch the hash for this field and keep track of the
// minima and maxima.
Hashtable[] h = limits[j];
if (h[0] == null) {
h[0] = new Hashtable();
h[1] = new Hashtable();
}
h[0].put(new Integer(minActual), nub);
h[1].put(new Integer(maxActual), nub);
if (minActual < minLow || minActual > minHigh) {
errln("Fail: " + ymdToString(cal) +
" Range for min of " + FIELD_NAME[f] +
"=" + minLow + ".." + minHigh +
", actual_min=" + minActual);
}
if (maxActual < maxLow || maxActual > maxHigh) {
errln("Fail: " + ymdToString(cal) +
" Range for max of " + FIELD_NAME[f] +
"=" + maxLow + ".." + maxHigh +
", actual_max=" + maxActual);
}
if (v < minActual || v > maxActual) {
errln("Fail: " + ymdToString(cal) +
" " + FIELD_NAME[f] + "=" + v +
", actual range=" + minActual + ".." + maxActual +
", allowed=(" + minLow + ".." + minHigh + ")..(" +
maxLow + ".." + maxHigh + ")");
}
}
greg.add(Calendar.DAY_OF_YEAR, 1);
}
// Check actual maxima and minima seen against ranges returned
// by API.
StringBuffer buf = new StringBuffer();
for (int j=0; j<fieldsToTest.length; ++j) {
int f = fieldsToTest[j];
buf.setLength(0);
buf.append(FIELD_NAME[f]);
Hashtable[] h = limits[j];
boolean fullRangeSeen = true;
for (int k=0; k<2; ++k) {
int rangeLow = (k==0) ?
cal.getMinimum(f) : cal.getLeastMaximum(f);
int rangeHigh = (k==0) ?
cal.getGreatestMinimum(f) : cal.getMaximum(f);
// If either the top of the range or the bottom was never
// seen, then there may be a problem.
if (h[k].get(new Integer(rangeLow)) == null ||
h[k].get(new Integer(rangeHigh)) == null) {
fullRangeSeen = false;
}
buf.append(k==0 ? " minima seen=(" : "; maxima seen=(");
for (Enumeration e=h[k].keys(); e.hasMoreElements(); ) {
int v = ((Integer) e.nextElement()).intValue();
buf.append(" " + v);
}
buf.append(") range=" + rangeLow + ".." + rangeHigh);
}
if (fullRangeSeen) {
logln("OK: " + buf.toString());
} else {
// This may or may not be an error -- if the range of dates
// we scan over doesn't happen to contain a minimum or
// maximum, it doesn't mean some other range won't.
logln("Warning: " + buf.toString());
}
}
logln("End: " + greg.getTime());
}
/**
* Convert year,month,day values to the form "year/month/day".
* On input the month value is zero-based, but in the result string it is one-based.
@ -169,4 +331,18 @@ public class CalendarTest extends TestFmwk {
static public String ymdToString(int year, int month, int day) {
return "" + year + "/" + (month+1) + "/" + day;
}
};
/**
* Convert year,month,day values to the form "year/month/day".
*/
static public String ymdToString(Calendar cal) {
if (cal instanceof ChineseCalendar) {
return "" + cal.get(Calendar.EXTENDED_YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) +
(cal.get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") + "/" +
cal.get(Calendar.DATE);
}
return ymdToString(cal.get(Calendar.EXTENDED_YEAR),
cal.get(MONTH), cal.get(DATE));
}
}

View File

@ -0,0 +1,192 @@
/*********************************************************************
* Copyright (C) 2000, International Business Machines Corporation and
* others. All Rights Reserved.
*********************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/ChineseTest.java,v $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.1 $
*/
package com.ibm.test.calendar;
import com.ibm.util.*;
import java.util.Date;
import java.util.Locale;
public class ChineseTest extends CalendarTest {
public static void main(String args[]) throws Exception {
new ChineseTest().run(args);
}
/**
* Test basic mapping to and from Gregorian.
*/
public void TestMapping() {
final int[] DATA = {
// (Note: months are 1-based)
// Gregorian Chinese
1964, 9, 4, 4601, 7,0, 28,
1964, 9, 5, 4601, 7,0, 29,
1964, 9, 6, 4601, 8,0, 1,
1964, 9, 7, 4601, 8,0, 2,
1961, 12, 25, 4598, 11,0, 18,
1999, 6, 4, 4636, 4,0, 21,
1990, 5, 23, 4627, 4,0, 29,
1990, 5, 24, 4627, 5,0, 1,
1990, 6, 22, 4627, 5,0, 30,
1990, 6, 23, 4627, 5,1, 1,
1990, 7, 20, 4627, 5,1, 28,
1990, 7, 21, 4627, 5,1, 29,
1990, 7, 22, 4627, 6,0, 1,
};
ChineseCalendar cal = new ChineseCalendar();
StringBuffer buf = new StringBuffer();
logln("Gregorian -> Chinese");
for (int i=0; i<DATA.length; ) {
Date date = new Date(DATA[i++]-1900, DATA[i++]-1, DATA[i++]);
cal.setTime(date);
int y = cal.get(Calendar.EXTENDED_YEAR);
int m = cal.get(Calendar.MONTH)+1; // 0-based -> 1-based
int L = cal.get(ChineseCalendar.IS_LEAP_MONTH);
int d = cal.get(Calendar.DAY_OF_MONTH);
int yE = DATA[i++]; // Expected y, m, isLeapMonth, d
int mE = DATA[i++]; // 1-based
int LE = DATA[i++];
int dE = DATA[i++];
buf.setLength(0);
buf.append(date + " -> ");
buf.append(y + "/" + m + (L==1?"(leap)":"") + "/" + d);
if (y == yE && m == mE && L == LE && d == dE) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " +
yE + "/" + mE + (LE==1?"(leap)":"") + "/" + dE);
}
}
logln("Chinese -> Gregorian");
for (int i=0; i<DATA.length; ) {
Date dexp = new Date(DATA[i++]-1900, DATA[i++]-1, DATA[i++]);
int cyear = DATA[i++];
int cmonth = DATA[i++];
int cisleapmonth = DATA[i++];
int cdayofmonth = DATA[i++];
cal.clear();
cal.set(Calendar.EXTENDED_YEAR, cyear);
cal.set(Calendar.MONTH, cmonth-1);
cal.set(ChineseCalendar.IS_LEAP_MONTH, cisleapmonth);
cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
Date date = cal.getTime();
buf.setLength(0);
buf.append(cyear + "/" + cmonth +
(cisleapmonth==1?"(leap)":"") + "/" + cdayofmonth);
buf.append(" -> " + date);
if (date.equals(dexp)) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " + dexp);
}
}
}
/**
* Make sure no Gregorian dates map to Chinese 1-based day of
* month zero. This was a problem with some of the astronomical
* new moon determinations.
*/
public void TestZeroDOM() {
ChineseCalendar cal = new ChineseCalendar();
GregorianCalendar greg = new GregorianCalendar(1989, Calendar.SEPTEMBER, 1);
logln("Start: " + greg.getTime());
for (int i=0; i<1000; ++i) {
cal.setTimeInMillis(greg.getTimeInMillis());
if (cal.get(Calendar.DAY_OF_MONTH) == 0) {
errln("Fail: " + greg.getTime() + " -> " +
cal.get(Calendar.EXTENDED_YEAR) + "/" +
cal.get(Calendar.MONTH) +
(cal.get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") +
"/" + cal.get(Calendar.DAY_OF_MONTH));
}
greg.add(Calendar.DAY_OF_YEAR, 1);
}
logln("End: " + greg.getTime());
}
/**
* Test minimum and maximum functions.
*/
public void TestLimits() {
// The number of days and the start date can be adjusted
// arbitrarily to either speed up the test or make it more
// thorough, but try to test at least a full year, preferably a
// full non-leap and a full leap year.
// Final parameter is either number of days, if > 0, or test
// duration in seconds, if < 0.
doLimitsTest(new ChineseCalendar(), null,
new Date(1989-1900, Calendar.NOVEMBER, 1), -10);
}
/**
* Run through several standard tests from Dershowitz & Reingold.
*/
public void TestJulianDayMapping() {
final TestCase[] tests = {
//
// From Dershowitz & Reingold, "Calendrical Calculations".
//
// The months in this table are 1-based rather than 0-based.
//
// * Failing fields->millis
// ** Millis->fields gives 0-based month -1
// These failures were fixed by changing the start search date
// for the winter solstice from Dec 15 to Dec 1.
//
// Julian Day Era Year Month Leap DOM WkDay
new ChineseTestCase(1507231.5, 35, 11, 6, false, 12, SUN),
new ChineseTestCase(1660037.5, 42, 9, 10, false, 27, WED),
new ChineseTestCase(1746893.5, 46, 7, 8, false, 4, WED),
new ChineseTestCase(1770641.5, 47, 12, 8, false, 9, SUN),
new ChineseTestCase(1892731.5, 52, 46, 11, false, 20, WED),
new ChineseTestCase(1931579.5, 54, 33, 4, false, 5, MON),
new ChineseTestCase(1974851.5, 56, 31, 10, false, 15, SAT),
new ChineseTestCase(2091164.5, 61, 50, 3, false, 7, SUN),
new ChineseTestCase(2121509.5, 63, 13, 4, false, 24, SUN),
new ChineseTestCase(2155779.5, 64, 47, 2, false, 9, FRI),
new ChineseTestCase(2174029.5, 65, 37, 2, false, 9, SAT),
new ChineseTestCase(2191584.5, 66, 25, 2, false, 23, FRI),
new ChineseTestCase(2195261.5, 66, 35, 3, false, 9, SUN), //*
new ChineseTestCase(2229274.5, 68, 8, 5, false, 2, SUN), //*
new ChineseTestCase(2245580.5, 68, 53, 1, false, 8, WED), //**
new ChineseTestCase(2266100.5, 69, 49, 3, false, 4, SAT),
new ChineseTestCase(2288542.5, 70, 50, 8, false, 2, SAT), //*
new ChineseTestCase(2290901.5, 70, 57, 1, false, 29, SAT), //*
new ChineseTestCase(2323140.5, 72, 25, 4, true, 20, WED), //*
new ChineseTestCase(2334848.5, 72, 57, 6, false, 5, SUN),
new ChineseTestCase(2348020.5, 73, 33, 6, false, 6, FRI),
new ChineseTestCase(2366978.5, 74, 25, 5, false, 5, SUN),
new ChineseTestCase(2385648.5, 75, 16, 6, false, 12, MON),
new ChineseTestCase(2392825.5, 75, 36, 2, false, 13, WED),
new ChineseTestCase(2416223.5, 76, 40, 3, false, 22, SUN),
new ChineseTestCase(2425848.5, 77, 6, 7, false, 21, SUN),
new ChineseTestCase(2430266.5, 77, 18, 8, false, 9, MON),
new ChineseTestCase(2430833.5, 77, 20, 3, false, 15, MON),
new ChineseTestCase(2431004.5, 77, 20, 9, false, 9, THU),
new ChineseTestCase(2448698.5, 78, 9, 2, false, 14, TUE),
new ChineseTestCase(2450138.5, 78, 13, 1, false, 7, SUN),
new ChineseTestCase(2465737.5, 78, 55, 10, false, 14, WED),
new ChineseTestCase(2486076.5, 79, 51, 6, false, 7, SUN),
// Additional tests not from D&R
new ChineseTestCase(2467496.5, 78, 60, 8, false, 2, FRI), // year 60
};
ChineseCalendar cal = new ChineseCalendar();
cal.setLenient(true);
doTestCases(tests, cal);
}
}

View File

@ -0,0 +1,48 @@
package com.ibm.test.calendar;
import com.ibm.util.*;
import java.util.Date;
public class ChineseTestCase extends TestCase {
/**
* Initialize an object using a Julian day number and
* the corresponding fields for the calendar being tested.
*
* @param era the ERA field of tested calendar on the given Julian
* day
* @param year the YEAR field of tested calendar on the given
* Julian day
* @param month the MONTH (1-based) field of tested calendar on
* the given Julian day
* @param isLeapMonth if true, treat month as a leap month
* @param dayOfMonth the DAY_OF_MONTH field of tested calendar on the
* given Julian day
* @param dayOfWeek the DAY_OF_WEEK field of tested calendar on given
* Julian day
*/
public ChineseTestCase(double julian,
int era, int year, int month,
boolean isLeapMonth, int dayOfMonth, int dayOfWeek) {
setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(ChineseCalendar.IS_LEAP_MONTH, isLeapMonth?1:0);
set(Calendar.DAY_OF_MONTH, dayOfMonth);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
}
/**
* Return a String representation of this test case's time.
*/
public String toString() {
return dowToString(get(Calendar.DAY_OF_WEEK)) +
get(Calendar.YEAR) + "of" + get(Calendar.ERA) +
"/" + (get(Calendar.MONTH)+1) +
(get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") + "/" +
get(Calendar.DAY_OF_MONTH);
}
}

View File

@ -17,6 +17,15 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
new CompatibilityTest().run(argv);
}
static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
/**
* Test the behavior of the GregorianCalendar around the changeover.
*/
@ -64,11 +73,62 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
* (first day of week, minimal days in first week).
*/
public void TestMapping() {
if (false) {
Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
Date PURE_JULIAN = GregorianCalendar.PURE_JULIAN; // new Date(Long.MAX_VALUE);
GregorianCalendar cal =
new GregorianCalendar(TimeZone.getTimeZone("UTC"));
final int EPOCH_JULIAN = 2440588;
final long ONE_DAY = 24*60*60*1000L;
com.ibm.text.SimpleDateFormat fmt =
new com.ibm.text.SimpleDateFormat("EEE MMM dd yyyy G");
/*HH:mm:ss.SSS z*/
for (int type=0; type<2; ++type) {
System.out.println(type==0 ? "Gregorian" : "Julian");
cal.setGregorianChange(type==0 ? PURE_GREGORIAN : PURE_JULIAN);
fmt.setCalendar(cal);
int[] J = {
0x7FFFFFFF,
0x7FFFFFF0,
0x7F000000,
0x78000000,
0x70000000,
0x60000000,
0x50000000,
0x40000000,
0x30000000,
0x20000000,
0x10000000,
};
for (int i=0; i<J.length; ++i) {
String[] lim = new String[2];
long[] ms = new long[2];
int jd = J[i];
for (int sign=0; sign<2; ++sign) {
int julian = jd;
if (sign==0) julian = -julian;
long millis = ((long)julian - EPOCH_JULIAN) * ONE_DAY;
ms[sign] = millis;
cal.setTime(new Date(millis));
lim[sign] = fmt.format(cal.getTime());
}
System.out.println("JD +/-" +
Long.toString(jd, 16) +
": " + ms[0] + ".." + ms[1] +
": " + lim[0] + ".." + lim[1]);
}
}
}
TimeZone saveZone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
Date PURE_JULIAN = new Date(Long.MAX_VALUE);
//NEWCAL
//Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
//Date PURE_JULIAN = new Date(Long.MAX_VALUE);
Date PURE_GREGORIAN = Calendar.MIN_DATE;
Date PURE_JULIAN = Calendar.MAX_DATE;
GregorianCalendar cal = new GregorianCalendar();
final int EPOCH_JULIAN = 2440588;
final long ONE_DAY = 24*60*60*1000L;
@ -227,7 +287,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
if (cal.getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
}
for (i=0; i<Calendar.FIELD_COUNT; ++i) {
for (i=0; i<cal.getFieldCount(); ++i) {
if (cal.getMinimum(i) != cal.getGreatestMinimum(i))
errln("FAIL: getMinimum doesn't match getGreatestMinimum for field " + i);
if (cal.getLeastMaximum(i) > cal.getMaximum(i))
@ -259,14 +319,17 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
}
cal.getTime();
for (i=0; i<Calendar.FIELD_COUNT; ++i) {
// This test is strange -- why do we expect certain fields to be set, and
// others not to be? Revisit the appropriateness of this. - Alan NEWCAL
for (i=0; i<cal.getFieldCount(); ++i) {
switch(i) {
case Calendar.YEAR: case Calendar.MONTH: case Calendar.DATE:
case Calendar.HOUR_OF_DAY: case Calendar.MINUTE: case Calendar.SECOND:
if (!cal.isSet(i)) errln("FAIL: Calendar.isSet failed");
case Calendar.EXTENDED_YEAR:
if (!cal.isSet(i)) errln("FAIL: " + FIELD_NAME[i] + " is not set");
break;
default:
if (cal.isSet(i)) errln("FAIL: Calendar.isSet failed");
if (cal.isSet(i)) errln("FAIL: " + FIELD_NAME[i] + " is set");
}
cal.clear(i);
if (cal.isSet(i)) errln("FAIL: Calendar.clear/isSet failed");
@ -433,7 +496,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
catch (IllegalArgumentException ex) {
e = ex;
}
verify765("1997 zero-th Tuesday in June = ", e);
verify765("1997 zero-th Tuesday in June = ", e, c);
c.clear();
c.set(Calendar.YEAR, 1997);
@ -485,20 +548,25 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
catch (IllegalArgumentException ex) {}
}
void verify765(String msg, Calendar c, int year, int month, int day) {
if (c.get(Calendar.YEAR) == year &&
c.get(Calendar.MONTH) == month &&
c.get(Calendar.DATE) == day) {
int cy = c.get(Calendar.YEAR); // NEWCAL
int cm = c.get(Calendar.MONTH);
int cd = c.get(Calendar.DATE);
if (cy == year &&
cm == month &&
cd == day) {
logln("PASS: " + msg + c.getTime());
}
else {
errln("FAIL: " + msg + c.getTime() +
errln("FAIL: " + msg + cy + "/" + (cm+1) + "/" + cd +
"=" + c.getTime() +
"; expected " +
year + "/" + (month+1) + "/" + day);
}
}
// Called when e expected to be non-null
void verify765(String msg, IllegalArgumentException e) {
if (e == null) errln("FAIL: No IllegalArgumentException for " + msg);
void verify765(String msg, IllegalArgumentException e, Calendar c) {
if (e == null) errln("FAIL: No IllegalArgumentException for " + msg +
c.getTime());
else logln("PASS: " + msg + "IllegalArgument as expected");
}
@ -883,6 +951,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
cal.set(Calendar.YEAR, 1976);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); // Added - Liu 11/6/00
// Using week settings of SUNDAY/3 (see above)
if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)
errln("Number of weeks in 1976 should have been 53; got " +
@ -893,7 +962,6 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
Calendar cal = new GregorianCalendar(1997, Calendar.JANUARY, 31);
int[] dayValues = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 };
for (int i = 0; i < dayValues.length; i++) {
Calendar cal2 = (Calendar)cal.clone();
cal2.roll(Calendar.MONTH, i);
@ -905,19 +973,24 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
cal.set(1996, Calendar.FEBRUARY, 29);
int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
//int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
//int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
// I've revised the expected values to make more sense -- rolling
// the year should pin the DAY_OF_MONTH. - Liu 11/6/00
int[] monthValues = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] dayValues2 = { 29, 28, 28, 28, 29, 28, 28, 28, 29, 28 };
for (int i = 0; i < dayValues2.length; i++) {
Calendar cal2 = (Calendar)cal.clone();
cal2.roll(Calendar.YEAR, i);
if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues2[i] || cal2.get(Calendar.MONTH)
!= monthValues[i])
errln("Rolling the year in 2/29/1996 up by " + i + " should have yielded "
errln("Roll 2/29/1996 by " + i + " year: expected "
+ (monthValues[i] + 1) + "/" + dayValues2[i] + "/"
+ (1996 + i) + ", but actually yielded "
+ (1996 + i) + ", got "
+ (cal2.get(Calendar.MONTH) + 1) + "/" +
cal2.get(Calendar.DAY_OF_MONTH) + "/" + (1996 + i) + ".");
cal2.get(Calendar.DAY_OF_MONTH) + "/" + cal2.get(Calendar.YEAR));
}
// Test rolling hour of day

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/HebrewTest.java,v $
* $Date: 2000/03/10 03:47:45 $
* $Revision: 1.2 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.3 $
*
*****************************************************************************************
*/
@ -14,9 +14,8 @@
package com.ibm.test.calendar;
import com.ibm.test.*;
import java.util.*;
import java.text.*;
import com.ibm.util.*;
import java.util.Locale;
/**
* Tests for the <code>HebrewCalendar</code> class.
@ -55,6 +54,7 @@ public class HebrewTest extends CalendarTest {
{ 5759, SHEVAT, 2, MONTH, 1, 5759, ADAR, 2 },
{ 5759, SHEVAT, 2, MONTH, 2, 5759, NISAN, 2 },
{ 5759, SHEVAT, 2, MONTH, 12, 5759, SHEVAT, 2 },
{ 5759, AV, 1, MONTH, 12, 5759, AV, 1 }, // Alan
{ 5757, HESHVAN, 2, MONTH, 1, 5757, KISLEV, 2 }, // leap years
{ 5757, SHEVAT, 2, MONTH, 1, 5757, ADAR_1, 2 },
@ -62,6 +62,7 @@ public class HebrewTest extends CalendarTest {
{ 5757, SHEVAT, 2, MONTH, 3, 5757, NISAN, 2 },
{ 5757, SHEVAT, 2, MONTH, 12, 5757, TEVET, 2 },
{ 5757, SHEVAT, 2, MONTH, 13, 5757, SHEVAT, 2 },
{ 5757, AV, 1, MONTH, 12, 5757, TAMUZ, 1 }, // Alan
{ 5757, KISLEV, 1, DATE, 30, 5757, KISLEV, 2 }, // 29-day month
{ 5758, KISLEV, 1, DATE, 31, 5758, KISLEV, 2 }, // 30-day month
@ -198,5 +199,116 @@ public class HebrewTest extends CalendarTest {
new TestCase(2487223.5, 0, 5858, 1, 1, SAT, 0, 0, 0),
};
};
/**
* Problem reported by Armand Bendanan in which setting of the MONTH
* field in a Hebrew calendar causes the time fields to go negative.
*/
public void TestTimeFields() {
HebrewCalendar calendar = new HebrewCalendar(5761, 0, 11, 12, 28, 15);
calendar.set(Calendar.YEAR, 5717);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 23);
if (calendar.get(Calendar.HOUR_OF_DAY) != 12) {
errln("Fail: HebrewCalendar HOUR_OF_DAY = " + calendar.get(Calendar.HOUR_OF_DAY));
}
}
/**
* Test of the behavior of the month field. This requires special
* handling in the Hebrew calendar because of the pattern of leap
* years.
*/
public void TestMonthMovement() {
HebrewCalendar cal = new HebrewCalendar();
// Leap years are:
// 3 6 8 11 14 17 19 (and so on - 19-year cycle)
// We can't test complete() on some lines below because of ADAR_1 -- if
// the calendar is set to ADAR_1 on a non-leap year, the result is undefined.
int[] DATA = {
// c - test complete() or not
// m/y - before and after month/year
// delta - amount to add to month field
//c m1 y1 delta m2 y2
1, 10, 2, +24, 9, 4, // (year 2, month 10) + 24 months -> (y 4, m 9)
1, 10, 2, +60, 8, 7, // (y 2, m 10) + 60 months -> (y 7, m 8)
1, 1, 2, +12, 1, 3, // (y 2, m 1) + 12 months -> (y 3, m 1)
1, 3, 18, -24, 4, 16, // (y 18, m 3) - 24 months -> (y 16, m 4)
1, 1, 6, -24, 1, 4,
1, 4, 3, +2, 6, 3, // Leap year - no skip 4,5,6,7,8
1, 8, 3, -2, 6, 3, // Leap year - no skip
0, 4, 2, +2, 7, 2, // Skip over leap month 4,5,(6),7,8
0, 8, 2, -2, 5, 2, // Skip over leap month going backward
};
for (int i=0; i<DATA.length; ) {
boolean testComplete = DATA[i++] != 0;
int m = DATA[i++], y = DATA[i++];
int monthDelta = DATA[i++];
int m2 = DATA[i++], y2 = DATA[i++];
int mact, yact;
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
cal.add(Calendar.MONTH, monthDelta);
yact = cal.get(Calendar.YEAR); mact = cal.get(Calendar.MONTH) + 1;
if (y2 != yact || m2 != mact) {
errln("Fail: " + m + "/" + y +
" -> add(MONTH, " + monthDelta + ") -> " +
mact + "/" + yact + ", expected " +
m2 + "/" + y2);
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
logln("Start: " + m + "/" + y);
int delta = monthDelta > 0 ? 1 : -1;
for (int c=0; c!=monthDelta; c+=delta) {
cal.add(Calendar.MONTH, delta);
logln("+ " + delta + " MONTH -> " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR));
}
}
if (testComplete) {
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m + monthDelta - 1);
yact = cal.get(Calendar.YEAR); mact = cal.get(Calendar.MONTH) + 1;
if (y2 != yact || m2 != mact) {
errln("Fail: " + (m+monthDelta) + "/" + y +
" -> complete() -> " +
mact + "/" + yact + ", expected " +
m2 + "/" + y2);
}
}
}
}
/**
* Test handling of ADAR_1.
*/
/*
public void TestAdar1() {
HebrewCalendar cal = new HebrewCalendar();
cal.clear();
cal.set(Calendar.YEAR, 1903); // leap
cal.set(Calendar.MONTH, HebrewCalendar.ADAR_1);
logln("1903(leap)/ADAR_1 => " +
cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH)+1));
cal.clear();
cal.set(Calendar.YEAR, 1904); // non-leap
cal.set(Calendar.MONTH, HebrewCalendar.ADAR_1);
logln("1904(non-leap)/ADAR_1 => " +
cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH)+1));
}
*/
/**
* With no fields set, the calendar should use default values.
*/
public void TestDefaultFieldValues() {
HebrewCalendar cal = new HebrewCalendar();
cal.clear();
logln("cal.clear() -> " + cal.getTime());
}
};

View File

@ -4,8 +4,8 @@
* others. All Rights Reserved.
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java,v $
* $Date: 2000/10/17 18:32:50 $
* $Revision: 1.3 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.4 $
*******************************************************************************
*/
package com.ibm.test.calendar;
@ -19,7 +19,7 @@ import java.util.Locale;
* @test
* @summary Tests of new functionality in IBMCalendar
*/
public class IBMCalendarTest extends TestFmwk {
public class IBMCalendarTest extends CalendarTest {
public static void main(String[] args) throws Exception {
new IBMCalendarTest().run(args);
@ -126,4 +126,89 @@ public class IBMCalendarTest extends TestFmwk {
}
}
}
/**
* Run a test of a quasi-Gregorian calendar. This is a calendar
* that behaves like a Gregorian but has different year/era mappings.
* The int[] data array should have the format:
*
* { era, year, gregorianYear, month, dayOfMonth, ... }
*/
void quasiGregorianTest(Calendar cal, int[] data) {
for (int i=0; i<data.length; ) {
int era = data[i++];
int year = data[i++];
int gregorianYear = data[i++];
int month = data[i++];
int dayOfMonth = data[i++];
Date D = new Date(gregorianYear - 1900, month, dayOfMonth);
cal.clear();
cal.set(Calendar.ERA, era);
cal.set(year, month, dayOfMonth);
Date d = cal.getTime();
if (d.equals(D)) {
logln("OK: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
" => " + d);
} else {
errln("Fail: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
" => " + d + ", expected " + D);
}
cal.clear();
cal.setTime(D);
int e = cal.get(Calendar.ERA);
int y = cal.get(Calendar.YEAR);
if (y == year && e == era) {
logln("OK: " + D + " => " + cal.get(Calendar.ERA) + ":" +
cal.get(Calendar.YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE));
} else {
logln("Fail: " + D + " => " + cal.get(Calendar.ERA) + ":" +
cal.get(Calendar.YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE) +
", expected " + era + ":" + year + "/" + (month+1) + "/" +
dayOfMonth);
}
}
}
/**
* Verify that BuddhistCalendar shifts years to Buddhist Era but otherwise
* behaves like GregorianCalendar.
*/
public void TestBuddhist() {
quasiGregorianTest(new BuddhistCalendar(),
new int[] {
// BE 2542 == 1999 CE
0, 2542, 1999, Calendar.JUNE, 4
});
}
/**
* Verify that JapaneseCalendar shifts years to Buddhist Era but otherwise
* behaves like GregorianCalendar.
*/
public void TestJapanese() {
int[] data = {
JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 8,
JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 9,
JapaneseCalendar.MEIJI, 2, 1869, Calendar.JUNE, 4,
JapaneseCalendar.MEIJI, 45, 1912, Calendar.JULY, 29,
JapaneseCalendar.TAISHO, 1, 1912, Calendar.JULY, 30,
JapaneseCalendar.TAISHO, 1, 1912, Calendar.AUGUST, 1,
};
quasiGregorianTest(new JapaneseCalendar(), data);
}
/**
* Test limits of the Gregorian calendar.
*/
public void TestGregorianLimits() {
// Final parameter is either number of days, if > 0, or test
// duration in seconds, if < 0.
doLimitsTest(new GregorianCalendar(), null,
new Date(2004-1900, Calendar.JANUARY, 1), -10);
}
}

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/IslamicTest.java,v $
* $Date: 2000/03/10 03:47:45 $
* $Revision: 1.2 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.3 $
*
*****************************************************************************************
*/
@ -119,10 +119,26 @@ public class IslamicTest extends CalendarTest {
doTestCases(tests, civilCalendar);
}
public void TestBasic() {
IslamicCalendar cal = newCivil();
cal.clear();
cal.set(1000, 0, 30);
logln("1000/0/30 -> " +
cal.get(YEAR) + "/" +
cal.get(MONTH) + "/" +
cal.get(DATE));
cal.clear();
cal.set(1, 0, 30);
logln("1/0/30 -> " +
cal.get(YEAR) + "/" +
cal.get(MONTH) + "/" +
cal.get(DATE));
}
private static IslamicCalendar newCivil() {
IslamicCalendar civilCalendar = new IslamicCalendar();
civilCalendar.setCivil(true);
return civilCalendar;
}
};
};

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/calendar/TestCase.java,v $
* $Date: 2000/10/17 18:32:50 $
* $Revision: 1.5 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.6 $
*
*****************************************************************************************
*/
@ -20,7 +20,7 @@ import com.ibm.util.SimpleTimeZone;
import java.util.Locale;
/**
* A dummy <code>Calendar</code> subclass that is useful for testing
* A pseudo <code>Calendar</code> that is useful for testing
* new calendars. A <code>TestCase</code> object is used to hold the
* field and millisecond values that the calendar should have at one
* particular instant in time. The applyFields and applyTime
@ -28,15 +28,63 @@ import java.util.Locale;
* tested, and the equals and fieldsEqual methods are used to ensure
* that the calendar has ended up in the right state.
*/
public class TestCase extends Calendar {
public class TestCase {
//------------------------------------------------------------------
// Pseudo-Calendar fields and methods
//------------------------------------------------------------------
protected int[] fields = new int[32];
protected boolean[] isSet = new boolean[32];
protected long time;
protected void set(int field, int value) {
fields[field] = value;
isSet[field] = true;
}
protected int get(int field) {
return fields[field];
}
protected boolean isSet(int field) {
return isSet[field];
}
protected void setTime(Date d) {
time = d.getTime();
}
public Date getTime() {
return new Date(time);
}
/**
* Return a String representation of this test case's time.
*/
public String toString() {
return dowToString(get(Calendar.DAY_OF_WEEK)) +
get(Calendar.YEAR) + "/" + (get(Calendar.MONTH)+1) + "/" +
get(Calendar.DATE);
}
private static final String[] DOW_NAMES = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
public static String dowToString(int dow) {
--dow;
return (dow < 0 || dow > 6) ?
("<DOW " + dow + ">") : DOW_NAMES[dow];
}
/**
* Initialize a TestCase object using a julian day number and
* the corresponding fields for the calendar being tested.
*
* @param era The ERA field of tested calendar on the given julian day
* @param year The YEAR field of tested calendar on the given julian day
* @param month The MONTH (0-based) field of tested calendar on the given julian day
* @param month The MONTH (1-based) field of tested calendar on the given julian day
* @param day The DAY_OF_MONTH field of tested calendar on the given julian day
* @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given julian day
* @param hour The HOUR field of tested calendar on the given julian day
@ -44,22 +92,20 @@ public class TestCase extends Calendar {
* @param sec The SECOND field of tested calendar on the given julian day
*/
public TestCase(double julian,
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
{
super(UTC, Locale.getDefault());
setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));
setTime(new Date(JULIAN_EPOCH + (long)(DAY_MS * julian)));
set(ERA, era);
set(YEAR, year);
set(MONTH, month - 1);
set(DATE, day);
set(DAY_OF_WEEK, dayOfWeek);
set(HOUR, hour);
set(MINUTE, min);
set(SECOND, sec);
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(Calendar.DATE, day);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
set(Calendar.HOUR, hour);
set(Calendar.MINUTE, min);
set(Calendar.SECOND, sec);
}
/**
@ -80,27 +126,30 @@ public class TestCase extends Calendar {
* @param sec The SECOND field of tested calendar on the given gregorian date
*/
public TestCase(int gregYear, int gregMonth, int gregDay,
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
{
super(UTC, Locale.getDefault());
GregorianCalendar greg = new GregorianCalendar(UTC, Locale.getDefault());
greg.clear();
greg.set(gregYear, gregMonth-1, gregDay);
setTime(greg.getTime());
set(ERA, era);
set(YEAR, year);
set(MONTH, month - 1);
set(DATE, day);
set(DAY_OF_WEEK, dayOfWeek);
set(HOUR, hour);
set(MINUTE, min);
set(SECOND, sec);
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(Calendar.DATE, day);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
set(Calendar.HOUR, hour);
set(Calendar.MINUTE, min);
set(Calendar.SECOND, sec);
}
/**
* For subclasses.
*/
protected TestCase() {}
/**
* Apply this test case's field values to another calendar
* by calling its set method for each field. This is useful in combination
@ -109,13 +158,11 @@ public class TestCase extends Calendar {
* @see #equal
*/
public void applyFields(Calendar c) {
c.set(ERA, fields[ERA]);
c.set(YEAR, fields[YEAR]);
c.set(MONTH, fields[MONTH]);
c.set(DATE, fields[DATE]);
c.set(HOUR, fields[HOUR]);
c.set(MINUTE, fields[MINUTE]);
c.set(SECOND, fields[SECOND]);
for (int i=0; i < c.getFieldCount(); i++) {
if (isSet(i)) {
c.set(i, get(i));
}
}
}
/**
@ -145,10 +192,22 @@ public class TestCase extends Calendar {
*
* @see #applyTime
*/
public boolean fieldsEqual(Calendar c) {
for (int i=0; i < Calendar.FIELD_COUNT; i++) {
public boolean fieldsEqual(Calendar c, TestLog log) {
for (int i=0; i < c.getFieldCount(); i++) {
if (isSet(i) && get(i) != c.get(i)) {
System.out.println("field " + i + ": expected " + get(i) + ", got " + c.get(i));
log.errln("Fail: " + CalendarTest.fieldName(i) + " = " + c.get(i) +
", expected " + get(i));
for (int j=0; j<c.getFieldCount(); ++j) {
if (isSet(j)) {
if (get(j) == c.get(j)) {
log.errln(" OK: " + CalendarTest.fieldName(j) + " = " +
c.get(j));
} else {
log.errln(" Fail: " + CalendarTest.fieldName(j) + " = " +
c.get(j) + ", expected " + get(j));
}
}
}
return false;
}
}
@ -176,38 +235,11 @@ public class TestCase extends Calendar {
return time == ((Calendar)obj).getTime().getTime();
}
/**
* Determine whether time of this calendar (as returned by getTime)
* is before that of the specified calendar
*/
public boolean before(Object obj) {
return time < ((Calendar)obj).getTime().getTime();
}
/**
* Determine whether time of this calendar (as returned by getTime)
* is after that of the specified calendar
*/
public boolean after(Object obj) {
return time > ((Calendar)obj).getTime().getTime();
}
// This object is only pretending to be a Calendar; it doesn't do any real
// calendar computatations. But we have to pretend it does, because Calendar
// declares all of these abstract methods....
protected void computeTime() {}
protected void computeFields() {}
public void add(int field, int amt) {}
public int getMinimum(int field) { return 0; }
public int getMaximum(int field) { return 0; }
public int getGreatestMinimum(int field) { return 0; }
public int getLeastMaximum(int field) { return 0; }
private static final int SECOND_MS = 1000;
private static final int MINUTE_MS = 60*SECOND_MS;
private static final int HOUR_MS = 60*MINUTE_MS;
private static final long DAY_MS = 24*HOUR_MS;
private static final long JULIAN_EPOCH = -210866760000000L; // 1/1/4713 BC 12:00
protected static final int ONE_SECOND = 1000;
protected static final int ONE_MINUTE = 60*ONE_SECOND;
protected static final int ONE_HOUR = 60*ONE_MINUTE;
protected static final long ONE_DAY = 24*ONE_HOUR;
protected static final long JULIAN_EPOCH = -210866760000000L; // 1/1/4713 BC 12:00
public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");
}

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/Attic/TestAll.java,v $
* $Date: 2000/10/06 21:42:38 $
* $Revision: 1.4 $
* $Date: 2000/11/18 00:17:43 $
* $Revision: 1.5 $
*
*****************************************************************************************
*/
@ -86,7 +86,8 @@ public class TestAll extends TestFmwk {
new com.ibm.test.calendar.CompatibilityTest(),
new com.ibm.test.calendar.HebrewTest(),
new com.ibm.test.calendar.IBMCalendarTest(),
new com.ibm.test.calendar.IslamicTest()
new com.ibm.test.calendar.IslamicTest(),
new com.ibm.test.calendar.ChineseTest()
});
}

View File

@ -18,6 +18,15 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
new CalendarRegression().run(args);
}
static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
/*
Synopsis: java.sql.Timestamp constructor works wrong on Windows 95
@ -48,12 +57,18 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear();
cal.set(1900, 15, 5, 5, 8, 13);
if (cal.get(Calendar.HOUR) != 5) {
logln(zone.getID() + " " +
//zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
logln("Fail: " + zone.getID() + " " +
zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
bad = true;
} else if (false) { // Change to true to debug
logln("OK: " + zone.getID() + " " +
zone.useDaylightTime() + " " +
cal.get(Calendar.DST_OFFSET) / (60*60*1000) + " " +
zone.getRawOffset() / (60*60*1000) +
": HOUR = " + cal.get(Calendar.HOUR));
}
}
if (bad) errln("TimeZone problems with GC");
@ -650,6 +665,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
Date d01 = new Date(97, Calendar.MARCH, 15, 12, 00, 56);
Date d10 = new Date(97, Calendar.MARCH, 15, 12, 34, 00);
Date d11 = new Date(97, Calendar.MARCH, 15, 12, 34, 56);
Date dM = new Date(97, Calendar.JANUARY, 15, 12, 34, 56);
Date epoch = new Date(70, Calendar.JANUARY, 1);
Calendar cal = Calendar.getInstance();
@ -657,8 +673,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear( Calendar.MINUTE );
logln(cal.getTime().toString());
if (!cal.getTime().equals(d01))
errln("Fail: clear(MINUTE) broken");
if (!cal.getTime().equals(d01)) {
errln("Fail: " + d11 + " clear(MINUTE) => expect " +
d01 + ", got " + cal.getTime());
}
cal.set( Calendar.SECOND, 0 );
logln(cal.getTime().toString());
@ -679,7 +697,15 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.clear();
logln(cal.getTime().toString());
if (!cal.getTime().equals(epoch))
errln("Fail: clear() broken Want " + epoch);
errln("Fail: after clear() expect " + epoch + ", got " + cal.getTime());
cal.setTime(d11);
cal.clear( Calendar.MONTH );
logln(cal.getTime().toString());
if (!cal.getTime().equals(dM)) {
errln("Fail: " + d11 + " clear(MONTH) => expect " +
dM + ", got " + cal.getTime());
}
}
public void Test4114578() {
@ -891,17 +917,20 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
* other of these will wrap. We've modified the test given in the bug
* report to therefore only check the behavior of a calendar with a zero raw
* offset zone.
*
* NEWCAL We no longer support dates before Calendar.MIN_DATE or after
* Calendar.MAX_DATE. Modify this test.
*/
public void Test4145158() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar.setTime(new Date(Long.MIN_VALUE));
calendar.setTime(Calendar.MIN_DATE);
int year1 = calendar.get(Calendar.YEAR);
int era1 = calendar.get(Calendar.ERA);
calendar.setTime(new Date(Long.MAX_VALUE));
calendar.setTime(Calendar.MAX_DATE);
int year2 = calendar.get(Calendar.YEAR);
int era2 = calendar.get(Calendar.ERA);
@ -916,7 +945,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
public void Test4145983() {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
Date[] DATES = { new Date(Long.MAX_VALUE), new Date(Long.MIN_VALUE) };
Date[] DATES = { Calendar.MAX_DATE, Calendar.MIN_DATE };
for (int i=0; i<DATES.length; ++i) {
calendar.setTime(DATES[i]);
int year = calendar.get(Calendar.YEAR);
@ -935,29 +964,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
* field. - liu 6/29/98
*/
public void Test4147269() {
final String[] fieldName = {
"ERA",
"YEAR",
"MONTH",
"WEEK_OF_YEAR",
"WEEK_OF_MONTH",
"DAY_OF_MONTH",
"DAY_OF_YEAR",
"DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH",
"AM_PM",
"HOUR",
"HOUR_OF_DAY",
"MINUTE",
"SECOND",
"MILLISECOND",
"ZONE_OFFSET",
"DST_OFFSET"
};
GregorianCalendar calendar = new GregorianCalendar();
calendar.setLenient(false);
Date date = new Date(1996-1900, Calendar.JANUARY, 3); // Arbitrary date
for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
for (int field = 0; field < calendar.getFieldCount(); field++) {
calendar.setTime(date);
// Note: In the bug report, getActualMaximum() was called instead
// of getMaximum() -- this was an error. The validation code doesn't
@ -969,7 +979,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
calendar.getTime(); // Force time computation
// We expect an exception to be thrown. If we fall through
// to the next line, then we have a bug.
errln("Test failed with field " + fieldName[field] +
errln("Test failed with field " + FIELD_NAME[field] +
", date before: " + date +
", date after: " + calendar.getTime() +
", value: " + value + " (max = " + max +")");
@ -990,22 +1000,22 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
GregorianCalendar calendar = new GregorianCalendar(zones[i]);
// Make sure extreme values don't wrap around
calendar.setTime(new Date(Long.MIN_VALUE));
calendar.setTime(Calendar.MIN_DATE);
if (calendar.get(Calendar.ERA) != GregorianCalendar.BC) {
errln("Fail: Date(Long.MIN_VALUE) has an AD year");
errln("Fail: Calendar.MIN_DATE has an AD year");
}
calendar.setTime(new Date(Long.MAX_VALUE));
calendar.setTime(Calendar.MAX_DATE);
if (calendar.get(Calendar.ERA) != GregorianCalendar.AD) {
errln("Fail: Date(Long.MAX_VALUE) has a BC year");
errln("Fail: Calendar.MAX_DATE has a BC year");
}
calendar.setGregorianChange(new Date(Long.MAX_VALUE));
calendar.setGregorianChange(Calendar.MAX_DATE);
// to obtain a pure Julian calendar
boolean is100Leap = calendar.isLeapYear(100);
if (!is100Leap) {
errln("test failed with zone " + zones[i].getID());
errln(" cutover date is Date(Long.MAX_VALUE)");
errln(" cutover date is Calendar.MAX_DATE");
errln(" isLeapYear(100) returns: " + is100Leap);
}
}
@ -1134,9 +1144,9 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
for (int j=0; j<calendars.length; ++j) {
GregorianCalendar calendar = calendars[j];
if (k == 1) {
calendar.setGregorianChange(new Date(Long.MIN_VALUE));
calendar.setGregorianChange(Calendar.MIN_DATE);
} else if (k == 2) {
calendar.setGregorianChange(new Date(Long.MAX_VALUE));
calendar.setGregorianChange(Calendar.MAX_DATE);
}
format.setCalendar((Calendar)calendar.clone());
@ -1161,7 +1171,10 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
if (valid && newYear != years[i]) {
errln(" FAIL: " + newYear + " should be valid; date, month and time shouldn't change");
} else if (!valid && newYear == years[i]) {
errln(" FAIL: " + newYear + " should be invalid");
// We no longer require strict year maxima. That is, the calendar
// algorithm may work for values > the stated maximum.
//errln(" FAIL: " + newYear + " should be invalid");
logln(" Note: " + newYear + " > maximum, but still valid");
}
}
}
@ -1177,12 +1190,6 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
{ 1997, Calendar.FEBRUARY, 1, 10, 45, 15, 900 },
{ 1999, Calendar.DECEMBER, 22, 23, 59, 59, 999 }
};
String[] fieldNames = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH", "DATE",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK", "DAY_OF_WEEK_IN_MONTH",
"AM_PM", "HOUR", "HOUR_OF_DAY", "MINUTE", "SECOND", "MILLISECOND",
"ZONE_OFFSET", "DST_OFFSET"
};
int limit = 40;
GregorianCalendar cal = new GregorianCalendar();
@ -1192,11 +1199,11 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
if (cal.getTime().getTime() != 0) {
errln("Hour rolling broken");
}
for (int op=0; op<2; ++op) {
logln("Testing GregorianCalendar " +
(op==0 ? "add" : "roll"));
for (int field=0; field < Calendar.FIELD_COUNT; ++field) {
for (int field=0; field < cal.getFieldCount(); ++field) {
if (field != Calendar.ZONE_OFFSET &&
field != Calendar.DST_OFFSET) {
for (int j=0; j<fieldsList.length; ++j) {
@ -1220,7 +1227,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.get(Calendar.SECOND) != fields[5] ||
cal.get(Calendar.MILLISECOND) != fields[6]) {
errln("Field " + field +
" (" + fieldNames[field] +
" (" + FIELD_NAME[field] +
") FAIL, expected " +
fields[0] +
"/" + (fields[1] + 1) +
@ -1240,7 +1247,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
cal.set(fields[0], fields[1], fields[2],
fields[3], fields[4], fields[5]);
cal.set(Calendar.MILLISECOND, fields[6]);
errln(cal.get(Calendar.YEAR) +
logln("Start date: " + cal.get(Calendar.YEAR) +
"/" + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) +
" " + cal.get(Calendar.HOUR_OF_DAY) +
@ -1257,8 +1264,8 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
long t = cal.getTime().getTime();
long delta = t - prev;
prev = t;
errln((op == 0 ? "add" : "roll") +
(i < limit ? " +> " : " -> ") +
logln((op == 0 ? "add(" : "roll(") + FIELD_NAME[field] +
(i < limit ? ", +1) => " : ", -1) => ") +
cal.get(Calendar.YEAR) +
"/" + (cal.get(Calendar.MONTH) + 1) +
"/" + cal.get(Calendar.DATE) +
@ -1266,7 +1273,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
":" + cal.get(Calendar.MINUTE) +
":" + cal.get(Calendar.SECOND) +
"." + cal.get(Calendar.MILLISECOND) +
" d=" + delta);
" delta=" + delta + " ms");
}
}
}
@ -1274,7 +1281,7 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
}
}
}
public void Test4174361() {
GregorianCalendar calendar = new GregorianCalendar(1996, 1, 29);
@ -1577,6 +1584,77 @@ public class CalendarRegression extends com.ibm.test.TestFmwk {
}
}
}
/**
* Set behavior of DST_OFFSET field. ICU4J Jitterbug 9.
*/
public void TestJ9() {
int HOURS = 60*60*1000;
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("PST"),
Locale.US);
final int END_FIELDS = 0x1234;
int[] DATA = {
// With no explicit ZONE/DST expect 12:00 am
Calendar.MONTH, Calendar.JUNE,
END_FIELDS,
0, 0, // expected hour, min
// Normal ZONE/DST for June 1 Pacific is 8:00/1:00
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
Calendar.DST_OFFSET, HOURS,
END_FIELDS,
0, 0, // expected hour, min
// With ZONE/DST of 8:00/0:30 expect time of 12:30 am
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
Calendar.DST_OFFSET, HOURS/2,
END_FIELDS,
0, 30, // expected hour, min
// With ZONE/DST of 8:00/UNSET expect time of 1:00 am
Calendar.MONTH, Calendar.JUNE,
Calendar.ZONE_OFFSET, -8*HOURS,
END_FIELDS,
1, 0, // expected hour, min
// With ZONE/DST of UNSET/0:30 expect 4:30 pm (day before)
Calendar.MONTH, Calendar.JUNE,
Calendar.DST_OFFSET, HOURS/2,
END_FIELDS,
16, 30, // expected hour, min
};
for (int i=0; i<DATA.length; ) {
int start = i;
cal.clear();
// Set fields
while (DATA[i] != END_FIELDS) {
cal.set(DATA[i++], DATA[i++]);
}
++i; // skip over END_FIELDS
// Get hour/minute
int h = cal.get(Calendar.HOUR_OF_DAY);
int m = cal.get(Calendar.MINUTE);
// Check
if (h != DATA[i] || m != DATA[i+1]) {
errln("Fail: expected " + DATA[i] + ":" + DATA[i+1] +
", got " + h + ":" + m + " after:");
while (DATA[start] != END_FIELDS) {
logln("set(" + FIELD_NAME[DATA[start++]] +
", " + DATA[start++] + ");");
}
}
i += 2; // skip over expected hour, min
}
}
}
//eof

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/CalendarTest.java,v $
* $Date: 2000/10/17 20:55:09 $
* $Revision: 1.6 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.7 $
*
*****************************************************************************************
*/
@ -18,6 +18,8 @@ import com.ibm.text.DateFormat;
import com.ibm.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Hashtable;
import java.util.Enumeration;
import com.ibm.util.*;
/**
@ -50,8 +52,20 @@ public class CalendarTest extends TestFmwk {
public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");
final String pattern = "E, MM/dd/yyyy G HH:mm:ss.S z";
private static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
public static final String fieldName(int f) {
return (f>=0 && f<FIELD_NAME.length) ?
FIELD_NAME[f] : ("<Field " + f + ">");
}
/**
* Iterates through a list of calendar <code>TestCase</code> objects and
* makes sure that the time-to-fields and fields-to-time calculations work
@ -64,18 +78,25 @@ public class CalendarTest extends TestFmwk {
// Get a format to use for printing dates in the calendar system we're testing
DateFormat format = DateFormat.getDateTimeInstance(cal, DateFormat.SHORT, -1, Locale.getDefault());
// TODO Fix this to include the ERA ("G") again once ChineseDateFormat
// is implemented - Liu
final String pattern = "E, MM/dd/yyyy HH:mm:ss.S z";
((SimpleDateFormat)format).applyPattern(pattern);
DateFormat testFmt = (DateFormat)format.clone();
// This format is used for pringing Gregorian dates. This one is easier
// This format is used for printing Gregorian dates.
DateFormat gregFormat = new SimpleDateFormat(pattern);
gregFormat.setTimeZone(UTC);
GregorianCalendar pureGreg = new GregorianCalendar(UTC);
pureGreg.setGregorianChange(Calendar.MIN_DATE);
DateFormat pureGregFmt = new SimpleDateFormat("E M/d/yyyy G");
pureGregFmt.setCalendar(pureGreg);
// Now iterate through the test cases and see what happens
for (int i = 0; i < cases.length; i++)
{
TestCase test = cases[i];
testFmt.setCalendar(test);
//
// First we want to make sure that the millis -> fields calculation works
@ -84,27 +105,24 @@ public class CalendarTest extends TestFmwk {
// that they're the same as the ones in the testcase
//
test.applyTime(cal);
if (!test.fieldsEqual(cal)) {
errln("ERROR: millis --> fields calculation incorrect for "
+ gregFormat.format(test.getTime()));
logln(" expected " + testFmt.format(test.getTime()));
logln(" got " + format.format(cal.getTime()) );
if (!test.fieldsEqual(cal, this)) {
errln("Fail: (millis=>fields) " +
gregFormat.format(test.getTime()) + " => " +
format.format(cal.getTime()) +
", expected " + test);
}
else {
//
// If that was OK, check the fields -> millis calculation
// test.applyFields will set all of the calendar's fields to
// match those in the test case.
//
cal.setTime(new Date(0));
test.applyFields(cal);
if (!test.equals(cal)) {
errln("ERROR: fields --> millis calculation incorrect for "
+ testFmt.format(test.getTime()));
logln(" expected " + test.getTime().getTime());
logln(" got " + cal.getTime().getTime() );
}
//
// If that was OK, check the fields -> millis calculation
// test.applyFields will set all of the calendar's fields to
// match those in the test case.
//
cal.clear();
test.applyFields(cal);
if (!test.equals(cal)) {
errln("Fail: (fields=>millis) " + test + " => " +
pureGregFmt.format(cal.getTime()) +
", expected " + pureGregFmt.format(test.getTime()));
}
}
}
@ -144,7 +162,6 @@ public class CalendarTest extends TestFmwk {
cal.clear();
cal.set(test[0], test[1], test[2]);
if (roll) {
cal.roll(test[3], test[4]);
} else {
@ -155,13 +172,158 @@ public class CalendarTest extends TestFmwk {
|| cal.get(DATE) != test[7])
{
errln("Error " + name + " "+ ymdToString(test[0], test[1], test[2])
+ " field " + test[3] + " by " + test[4]
+ " " + FIELD_NAME[test[3]] + " by " + test[4]
+ ": expected " + ymdToString(test[5], test[6], test[7])
+ ", got " + ymdToString(cal.get(YEAR), cal.get(MONTH), cal.get(DATE)));
+ ", got " + ymdToString(cal));
}
}
}
/**
* Test the functions getXxxMinimum() and getXxxMaximum() by marching a
* test calendar 'cal' through 'numberOfDays' sequential days starting
* with 'startDate'. For each date, read a field value along with its
* reported actual minimum and actual maximum. These values are
* checked against one another as well as against getMinimum(),
* getGreatestMinimum(), getLeastMaximum(), and getMaximum(). We
* expect to see:
*
* 1. minimum <= actualMinimum <= greatestMinimum <=
* leastMaximum <= actualMaximum <= maximum
*
* 2. actualMinimum <= value <= actualMaximum
*
* Note: In addition to outright failures, this test reports some
* results as warnings. These are not generally of concern, but they
* should be evaluated by a human. To see these, run this test in
* verbose mode.
* @param cal the calendar to be tested
* @param fieldsToTest an array of field values to be tested, e.g., new
* int[] { Calendar.MONTH, Calendar.DAY_OF_MONTH }. It only makes
* sense to test the day fields; the time fields are not tested by this
* method. If null, then test all standard fields.
* @param startDate the first date to test
* @param testDuration if positive, the number of days to be tested.
* If negative, the number of seconds to run the test.
*/
protected void doLimitsTest(Calendar cal, int[] fieldsToTest,
Date startDate, int testDuration) {
GregorianCalendar greg = new GregorianCalendar();
greg.setTime(startDate);
logln("Start: " + startDate);
if (fieldsToTest == null) {
fieldsToTest = new int[] {
Calendar.ERA, Calendar.YEAR, Calendar.MONTH,
Calendar.WEEK_OF_YEAR, Calendar.WEEK_OF_MONTH,
Calendar.DAY_OF_MONTH, Calendar.DAY_OF_YEAR,
Calendar.DAY_OF_WEEK_IN_MONTH, Calendar.YEAR_WOY,
Calendar.EXTENDED_YEAR
};
}
// Keep a record of minima and maxima that we actually see.
// These are kept in an array of arrays of hashes.
Hashtable[][] limits = new Hashtable[fieldsToTest.length][2];
Object nub = new Object(); // Meaningless placeholder
// This test can run for a long time; show progress.
long millis = System.currentTimeMillis();
long mark = millis + 5000; // 5 sec
millis -= testDuration * 1000; // stop time if testDuration<0
for (int i=0;
testDuration>0 ? i<testDuration
: System.currentTimeMillis()<millis;
++i) {
if (System.currentTimeMillis() >= mark) {
logln("(" + i + " days)");
mark += 5000; // 5 sec
}
cal.setTimeInMillis(greg.getTimeInMillis());
for (int j=0; j<fieldsToTest.length; ++j) {
int f = fieldsToTest[j];
int v = cal.get(f);
int minActual = cal.getActualMinimum(f);
int maxActual = cal.getActualMaximum(f);
int minLow = cal.getMinimum(f);
int minHigh = cal.getGreatestMinimum(f);
int maxLow = cal.getLeastMaximum(f);
int maxHigh = cal.getMaximum(f);
// Fetch the hash for this field and keep track of the
// minima and maxima.
Hashtable[] h = limits[j];
if (h[0] == null) {
h[0] = new Hashtable();
h[1] = new Hashtable();
}
h[0].put(new Integer(minActual), nub);
h[1].put(new Integer(maxActual), nub);
if (minActual < minLow || minActual > minHigh) {
errln("Fail: " + ymdToString(cal) +
" Range for min of " + FIELD_NAME[f] +
"=" + minLow + ".." + minHigh +
", actual_min=" + minActual);
}
if (maxActual < maxLow || maxActual > maxHigh) {
errln("Fail: " + ymdToString(cal) +
" Range for max of " + FIELD_NAME[f] +
"=" + maxLow + ".." + maxHigh +
", actual_max=" + maxActual);
}
if (v < minActual || v > maxActual) {
errln("Fail: " + ymdToString(cal) +
" " + FIELD_NAME[f] + "=" + v +
", actual range=" + minActual + ".." + maxActual +
", allowed=(" + minLow + ".." + minHigh + ")..(" +
maxLow + ".." + maxHigh + ")");
}
}
greg.add(Calendar.DAY_OF_YEAR, 1);
}
// Check actual maxima and minima seen against ranges returned
// by API.
StringBuffer buf = new StringBuffer();
for (int j=0; j<fieldsToTest.length; ++j) {
int f = fieldsToTest[j];
buf.setLength(0);
buf.append(FIELD_NAME[f]);
Hashtable[] h = limits[j];
boolean fullRangeSeen = true;
for (int k=0; k<2; ++k) {
int rangeLow = (k==0) ?
cal.getMinimum(f) : cal.getLeastMaximum(f);
int rangeHigh = (k==0) ?
cal.getGreatestMinimum(f) : cal.getMaximum(f);
// If either the top of the range or the bottom was never
// seen, then there may be a problem.
if (h[k].get(new Integer(rangeLow)) == null ||
h[k].get(new Integer(rangeHigh)) == null) {
fullRangeSeen = false;
}
buf.append(k==0 ? " minima seen=(" : "; maxima seen=(");
for (Enumeration e=h[k].keys(); e.hasMoreElements(); ) {
int v = ((Integer) e.nextElement()).intValue();
buf.append(" " + v);
}
buf.append(") range=" + rangeLow + ".." + rangeHigh);
}
if (fullRangeSeen) {
logln("OK: " + buf.toString());
} else {
// This may or may not be an error -- if the range of dates
// we scan over doesn't happen to contain a minimum or
// maximum, it doesn't mean some other range won't.
logln("Warning: " + buf.toString());
}
}
logln("End: " + greg.getTime());
}
/**
* Convert year,month,day values to the form "year/month/day".
* On input the month value is zero-based, but in the result string it is one-based.
@ -169,4 +331,18 @@ public class CalendarTest extends TestFmwk {
static public String ymdToString(int year, int month, int day) {
return "" + year + "/" + (month+1) + "/" + day;
}
};
/**
* Convert year,month,day values to the form "year/month/day".
*/
static public String ymdToString(Calendar cal) {
if (cal instanceof ChineseCalendar) {
return "" + cal.get(Calendar.EXTENDED_YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) +
(cal.get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") + "/" +
cal.get(Calendar.DATE);
}
return ymdToString(cal.get(Calendar.EXTENDED_YEAR),
cal.get(MONTH), cal.get(DATE));
}
}

View File

@ -0,0 +1,192 @@
/*********************************************************************
* Copyright (C) 2000, International Business Machines Corporation and
* others. All Rights Reserved.
*********************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/ChineseTest.java,v $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.1 $
*/
package com.ibm.test.calendar;
import com.ibm.util.*;
import java.util.Date;
import java.util.Locale;
public class ChineseTest extends CalendarTest {
public static void main(String args[]) throws Exception {
new ChineseTest().run(args);
}
/**
* Test basic mapping to and from Gregorian.
*/
public void TestMapping() {
final int[] DATA = {
// (Note: months are 1-based)
// Gregorian Chinese
1964, 9, 4, 4601, 7,0, 28,
1964, 9, 5, 4601, 7,0, 29,
1964, 9, 6, 4601, 8,0, 1,
1964, 9, 7, 4601, 8,0, 2,
1961, 12, 25, 4598, 11,0, 18,
1999, 6, 4, 4636, 4,0, 21,
1990, 5, 23, 4627, 4,0, 29,
1990, 5, 24, 4627, 5,0, 1,
1990, 6, 22, 4627, 5,0, 30,
1990, 6, 23, 4627, 5,1, 1,
1990, 7, 20, 4627, 5,1, 28,
1990, 7, 21, 4627, 5,1, 29,
1990, 7, 22, 4627, 6,0, 1,
};
ChineseCalendar cal = new ChineseCalendar();
StringBuffer buf = new StringBuffer();
logln("Gregorian -> Chinese");
for (int i=0; i<DATA.length; ) {
Date date = new Date(DATA[i++]-1900, DATA[i++]-1, DATA[i++]);
cal.setTime(date);
int y = cal.get(Calendar.EXTENDED_YEAR);
int m = cal.get(Calendar.MONTH)+1; // 0-based -> 1-based
int L = cal.get(ChineseCalendar.IS_LEAP_MONTH);
int d = cal.get(Calendar.DAY_OF_MONTH);
int yE = DATA[i++]; // Expected y, m, isLeapMonth, d
int mE = DATA[i++]; // 1-based
int LE = DATA[i++];
int dE = DATA[i++];
buf.setLength(0);
buf.append(date + " -> ");
buf.append(y + "/" + m + (L==1?"(leap)":"") + "/" + d);
if (y == yE && m == mE && L == LE && d == dE) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " +
yE + "/" + mE + (LE==1?"(leap)":"") + "/" + dE);
}
}
logln("Chinese -> Gregorian");
for (int i=0; i<DATA.length; ) {
Date dexp = new Date(DATA[i++]-1900, DATA[i++]-1, DATA[i++]);
int cyear = DATA[i++];
int cmonth = DATA[i++];
int cisleapmonth = DATA[i++];
int cdayofmonth = DATA[i++];
cal.clear();
cal.set(Calendar.EXTENDED_YEAR, cyear);
cal.set(Calendar.MONTH, cmonth-1);
cal.set(ChineseCalendar.IS_LEAP_MONTH, cisleapmonth);
cal.set(Calendar.DAY_OF_MONTH, cdayofmonth);
Date date = cal.getTime();
buf.setLength(0);
buf.append(cyear + "/" + cmonth +
(cisleapmonth==1?"(leap)":"") + "/" + cdayofmonth);
buf.append(" -> " + date);
if (date.equals(dexp)) {
logln("OK: " + buf.toString());
} else {
errln("Fail: " + buf.toString() + ", expected " + dexp);
}
}
}
/**
* Make sure no Gregorian dates map to Chinese 1-based day of
* month zero. This was a problem with some of the astronomical
* new moon determinations.
*/
public void TestZeroDOM() {
ChineseCalendar cal = new ChineseCalendar();
GregorianCalendar greg = new GregorianCalendar(1989, Calendar.SEPTEMBER, 1);
logln("Start: " + greg.getTime());
for (int i=0; i<1000; ++i) {
cal.setTimeInMillis(greg.getTimeInMillis());
if (cal.get(Calendar.DAY_OF_MONTH) == 0) {
errln("Fail: " + greg.getTime() + " -> " +
cal.get(Calendar.EXTENDED_YEAR) + "/" +
cal.get(Calendar.MONTH) +
(cal.get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") +
"/" + cal.get(Calendar.DAY_OF_MONTH));
}
greg.add(Calendar.DAY_OF_YEAR, 1);
}
logln("End: " + greg.getTime());
}
/**
* Test minimum and maximum functions.
*/
public void TestLimits() {
// The number of days and the start date can be adjusted
// arbitrarily to either speed up the test or make it more
// thorough, but try to test at least a full year, preferably a
// full non-leap and a full leap year.
// Final parameter is either number of days, if > 0, or test
// duration in seconds, if < 0.
doLimitsTest(new ChineseCalendar(), null,
new Date(1989-1900, Calendar.NOVEMBER, 1), -10);
}
/**
* Run through several standard tests from Dershowitz & Reingold.
*/
public void TestJulianDayMapping() {
final TestCase[] tests = {
//
// From Dershowitz & Reingold, "Calendrical Calculations".
//
// The months in this table are 1-based rather than 0-based.
//
// * Failing fields->millis
// ** Millis->fields gives 0-based month -1
// These failures were fixed by changing the start search date
// for the winter solstice from Dec 15 to Dec 1.
//
// Julian Day Era Year Month Leap DOM WkDay
new ChineseTestCase(1507231.5, 35, 11, 6, false, 12, SUN),
new ChineseTestCase(1660037.5, 42, 9, 10, false, 27, WED),
new ChineseTestCase(1746893.5, 46, 7, 8, false, 4, WED),
new ChineseTestCase(1770641.5, 47, 12, 8, false, 9, SUN),
new ChineseTestCase(1892731.5, 52, 46, 11, false, 20, WED),
new ChineseTestCase(1931579.5, 54, 33, 4, false, 5, MON),
new ChineseTestCase(1974851.5, 56, 31, 10, false, 15, SAT),
new ChineseTestCase(2091164.5, 61, 50, 3, false, 7, SUN),
new ChineseTestCase(2121509.5, 63, 13, 4, false, 24, SUN),
new ChineseTestCase(2155779.5, 64, 47, 2, false, 9, FRI),
new ChineseTestCase(2174029.5, 65, 37, 2, false, 9, SAT),
new ChineseTestCase(2191584.5, 66, 25, 2, false, 23, FRI),
new ChineseTestCase(2195261.5, 66, 35, 3, false, 9, SUN), //*
new ChineseTestCase(2229274.5, 68, 8, 5, false, 2, SUN), //*
new ChineseTestCase(2245580.5, 68, 53, 1, false, 8, WED), //**
new ChineseTestCase(2266100.5, 69, 49, 3, false, 4, SAT),
new ChineseTestCase(2288542.5, 70, 50, 8, false, 2, SAT), //*
new ChineseTestCase(2290901.5, 70, 57, 1, false, 29, SAT), //*
new ChineseTestCase(2323140.5, 72, 25, 4, true, 20, WED), //*
new ChineseTestCase(2334848.5, 72, 57, 6, false, 5, SUN),
new ChineseTestCase(2348020.5, 73, 33, 6, false, 6, FRI),
new ChineseTestCase(2366978.5, 74, 25, 5, false, 5, SUN),
new ChineseTestCase(2385648.5, 75, 16, 6, false, 12, MON),
new ChineseTestCase(2392825.5, 75, 36, 2, false, 13, WED),
new ChineseTestCase(2416223.5, 76, 40, 3, false, 22, SUN),
new ChineseTestCase(2425848.5, 77, 6, 7, false, 21, SUN),
new ChineseTestCase(2430266.5, 77, 18, 8, false, 9, MON),
new ChineseTestCase(2430833.5, 77, 20, 3, false, 15, MON),
new ChineseTestCase(2431004.5, 77, 20, 9, false, 9, THU),
new ChineseTestCase(2448698.5, 78, 9, 2, false, 14, TUE),
new ChineseTestCase(2450138.5, 78, 13, 1, false, 7, SUN),
new ChineseTestCase(2465737.5, 78, 55, 10, false, 14, WED),
new ChineseTestCase(2486076.5, 79, 51, 6, false, 7, SUN),
// Additional tests not from D&R
new ChineseTestCase(2467496.5, 78, 60, 8, false, 2, FRI), // year 60
};
ChineseCalendar cal = new ChineseCalendar();
cal.setLenient(true);
doTestCases(tests, cal);
}
}

View File

@ -0,0 +1,48 @@
package com.ibm.test.calendar;
import com.ibm.util.*;
import java.util.Date;
public class ChineseTestCase extends TestCase {
/**
* Initialize an object using a Julian day number and
* the corresponding fields for the calendar being tested.
*
* @param era the ERA field of tested calendar on the given Julian
* day
* @param year the YEAR field of tested calendar on the given
* Julian day
* @param month the MONTH (1-based) field of tested calendar on
* the given Julian day
* @param isLeapMonth if true, treat month as a leap month
* @param dayOfMonth the DAY_OF_MONTH field of tested calendar on the
* given Julian day
* @param dayOfWeek the DAY_OF_WEEK field of tested calendar on given
* Julian day
*/
public ChineseTestCase(double julian,
int era, int year, int month,
boolean isLeapMonth, int dayOfMonth, int dayOfWeek) {
setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(ChineseCalendar.IS_LEAP_MONTH, isLeapMonth?1:0);
set(Calendar.DAY_OF_MONTH, dayOfMonth);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
}
/**
* Return a String representation of this test case's time.
*/
public String toString() {
return dowToString(get(Calendar.DAY_OF_WEEK)) +
get(Calendar.YEAR) + "of" + get(Calendar.ERA) +
"/" + (get(Calendar.MONTH)+1) +
(get(ChineseCalendar.IS_LEAP_MONTH)==1?"(leap)":"") + "/" +
get(Calendar.DAY_OF_MONTH);
}
}

View File

@ -17,6 +17,15 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
new CompatibilityTest().run(argv);
}
static final String[] FIELD_NAME = {
"ERA", "YEAR", "MONTH", "WEEK_OF_YEAR", "WEEK_OF_MONTH",
"DAY_OF_MONTH", "DAY_OF_YEAR", "DAY_OF_WEEK",
"DAY_OF_WEEK_IN_MONTH", "AM_PM", "HOUR", "HOUR_OF_DAY",
"MINUTE", "SECOND", "MILLISECOND", "ZONE_OFFSET",
"DST_OFFSET", "YEAR_WOY", "DOW_LOCAL", "EXTENDED_YEAR",
"JULIAN_DAY", "MILLISECONDS_IN_DAY",
};
/**
* Test the behavior of the GregorianCalendar around the changeover.
*/
@ -64,11 +73,62 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
* (first day of week, minimal days in first week).
*/
public void TestMapping() {
if (false) {
Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
Date PURE_JULIAN = GregorianCalendar.PURE_JULIAN; // new Date(Long.MAX_VALUE);
GregorianCalendar cal =
new GregorianCalendar(TimeZone.getTimeZone("UTC"));
final int EPOCH_JULIAN = 2440588;
final long ONE_DAY = 24*60*60*1000L;
com.ibm.text.SimpleDateFormat fmt =
new com.ibm.text.SimpleDateFormat("EEE MMM dd yyyy G");
/*HH:mm:ss.SSS z*/
for (int type=0; type<2; ++type) {
System.out.println(type==0 ? "Gregorian" : "Julian");
cal.setGregorianChange(type==0 ? PURE_GREGORIAN : PURE_JULIAN);
fmt.setCalendar(cal);
int[] J = {
0x7FFFFFFF,
0x7FFFFFF0,
0x7F000000,
0x78000000,
0x70000000,
0x60000000,
0x50000000,
0x40000000,
0x30000000,
0x20000000,
0x10000000,
};
for (int i=0; i<J.length; ++i) {
String[] lim = new String[2];
long[] ms = new long[2];
int jd = J[i];
for (int sign=0; sign<2; ++sign) {
int julian = jd;
if (sign==0) julian = -julian;
long millis = ((long)julian - EPOCH_JULIAN) * ONE_DAY;
ms[sign] = millis;
cal.setTime(new Date(millis));
lim[sign] = fmt.format(cal.getTime());
}
System.out.println("JD +/-" +
Long.toString(jd, 16) +
": " + ms[0] + ".." + ms[1] +
": " + lim[0] + ".." + lim[1]);
}
}
}
TimeZone saveZone = TimeZone.getDefault();
try {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
Date PURE_JULIAN = new Date(Long.MAX_VALUE);
//NEWCAL
//Date PURE_GREGORIAN = new Date(Long.MIN_VALUE);
//Date PURE_JULIAN = new Date(Long.MAX_VALUE);
Date PURE_GREGORIAN = Calendar.MIN_DATE;
Date PURE_JULIAN = Calendar.MAX_DATE;
GregorianCalendar cal = new GregorianCalendar();
final int EPOCH_JULIAN = 2440588;
final long ONE_DAY = 24*60*60*1000L;
@ -227,7 +287,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
if (cal.getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
}
for (i=0; i<Calendar.FIELD_COUNT; ++i) {
for (i=0; i<cal.getFieldCount(); ++i) {
if (cal.getMinimum(i) != cal.getGreatestMinimum(i))
errln("FAIL: getMinimum doesn't match getGreatestMinimum for field " + i);
if (cal.getLeastMaximum(i) > cal.getMaximum(i))
@ -259,14 +319,17 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
}
cal.getTime();
for (i=0; i<Calendar.FIELD_COUNT; ++i) {
// This test is strange -- why do we expect certain fields to be set, and
// others not to be? Revisit the appropriateness of this. - Alan NEWCAL
for (i=0; i<cal.getFieldCount(); ++i) {
switch(i) {
case Calendar.YEAR: case Calendar.MONTH: case Calendar.DATE:
case Calendar.HOUR_OF_DAY: case Calendar.MINUTE: case Calendar.SECOND:
if (!cal.isSet(i)) errln("FAIL: Calendar.isSet failed");
case Calendar.EXTENDED_YEAR:
if (!cal.isSet(i)) errln("FAIL: " + FIELD_NAME[i] + " is not set");
break;
default:
if (cal.isSet(i)) errln("FAIL: Calendar.isSet failed");
if (cal.isSet(i)) errln("FAIL: " + FIELD_NAME[i] + " is set");
}
cal.clear(i);
if (cal.isSet(i)) errln("FAIL: Calendar.clear/isSet failed");
@ -433,7 +496,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
catch (IllegalArgumentException ex) {
e = ex;
}
verify765("1997 zero-th Tuesday in June = ", e);
verify765("1997 zero-th Tuesday in June = ", e, c);
c.clear();
c.set(Calendar.YEAR, 1997);
@ -485,20 +548,25 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
catch (IllegalArgumentException ex) {}
}
void verify765(String msg, Calendar c, int year, int month, int day) {
if (c.get(Calendar.YEAR) == year &&
c.get(Calendar.MONTH) == month &&
c.get(Calendar.DATE) == day) {
int cy = c.get(Calendar.YEAR); // NEWCAL
int cm = c.get(Calendar.MONTH);
int cd = c.get(Calendar.DATE);
if (cy == year &&
cm == month &&
cd == day) {
logln("PASS: " + msg + c.getTime());
}
else {
errln("FAIL: " + msg + c.getTime() +
errln("FAIL: " + msg + cy + "/" + (cm+1) + "/" + cd +
"=" + c.getTime() +
"; expected " +
year + "/" + (month+1) + "/" + day);
}
}
// Called when e expected to be non-null
void verify765(String msg, IllegalArgumentException e) {
if (e == null) errln("FAIL: No IllegalArgumentException for " + msg);
void verify765(String msg, IllegalArgumentException e, Calendar c) {
if (e == null) errln("FAIL: No IllegalArgumentException for " + msg +
c.getTime());
else logln("PASS: " + msg + "IllegalArgument as expected");
}
@ -883,6 +951,7 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
cal.getActualMaximum(Calendar.WEEK_OF_YEAR));
cal.set(Calendar.YEAR, 1976);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); // Added - Liu 11/6/00
// Using week settings of SUNDAY/3 (see above)
if (cal.getActualMaximum(Calendar.WEEK_OF_YEAR) != 53)
errln("Number of weeks in 1976 should have been 53; got " +
@ -893,7 +962,6 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
Calendar cal = new GregorianCalendar(1997, Calendar.JANUARY, 31);
int[] dayValues = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31 };
for (int i = 0; i < dayValues.length; i++) {
Calendar cal2 = (Calendar)cal.clone();
cal2.roll(Calendar.MONTH, i);
@ -905,19 +973,24 @@ public class CompatibilityTest extends com.ibm.test.TestFmwk {
cal.set(1996, Calendar.FEBRUARY, 29);
int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
//int[] monthValues = { 1, 2, 2, 2, 1, 2, 2, 2, 1, 2 };
//int[] dayValues2 = { 29, 1, 1, 1, 29, 1, 1, 1, 29, 1 };
// I've revised the expected values to make more sense -- rolling
// the year should pin the DAY_OF_MONTH. - Liu 11/6/00
int[] monthValues = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] dayValues2 = { 29, 28, 28, 28, 29, 28, 28, 28, 29, 28 };
for (int i = 0; i < dayValues2.length; i++) {
Calendar cal2 = (Calendar)cal.clone();
cal2.roll(Calendar.YEAR, i);
if (cal2.get(Calendar.DAY_OF_MONTH) != dayValues2[i] || cal2.get(Calendar.MONTH)
!= monthValues[i])
errln("Rolling the year in 2/29/1996 up by " + i + " should have yielded "
errln("Roll 2/29/1996 by " + i + " year: expected "
+ (monthValues[i] + 1) + "/" + dayValues2[i] + "/"
+ (1996 + i) + ", but actually yielded "
+ (1996 + i) + ", got "
+ (cal2.get(Calendar.MONTH) + 1) + "/" +
cal2.get(Calendar.DAY_OF_MONTH) + "/" + (1996 + i) + ".");
cal2.get(Calendar.DAY_OF_MONTH) + "/" + cal2.get(Calendar.YEAR));
}
// Test rolling hour of day

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/HebrewTest.java,v $
* $Date: 2000/03/10 03:47:45 $
* $Revision: 1.2 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.3 $
*
*****************************************************************************************
*/
@ -14,9 +14,8 @@
package com.ibm.test.calendar;
import com.ibm.test.*;
import java.util.*;
import java.text.*;
import com.ibm.util.*;
import java.util.Locale;
/**
* Tests for the <code>HebrewCalendar</code> class.
@ -55,6 +54,7 @@ public class HebrewTest extends CalendarTest {
{ 5759, SHEVAT, 2, MONTH, 1, 5759, ADAR, 2 },
{ 5759, SHEVAT, 2, MONTH, 2, 5759, NISAN, 2 },
{ 5759, SHEVAT, 2, MONTH, 12, 5759, SHEVAT, 2 },
{ 5759, AV, 1, MONTH, 12, 5759, AV, 1 }, // Alan
{ 5757, HESHVAN, 2, MONTH, 1, 5757, KISLEV, 2 }, // leap years
{ 5757, SHEVAT, 2, MONTH, 1, 5757, ADAR_1, 2 },
@ -62,6 +62,7 @@ public class HebrewTest extends CalendarTest {
{ 5757, SHEVAT, 2, MONTH, 3, 5757, NISAN, 2 },
{ 5757, SHEVAT, 2, MONTH, 12, 5757, TEVET, 2 },
{ 5757, SHEVAT, 2, MONTH, 13, 5757, SHEVAT, 2 },
{ 5757, AV, 1, MONTH, 12, 5757, TAMUZ, 1 }, // Alan
{ 5757, KISLEV, 1, DATE, 30, 5757, KISLEV, 2 }, // 29-day month
{ 5758, KISLEV, 1, DATE, 31, 5758, KISLEV, 2 }, // 30-day month
@ -198,5 +199,116 @@ public class HebrewTest extends CalendarTest {
new TestCase(2487223.5, 0, 5858, 1, 1, SAT, 0, 0, 0),
};
};
/**
* Problem reported by Armand Bendanan in which setting of the MONTH
* field in a Hebrew calendar causes the time fields to go negative.
*/
public void TestTimeFields() {
HebrewCalendar calendar = new HebrewCalendar(5761, 0, 11, 12, 28, 15);
calendar.set(Calendar.YEAR, 5717);
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DAY_OF_MONTH, 23);
if (calendar.get(Calendar.HOUR_OF_DAY) != 12) {
errln("Fail: HebrewCalendar HOUR_OF_DAY = " + calendar.get(Calendar.HOUR_OF_DAY));
}
}
/**
* Test of the behavior of the month field. This requires special
* handling in the Hebrew calendar because of the pattern of leap
* years.
*/
public void TestMonthMovement() {
HebrewCalendar cal = new HebrewCalendar();
// Leap years are:
// 3 6 8 11 14 17 19 (and so on - 19-year cycle)
// We can't test complete() on some lines below because of ADAR_1 -- if
// the calendar is set to ADAR_1 on a non-leap year, the result is undefined.
int[] DATA = {
// c - test complete() or not
// m/y - before and after month/year
// delta - amount to add to month field
//c m1 y1 delta m2 y2
1, 10, 2, +24, 9, 4, // (year 2, month 10) + 24 months -> (y 4, m 9)
1, 10, 2, +60, 8, 7, // (y 2, m 10) + 60 months -> (y 7, m 8)
1, 1, 2, +12, 1, 3, // (y 2, m 1) + 12 months -> (y 3, m 1)
1, 3, 18, -24, 4, 16, // (y 18, m 3) - 24 months -> (y 16, m 4)
1, 1, 6, -24, 1, 4,
1, 4, 3, +2, 6, 3, // Leap year - no skip 4,5,6,7,8
1, 8, 3, -2, 6, 3, // Leap year - no skip
0, 4, 2, +2, 7, 2, // Skip over leap month 4,5,(6),7,8
0, 8, 2, -2, 5, 2, // Skip over leap month going backward
};
for (int i=0; i<DATA.length; ) {
boolean testComplete = DATA[i++] != 0;
int m = DATA[i++], y = DATA[i++];
int monthDelta = DATA[i++];
int m2 = DATA[i++], y2 = DATA[i++];
int mact, yact;
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
cal.add(Calendar.MONTH, monthDelta);
yact = cal.get(Calendar.YEAR); mact = cal.get(Calendar.MONTH) + 1;
if (y2 != yact || m2 != mact) {
errln("Fail: " + m + "/" + y +
" -> add(MONTH, " + monthDelta + ") -> " +
mact + "/" + yact + ", expected " +
m2 + "/" + y2);
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m-1);
logln("Start: " + m + "/" + y);
int delta = monthDelta > 0 ? 1 : -1;
for (int c=0; c!=monthDelta; c+=delta) {
cal.add(Calendar.MONTH, delta);
logln("+ " + delta + " MONTH -> " +
(cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR));
}
}
if (testComplete) {
cal.clear();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m + monthDelta - 1);
yact = cal.get(Calendar.YEAR); mact = cal.get(Calendar.MONTH) + 1;
if (y2 != yact || m2 != mact) {
errln("Fail: " + (m+monthDelta) + "/" + y +
" -> complete() -> " +
mact + "/" + yact + ", expected " +
m2 + "/" + y2);
}
}
}
}
/**
* Test handling of ADAR_1.
*/
/*
public void TestAdar1() {
HebrewCalendar cal = new HebrewCalendar();
cal.clear();
cal.set(Calendar.YEAR, 1903); // leap
cal.set(Calendar.MONTH, HebrewCalendar.ADAR_1);
logln("1903(leap)/ADAR_1 => " +
cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH)+1));
cal.clear();
cal.set(Calendar.YEAR, 1904); // non-leap
cal.set(Calendar.MONTH, HebrewCalendar.ADAR_1);
logln("1904(non-leap)/ADAR_1 => " +
cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH)+1));
}
*/
/**
* With no fields set, the calendar should use default values.
*/
public void TestDefaultFieldValues() {
HebrewCalendar cal = new HebrewCalendar();
cal.clear();
logln("cal.clear() -> " + cal.getTime());
}
};

View File

@ -4,8 +4,8 @@
* others. All Rights Reserved.
*******************************************************************************
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/IBMCalendarTest.java,v $
* $Date: 2000/10/17 18:32:50 $
* $Revision: 1.3 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.4 $
*******************************************************************************
*/
package com.ibm.test.calendar;
@ -19,7 +19,7 @@ import java.util.Locale;
* @test
* @summary Tests of new functionality in IBMCalendar
*/
public class IBMCalendarTest extends TestFmwk {
public class IBMCalendarTest extends CalendarTest {
public static void main(String[] args) throws Exception {
new IBMCalendarTest().run(args);
@ -126,4 +126,89 @@ public class IBMCalendarTest extends TestFmwk {
}
}
}
/**
* Run a test of a quasi-Gregorian calendar. This is a calendar
* that behaves like a Gregorian but has different year/era mappings.
* The int[] data array should have the format:
*
* { era, year, gregorianYear, month, dayOfMonth, ... }
*/
void quasiGregorianTest(Calendar cal, int[] data) {
for (int i=0; i<data.length; ) {
int era = data[i++];
int year = data[i++];
int gregorianYear = data[i++];
int month = data[i++];
int dayOfMonth = data[i++];
Date D = new Date(gregorianYear - 1900, month, dayOfMonth);
cal.clear();
cal.set(Calendar.ERA, era);
cal.set(year, month, dayOfMonth);
Date d = cal.getTime();
if (d.equals(D)) {
logln("OK: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
" => " + d);
} else {
errln("Fail: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
" => " + d + ", expected " + D);
}
cal.clear();
cal.setTime(D);
int e = cal.get(Calendar.ERA);
int y = cal.get(Calendar.YEAR);
if (y == year && e == era) {
logln("OK: " + D + " => " + cal.get(Calendar.ERA) + ":" +
cal.get(Calendar.YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE));
} else {
logln("Fail: " + D + " => " + cal.get(Calendar.ERA) + ":" +
cal.get(Calendar.YEAR) + "/" +
(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE) +
", expected " + era + ":" + year + "/" + (month+1) + "/" +
dayOfMonth);
}
}
}
/**
* Verify that BuddhistCalendar shifts years to Buddhist Era but otherwise
* behaves like GregorianCalendar.
*/
public void TestBuddhist() {
quasiGregorianTest(new BuddhistCalendar(),
new int[] {
// BE 2542 == 1999 CE
0, 2542, 1999, Calendar.JUNE, 4
});
}
/**
* Verify that JapaneseCalendar shifts years to Buddhist Era but otherwise
* behaves like GregorianCalendar.
*/
public void TestJapanese() {
int[] data = {
JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 8,
JapaneseCalendar.MEIJI, 1, 1868, Calendar.SEPTEMBER, 9,
JapaneseCalendar.MEIJI, 2, 1869, Calendar.JUNE, 4,
JapaneseCalendar.MEIJI, 45, 1912, Calendar.JULY, 29,
JapaneseCalendar.TAISHO, 1, 1912, Calendar.JULY, 30,
JapaneseCalendar.TAISHO, 1, 1912, Calendar.AUGUST, 1,
};
quasiGregorianTest(new JapaneseCalendar(), data);
}
/**
* Test limits of the Gregorian calendar.
*/
public void TestGregorianLimits() {
// Final parameter is either number of days, if > 0, or test
// duration in seconds, if < 0.
doLimitsTest(new GregorianCalendar(), null,
new Date(2004-1900, Calendar.JANUARY, 1), -10);
}
}

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/IslamicTest.java,v $
* $Date: 2000/03/10 03:47:45 $
* $Revision: 1.2 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.3 $
*
*****************************************************************************************
*/
@ -119,10 +119,26 @@ public class IslamicTest extends CalendarTest {
doTestCases(tests, civilCalendar);
}
public void TestBasic() {
IslamicCalendar cal = newCivil();
cal.clear();
cal.set(1000, 0, 30);
logln("1000/0/30 -> " +
cal.get(YEAR) + "/" +
cal.get(MONTH) + "/" +
cal.get(DATE));
cal.clear();
cal.set(1, 0, 30);
logln("1/0/30 -> " +
cal.get(YEAR) + "/" +
cal.get(MONTH) + "/" +
cal.get(DATE));
}
private static IslamicCalendar newCivil() {
IslamicCalendar civilCalendar = new IslamicCalendar();
civilCalendar.setCivil(true);
return civilCalendar;
}
};
};

View File

@ -5,8 +5,8 @@
*******************************************************************************
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/test/calendar/Attic/TestCase.java,v $
* $Date: 2000/10/17 18:32:50 $
* $Revision: 1.5 $
* $Date: 2000/11/18 00:17:58 $
* $Revision: 1.6 $
*
*****************************************************************************************
*/
@ -20,7 +20,7 @@ import com.ibm.util.SimpleTimeZone;
import java.util.Locale;
/**
* A dummy <code>Calendar</code> subclass that is useful for testing
* A pseudo <code>Calendar</code> that is useful for testing
* new calendars. A <code>TestCase</code> object is used to hold the
* field and millisecond values that the calendar should have at one
* particular instant in time. The applyFields and applyTime
@ -28,15 +28,63 @@ import java.util.Locale;
* tested, and the equals and fieldsEqual methods are used to ensure
* that the calendar has ended up in the right state.
*/
public class TestCase extends Calendar {
public class TestCase {
//------------------------------------------------------------------
// Pseudo-Calendar fields and methods
//------------------------------------------------------------------
protected int[] fields = new int[32];
protected boolean[] isSet = new boolean[32];
protected long time;
protected void set(int field, int value) {
fields[field] = value;
isSet[field] = true;
}
protected int get(int field) {
return fields[field];
}
protected boolean isSet(int field) {
return isSet[field];
}
protected void setTime(Date d) {
time = d.getTime();
}
public Date getTime() {
return new Date(time);
}
/**
* Return a String representation of this test case's time.
*/
public String toString() {
return dowToString(get(Calendar.DAY_OF_WEEK)) +
get(Calendar.YEAR) + "/" + (get(Calendar.MONTH)+1) + "/" +
get(Calendar.DATE);
}
private static final String[] DOW_NAMES = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
public static String dowToString(int dow) {
--dow;
return (dow < 0 || dow > 6) ?
("<DOW " + dow + ">") : DOW_NAMES[dow];
}
/**
* Initialize a TestCase object using a julian day number and
* the corresponding fields for the calendar being tested.
*
* @param era The ERA field of tested calendar on the given julian day
* @param year The YEAR field of tested calendar on the given julian day
* @param month The MONTH (0-based) field of tested calendar on the given julian day
* @param month The MONTH (1-based) field of tested calendar on the given julian day
* @param day The DAY_OF_MONTH field of tested calendar on the given julian day
* @param dayOfWeek The DAY_OF_WEEK field of tested calendar on the given julian day
* @param hour The HOUR field of tested calendar on the given julian day
@ -44,22 +92,20 @@ public class TestCase extends Calendar {
* @param sec The SECOND field of tested calendar on the given julian day
*/
public TestCase(double julian,
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
{
super(UTC, Locale.getDefault());
setTime(new Date(JULIAN_EPOCH + (long)(ONE_DAY * julian)));
setTime(new Date(JULIAN_EPOCH + (long)(DAY_MS * julian)));
set(ERA, era);
set(YEAR, year);
set(MONTH, month - 1);
set(DATE, day);
set(DAY_OF_WEEK, dayOfWeek);
set(HOUR, hour);
set(MINUTE, min);
set(SECOND, sec);
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(Calendar.DATE, day);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
set(Calendar.HOUR, hour);
set(Calendar.MINUTE, min);
set(Calendar.SECOND, sec);
}
/**
@ -80,27 +126,30 @@ public class TestCase extends Calendar {
* @param sec The SECOND field of tested calendar on the given gregorian date
*/
public TestCase(int gregYear, int gregMonth, int gregDay,
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
int era, int year, int month, int day,
int dayOfWeek,
int hour, int min, int sec)
{
super(UTC, Locale.getDefault());
GregorianCalendar greg = new GregorianCalendar(UTC, Locale.getDefault());
greg.clear();
greg.set(gregYear, gregMonth-1, gregDay);
setTime(greg.getTime());
set(ERA, era);
set(YEAR, year);
set(MONTH, month - 1);
set(DATE, day);
set(DAY_OF_WEEK, dayOfWeek);
set(HOUR, hour);
set(MINUTE, min);
set(SECOND, sec);
set(Calendar.ERA, era);
set(Calendar.YEAR, year);
set(Calendar.MONTH, month - 1);
set(Calendar.DATE, day);
set(Calendar.DAY_OF_WEEK, dayOfWeek);
set(Calendar.HOUR, hour);
set(Calendar.MINUTE, min);
set(Calendar.SECOND, sec);
}
/**
* For subclasses.
*/
protected TestCase() {}
/**
* Apply this test case's field values to another calendar
* by calling its set method for each field. This is useful in combination
@ -109,13 +158,11 @@ public class TestCase extends Calendar {
* @see #equal
*/
public void applyFields(Calendar c) {
c.set(ERA, fields[ERA]);
c.set(YEAR, fields[YEAR]);
c.set(MONTH, fields[MONTH]);
c.set(DATE, fields[DATE]);
c.set(HOUR, fields[HOUR]);
c.set(MINUTE, fields[MINUTE]);
c.set(SECOND, fields[SECOND]);
for (int i=0; i < c.getFieldCount(); i++) {
if (isSet(i)) {
c.set(i, get(i));
}
}
}
/**
@ -145,10 +192,22 @@ public class TestCase extends Calendar {
*
* @see #applyTime
*/
public boolean fieldsEqual(Calendar c) {
for (int i=0; i < Calendar.FIELD_COUNT; i++) {
public boolean fieldsEqual(Calendar c, TestLog log) {
for (int i=0; i < c.getFieldCount(); i++) {
if (isSet(i) && get(i) != c.get(i)) {
System.out.println("field " + i + ": expected " + get(i) + ", got " + c.get(i));
log.errln("Fail: " + CalendarTest.fieldName(i) + " = " + c.get(i) +
", expected " + get(i));
for (int j=0; j<c.getFieldCount(); ++j) {
if (isSet(j)) {
if (get(j) == c.get(j)) {
log.errln(" OK: " + CalendarTest.fieldName(j) + " = " +
c.get(j));
} else {
log.errln(" Fail: " + CalendarTest.fieldName(j) + " = " +
c.get(j) + ", expected " + get(j));
}
}
}
return false;
}
}
@ -176,38 +235,11 @@ public class TestCase extends Calendar {
return time == ((Calendar)obj).getTime().getTime();
}
/**
* Determine whether time of this calendar (as returned by getTime)
* is before that of the specified calendar
*/
public boolean before(Object obj) {
return time < ((Calendar)obj).getTime().getTime();
}
/**
* Determine whether time of this calendar (as returned by getTime)
* is after that of the specified calendar
*/
public boolean after(Object obj) {
return time > ((Calendar)obj).getTime().getTime();
}
// This object is only pretending to be a Calendar; it doesn't do any real
// calendar computatations. But we have to pretend it does, because Calendar
// declares all of these abstract methods....
protected void computeTime() {}
protected void computeFields() {}
public void add(int field, int amt) {}
public int getMinimum(int field) { return 0; }
public int getMaximum(int field) { return 0; }
public int getGreatestMinimum(int field) { return 0; }
public int getLeastMaximum(int field) { return 0; }
private static final int SECOND_MS = 1000;
private static final int MINUTE_MS = 60*SECOND_MS;
private static final int HOUR_MS = 60*MINUTE_MS;
private static final long DAY_MS = 24*HOUR_MS;
private static final long JULIAN_EPOCH = -210866760000000L; // 1/1/4713 BC 12:00
protected static final int ONE_SECOND = 1000;
protected static final int ONE_MINUTE = 60*ONE_SECOND;
protected static final int ONE_HOUR = 60*ONE_MINUTE;
protected static final long ONE_DAY = 24*ONE_HOUR;
protected static final long JULIAN_EPOCH = -210866760000000L; // 1/1/4713 BC 12:00
public final static SimpleTimeZone UTC = new SimpleTimeZone(0, "GMT");
}