File: CalendarTestEngine.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (782 lines) | stat: -rw-r--r-- 30,173 bytes parent folder | download | duplicates (9)
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

public class CalendarTestEngine {
    private static File file;
    private static BufferedReader in;
    private static int testCount;
    private static int lineno;
    private static Locale locale;
    private static TimeZone timezone;
    private static boolean leniency = true;

    public static void main(String[] args) throws Exception {
        Locale loc = Locale.getDefault();
        TimeZone tz = TimeZone.getDefault();
        locale = loc;
        timezone = tz;
        try {
            for (String arg : args) {
                file = new File(arg);
                FileInputStream fis = new FileInputStream(file);
                System.out.println("Starting " + file.getName() + "...");
                in = new BufferedReader(new InputStreamReader(fis));
                testCount = lineno = 0;
                run();
                System.out.println("Completed " + file.getName());
            }
        } finally {
            Locale.setDefault(loc);
            TimeZone.setDefault(tz);
        }
    }

    private static void run() throws Exception {
        String line;
        int section = 0;
        Map<String, CalendarAdapter> cals = new HashMap<String, CalendarAdapter>();
        CalendarAdapter calendar = null;
        Result result = new Result();

        while ((line = in.readLine()) != null) {
            lineno++;
            line = line.trim();
            // Skip blank and comment lines
            if (line.length() == 0 || line.charAt(0) == '#') {
                continue;
            }
            int comment = line.indexOf('#');
            if (comment != -1) {
                line = line.substring(0, comment).trim();
            }
            Scanner sc = new Scanner(line);
            String token = sc.next();
            Symbol operation = symbol(token);
            if (operation == null) {
                throw new RuntimeException(lineno() + "wrong op? " + token);
            }


            if (operation.type == Symbol.Type.EXCEPTION) {
                String className = sc.next();
                Class clazz = Exceptions.get(className);
                Exception e = result.getException();
                if (!clazz.isInstance(e)) {
                    throw new RuntimeException(lineno() + "unexpected exception: got: " + e
                                               + ", expected=" + clazz);
                }
            }

            Exception x = result.getException();
            if (x != null) {
                throw new RuntimeException(lineno(result.getLineno()) + "Unexpected exception", x);
            }

            try {
                switch (operation.type) {
                case LOCALE:
                    {
                        String lang = sc.next();
                        String country = "", var = "";
                        if (sc.hasNext()) {
                            country = sc.next();
                            if (sc.hasNext()) {
                                var = sc.next();
                            }
                        }
                        locale = new Locale(lang, country, var);
                    }
                    break;

                case TIMEZONE:
                    {
                        if (sc.hasNext()) {
                            String id = sc.next();
                            timezone = TimeZone.getTimeZone(id);
                            if (!timezone.getID().equals(id)) {
                                System.err.printf("Warning: line %d: may get wrong time zone? "
                                                  +"(specified: %s vs. actual: %s)%n",
                                                  lineno, id, timezone.getID());
                            }
                        }
                    }
                    break;

                case NEW:
                    {
                        Symbol op = symbol(sc.next());
                        Calendar cal = null;
                        switch (op.type) {
                        case INSTANCE:
                            cal = Calendar.getInstance(timezone, locale);
                            break;
                        case GREGORIAN:
                            cal = new GregorianAdapter(timezone, locale);
                            break;
                        default:
                            symbolError(op);
                            break;
                        }
                        cal.setLenient(leniency);
                        calendar = new CalendarAdapter(cal);
                        if (sc.hasNext()) {
                            String name = sc.next();
                            cals.put(name.toLowerCase(Locale.ROOT), calendar);
                            if (!leniency) {
                                System.out.printf("%s%s is non-lenient%n", lineno(), name);
                            }
                        } else {
                            throw new RuntimeException(lineno() + "Missing associated name");
                        }
                    }
                    break;

                case TEST:
                    testCount++;
                    if (sc.hasNext()) {
                        System.out.printf("Test#%d:%s%n", testCount, sc.findInLine(".+"));
                    } else {
                        System.out.printf("Test#%d:%n", testCount);
                    }
                    break;

                case USE:
                    {
                        String name = sc.next().toLowerCase(Locale.ROOT);
                        CalendarAdapter c = cals.get(name);
                        if (c == null) {
                            throw new CalendarTestException(lineno() + "calendar " + name
                                                            + " not found.");
                        }
                        calendar = c;
                    }
                    break;

                case ASSIGN:
                    {
                        long v = getLong(sc);
                        String to = sc.next().toLowerCase(Locale.ROOT);
                        boolean assign = true;
                        if (sc.hasNext()) {
                            Symbol condition = symbol(sc.next());
                            if (condition.type == Symbol.Type.IF) {
                                long v1 = getLong(sc);
                                Symbol op = symbol(sc.next());
                                long v2 = getLong(sc);
                                assign = relation(v1, op, v2);
                            } else {
                                symbolError(condition);
                            }
                        }
                        if (assign)
                            Variable.newVar(to, v);
                    }
                    break;

                case EVAL:
                    {
                        long v1 = getLong(sc);
                        String op = sc.next();
                        Symbol operator = symbol(op);
                        if (operator == null) {
                            throw new RuntimeException("op " + op + " invalid");
                        }
                        long v2 = getLong(sc);
                        if (operator.isArithmetic()) {
                            long value = 0;
                            switch (operator.type) {
                            case PLUS:
                                value = v1 + v2;
                                break;

                            case MINUS:
                                value = v1 - v2;
                                break;

                            case MULTIPLY:
                                value = v1 * v2;
                                break;

                            case DIVIDE:
                                value = v1 / v2;
                                break;

                            case MOD:
                                value = v1 % v2;
                                break;

                            default:
                                symbolError(operator);
                                break;
                            }
                            result.setValue(value);
                        } else {
                            if (!relation(v1, operator, v2)) {
                                throw new RuntimeException("not " + v1 + " " + op + " " + v2);
                            }
                        }
                    }
                    break;

                case CLEAR:
                    {
                        Symbol sym = symbol(sc.next());
                        if (sym.type == Symbol.Type.ALL) {
                            calendar.clearAll();
                        } else if (sym.type == Symbol.Type.FIELD) {
                            int f = sym.value();
                            calendar.clearField(f);
                        } else {
                            symbolError(sym);
                        }
                    }
                    break;

                case GET:
                    {
                        Symbol sym = symbol(sc.next());
                        switch (sym.type) {
                        case FIELD:
                            {
                                int f = sym.value();
                                int v = calendar.get(f);
                                result.setValue(v);
                            }
                            break;

                        case MILLIS:
                            {
                                long v = calendar.getTimeInMillis();
                                result.setValue(v);
                            }
                            break;

                        case MINIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getMinimum(f);
                                result.setValue(v);
                            }
                            break;

                        case GREATESTMINIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getGreatestMinimum(f);
                                result.setValue(v);
                            }
                            break;

                        case ACTUALMINIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getActualMinimum(f);
                                result.setValue(v);
                            }
                            break;

                        case MAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getMaximum(f);
                                result.setValue(v);
                            }
                            break;

                        case LEASTMAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getLeastMaximum(f);
                                result.setValue(v);
                            }
                            break;

                        case ACTUALMAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = calendar.getActualMaximum(f);
                                result.setValue(v);
                            }
                            break;

