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