| 1 |
project('test number base conversions') |
| 2 |
|
| 3 |
### .to_int() |
| 4 |
|
| 5 |
# hexadecimal strings |
| 6 |
assert('0x10'.to_int() == 16, 'Hex string conversion failed') |
| 7 |
assert('0X10'.to_int() == 16, 'Hex string conversion (uppercase X) failed') |
| 8 |
assert('0xff'.to_int() == 255, 'Hex string conversion (lowercase) failed') |
| 9 |
assert('0xFF'.to_int() == 255, 'Hex string conversion (uppercase) failed') |
| 10 |
assert('0xDEADBEEF'.to_int() == 3735928559, 'Large hex conversion failed') |
| 11 |
assert('0x1'.to_int() == 1, 'Small hex conversion failed') |
| 12 |
assert('0x0'.to_int() == 0, 'Zero hex conversion failed') |
| 13 |
|
| 14 |
# signed hexadecimal strings |
| 15 |
assert('-0xf'.to_int() == -15, 'Negative hex string conversion failed') |
| 16 |
assert('+0x10'.to_int() == 16, 'Positive hex string conversion failed') |
| 17 |
assert('-0xFF'.to_int() == -255, 'Negative hex string (uppercase) conversion failed') |
| 18 |
|
| 19 |
# octal strings |
| 20 |
assert('0o10'.to_int() == 8, 'Octal string conversion failed') |
| 21 |
assert('0O10'.to_int() == 8, 'Octal string conversion (uppercase O) failed') |
| 22 |
assert('0o77'.to_int() == 63, 'Octal string conversion failed') |
| 23 |
assert('0o755'.to_int() == 493, 'Octal permission-like conversion failed') |
| 24 |
assert('0o0'.to_int() == 0, 'Zero octal conversion failed') |
| 25 |
|
| 26 |
# signed octal strings |
| 27 |
assert('-0o10'.to_int() == -8, 'Negative octal string conversion failed') |
| 28 |
assert('+0o77'.to_int() == 63, 'Positive octal string conversion failed') |
| 29 |
|
| 30 |
# binary strings |
| 31 |
assert('0b10'.to_int() == 2, 'Binary string conversion failed') |
| 32 |
assert('0B10'.to_int() == 2, 'Binary string conversion (uppercase B) failed') |
| 33 |
assert('0b1111'.to_int() == 15, 'Binary string conversion failed') |
| 34 |
assert('0b11111111'.to_int() == 255, 'Binary byte conversion failed') |
| 35 |
assert('0b0'.to_int() == 0, 'Zero binary conversion failed') |
| 36 |
|
| 37 |
# signed binary strings |
| 38 |
assert('-0b101'.to_int() == -5, 'Negative binary string conversion failed') |
| 39 |
assert('+0b1111'.to_int() == 15, 'Positive binary string conversion failed') |
| 40 |
|
| 41 |
# decimal strings (backwards compat) |
| 42 |
assert('10'.to_int() == 10, 'Decimal string conversion failed') |
| 43 |
assert('255'.to_int() == 255, 'Decimal string conversion failed') |
| 44 |
assert('0'.to_int() == 0, 'Zero decimal conversion failed') |
| 45 |
assert('12345'.to_int() == 12345, 'Large decimal conversion failed') |
| 46 |
assert('1_234'.to_int() == 1234, 'Decimal string conversion with underscores failed') |
| 47 |
assert('0_100'.to_int() == 100, 'Zero-prefixed decimal string with underscores failed') |
| 48 |
|
| 49 |
# leading zeroes are decimal (backwards compat) |
| 50 |
assert('010'.to_int() == 10, 'Decimal with leading zero broke backward compatibility') |
| 51 |
assert('0123'.to_int() == 123, 'Decimal with leading zeros broke backward compatibility') |
| 52 |
assert('007'.to_int() == 7, 'Decimal with leading zeros broke backward compatibility') |
| 53 |
assert('0x_FF'.to_int() == 255, 'Hex string conversion with underscores failed') |
| 54 |
assert('0o_755'.to_int() == 493, 'Octal string conversion with underscores failed') |
| 55 |
assert('0b_1010'.to_int() == 10, 'Binary string conversion with underscores failed') |
| 56 |
|
| 57 |
### .to_string() |
| 58 |
|
| 59 |
# hex format |
| 60 |
assert(16.to_string(format: 'hex') == '0x10', 'Int to hex string failed') |
| 61 |
assert(255.to_string(format: 'hex') == '0xff', 'Int to hex string failed') |
| 62 |
assert(0.to_string(format: 'hex') == '0x0', 'Zero to hex string failed') |
| 63 |
assert(1.to_string(format: 'hex') == '0x1', 'One to hex string failed') |
| 64 |
assert(3735928559.to_string(format: 'hex') == '0xdeadbeef', 'Large hex string failed') |
| 65 |
|
| 66 |
# octal format |
| 67 |
assert(8.to_string(format: 'oct') == '0o10', 'Int to octal string failed') |
| 68 |
assert(63.to_string(format: 'oct') == '0o77', 'Int to octal string failed') |
| 69 |
assert(493.to_string(format: 'oct') == '0o755', 'Permission to octal string failed') |
| 70 |
assert(0.to_string(format: 'oct') == '0o0', 'Zero to octal string failed') |
| 71 |
|
| 72 |
# binary format |
| 73 |
assert(2.to_string(format: 'bin') == '0b10', 'Int to binary string failed') |
| 74 |
assert(15.to_string(format: 'bin') == '0b1111', 'Int to binary string failed') |
| 75 |
assert(255.to_string(format: 'bin') == '0b11111111', 'Byte to binary string failed') |
| 76 |
assert(0.to_string(format: 'bin') == '0b0', 'Zero to binary string failed') |
| 77 |
|
| 78 |
# decimal format (explicit) |
| 79 |
assert(10.to_string(format: 'dec') == '10', 'Int to decimal string failed') |
| 80 |
assert(255.to_string(format: 'dec') == '255', 'Int to decimal string failed') |
| 81 |
|
| 82 |
# default |
| 83 |
assert(42.to_string() == '42', 'Default int to string failed') |
| 84 |
|
| 85 |
# fill and hex format |
| 86 |
assert(255.to_string(format: 'hex', fill: 8) == '0x0000ff', 'Hex with fill failed') |
| 87 |
assert(1.to_string(format: 'hex', fill: 6) == '0x0001', 'Hex with fill failed') |
| 88 |
assert(255.to_string(format: 'hex', fill: 4) == '0xff', 'Hex with fill (no padding needed) failed') |
| 89 |
|
| 90 |
# fill and other formats |
| 91 |
assert(8.to_string(format: 'oct', fill: 6) == '0o0010', 'Octal with fill failed') |
| 92 |
assert(2.to_string(format: 'bin', fill: 10) == '0b00000010', 'Binary with fill failed') |
| 93 |
|
| 94 |
# negative numbers |
| 95 |
assert((-15).to_string(format: 'hex') == '-0xf', 'Negative hex conversion failed') |
| 96 |
assert((-8).to_string(format: 'oct') == '-0o10', 'Negative octal conversion failed') |
| 97 |
assert((-5).to_string(format: 'bin') == '-0b101', 'Negative binary conversion failed') |
| 98 |
|
| 99 |
# negative numbers and fill |
| 100 |
assert((-15).to_string(format: 'hex', fill: 6) == '-0x00f', 'Negative hex with fill failed') |
| 101 |
assert((-8).to_string(format: 'oct', fill: 7) == '-0o0010', 'Negative octal with fill failed') |
| 102 |
assert((-5).to_string(format: 'bin', fill: 8) == '-0b00101', 'Negative binary with fill failed') |
| 103 |
assert((-4).to_string(fill: 3) == '-04', 'Negative decimal with fill failed') |
| 104 |
|
| 105 |
# fill and decimal |
| 106 |
assert(4.to_string(fill: 3) == '004', 'Decimal with fill failed') |
| 107 |
assert(4.to_string(fill: -1) == '4', 'Negative decimal fill should clamp to zero') |
| 108 |
assert(255.to_string(format: 'hex', fill: -1) == '0xff', 'Negative hex fill should clamp to zero') |
| 109 |
|
| 110 |
### Round trip conversions |
| 111 |
|
| 112 |
# positive |
| 113 |
|
| 114 |
hex_val = 0x200 |
| 115 |
hex_str = hex_val.to_string(format: 'hex') |
| 116 |
assert(hex_str.to_int() == hex_val, 'Hex round-trip failed') |
| 117 |
|
| 118 |
oct_val = 0o755 |
| 119 |
oct_str = oct_val.to_string(format: 'oct') |
| 120 |
assert(oct_str.to_int() == oct_val, 'Octal round-trip failed') |
| 121 |
|
| 122 |
bin_val = 0b11010 |
| 123 |
bin_str = bin_val.to_string(format: 'bin') |
| 124 |
assert(bin_str.to_int() == bin_val, 'Binary round-trip failed') |
| 125 |
|
| 126 |
# negative |
| 127 |
|
| 128 |
neg_hex = -255 |
| 129 |
neg_hex_str = neg_hex.to_string(format: 'hex') |
| 130 |
assert(neg_hex_str.to_int() == neg_hex, 'Negative hex round-trip failed') |
| 131 |
|
| 132 |
neg_oct = -63 |
| 133 |
neg_oct_str = neg_oct.to_string(format: 'oct') |
| 134 |
assert(neg_oct_str.to_int() == neg_oct, 'Negative octal round-trip failed') |
| 135 |
|
| 136 |
neg_bin = -15 |
| 137 |
neg_bin_str = neg_bin.to_string(format: 'bin') |
| 138 |
assert(neg_bin_str.to_int() == neg_bin, 'Negative binary round-trip failed') |