test/java/time/tck/java/time/TCKPeriod.java

Print this page




 104         assertPeriod(Period.ofYears(234), 234, 0, 0);
 105         assertPeriod(Period.ofYears(-100), -100, 0, 0);
 106         assertPeriod(Period.ofYears(Integer.MAX_VALUE), Integer.MAX_VALUE, 0, 0);
 107         assertPeriod(Period.ofYears(Integer.MIN_VALUE), Integer.MIN_VALUE, 0, 0);
 108     }
 109 
 110     //-----------------------------------------------------------------------
 111     // ofMonths(int)
 112     //-----------------------------------------------------------------------
 113     @Test
 114     public void factory_ofMonths_int() {
 115         assertPeriod(Period.ofMonths(0), 0, 0, 0);
 116         assertPeriod(Period.ofMonths(1), 0, 1, 0);
 117         assertPeriod(Period.ofMonths(234), 0, 234, 0);
 118         assertPeriod(Period.ofMonths(-100), 0, -100, 0);
 119         assertPeriod(Period.ofMonths(Integer.MAX_VALUE), 0, Integer.MAX_VALUE, 0);
 120         assertPeriod(Period.ofMonths(Integer.MIN_VALUE), 0, Integer.MIN_VALUE, 0);
 121     }
 122 
 123     //-----------------------------------------------------------------------













 124     // ofDays(int)
 125     //-----------------------------------------------------------------------
 126     @Test
 127     public void factory_ofDay_int() {
 128         assertPeriod(Period.ofDays(0), 0, 0, 0);
 129         assertPeriod(Period.ofDays(1), 0, 0, 1);
 130         assertPeriod(Period.ofDays(234), 0, 0, 234);
 131         assertPeriod(Period.ofDays(-100), 0, 0, -100);
 132         assertPeriod(Period.ofDays(Integer.MAX_VALUE), 0, 0, Integer.MAX_VALUE);
 133         assertPeriod(Period.ofDays(Integer.MIN_VALUE), 0, 0, Integer.MIN_VALUE);
 134     }
 135 
 136     //-----------------------------------------------------------------------
 137     // of(int3)
 138     //-----------------------------------------------------------------------
 139     @Test
 140     public void factory_of_ints() {
 141         assertPeriod(Period.of(1, 2, 3), 1, 2, 3);
 142         assertPeriod(Period.of(0, 2, 3), 0, 2, 3);
 143         assertPeriod(Period.of(1, 0, 0), 1, 0, 0);
 144         assertPeriod(Period.of(0, 0, 0), 0, 0, 0);
 145         assertPeriod(Period.of(-1, -2, -3), -1, -2, -3);
 146     }
 147 


 234                 {"P-0Y", Period.ofYears(0)},
 235                 {"P-25Y", Period.ofYears(-25)},
 236                 {"P-987654321Y", Period.ofYears(-987654321)},
 237                 {"P" + Integer.MAX_VALUE + "Y", Period.ofYears(Integer.MAX_VALUE)},
 238                 {"P" + Integer.MIN_VALUE + "Y", Period.ofYears(Integer.MIN_VALUE)},
 239 
 240                 {"P1M", Period.ofMonths(1)},
 241                 {"P12M", Period.ofMonths(12)},
 242                 {"P987654321M", Period.ofMonths(987654321)},
 243                 {"P+1M", Period.ofMonths(1)},
 244                 {"P+12M", Period.ofMonths(12)},
 245                 {"P+987654321M", Period.ofMonths(987654321)},
 246                 {"P+0M", Period.ofMonths(0)},
 247                 {"P0M", Period.ofMonths(0)},
 248                 {"P-0M", Period.ofMonths(0)},
 249                 {"P-25M", Period.ofMonths(-25)},
 250                 {"P-987654321M", Period.ofMonths(-987654321)},
 251                 {"P" + Integer.MAX_VALUE + "M", Period.ofMonths(Integer.MAX_VALUE)},
 252                 {"P" + Integer.MIN_VALUE + "M", Period.ofMonths(Integer.MIN_VALUE)},
 253 












 254                 {"P1D", Period.ofDays(1)},
 255                 {"P12D", Period.ofDays(12)},
 256                 {"P987654321D", Period.ofDays(987654321)},
 257                 {"P+1D", Period.ofDays(1)},
 258                 {"P+12D", Period.ofDays(12)},
 259                 {"P+987654321D", Period.ofDays(987654321)},
 260                 {"P+0D", Period.ofDays(0)},
 261                 {"P0D", Period.ofDays(0)},
 262                 {"P-0D", Period.ofDays(0)},
 263                 {"P-25D", Period.ofDays(-25)},
 264                 {"P-987654321D", Period.ofDays(-987654321)},
 265                 {"P" + Integer.MAX_VALUE + "D", Period.ofDays(Integer.MAX_VALUE)},
 266                 {"P" + Integer.MIN_VALUE + "D", Period.ofDays(Integer.MIN_VALUE)},
 267 
 268                 {"P0Y0M0D", Period.of(0, 0, 0)},
 269                 {"P2Y0M0D", Period.of(2, 0, 0)},
 270                 {"P0Y3M0D", Period.of(0, 3, 0)},
 271                 {"P0Y0M4D", Period.of(0, 0, 4)},
 272                 {"P2Y3M25D", Period.of(2, 3, 25)},
 273                 {"P-2Y3M25D", Period.of(-2, 3, 25)},
 274                 {"P2Y-3M25D", Period.of(2, -3, 25)},
 275                 {"P2Y3M-25D", Period.of(2, 3, -25)},
 276                 {"P-2Y-3M-25D", Period.of(-2, -3, -25)},




 277         };
 278     }
 279 
 280     @Test(dataProvider="parseSuccess")
 281     public void factory_parse(String text, Period expected) {
 282         Period p = Period.parse(text);
 283         assertEquals(p, expected);
 284     }
 285 
 286     @Test(dataProvider="parseSuccess")
 287     public void factory_parse_plus(String text, Period expected) {
 288         Period p = Period.parse("+" + text);
 289         assertEquals(p, expected);
 290     }
 291 
 292     @Test(dataProvider="parseSuccess")
 293     public void factory_parse_minus(String text, Period expected) {
 294         Period p = null;
 295         try {
 296             p = Period.parse("-" + text);


 317                 {"PTD"},
 318                 {"AT0D"},
 319                 {"PA0D"},
 320                 {"PT0A"},
 321 
 322                 {"PT+D"},
 323                 {"PT-D"},
 324                 {"PT.D"},
 325                 {"PTAD"},
 326 
 327                 {"PT+0D"},
 328                 {"PT-0D"},
 329                 {"PT+1D"},
 330                 {"PT-.D"},
 331 
 332                 {"P1Y1MT1D"},
 333                 {"P1YMD"},
 334                 {"P1Y2Y"},
 335                 {"PT1M+3S"},
 336 







 337                 {"PT1S1"},
 338                 {"PT1S."},
 339                 {"PT1SA"},
 340                 {"PT1M1"},
 341                 {"PT1M."},
 342                 {"PT1MA"},
 343 
 344                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "Y"},
 345                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "M"},
 346                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "D"},
 347                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "Y"},
 348                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "M"},
 349                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "D"},
 350 
 351                 {"Rubbish"},
 352         };
 353     }
 354 
 355     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
 356     public void factory_parseFailures(String text) {




 104         assertPeriod(Period.ofYears(234), 234, 0, 0);
 105         assertPeriod(Period.ofYears(-100), -100, 0, 0);
 106         assertPeriod(Period.ofYears(Integer.MAX_VALUE), Integer.MAX_VALUE, 0, 0);
 107         assertPeriod(Period.ofYears(Integer.MIN_VALUE), Integer.MIN_VALUE, 0, 0);
 108     }
 109 
 110     //-----------------------------------------------------------------------
 111     // ofMonths(int)
 112     //-----------------------------------------------------------------------
 113     @Test
 114     public void factory_ofMonths_int() {
 115         assertPeriod(Period.ofMonths(0), 0, 0, 0);
 116         assertPeriod(Period.ofMonths(1), 0, 1, 0);
 117         assertPeriod(Period.ofMonths(234), 0, 234, 0);
 118         assertPeriod(Period.ofMonths(-100), 0, -100, 0);
 119         assertPeriod(Period.ofMonths(Integer.MAX_VALUE), 0, Integer.MAX_VALUE, 0);
 120         assertPeriod(Period.ofMonths(Integer.MIN_VALUE), 0, Integer.MIN_VALUE, 0);
 121     }
 122 
 123     //-----------------------------------------------------------------------
 124     // ofWeeks(int)
 125     //-----------------------------------------------------------------------
 126     @Test
 127     public void factory_ofWeeks_int() {
 128         assertPeriod(Period.ofWeeks(0), 0, 0, 0);
 129         assertPeriod(Period.ofWeeks(1), 0, 0, 7);
 130         assertPeriod(Period.ofWeeks(234), 0, 0, 234 * 7);
 131         assertPeriod(Period.ofWeeks(-100), 0, 0, -100 * 7);
 132         assertPeriod(Period.ofWeeks(Integer.MAX_VALUE / 7), 0, 0, (Integer.MAX_VALUE / 7) * 7);
 133         assertPeriod(Period.ofWeeks(Integer.MIN_VALUE / 7), 0, 0, (Integer.MIN_VALUE / 7) * 7);
 134     }
 135 
 136     //-----------------------------------------------------------------------
 137     // ofDays(int)
 138     //-----------------------------------------------------------------------
 139     @Test
 140     public void factory_ofDays_int() {
 141         assertPeriod(Period.ofDays(0), 0, 0, 0);
 142         assertPeriod(Period.ofDays(1), 0, 0, 1);
 143         assertPeriod(Period.ofDays(234), 0, 0, 234);
 144         assertPeriod(Period.ofDays(-100), 0, 0, -100);
 145         assertPeriod(Period.ofDays(Integer.MAX_VALUE), 0, 0, Integer.MAX_VALUE);
 146         assertPeriod(Period.ofDays(Integer.MIN_VALUE), 0, 0, Integer.MIN_VALUE);
 147     }
 148 
 149     //-----------------------------------------------------------------------
 150     // of(int3)
 151     //-----------------------------------------------------------------------
 152     @Test
 153     public void factory_of_ints() {
 154         assertPeriod(Period.of(1, 2, 3), 1, 2, 3);
 155         assertPeriod(Period.of(0, 2, 3), 0, 2, 3);
 156         assertPeriod(Period.of(1, 0, 0), 1, 0, 0);
 157         assertPeriod(Period.of(0, 0, 0), 0, 0, 0);
 158         assertPeriod(Period.of(-1, -2, -3), -1, -2, -3);
 159     }
 160 


 247                 {"P-0Y", Period.ofYears(0)},
 248                 {"P-25Y", Period.ofYears(-25)},
 249                 {"P-987654321Y", Period.ofYears(-987654321)},
 250                 {"P" + Integer.MAX_VALUE + "Y", Period.ofYears(Integer.MAX_VALUE)},
 251                 {"P" + Integer.MIN_VALUE + "Y", Period.ofYears(Integer.MIN_VALUE)},
 252 
 253                 {"P1M", Period.ofMonths(1)},
 254                 {"P12M", Period.ofMonths(12)},
 255                 {"P987654321M", Period.ofMonths(987654321)},
 256                 {"P+1M", Period.ofMonths(1)},
 257                 {"P+12M", Period.ofMonths(12)},
 258                 {"P+987654321M", Period.ofMonths(987654321)},
 259                 {"P+0M", Period.ofMonths(0)},
 260                 {"P0M", Period.ofMonths(0)},
 261                 {"P-0M", Period.ofMonths(0)},
 262                 {"P-25M", Period.ofMonths(-25)},
 263                 {"P-987654321M", Period.ofMonths(-987654321)},
 264                 {"P" + Integer.MAX_VALUE + "M", Period.ofMonths(Integer.MAX_VALUE)},
 265                 {"P" + Integer.MIN_VALUE + "M", Period.ofMonths(Integer.MIN_VALUE)},
 266 
 267                 {"P1W", Period.ofDays(1 * 7)},
 268                 {"P12W", Period.ofDays(12 * 7)},
 269                 {"P7654321W", Period.ofDays(7654321 * 7)},
 270                 {"P+1W", Period.ofDays(1 * 7)},
 271                 {"P+12W", Period.ofDays(12 * 7)},
 272                 {"P+7654321W", Period.ofDays(7654321 * 7)},
 273                 {"P+0W", Period.ofDays(0)},
 274                 {"P0W", Period.ofDays(0)},
 275                 {"P-0W", Period.ofDays(0)},
 276                 {"P-25W", Period.ofDays(-25 * 7)},
 277                 {"P-7654321W", Period.ofDays(-7654321 * 7)},
 278 
 279                 {"P1D", Period.ofDays(1)},
 280                 {"P12D", Period.ofDays(12)},
 281                 {"P987654321D", Period.ofDays(987654321)},
 282                 {"P+1D", Period.ofDays(1)},
 283                 {"P+12D", Period.ofDays(12)},
 284                 {"P+987654321D", Period.ofDays(987654321)},
 285                 {"P+0D", Period.ofDays(0)},
 286                 {"P0D", Period.ofDays(0)},
 287                 {"P-0D", Period.ofDays(0)},
 288                 {"P-25D", Period.ofDays(-25)},
 289                 {"P-987654321D", Period.ofDays(-987654321)},
 290                 {"P" + Integer.MAX_VALUE + "D", Period.ofDays(Integer.MAX_VALUE)},
 291                 {"P" + Integer.MIN_VALUE + "D", Period.ofDays(Integer.MIN_VALUE)},
 292 
 293                 {"P0Y0M0D", Period.of(0, 0, 0)},
 294                 {"P2Y0M0D", Period.of(2, 0, 0)},
 295                 {"P0Y3M0D", Period.of(0, 3, 0)},
 296                 {"P0Y0M4D", Period.of(0, 0, 4)},
 297                 {"P2Y3M25D", Period.of(2, 3, 25)},
 298                 {"P-2Y3M25D", Period.of(-2, 3, 25)},
 299                 {"P2Y-3M25D", Period.of(2, -3, 25)},
 300                 {"P2Y3M-25D", Period.of(2, 3, -25)},
 301                 {"P-2Y-3M-25D", Period.of(-2, -3, -25)},
 302 
 303                 {"P0Y0M0W0D", Period.of(0, 0, 0)},
 304                 {"P2Y3M4W25D", Period.of(2, 3, 4 * 7 + 25)},
 305                 {"P-2Y-3M-4W-25D", Period.of(-2, -3, -4 * 7 - 25)},
 306         };
 307     }
 308 
 309     @Test(dataProvider="parseSuccess")
 310     public void factory_parse(String text, Period expected) {
 311         Period p = Period.parse(text);
 312         assertEquals(p, expected);
 313     }
 314 
 315     @Test(dataProvider="parseSuccess")
 316     public void factory_parse_plus(String text, Period expected) {
 317         Period p = Period.parse("+" + text);
 318         assertEquals(p, expected);
 319     }
 320 
 321     @Test(dataProvider="parseSuccess")
 322     public void factory_parse_minus(String text, Period expected) {
 323         Period p = null;
 324         try {
 325             p = Period.parse("-" + text);


 346                 {"PTD"},
 347                 {"AT0D"},
 348                 {"PA0D"},
 349                 {"PT0A"},
 350 
 351                 {"PT+D"},
 352                 {"PT-D"},
 353                 {"PT.D"},
 354                 {"PTAD"},
 355 
 356                 {"PT+0D"},
 357                 {"PT-0D"},
 358                 {"PT+1D"},
 359                 {"PT-.D"},
 360 
 361                 {"P1Y1MT1D"},
 362                 {"P1YMD"},
 363                 {"P1Y2Y"},
 364                 {"PT1M+3S"},
 365 
 366                 {"P1M2Y"},
 367                 {"P1W2Y"},
 368                 {"P1D2Y"},
 369                 {"P1W2M"},
 370                 {"P1D2M"},
 371                 {"P1D2W"},
 372 
 373                 {"PT1S1"},
 374                 {"PT1S."},
 375                 {"PT1SA"},
 376                 {"PT1M1"},
 377                 {"PT1M."},
 378                 {"PT1MA"},
 379 
 380                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "Y"},
 381                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "M"},
 382                 {"P"+ (((long) Integer.MAX_VALUE) + 1) + "D"},
 383                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "Y"},
 384                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "M"},
 385                 {"P"+ (((long) Integer.MIN_VALUE) - 1) + "D"},
 386 
 387                 {"Rubbish"},
 388         };
 389     }
 390 
 391     @Test(dataProvider="parseFailure", expectedExceptions=DateTimeParseException.class)
 392     public void factory_parseFailures(String text) {