1--TEST--
2numfmt_get/set_attribute()
3--SKIPIF--
4<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
5<?php if(version_compare(INTL_ICU_VERSION, '4.2', '<') != 1) print 'skip for ICU 4.4+'; ?>
6--FILE--
7<?php
8
9/*
10 * Get/set various number formatting attributes.
11 */
12
13
14function ut_main()
15{
16    // attr_name => array( attr, value )
17    $attributes = array(
18        'PARSE_INT_ONLY' => array( NumberFormatter::PARSE_INT_ONLY, 1, 12345.123456 ),
19        'GROUPING_USED' => array( NumberFormatter::GROUPING_USED, 0, 12345.123456 ),
20        'DECIMAL_ALWAYS_SHOWN' => array( NumberFormatter::DECIMAL_ALWAYS_SHOWN, 1, 12345 ),
21        'MAX_INTEGER_DIGITS' => array( NumberFormatter::MAX_INTEGER_DIGITS, 2, 12345.123456 ),
22        'MIN_INTEGER_DIGITS' => array( NumberFormatter::MIN_INTEGER_DIGITS, 20, 12345.123456 ),
23        'INTEGER_DIGITS' => array( NumberFormatter::INTEGER_DIGITS, 7, 12345.123456 ),
24        'MAX_FRACTION_DIGITS' => array( NumberFormatter::MAX_FRACTION_DIGITS, 2, 12345.123456 ),
25        'MIN_FRACTION_DIGITS' => array( NumberFormatter::MIN_FRACTION_DIGITS, 20, 12345.123456 ),
26        'FRACTION_DIGITS' => array( NumberFormatter::FRACTION_DIGITS, 5, 12345.123456 ),
27        'MULTIPLIER' => array( NumberFormatter::MULTIPLIER, 2, 12345.123456 ),
28        'GROUPING_SIZE' => array( NumberFormatter::GROUPING_SIZE, 2, 12345.123456 ),
29        'ROUNDING_MODE' => array( NumberFormatter::ROUNDING_MODE, 1, 12345.123456 ),
30        'ROUNDING_INCREMENT' => array( NumberFormatter::ROUNDING_INCREMENT, (float)2, 12345.123456 ),
31        'FORMAT_WIDTH' => array( NumberFormatter::FORMAT_WIDTH, 27, 12345.123456 ),
32        'PADDING_POSITION' => array( NumberFormatter::PADDING_POSITION, 2, 12345.123456 ),
33        'SECONDARY_GROUPING_SIZE' => array( NumberFormatter::SECONDARY_GROUPING_SIZE, 2, 12345.123456 ),
34        'SIGNIFICANT_DIGITS_USED' => array( NumberFormatter::SIGNIFICANT_DIGITS_USED, 1, 12345.123456 ),
35        'MIN_SIGNIFICANT_DIGITS' => array( NumberFormatter::MIN_SIGNIFICANT_DIGITS, 3, 1 ),
36        'MAX_SIGNIFICANT_DIGITS' => array( NumberFormatter::MAX_SIGNIFICANT_DIGITS, 4, 12345.123456 ),
37        // 'LENIENT_PARSE' => array( NumberFormatter::LENIENT_PARSE, 2, 12345.123456 )
38    );
39
40    $res_str = '';
41
42    $fmt = ut_nfmt_create( "en_US", NumberFormatter::DECIMAL );
43
44    foreach( $attributes as $attr_name => $args )
45    {
46        list( $attr, $new_val, $number ) = $args;
47        $res_str .= "\nAttribute $attr_name\n";
48
49        // Get original value of the attribute.
50        $orig_val = ut_nfmt_get_attribute( $fmt, $attr );
51
52        // Format the number using the original attribute value.
53        $rc = ut_nfmt_format( $fmt, $number );
54
55        $ps = ut_nfmt_parse( $fmt, $rc );
56
57        $res_str .= sprintf( "Old attribute value: %s ;  Format result: %s ; Parse result: %s\n",
58                             dump( $orig_val ),
59                             dump( $rc ),
60                             dump( $ps ) );
61
62        // Set new attribute value.
63        $rc = ut_nfmt_set_attribute( $fmt, $attr, $new_val );
64        if( $rc )
65            $res_str .= "Setting attribute: ok\n";
66        else
67            $res_str .= sprintf( "Setting attribute failed: %s\n", ut_nfmt_get_error_message( $fmt ) );
68
69        // Format the number using the new value.
70        $rc = ut_nfmt_format( $fmt, $number );
71
72        // Get current value of the attribute and check if it equals $new_val.
73        $attr_val_check = ut_nfmt_get_attribute( $fmt, $attr );
74        if( $attr_val_check !== $new_val )
75            $res_str .= "ERROR: New $attr_name attribute value has not been set correctly.\n";
76
77        $ps = ut_nfmt_parse( $fmt, $rc );
78
79        $res_str .= sprintf( "New attribute value: %s ;  Format result: %s ; Parse result: %s\n",
80                             dump( $new_val ),
81                             dump( $rc ),
82                             dump( $ps ) );
83
84
85        // Restore original attribute of the  value
86        if( $attr != NumberFormatter::INTEGER_DIGITS && $attr != NumberFormatter::FRACTION_DIGITS
87             && $attr != NumberFormatter::FORMAT_WIDTH && $attr != NumberFormatter::SIGNIFICANT_DIGITS_USED )
88            ut_nfmt_set_attribute( $fmt, $attr, $orig_val );
89    }
90
91    return $res_str;
92}
93
94include_once( 'ut_common.inc' );
95
96// Run the test
97ut_run();
98
99?>
100--EXPECT--
101Attribute PARSE_INT_ONLY
102Old attribute value: 0 ;  Format result: '12,345.123' ; Parse result: 12345.123
103Setting attribute: ok
104New attribute value: 1 ;  Format result: '12,345.123' ; Parse result: 12345
105
106Attribute GROUPING_USED
107Old attribute value: 1 ;  Format result: '12,345.123' ; Parse result: 12345.123
108Setting attribute: ok
109New attribute value: 0 ;  Format result: '12345.123' ; Parse result: 12345.123
110
111Attribute DECIMAL_ALWAYS_SHOWN
112Old attribute value: 0 ;  Format result: '12,345' ; Parse result: 12345
113Setting attribute: ok
114New attribute value: 1 ;  Format result: '12,345.' ; Parse result: 12345
115
116Attribute MAX_INTEGER_DIGITS
117Old attribute value: 309 ;  Format result: '12,345.123' ; Parse result: 12345.123
118Setting attribute: ok
119New attribute value: 2 ;  Format result: '45.123' ; Parse result: 45.123
120
121Attribute MIN_INTEGER_DIGITS
122Old attribute value: 1 ;  Format result: '12,345.123' ; Parse result: 12345.123
123Setting attribute: ok
124New attribute value: 20 ;  Format result: '00,000,000,000,000,012,345.123' ; Parse result: 12345.123
125
126Attribute INTEGER_DIGITS
127Old attribute value: 1 ;  Format result: '12,345.123' ; Parse result: 12345.123
128Setting attribute: ok
129New attribute value: 7 ;  Format result: '0,012,345.123' ; Parse result: 12345.123
130
131Attribute MAX_FRACTION_DIGITS
132Old attribute value: 3 ;  Format result: '0,012,345.123' ; Parse result: 12345.123
133Setting attribute: ok
134New attribute value: 2 ;  Format result: '0,012,345.12' ; Parse result: 12345.12
135
136Attribute MIN_FRACTION_DIGITS
137Old attribute value: 0 ;  Format result: '0,012,345.123' ; Parse result: 12345.123
138Setting attribute: ok
139New attribute value: 20 ;  Format result: '0,012,345.12345600000000000000' ; Parse result: 12345.123456
140
141Attribute FRACTION_DIGITS
142Old attribute value: 0 ;  Format result: '0,012,345.123456' ; Parse result: 12345.123456
143Setting attribute: ok
144New attribute value: 5 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
145
146Attribute MULTIPLIER
147Old attribute value: 1 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
148Setting attribute: ok
149New attribute value: 2 ;  Format result: '0,024,690.24691' ; Parse result: 12345.123455
150
151Attribute GROUPING_SIZE
152Old attribute value: 3 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
153Setting attribute: ok
154New attribute value: 2 ;  Format result: '0,01,23,45.12346' ; Parse result: 12345.12346
155
156Attribute ROUNDING_MODE
157Old attribute value: 4 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
158Setting attribute: ok
159New attribute value: 1 ;  Format result: '0,012,345.12345' ; Parse result: 12345.12345
160
161Attribute ROUNDING_INCREMENT
162Old attribute value: 0 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
163Setting attribute: ok
164New attribute value: 2 ;  Format result: '0,012,346.00000' ; Parse result: 12346
165
166Attribute FORMAT_WIDTH
167Old attribute value: 0 ;  Format result: '0,012,345.12346' ; Parse result: 12345.12346
168Setting attribute: ok
169New attribute value: 27 ;  Format result: '************0,012,345.12346' ; Parse result: 12345.12346
170
171Attribute PADDING_POSITION
172Old attribute value: 0 ;  Format result: '************0,012,345.12346' ; Parse result: 12345.12346
173Setting attribute: ok
174New attribute value: 2 ;  Format result: '0,012,345.12346************' ; Parse result: 12345.12346
175
176Attribute SECONDARY_GROUPING_SIZE
177Old attribute value: 0 ;  Format result: '************0,012,345.12346' ; Parse result: 12345.12346
178Setting attribute: ok
179New attribute value: 2 ;  Format result: '************00,12,345.12346' ; Parse result: 12345.12346
180
181Attribute SIGNIFICANT_DIGITS_USED
182Old attribute value: 0 ;  Format result: '************0,012,345.12346' ; Parse result: 12345.12346
183Setting attribute: ok
184New attribute value: 1 ;  Format result: '*******************12,345.1' ; Parse result: 12345.1
185
186Attribute MIN_SIGNIFICANT_DIGITS
187Old attribute value: 1 ;  Format result: '**************************1' ; Parse result: 1
188Setting attribute: ok
189New attribute value: 3 ;  Format result: '***********************1.00' ; Parse result: 1
190
191Attribute MAX_SIGNIFICANT_DIGITS
192Old attribute value: 6 ;  Format result: '*******************12,345.1' ; Parse result: 12345.1
193Setting attribute: ok
194New attribute value: 4 ;  Format result: '*********************12,350' ; Parse result: 12350
195