1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
// Copyright (C) 2007 Red Hat, Inc.
// Written by Gary Benson <gbenson@redhat.com>
// This file is part of Mauve.
// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING. If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Tags: JDK1.1
package gnu.testlet.java.util.GregorianCalendar;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Locale;
import java.util.TimeZone;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
public class internal implements Testlet
{
private static class TestCalendar extends GregorianCalendar
{
/**
* Creates a calendar with all values unset and zeroed.
*/
public static TestCalendar getTestCalendar(TimeZone zone, Locale locale)
{
// There is no way to get a calendar with a specific
// locale without calling one of the constructors that
// calls setTimeInMillis(). So we do it this way...
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(locale);
try
{
TestCalendar c = new TestCalendar();
c.setTimeZone(zone);
return c;
}
finally
{
Locale.setDefault(defaultLocale);
}
}
private TestCalendar()
{
// Initializing this way avoids calling setTimeInMillis(),
// giving us an object whose time has never been set.
super(2007, Calendar.APRIL, 11);
clear();
}
/**
* Compare the internal state of this calendar with the supplied values.
*/
public void checkState(
TestHarness harness,
boolean areFieldsSet, int isSetBitmask, int[] fields,
boolean isTimeSet, long time)
{
String expected, actual;
boolean success;
expected = stateString(areFieldsSet, isSetBitmask, isTimeSet, time);
harness.debug("expecting " + expected);
actual = stateString(
this.areFieldsSet, isSet, this.isTimeSet, this.time);
success = expected.equals(actual);
if (!success)
harness.debug(" got " + actual);
harness.check(success);
expected = stateString(fields);
harness.debug("expecting " + expected);
actual = stateString(this.fields);
success = expected.equals(actual);
if (!success)
harness.debug(" got " + actual);
harness.check(success);
}
/**
* Return a string representation of part of the internal state.
*/
private String stateString(
boolean areFieldsSet, int isSetBitmask, boolean isTimeSet, long time)
{
String result = areFieldsSet + " (";
for (int i = 0; i < FIELD_COUNT; i++)
result += ((isSetBitmask & (1 << i)) != 0) ? "S" : "-";
result += "), " + isTimeSet + " (" + time + ")";
return result;
}
/**
* Return a string representation of part of the internal state.
*/
private String stateString(
boolean areFieldsSet, boolean[] isSet, boolean isTimeSet, long time)
{
int isSetBitmask = 0;
for (int i = 0; i < FIELD_COUNT; i++)
{
if (isSet[i])
isSetBitmask |= 1 << i;
}
return stateString(areFieldsSet, isSetBitmask, isTimeSet, time);
}
/**
* Return a string representation of part of the internal state.
*/
private String stateString(int[] fields)
{
String result = "{";
for (int i = 0; i < FIELD_COUNT; i++)
{
if (i > 0)
result += ", ";
result += fields[i];
}
result += "}";
return result;
}
// public void debug()
// {
// System.out.print(areFieldsSet + " (");
// for (int i = 0; i < FIELD_COUNT; i++)
// System.out.print(isSet[i] ? "S" : "-");
// System.out.print("/");
// for (int i = 0; i < FIELD_COUNT; i++)
// System.out.print(isSet(i) ? "S" : "-");
// System.out.println("), " + isTimeSet + " (" + time + ")");
//
// System.out.print(" {");
// for (int i = 0; i < FIELD_COUNT; i++)
// {
// if (i > 0)
// System.out.print(", ");
// // if (i > 2 && i < 5)
// // System.out.print("\u001B[1;33m");
// System.out.print(fields[i]);
// // if (i > 2 && i < 5)
// // System.out.print("\u001B[0m");
// }
// System.out.println("}");
// }
// protected void computeTime()
// {
// System.out.println(" computeTime:");
// debug();
// super.computeTime();
// debug();
// System.out.println(" /computeTime");
// }
// protected void computeFields()
// {
// //areFieldsSet = false;
// System.out.println(" computeFields:");
// debug();
// super.computeFields();
// debug();
// System.out.println(" /computeFields");
// }
// protected void complete()
// {
// System.out.println(" complete:");
// debug();
// super.complete();
// debug();
// System.out.println(" /complete");
// }
}
/**
* Generate every possible permutation of a specified length
*/
private static class Permutator implements Iterator
{
private int size, length, pos;
public Permutator(int s)
{
size = s;
length = 1;
for (int i = 2; i <= size; i++)
length *= i;
pos = 0;
}
public boolean hasNext()
{
return pos < length;
}
public Object next()
{
int[] result = new int[size];
boolean[] used = new boolean[size];
for (int tmp = pos, j = size; j > 0; tmp /= j, j--)
{
int choice = tmp % j;
for (int k = 0; k < size; k++)
{
if (!used[k])
{
if (choice == 0)
{
result[size - j] = k;
used[k] = true;
break;
}
choice--;
}
}
}
pos++;
return result;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
public void test(TestHarness harness)
{
int[][] checkFields = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, -18000000, 0},
{1, 1969, 11, 1, 5, 31, 365, 4, 5, 1, 10, 22, 0, 0, 0, -25200000, 0},
{1, 1970, 11, 1, 5, 31, 365, 5, 5, 1, 10, 22, 0, 0, 0, -25200000, 0},
{1, 1969, 0, 5, 5, 31, 31, 6, 5, 1, 10, 22, 0, 0, 0, -25200000, 0},
{1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, -25200000, 0},
{0, 2007, 3, 15, 4, 18, 107, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0},
};
TestCalendar c = TestCalendar.getTestCalendar(
TimeZone.getTimeZone("EST"), Locale.US);
// check it really is as blank as it should be.
c.checkState(harness, false, 0, checkFields[0], false, 0);
// check that things that affect week numbering are as expected.
harness.check(c.getFirstDayOfWeek() == Calendar.SUNDAY);
harness.check(c.getMinimalDaysInFirstWeek() == 1);
// // print the heading.
// String vendor = System.getProperty("java.vendor");
// if (vendor.endsWith("."))
// vendor = vendor.substring(0, vendor.length() - 1);
// System.out.println(vendor);
// for (int i = 0; i < vendor.length(); i++)
// System.out.print('=');
// System.out.println();
// cause a complete() and check we got the epoch right.
c.get(Calendar.YEAR);
c.checkState(harness, true, 0x1ffff, checkFields[1], true, 18000000);
// set a different timezone and check that areFieldsSet is cleared.
c.setTimeZone(TimeZone.getTimeZone("MST"));
c.checkState(harness, false, 0x1ffff, checkFields[1], true, 18000000);
// cause a complete() and check the fields recalculate correctly.
c.get(Calendar.YEAR);
c.checkState(harness, true, 0x1ffff, checkFields[2], true, 18000000);
// clear each field in turn and check everything recalculates correctly.
for (int i = 0; i < Calendar.FIELD_COUNT; i++)
{
TestCalendar d = (TestCalendar) c.clone();
int[] expectFields = (int[]) checkFields[2].clone();
long expectTime = 18000000;
d.checkState(harness, true, 0x1ffff, expectFields, true, expectTime);
d.clear(i);
expectFields[i] = 0;
d.checkState(harness, false, ~(1<<i), expectFields, false, expectTime);
d.get(Calendar.YEAR);
if (i == Calendar.YEAR)
{
expectFields = checkFields[3];
expectTime = 31554000000L;
}
else if (i == Calendar.MONTH)
{
expectFields = checkFields[4];
expectTime = -28839600000L;
}
else
{
expectFields = checkFields[2];
expectTime = 18000000;
}
d.checkState(harness, true, 0x1ffff, expectFields, true, expectTime);
}
// clear the entire calendar and check it recalculates to the epoch.
c.clear();
c.checkState(harness, false, 0, checkFields[0], false, 18000000);
c.get(Calendar.YEAR);
c.checkState(harness, true, 0x1ffff, checkFields[5], true, 25200000);
// set the time and check that the fields are recalculated correctly
c.setTimeInMillis(18000000);
c.checkState(harness, true, 0x1ffff, checkFields[2], true, 18000000);
// check that setting fields in different orders works
for (Iterator iter = new Permutator(8); iter.hasNext(); )
{
int[] order = (int[]) iter.next();
for (int i = 0; i < order.length; i++)
order[i] += Calendar.YEAR;
c.setTimeInMillis(0);
c.clear();
for (int i = 0; i < order.length; i++)
c.set(order[i], checkFields[6][order[i]]);
c.checkState(harness, false, 0x1fe, checkFields[6], false, 0);
// checkFields[6] is a set of inconsistant fields which yield
// different times depending on which of them were used in the
// calculation:
//
// 1176879600000 YEAR + MONTH + DAY_OF_MONTH
// 1177570800000 YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
// 1178175600000 YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
// 1176793200000 YEAR + DAY_OF_YEAR
// 1176361200000 YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
int[] setOrder = new int[Calendar.AM_PM - Calendar.WEEK_OF_YEAR];
for (int i = 0, j = setOrder.length - 1; j >= 0; i++)
{
if (order[i] >= Calendar.WEEK_OF_YEAR
&& order[i] <= Calendar.DAY_OF_WEEK_IN_MONTH)
setOrder[j--] = order[i];
}
long expectTime = -1;
if (setOrder[0] == Calendar.DAY_OF_MONTH)
{
// YEAR + MONTH + DAY_OF_MONTH
expectTime = 1176879600000L;
}
else if (setOrder[0] == Calendar.DAY_OF_WEEK_IN_MONTH)
{
// YEAR + MONTH + DAY_OF_WEEK_IN_MONTH + DAY_OF_WEEK
expectTime = 1178175600000L;
}
else if (setOrder[0] == Calendar.DAY_OF_YEAR)
{
// YEAR + DAY_OF_YEAR
expectTime = 1176793200000L;
}
else if (setOrder[0] == Calendar.WEEK_OF_YEAR)
{
// YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
// (some of them)
expectTime = 1176361200000L;
}
else if (setOrder[0] == Calendar.DAY_OF_WEEK)
{
for (int i = 1; i < setOrder.length; i++)
{
if (setOrder[i] == Calendar.DAY_OF_MONTH
|| setOrder[i] == Calendar.DAY_OF_YEAR)
continue;
if (setOrder[i] == Calendar.WEEK_OF_YEAR)
{
// YEAR + DAY_OF_WEEK + WEEK_OF_YEAR
expectTime = 1176361200000L;
}
else if (setOrder[i] == Calendar.WEEK_OF_MONTH)
{
// YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
expectTime = 1177570800000L;
}
else {
// rest of them
expectTime = 1178175600000L;
}
break;
}
}
else
{
// YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK
// (the rest)
expectTime = 1177570800000L;
}
long actualTime = c.getTimeInMillis();
// if (actualTime != expectTime)
// {
// System.out.print("expect = " + expectTime + ", actual = "
// + actualTime + ", setOrder = [");
// for (int i = 0; i < setOrder.length; i++)
// {
// if (i > 0)
// System.out.print(", ");
// System.out.print(setOrder[i]);
// }
// System.out.println("]");
// }
harness.check(actualTime == expectTime);
}
}
}
|