| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use math; |
| 5 |
use strconv; |
| 6 |
|
| 7 |
@test fn print() void = { |
| 8 |
let buf: [1024]u8 = [0...]; |
| 9 |
|
| 10 |
assert(bsprint(buf, "hello world")! == "hello world"); |
| 11 |
assert(bsprintf(buf, "hello world")! == "hello world"); |
| 12 |
assert(bsprintf(buf, "{} {}", "hello", "world")! == "hello world"); |
| 13 |
assert(bsprintf(buf, "{0} {1}", "hello", "world")! == "hello world"); |
| 14 |
assert(bsprintf(buf, "{0} {0}", "hello", "world")! == "hello hello"); |
| 15 |
assert(bsprintf(buf, "{1} {0} {1}", "hello", "world")! == "world hello world"); |
| 16 |
|
| 17 |
const mod = &mods { width = 7, pad = ' ', ... }; |
| 18 |
assert(bsprintf(buf, "{%}", "hello", mod)! == " hello"); |
| 19 |
assert(bsprintf(buf, "{%1}", "hello", mod)! == " hello"); |
| 20 |
assert(bsprintf(buf, "{0%1}", "hello", mod)! == " hello"); |
| 21 |
assert(bsprintf(buf, "{0%2}", "hello", 0, mod)! == " hello"); |
| 22 |
assert(bsprintf(buf, "{1%2}", 0, "hello", mod)! == " hello"); |
| 23 |
assert(bsprintf(buf, "{2%0}", mod, 0, "hello")! == " hello"); |
| 24 |
assert(bsprintf(buf, "{2%}", mod, 0, "hello")! == " hello"); |
| 25 |
assert(bsprintf(buf, "|{1%}|{}|", mod, "hello")! == "| hello|hello|"); |
| 26 |
assert(bsprintf(buf, "|{}|{2%}|", "hello", mod, "world")! == "|hello| world|"); |
| 27 |
assert(bsprintf(buf, "|{%}|{%}|{%}|{%}|", |
| 28 |
"hello", &mods { ... }, |
| 29 |
"world", &mods { width = 10, pad = ' ', ... }, |
| 30 |
123, &mods { prec = 10, ... }, |
| 31 |
0xBEEF, &mods { base = strconv::base::HEX, ... }, |
| 32 |
)! == "|hello| world|0000000123|BEEF|"); |
| 33 |
assert(bsprintf(buf, "|{%}|{%}|{0%1}|", |
| 34 |
"hello", &mods { ... }, |
| 35 |
"world", &mods { ... }, |
| 36 |
)! == "|hello|world|hello|"); |
| 37 |
|
| 38 |
assert(bsprintf(buf, "x: {:8X}", 0xBEEF)! == "x: BEEF"); |
| 39 |
assert(bsprintf(buf, "x: {:8X}", -0xBEEF)! == "x: -BEEF"); |
| 40 |
assert(bsprintf(buf, "x: {: 8X}", 0xBEEF)! == "x: BEEF"); |
| 41 |
assert(bsprintf(buf, "x: {:+ 8X}", 0xBEEF)! == "x: BEEF"); |
| 42 |
assert(bsprintf(buf, "x: {:+8X}", 0xBEEF)! == "x: +BEEF"); |
| 43 |
assert(bsprintf(buf, "x: {: +8X}", 0xBEEF)! == "x: +BEEF"); |
| 44 |
|
| 45 |
assert(bsprintf(buf, "x: {:-8X}", 0xBEEF)! == "x: BEEF "); |
| 46 |
assert(bsprintf(buf, "x: {:-8X}", -0xBEEF)! == "x: -BEEF "); |
| 47 |
assert(bsprintf(buf, "x: {:-+8X}", 0xBEEF)! == "x: +BEEF "); |
| 48 |
assert(bsprintf(buf, "x: {:- 8X}", 0xBEEF)! == "x: BEEF "); |
| 49 |
|
| 50 |
assert(bsprintf(buf, "x: {:.8x}", 0xBEEF)! == "x: 0000beef"); |
| 51 |
assert(bsprintf(buf, "x: {:.8x}", -0xBEEF)! == "x: -000beef"); |
| 52 |
assert(bsprintf(buf, "x: {:+.8x}", 0xBEEF)! == "x: +000beef"); |
| 53 |
assert(bsprintf(buf, "x: {: .8x}", 0xBEEF)! == "x: 000beef"); |
| 54 |
assert(bsprintf(buf, "x: {:-_08X}", 0xBEEF)! == "x: BEEF0000"); |
| 55 |
|
| 56 |
assert(bsprintf(buf, "x: {:o}", 0o755)! == "x: 755"); |
| 57 |
assert(bsprintf(buf, "x: {:b}", 0b11011)! == "x: 11011"); |
| 58 |
|
| 59 |
assert(bsprintf(buf, "x: {:8}", "hello")! == "x: hello"); |
| 60 |
assert(bsprintf(buf, "x: {:-8}", "hello")! == "x: hello "); |
| 61 |
assert(bsprintf(buf, "x: {:_08}", "hello")! == "x: 000hello"); |
| 62 |
|
| 63 |
assert(bsprintf(buf, "{:.5}", "hello world")! == "hello"); |
| 64 |
assert(bsprintf(buf, "{:.5}", "hi")! == "hi"); |
| 65 |
assert(bsprintf(buf, "{:5.2}", "hello")! == " he"); |
| 66 |
|
| 67 |
assert(bsprintf(buf, "{:.1}", 123.0)! == "100"); |
| 68 |
assert(bsprintf(buf, "{:.5}", 123.0)! == "123"); |
| 69 |
|
| 70 |
assert(bsprintf(buf, "{:f}", 1.0e4)! == "10000"); |
| 71 |
assert(bsprintf(buf, "{:e}", 123.45)! == "1.2345e2"); |
| 72 |
assert(bsprintf(buf, "{:Fs}", 1.0)! == "+1"); |
| 73 |
assert(bsprintf(buf, "{:F.}", 1.0)! == "1.0"); |
| 74 |
assert(bsprintf(buf, "{:FU}", math::INF)! == "INFINITY"); |
| 75 |
assert(bsprintf(buf, "{:FE}", 1.0e4)! == "1E4"); |
| 76 |
assert(bsprintf(buf, "{:FS}", 1.0e4)! == "1e+4"); |
| 77 |
assert(bsprintf(buf, "{:F2}", 1.0e4)! == "1e04"); |
| 78 |
|
| 79 |
assert(bsprintf(buf, "{:=5}", "hi")! == " hi "); |
| 80 |
assert(bsprintf(buf, "{:=6}", "hi")! == " hi "); |
| 81 |
|
| 82 |
assert(bsprintf(buf, "{} {} {} {} {}", true, false, null, 'x', void)! |
| 83 |
== "true false (null) x void"); |
| 84 |
}; |