2019-07-03 23:11:27 +03:00
|
|
|
import strings
|
2019-11-10 03:08:53 +03:00
|
|
|
|
2020-07-13 19:59:39 +03:00
|
|
|
type MyInt = int
|
|
|
|
|
2019-07-03 23:11:27 +03:00
|
|
|
fn test_sb() {
|
|
|
|
mut sb := strings.Builder{}
|
|
|
|
sb.write('hi')
|
|
|
|
sb.write('!')
|
|
|
|
sb.write('hello')
|
2019-11-11 08:04:37 +03:00
|
|
|
assert sb.len == 8
|
2020-07-01 01:53:53 +03:00
|
|
|
sb_end := sb.str()
|
|
|
|
assert sb_end == 'hi!hello'
|
2020-04-28 12:20:19 +03:00
|
|
|
assert sb.len == 0
|
2020-06-17 01:59:33 +03:00
|
|
|
///
|
2019-07-03 23:11:27 +03:00
|
|
|
sb = strings.new_builder(10)
|
|
|
|
sb.write('a')
|
|
|
|
sb.write('b')
|
2019-11-11 08:04:37 +03:00
|
|
|
assert sb.len == 2
|
2020-04-28 12:20:19 +03:00
|
|
|
assert sb.str() == 'ab'
|
2020-07-13 19:59:39 +03:00
|
|
|
// Test interpolation optimization
|
|
|
|
sb = strings.new_builder(10)
|
|
|
|
x := 10
|
|
|
|
y := MyInt(20)
|
|
|
|
sb.writeln('x = $x y = $y')
|
|
|
|
res := sb.str()
|
2020-12-21 23:00:32 +03:00
|
|
|
assert res[res.len - 1] == `\n`
|
2020-07-13 19:59:39 +03:00
|
|
|
println('"$res"')
|
|
|
|
assert res.trim_space() == 'x = 10 y = 20'
|
|
|
|
//
|
|
|
|
sb = strings.new_builder(10)
|
|
|
|
sb.write('x = $x y = $y')
|
|
|
|
assert sb.str() == 'x = 10 y = 20'
|
2020-06-17 05:05:13 +03:00
|
|
|
$if !windows {
|
|
|
|
// TODO msvc bug
|
|
|
|
sb = strings.new_builder(10)
|
|
|
|
sb.write('123456')
|
2020-07-01 01:53:53 +03:00
|
|
|
last_2 := sb.cut_last(2)
|
|
|
|
assert last_2 == '56'
|
|
|
|
final_sb := sb.str()
|
|
|
|
assert final_sb == '1234'
|
2020-06-17 05:05:13 +03:00
|
|
|
}
|
2019-07-03 23:11:27 +03:00
|
|
|
}
|
|
|
|
|
2019-11-10 03:08:53 +03:00
|
|
|
const (
|
2020-07-01 01:53:53 +03:00
|
|
|
maxn = 100000
|
2019-11-10 03:08:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn test_big_sb() {
|
|
|
|
mut sb := strings.new_builder(100)
|
2019-11-11 08:04:37 +03:00
|
|
|
mut sb2 := strings.new_builder(10000)
|
2020-12-21 23:00:32 +03:00
|
|
|
for i in 0 .. maxn {
|
2019-11-10 03:08:53 +03:00
|
|
|
sb.writeln(i.str())
|
2019-11-11 08:04:37 +03:00
|
|
|
sb2.write('+')
|
2019-12-08 14:34:51 +03:00
|
|
|
}
|
2019-11-10 03:08:53 +03:00
|
|
|
s := sb.str()
|
|
|
|
lines := s.split_into_lines()
|
2020-07-01 01:53:53 +03:00
|
|
|
assert lines.len == maxn
|
2019-11-10 03:08:53 +03:00
|
|
|
assert lines[0] == '0'
|
|
|
|
assert lines[1] == '1'
|
|
|
|
assert lines[777] == '777'
|
|
|
|
assert lines[98765] == '98765'
|
2019-11-11 08:04:37 +03:00
|
|
|
println(sb2.len)
|
2020-07-01 01:53:53 +03:00
|
|
|
assert sb2.len == maxn
|
2019-12-08 14:34:51 +03:00
|
|
|
}
|
2019-11-10 03:08:53 +03:00
|
|
|
|
2019-12-06 23:02:09 +03:00
|
|
|
fn test_byte_write() {
|
|
|
|
mut sb := strings.new_builder(100)
|
2020-12-21 23:00:32 +03:00
|
|
|
temp_str := 'byte testing'
|
2019-12-06 23:02:09 +03:00
|
|
|
mut count := 0
|
|
|
|
for word in temp_str {
|
|
|
|
sb.write_b(word)
|
2019-12-08 14:34:51 +03:00
|
|
|
count++
|
2019-12-06 23:02:09 +03:00
|
|
|
assert count == sb.len
|
|
|
|
}
|
2020-07-01 01:53:53 +03:00
|
|
|
sb_final := sb.str()
|
|
|
|
assert sb_final == temp_str
|
2019-12-06 23:02:09 +03:00
|
|
|
}
|