1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
34 import java.math.BigDecimal;
35 import java.math.BigInteger;
36 import java.text.DateFormatSymbols;
37 import java.util.*;
38 #if[double]
39 import sun.misc.DoubleConsts;
40 #end[double]
41
42 import static java.util.Calendar.*;
43 #if[datetime]
44 import static java.util.SimpleTimeZone.*;
45 import java.util.regex.Pattern;
46 #end[datetime]
47
48 public class Basic$Type$ extends Basic {
49
50 private static void test(String fs, String exp, Object ... args) {
51 Formatter f = new Formatter(new StringBuilder(), Locale.US);
52 f.format(fs, args);
53 ck(fs, exp, f.toString());
54 }
55
56 private static void test(Locale l, String fs, String exp, Object ... args)
57 {
58 Formatter f = new Formatter(new StringBuilder(), l);
59 f.format(fs, args);
60 ck(fs, exp, f.toString());
61 }
62
63 private static void test(String fs, Object ... args) {
64 Formatter f = new Formatter(new StringBuilder(), Locale.US);
65 f.format(fs, args);
66 ck(fs, "fail", f.toString());
67 }
68
69 private static void test(String fs) {
70 Formatter f = new Formatter(new StringBuilder(), Locale.US);
71 f.format(fs, "fail");
72 ck(fs, "fail", f.toString());
73 }
74
75 private static void testSysOut(String fs, String exp, Object ... args) {
76 FileOutputStream fos = null;
77 FileInputStream fis = null;
78 try {
79 PrintStream saveOut = System.out;
80 fos = new FileOutputStream("testSysOut");
555 #else[byte]
556 #if[short]
557 //---------------------------------------------------------------------
558 // %d - short
559 //---------------------------------------------------------------------
560 $type$ oneToFive = ($type$) 12345;
561 test("%d", "12345", oneToFive);
562 test("%,d", "12,345", oneToFive);
563 test("%,d", "-12,345", negate(oneToFive));
564 test("%(d", "12345", oneToFive);
565 test("%(d", "(12345)", negate(oneToFive));
566 test("% d", " 12345", oneToFive);
567 test("% d", "-12345", negate(oneToFive));
568 test("%+d", "+12345", oneToFive);
569 test("%+d", "-12345", negate(oneToFive));
570 test("%010d", "0000012345", oneToFive);
571 test("%010d", "-000012345", negate(oneToFive));
572 test("%(10d", " (12345)", negate(oneToFive));
573 test("%-10d", "12345 ", oneToFive);
574 test("%-10d", "-12345 ", negate(oneToFive));
575
576 #else[short]
577 #if[prim]
578 //---------------------------------------------------------------------
579 // %d - int and long
580 //---------------------------------------------------------------------
581 $type$ oneToSeven = ($type$) 1234567;
582 test("%d", "1234567", oneToSeven);
583 test("%,d", "1,234,567", oneToSeven);
584 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven);
585 test("%,d", "-1,234,567", negate(oneToSeven));
586 test("%(d", "1234567", oneToSeven);
587 test("%(d", "(1234567)", negate(oneToSeven));
588 test("% d", " 1234567", oneToSeven);
589 test("% d", "-1234567", negate(oneToSeven));
590 test("%+d", "+1234567", oneToSeven);
591 test("%+d", "-1234567", negate(oneToSeven));
592 test("%010d", "0001234567", oneToSeven);
593 test("%010d", "-001234567", negate(oneToSeven));
594 test("%(10d", " (1234567)", negate(oneToSeven));
595 test("%-10d", "1234567 ", oneToSeven);
596 test("%-10d", "-1234567 ", negate(oneToSeven));
597 #end[prim]
598 #end[short]
599 #end[byte]
600 //---------------------------------------------------------------------
601 // %d - errors
602 //---------------------------------------------------------------------
603 tryCatch("%#d", FormatFlagsConversionMismatchException.class);
604 tryCatch("%D", UnknownFormatConversionException.class);
605 tryCatch("%0d", MissingFormatWidthException.class);
606 tryCatch("%-d", MissingFormatWidthException.class);
607 tryCatch("%7.3d", IllegalFormatPrecisionException.class);
608
609 //---------------------------------------------------------------------
610 // %o
611 //
612 // Numeric conversion applicable to byte, short, int, long, and
613 // BigInteger.
614 //---------------------------------------------------------------------
615 test("%o", "null", (Object)null);
616
764 // %d - BigInteger
765 //---------------------------------------------------------------------
766 test("%d", "null", (Object)null);
767 test("%d", "1234567", new BigInteger("1234567", 10));
768 test("%,d", "1,234,567", new BigInteger("1234567", 10));
769 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", new BigInteger("1234567", 10));
770 test("%,d", "-1,234,567", new BigInteger("-1234567", 10));
771 test("%(d", "1234567", new BigInteger("1234567", 10));
772 test("%(d", "(1234567)", new BigInteger("-1234567", 10));
773 test("% d", " 1234567", new BigInteger("1234567", 10));
774 test("% d", "-1234567", new BigInteger("-1234567", 10));
775 test("%+d", "+1234567", new BigInteger("1234567", 10));
776 test("%+d", "-1234567", new BigInteger("-1234567", 10));
777 test("%010d", "0001234567", new BigInteger("1234567", 10));
778 test("%010d", "-001234567", new BigInteger("-1234567", 10));
779 test("%(10d", " (1234567)", new BigInteger("-1234567", 10));
780 test("%+d", "+1234567", new BigInteger("1234567", 10));
781 test("%+d", "-1234567", new BigInteger("-1234567", 10));
782 test("%-10d", "1234567 ", new BigInteger("1234567", 10));
783 test("%-10d", "-1234567 ", new BigInteger("-1234567", 10));
784
785 //---------------------------------------------------------------------
786 // %o - BigInteger
787 //---------------------------------------------------------------------
788 test("%o", "null", (Object)null);
789 test("%o", "1234567", new BigInteger("1234567", 8));
790 test("%(o", "1234567", new BigInteger("1234567", 8));
791 test("%(o", "(1234567)", new BigInteger("-1234567", 8));
792 test("% o", " 1234567", new BigInteger("1234567", 8));
793 test("% o", "-1234567", new BigInteger("-1234567", 8));
794 test("%+o", "+1234567", new BigInteger("1234567", 8));
795 test("%+o", "-1234567", new BigInteger("-1234567", 8));
796 test("%010o", "0001234567", new BigInteger("1234567", 8));
797 test("%010o", "-001234567", new BigInteger("-1234567", 8));
798 test("%(10o", " (1234567)", new BigInteger("-1234567", 8));
799 test("%+o", "+1234567", new BigInteger("1234567", 8));
800 test("%+o", "-1234567", new BigInteger("-1234567", 8));
801 test("%-10o", "1234567 ", new BigInteger("1234567", 8));
802 test("%-10o", "-1234567 ", new BigInteger("-1234567", 8));
803 test("%#10o", " 01234567", new BigInteger("1234567", 8));
1022 test("%3.0f", " 0", 0.000001);
1023 test("%3.0f", " 0", 0.00001);
1024 test("%3.0f", " 0", 0.0001);
1025 test("%3.0f", " 0", 0.001);
1026 test("%3.0f", " 0", 0.01);
1027 test("%3.0f", " 0", 0.1);
1028 test("%3.0f", " 1", 0.9);
1029 test("%3.1f", "0.9", 0.9);
1030 test("%3.0f", " 1", 1.00);
1031 test("%3.0f", " 10", 10.00);
1032 test("%3.0f", " 99", 99.19);
1033 test("%3.1f", "99.2", 99.19);
1034 test("%3.0f", "100", 99.99);
1035 test("%3.0f", "100", 100.00);
1036 test("%#3.0f", "1000.", 1000.00);
1037 test("%3.0f", "10000", 10000.00);
1038 test("%3.0f", "100000", 100000.00);
1039 test("%3.0f", "1000000", 1000000.00);
1040 test("%3.0f", "10000000", 10000000.00);
1041 test("%3.0f", "100000000", 100000000.00);
1042 #if[BigDecimal]
1043 //---------------------------------------------------------------------
1044 // %f - BigDecimal
1045 //---------------------------------------------------------------------
1046 test("%4.0f", " 99", new BigDecimal("99.19"));
1047 test("%4.1f", "99.2", new BigDecimal("99.19"));
1048
1049 BigDecimal val = new BigDecimal("99.95");
1050 test("%4.0f", " 100", val);
1051 test("%#4.0f", "100.", val);
1052 test("%4.1f", "100.0", val);
1053 test("%4.2f", "99.95", val);
1054 test("%4.3f", "99.950", val);
1055
1056 val = new BigDecimal(".99");
1057 test("%4.1f", " 1.0", val);
1058 test("%4.2f", "0.99", val);
1059 test("%4.3f", "0.990", val);
1060
1061 // #6476425
|
1 /*
2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
34 import java.math.BigDecimal;
35 import java.math.BigInteger;
36 import java.text.DateFormatSymbols;
37 import java.util.*;
38 #if[double]
39 import sun.misc.DoubleConsts;
40 #end[double]
41
42 import static java.util.Calendar.*;
43 #if[datetime]
44 import static java.util.SimpleTimeZone.*;
45 import java.util.regex.Pattern;
46 #end[datetime]
47
48 public class Basic$Type$ extends Basic {
49
50 private static void test(String fs, String exp, Object ... args) {
51 Formatter f = new Formatter(new StringBuilder(), Locale.US);
52 f.format(fs, args);
53 ck(fs, exp, f.toString());
54
55 f = new Formatter(new StringBuilder(), Locale.US);
56 f.format("foo " + fs + " bar", args);
57 ck(fs, "foo " + exp + " bar", f.toString());
58 }
59
60 private static void test(Locale l, String fs, String exp, Object ... args)
61 {
62 Formatter f = new Formatter(new StringBuilder(), l);
63 f.format(fs, args);
64 ck(fs, exp, f.toString());
65
66 f = new Formatter(new StringBuilder(), l);
67 f.format("foo " + fs + " bar", args);
68 ck(fs, "foo " + exp + " bar", f.toString());
69 }
70
71 private static void test(String fs, Object ... args) {
72 Formatter f = new Formatter(new StringBuilder(), Locale.US);
73 f.format(fs, args);
74 ck(fs, "fail", f.toString());
75 }
76
77 private static void test(String fs) {
78 Formatter f = new Formatter(new StringBuilder(), Locale.US);
79 f.format(fs, "fail");
80 ck(fs, "fail", f.toString());
81 }
82
83 private static void testSysOut(String fs, String exp, Object ... args) {
84 FileOutputStream fos = null;
85 FileInputStream fis = null;
86 try {
87 PrintStream saveOut = System.out;
88 fos = new FileOutputStream("testSysOut");
563 #else[byte]
564 #if[short]
565 //---------------------------------------------------------------------
566 // %d - short
567 //---------------------------------------------------------------------
568 $type$ oneToFive = ($type$) 12345;
569 test("%d", "12345", oneToFive);
570 test("%,d", "12,345", oneToFive);
571 test("%,d", "-12,345", negate(oneToFive));
572 test("%(d", "12345", oneToFive);
573 test("%(d", "(12345)", negate(oneToFive));
574 test("% d", " 12345", oneToFive);
575 test("% d", "-12345", negate(oneToFive));
576 test("%+d", "+12345", oneToFive);
577 test("%+d", "-12345", negate(oneToFive));
578 test("%010d", "0000012345", oneToFive);
579 test("%010d", "-000012345", negate(oneToFive));
580 test("%(10d", " (12345)", negate(oneToFive));
581 test("%-10d", "12345 ", oneToFive);
582 test("%-10d", "-12345 ", negate(oneToFive));
583 // , variations:
584 test("% ,d", " 12,345", oneToFive);
585 test("% ,d", "-12,345", negate(oneToFive));
586 test("%0,10d", "000012,345", oneToFive);
587 test("%0,10d", "-00012,345", negate(oneToFive));
588 test("%(,10d", " (12,345)", negate(oneToFive));
589 test("%-,10d", "12,345 ", oneToFive);
590 test("%-,10d", "-12,345 ", negate(oneToFive));
591
592 #else[short]
593 #if[prim]
594 //---------------------------------------------------------------------
595 // %d - int and long
596 //---------------------------------------------------------------------
597 $type$ oneToSeven = ($type$) 1234567;
598 test("%d", "1234567", oneToSeven);
599 test("%,d", "1,234,567", oneToSeven);
600 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", oneToSeven);
601 test("%,d", "-1,234,567", negate(oneToSeven));
602 test("%(d", "1234567", oneToSeven);
603 test("%(d", "(1234567)", negate(oneToSeven));
604 test("% d", " 1234567", oneToSeven);
605 test("% d", "-1234567", negate(oneToSeven));
606 test("%+d", "+1234567", oneToSeven);
607 test("%+d", "-1234567", negate(oneToSeven));
608 test("%010d", "0001234567", oneToSeven);
609 test("%010d", "-001234567", negate(oneToSeven));
610 test("%(10d", " (1234567)", negate(oneToSeven));
611 test("%-10d", "1234567 ", oneToSeven);
612 test("%-10d", "-1234567 ", negate(oneToSeven));
613 // , variations:
614 test("% ,d", " 1,234,567", oneToSeven);
615 test("% ,d", "-1,234,567", negate(oneToSeven));
616 test("%+,10d", "+1,234,567", oneToSeven);
617 test("%0,10d", "01,234,567", oneToSeven);
618 test("%0,10d", "-1,234,567", negate(oneToSeven));
619 test("%(,10d", "(1,234,567)", negate(oneToSeven));
620 test("%-,10d", "1,234,567 ", oneToSeven);
621 test("%-,10d", "-1,234,567", negate(oneToSeven));
622
623 #end[prim]
624 #end[short]
625 #end[byte]
626 //---------------------------------------------------------------------
627 // %d - errors
628 //---------------------------------------------------------------------
629 tryCatch("%#d", FormatFlagsConversionMismatchException.class);
630 tryCatch("%D", UnknownFormatConversionException.class);
631 tryCatch("%0d", MissingFormatWidthException.class);
632 tryCatch("%-d", MissingFormatWidthException.class);
633 tryCatch("%7.3d", IllegalFormatPrecisionException.class);
634
635 //---------------------------------------------------------------------
636 // %o
637 //
638 // Numeric conversion applicable to byte, short, int, long, and
639 // BigInteger.
640 //---------------------------------------------------------------------
641 test("%o", "null", (Object)null);
642
790 // %d - BigInteger
791 //---------------------------------------------------------------------
792 test("%d", "null", (Object)null);
793 test("%d", "1234567", new BigInteger("1234567", 10));
794 test("%,d", "1,234,567", new BigInteger("1234567", 10));
795 test(Locale.FRANCE, "%,d", "1\u00a0234\u00a0567", new BigInteger("1234567", 10));
796 test("%,d", "-1,234,567", new BigInteger("-1234567", 10));
797 test("%(d", "1234567", new BigInteger("1234567", 10));
798 test("%(d", "(1234567)", new BigInteger("-1234567", 10));
799 test("% d", " 1234567", new BigInteger("1234567", 10));
800 test("% d", "-1234567", new BigInteger("-1234567", 10));
801 test("%+d", "+1234567", new BigInteger("1234567", 10));
802 test("%+d", "-1234567", new BigInteger("-1234567", 10));
803 test("%010d", "0001234567", new BigInteger("1234567", 10));
804 test("%010d", "-001234567", new BigInteger("-1234567", 10));
805 test("%(10d", " (1234567)", new BigInteger("-1234567", 10));
806 test("%+d", "+1234567", new BigInteger("1234567", 10));
807 test("%+d", "-1234567", new BigInteger("-1234567", 10));
808 test("%-10d", "1234567 ", new BigInteger("1234567", 10));
809 test("%-10d", "-1234567 ", new BigInteger("-1234567", 10));
810 // , variations:
811 test("%0,10d", "01,234,567", new BigInteger("1234567", 10));
812 test("%0,10d", "-1,234,567", new BigInteger("-1234567", 10));
813 test("%(,10d", "(1,234,567)", new BigInteger("-1234567", 10));
814 test("%+,d", "+1,234,567", new BigInteger("1234567", 10));
815 test("%+,d", "-1,234,567", new BigInteger("-1234567", 10));
816 test("%-,10d", "1,234,567 ", new BigInteger("1234567", 10));
817 test("%-,10d", "-1,234,567", new BigInteger("-1234567", 10));
818
819 //---------------------------------------------------------------------
820 // %o - BigInteger
821 //---------------------------------------------------------------------
822 test("%o", "null", (Object)null);
823 test("%o", "1234567", new BigInteger("1234567", 8));
824 test("%(o", "1234567", new BigInteger("1234567", 8));
825 test("%(o", "(1234567)", new BigInteger("-1234567", 8));
826 test("% o", " 1234567", new BigInteger("1234567", 8));
827 test("% o", "-1234567", new BigInteger("-1234567", 8));
828 test("%+o", "+1234567", new BigInteger("1234567", 8));
829 test("%+o", "-1234567", new BigInteger("-1234567", 8));
830 test("%010o", "0001234567", new BigInteger("1234567", 8));
831 test("%010o", "-001234567", new BigInteger("-1234567", 8));
832 test("%(10o", " (1234567)", new BigInteger("-1234567", 8));
833 test("%+o", "+1234567", new BigInteger("1234567", 8));
834 test("%+o", "-1234567", new BigInteger("-1234567", 8));
835 test("%-10o", "1234567 ", new BigInteger("1234567", 8));
836 test("%-10o", "-1234567 ", new BigInteger("-1234567", 8));
837 test("%#10o", " 01234567", new BigInteger("1234567", 8));
1056 test("%3.0f", " 0", 0.000001);
1057 test("%3.0f", " 0", 0.00001);
1058 test("%3.0f", " 0", 0.0001);
1059 test("%3.0f", " 0", 0.001);
1060 test("%3.0f", " 0", 0.01);
1061 test("%3.0f", " 0", 0.1);
1062 test("%3.0f", " 1", 0.9);
1063 test("%3.1f", "0.9", 0.9);
1064 test("%3.0f", " 1", 1.00);
1065 test("%3.0f", " 10", 10.00);
1066 test("%3.0f", " 99", 99.19);
1067 test("%3.1f", "99.2", 99.19);
1068 test("%3.0f", "100", 99.99);
1069 test("%3.0f", "100", 100.00);
1070 test("%#3.0f", "1000.", 1000.00);
1071 test("%3.0f", "10000", 10000.00);
1072 test("%3.0f", "100000", 100000.00);
1073 test("%3.0f", "1000000", 1000000.00);
1074 test("%3.0f", "10000000", 10000000.00);
1075 test("%3.0f", "100000000", 100000000.00);
1076 test("%10.0f", " 1000000", 1000000.00);
1077 test("%,10.0f", " 1,000,000", 1000000.00);
1078 test("%,10.1f", "1,000,000.0", 1000000.00);
1079 test("%,3.0f", "1,000,000", 1000000.00);
1080 test("%,3.0f", "10,000,000", 10000000.00);
1081 test("%,3.0f", "100,000,000", 100000000.00);
1082 test("%,3.0f", "10,000,000", 10000000.00);
1083 test("%,3.0f", "100,000,000", 100000000.00);
1084 #if[BigDecimal]
1085 //---------------------------------------------------------------------
1086 // %f - BigDecimal
1087 //---------------------------------------------------------------------
1088 test("%4.0f", " 99", new BigDecimal("99.19"));
1089 test("%4.1f", "99.2", new BigDecimal("99.19"));
1090
1091 BigDecimal val = new BigDecimal("99.95");
1092 test("%4.0f", " 100", val);
1093 test("%#4.0f", "100.", val);
1094 test("%4.1f", "100.0", val);
1095 test("%4.2f", "99.95", val);
1096 test("%4.3f", "99.950", val);
1097
1098 val = new BigDecimal(".99");
1099 test("%4.1f", " 1.0", val);
1100 test("%4.2f", "0.99", val);
1101 test("%4.3f", "0.990", val);
1102
1103 // #6476425
|