                        case FIRSTDAYOFWEEK:
                            {
                                result.setValue(calendar.getFirstDayOfWeek());
                            }
                            break;

                        case MINIMALDAYSINFIRSTWEEK:
                            {
                                int v = calendar.getMinimalDaysInFirstWeek();
                                result.setValue(v);
                            }
                            break;

                        default:
                            symbolError(sym);
                            break;
                        }
                    }
                    break;

                case ADD:
                case ROLL:
                    {
                        Symbol sym = symbol(sc.next());
                        if (sym.type == Symbol.Type.FIELD) {
                            int f = sym.value();
                            int v = sc.nextInt();
                            switch (operation.type) {
                            case ADD:
                                calendar.add(f, v);
                                break;

                            case ROLL:
                                calendar.roll(f, v);
                                break;
                            }
                        } else {
                            symbolError(sym);
                        }
                    }
                    break;

                case SET:
                    {
                        Symbol sym = symbol(sc.next());
                        switch (sym.type) {
                        case FIELD:
                            {
                                int f = sym.value();
                                int v = getInt(sc);
                                calendar.set(f, v);
                            }
                            break;

                        case MILLIS:
                            {
                                long v = getLong(sc);
                                calendar.setTimeInMillis(v);
                            }
                            break;

                        case DATE:
                            {
                                int a = getInt(sc);
                                int b = getInt(sc);
                                int c = getInt(sc);
                                if (sc.hasNext()) {
                                    int d = getInt(sc);
                                    // era, year, month, dayOfMonth
                                    calendar.setDate(a, b, c, d);
                                } else {
                                    // year, month, dayOfMonth
                                    calendar.setDate(a, b, c);
                                }
                            }
                            break;

                        case DATETIME:
                            {
                                int y = getInt(sc);
                                int m = getInt(sc);
                                int d = getInt(sc);
                                int hh = getInt(sc);
                                int mm = getInt(sc);
                                int ss = getInt(sc);
                                calendar.setDateTime(y, m, d, hh, mm, ss);
                            }
                            break;

                        case TIMEOFDAY:
                            {
                                int hh = getInt(sc);
                                int mm = getInt(sc);
                                int ss = getInt(sc);
                                int ms = getInt(sc);
                                calendar.setTimeOfDay(hh, mm, ss, ms);
                            }
                            break;

                        case FIRSTDAYOFWEEK:
                            {
                                int v = getInt(sc);
                                calendar.setFirstDayOfWeek(v);
                            }
                            break;

                        case MINIMALDAYSINFIRSTWEEK:
                            {
                                int v = getInt(sc);
                                calendar.setMinimalDaysInFirstWeek(v);
                            }
                            break;

                        case LENIENT:
                            if (calendar != null) {
                                calendar.setLenient(true);
                            }
                            leniency = true;
                            break;

                        case NONLENIENT:
                            if (calendar != null) {
                                calendar.setLenient(false);
                            }
                            leniency = false;
                            break;

                        default:
                            symbolError(sym);
                        }
                        if (sc.hasNext()) {
                            throw new RuntimeException(lineno() + "extra param(s) "
                                                       + sc.findInLine(".+"));
                        }
                    }
                    break;

                case CHECK:
                    {
                        Symbol sym = symbol(sc.next());
                        boolean stat = false;
                        switch (sym.type) {
                        case MILLIS:
                            {
                                long millis = getLong(sc);
                                stat = calendar.checkMillis(millis);
                            }
                            break;

                        case FIELD:
                            {
                                int f = sym.value();
                                int v = getInt(sc);
                                stat = calendar.checkField(f, v);
                            }
                            break;

                        case DATE:
                            {
                                int a = getInt(sc);
                                int b = getInt(sc);
                                int c = getInt(sc);
                                if (sc.hasNext()) {
                                    int d = getInt(sc);
                                    // era year month dayOfMonth
                                    stat = calendar.checkDate(a, b, c, d);
                                } else {
                                    // year month dayOfMonth
                                    stat = calendar.checkDate(a, b, c);
                                }
                            }
                            break;

                        case DATETIME:
                            {
                                int y = getInt(sc);
                                int m = getInt(sc);
                                int d = getInt(sc);
                                int hh = getInt(sc);
                                int mm = getInt(sc);
                                int ss = getInt(sc);
                                if (sc.hasNext()) {
                                    int ms = getInt(sc);
                                    stat = calendar.checkDateTime(y, m, d, hh, mm, ss, ms);
                                } else {
                                    stat = calendar.checkDateTime(y, m, d, hh, mm, ss);
                                }
                            }
                            break;

                        case TIMEOFDAY:
                            {
                                int hh = sc.nextInt();
                                int mm = sc.nextInt();
                                int ss = sc.nextInt();
                                int millis = sc.nextInt();
                                stat = calendar.checkTimeOfDay(hh, mm, ss, millis);
                            }
                            break;

                        case MINIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkMinimum(f, v);
                            }
                            break;

                        case GREATESTMINIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkGreatestMinimum(f, v);
                            }
                            break;

                        case ACTUALMINIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkActualMinimum(f, v);
                            }
                            break;

                        case MAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkMaximum(f, v);
                            }
                            break;

                        case LEASTMAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkLeastMaximum(f, v);
                            }
                            break;

                        case ACTUALMAXIMUM:
                            {
                                int f = getInt(sc);
                                int v = getInt(sc);
                                stat = calendar.checkActualMaximum(f, v);
                            }
                            break;

                        default:
                            throw new RuntimeException(lineno() + "Unknown operand");
                        }
                        if (!stat) {
                            throw new RuntimeException(lineno() + calendar.getMessage());
                        }
                    }
                    break;

                case PRINT:
                    {
                        String s = sc.next();
                        if (s.charAt(0) == '$') {
                            Variable var = variable(s);
                            if (var == null)
                                throw new RuntimeException(lineno() + "Unknown token: " + s);
                            System.out.printf("%s%s=%d%n", lineno(), s, var.longValue());
                            break;
                        }

                        Symbol sym = symbol(s);
                        switch (sym.type) {
                        case INSTANCE:
                            {
                                Calendar cal = calendar;
                                String name = "current";
                                if (sc.hasNext()) {
                                    name = sc.next();
                                    cal = cals.get(name.toLowerCase(Locale.ROOT));
                                }
                                System.out.printf("%s%s=%s%n", lineno(), name, cal);
                            }
                            break;

                        case FIELD:
                            {
                                int f = sym.value();
                                String remark = "";
                                if (sc.hasNext()) {
                                    remark = sc.findInLine(".+");
                                }
                                System.out.printf("%s%s=%d %s%n", lineno(), calendar.fieldName(f),
                                                  calendar.get(f), remark);
                            }
                            break;

                        case MILLIS:
                            {
                                String remark = "";
                                if (sc.hasNext()) {
                                    remark = sc.findInLine(".+");
                                }
                                System.out.printf("%sMillis=%d %s%n", lineno(),
                                                  calendar.getTimeInMillis(), remark);
                            }
                            break;

                        case MINIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getMinimum(getInt(sc)));
                            break;

                        case GREATESTMINIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getGreatestMinimum(getInt(sc)));
                            break;

                        case ACTUALMINIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getActualMinimum(getInt(sc)));
                            break;

                        case MAXIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getMaximum(getInt(sc)));
                            break;

                        case LEASTMAXIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getLeastMaximum(getInt(sc)));
                            break;

                        case ACTUALMAXIMUM:
                            System.out.printf("%s%s=%d%n", lineno(),
                                              s, calendar.getActualMaximum(getInt(sc)));
                            break;

                        case DATE:
                            System.out.println(lineno() + calendar.toDateString());
                            break;

                        case DATETIME:
                            System.out.println(lineno() + calendar.toDateTimeString());
                            break;

                        case TIMEZONE:
                            System.out.println(lineno() + "timezone=" + timezone);
                            break;

                        case LOCALE:
                            System.out.println(lineno() + "locale=" + locale);
                            break;
                        }
                    }
                }
            } catch (CalendarTestException cte) {
                throw cte;
            } catch (NoSuchElementException nsee) {
                throw new NoSuchElementException(lineno() + "syntax error");
            } catch (Exception e) {
                result.setException(e);
                result.setLineno(lineno);
            }
        }
        Exception x = result.getException();
        if (x != null) {
            throw new RuntimeException(lineno(result.getLineno()) + "Unexpected exception", x);
        }
    }

    private static Symbol symbol(String s) {
        return Symbol.get(s.toLowerCase(Locale.ROOT));
    }

    private static Variable variable(String s) {
        return Variable.get(s.toLowerCase(Locale.ROOT));
    }

    private static int getInt(Scanner sc) {
        if (sc.hasNextInt()) {
            return sc.nextInt();
        }

        String s = sc.next();
        if (s.charAt(0) == '$') {
            Variable var = variable(s);
            if (var == null)
                throw new RuntimeException(lineno() + "Unknown token: " + s);
            return var.intValue();
        }
        Symbol sym = symbol(s);
        if (sym == null)
            throw new RuntimeException(lineno() + "Unknown token: " + s);
        return sym.value();
    }

    private static long getLong(Scanner sc) {
        if (sc.hasNextLong()) {
            return sc.nextLong();
        }

        String s = sc.next();
        if (s.charAt(0) == '$') {
            Variable var = variable(s);
            if (var == null)
                throw new RuntimeException(lineno() + "Unknown token: " + s);
            return var.longValue();
        }
        Symbol sym = symbol(s);
        if (sym == null)
            throw new RuntimeException(lineno() + "Unknown token: " + s);
        return sym.value();
    }

    private static boolean relation(long v1, Symbol relop, long v2) {
        boolean result = false;
        switch (relop.type) {
        case GT:
            result = v1 > v2;
            break;

        case GE:
            result = v1 >= v2;
            break;

        case EQ:
            result = v1 == v2;
            break;

        case NEQ:
            result = v1 != v2;
            break;

        case LE:
            result = v1 <= v2;
            break;

        case LT:
            result = v1 < v2;
            break;
        }
        return result;
    }

    private static String lineno() {
        return lineno(lineno);
    }

    private static String lineno(int ln) {
        return file.getName() + ":" + ln + ": ";
    }

    private static void symbolError(Symbol sym) {
        throw new RuntimeException(lineno + ": unexpected symbol: " + sym);
    }
}