| 1 | import strconv |
| 2 | |
| 3 | fn dec_to_string(n i64) string { |
| 4 | mut buf := []u8{len: 21, init: `x`} |
| 5 | written := strconv.write_dec(n, mut buf) |
| 6 | assert written >= 0 |
| 7 | return buf[..written].bytestr() |
| 8 | } |
| 9 | |
| 10 | fn dec_u_to_string(n u64) string { |
| 11 | mut buf := []u8{len: 20, init: `x`} |
| 12 | written := strconv.write_dec_u(n, mut buf) |
| 13 | assert written >= 0 |
| 14 | return buf[..written].bytestr() |
| 15 | } |
| 16 | |
| 17 | fn test_write_dec_signed_values() { |
| 18 | assert dec_to_string(0) == '0' |
| 19 | assert dec_to_string(1) == '1' |
| 20 | assert dec_to_string(-1) == '-1' |
| 21 | assert dec_to_string(1001) == '1001' |
| 22 | assert dec_to_string(-1001) == '-1001' |
| 23 | assert dec_to_string(1234567890) == '1234567890' |
| 24 | assert dec_to_string(-1234567890) == '-1234567890' |
| 25 | assert dec_to_string(max_i64) == '9223372036854775807' |
| 26 | assert dec_to_string(-9223372036854775807) == '-9223372036854775807' |
| 27 | assert dec_to_string(min_i64) == '-9223372036854775808' |
| 28 | assert dec_to_string('-9223372036854775808'.i64()) == '-9223372036854775808' |
| 29 | } |
| 30 | |
| 31 | fn test_write_dec_unsigned_values() { |
| 32 | assert dec_u_to_string(0) == '0' |
| 33 | assert dec_u_to_string(1) == '1' |
| 34 | assert dec_u_to_string(1001) == '1001' |
| 35 | assert dec_u_to_string(1234567890) == '1234567890' |
| 36 | assert dec_u_to_string(u64(max_i64)) == '9223372036854775807' |
| 37 | assert dec_u_to_string(u64(max_i64) + 1) == '9223372036854775808' |
| 38 | assert dec_u_to_string(max_u64) == '18446744073709551615' |
| 39 | } |
| 40 | |
| 41 | fn test_write_dec_returns_count_and_preserves_tail() { |
| 42 | mut buf := []u8{len: 8, init: `x`} |
| 43 | assert strconv.write_dec(42, mut buf) == 2 |
| 44 | assert buf.bytestr() == '42xxxxxx' |
| 45 | |
| 46 | buf = []u8{len: 8, init: `x`} |
| 47 | assert strconv.write_dec(-42, mut buf) == 3 |
| 48 | assert buf.bytestr() == '-42xxxxx' |
| 49 | |
| 50 | buf = []u8{len: 8, init: `x`} |
| 51 | assert strconv.write_dec_u(42, mut buf) == 2 |
| 52 | assert buf.bytestr() == '42xxxxxx' |
| 53 | } |
| 54 | |
| 55 | fn test_write_dec_returns_minus_one_for_small_buffer() { |
| 56 | mut buf := []u8{len: 2, init: `x`} |
| 57 | assert strconv.write_dec(123, mut buf) == -1 |
| 58 | assert buf.bytestr() == 'xx' |
| 59 | assert strconv.write_dec(-12, mut buf) == -1 |
| 60 | assert buf.bytestr() == 'xx' |
| 61 | assert strconv.write_dec_u(123, mut buf) == -1 |
| 62 | assert buf.bytestr() == 'xx' |
| 63 | |
| 64 | assert strconv.write_dec(-1, mut buf) == 2 |
| 65 | assert buf.bytestr() == '-1' |
| 66 | assert strconv.write_dec_u(99, mut buf) == 2 |
| 67 | assert buf.bytestr() == '99' |
| 68 | } |
| 69 | |