2019-07-03 22:11:27 +02:00
|
|
|
import strings
|
2019-11-10 01:08:53 +01:00
|
|
|
|
2020-07-13 18:59:39 +02:00
|
|
|
type MyInt = int
|
|
|
|
|
2021-08-19 06:14:20 +02:00
|
|
|
const maxn = 100000
|
|
|
|
|
2019-07-03 22:11:27 +02:00
|
|
|
fn test_sb() {
|
2021-05-31 13:21:06 +02:00
|
|
|
mut sb := strings.new_builder(100)
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('hi')
|
|
|
|
sb.write_string('!')
|
|
|
|
sb.write_string('hello')
|
2019-11-11 06:04:37 +01:00
|
|
|
assert sb.len == 8
|
2020-07-01 00:53:53 +02:00
|
|
|
sb_end := sb.str()
|
|
|
|
assert sb_end == 'hi!hello'
|
2020-04-28 11:20:19 +02:00
|
|
|
assert sb.len == 0
|
2020-06-17 00:59:33 +02:00
|
|
|
///
|
2019-07-03 22:11:27 +02:00
|
|
|
sb = strings.new_builder(10)
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('a')
|
|
|
|
sb.write_string('b')
|
2019-11-11 06:04:37 +01:00
|
|
|
assert sb.len == 2
|
2020-04-28 11:20:19 +02:00
|
|
|
assert sb.str() == 'ab'
|
2020-07-13 18:59:39 +02: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 21:00:32 +01:00
|
|
|
assert res[res.len - 1] == `\n`
|
2020-07-13 18:59:39 +02:00
|
|
|
println('"$res"')
|
|
|
|
assert res.trim_space() == 'x = 10 y = 20'
|
|
|
|
//
|
|
|
|
sb = strings.new_builder(10)
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('x = $x y = $y')
|
2020-07-13 18:59:39 +02:00
|
|
|
assert sb.str() == 'x = 10 y = 20'
|
2021-03-04 18:35:17 +01:00
|
|
|
//$if !windows {
|
|
|
|
sb = strings.new_builder(10)
|
|
|
|
sb.write_string('123456')
|
|
|
|
last_2 := sb.cut_last(2)
|
|
|
|
assert last_2 == '56'
|
|
|
|
final_sb := sb.str()
|
|
|
|
assert final_sb == '1234'
|
|
|
|
//}
|
2019-07-03 22:11:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-10 01:08:53 +01:00
|
|
|
fn test_big_sb() {
|
|
|
|
mut sb := strings.new_builder(100)
|
2019-11-11 06:04:37 +01:00
|
|
|
mut sb2 := strings.new_builder(10000)
|
2021-01-25 12:08:43 +01:00
|
|
|
for i in 0 .. maxn {
|
2019-11-10 01:08:53 +01:00
|
|
|
sb.writeln(i.str())
|
2021-02-22 12:18:11 +01:00
|
|
|
sb2.write_string('+')
|
2019-12-08 12:34:51 +01:00
|
|
|
}
|
2019-11-10 01:08:53 +01:00
|
|
|
s := sb.str()
|
|
|
|
lines := s.split_into_lines()
|
2021-01-25 12:08:43 +01:00
|
|
|
assert lines.len == maxn
|
2019-11-10 01:08:53 +01:00
|
|
|
assert lines[0] == '0'
|
|
|
|
assert lines[1] == '1'
|
|
|
|
assert lines[777] == '777'
|
|
|
|
assert lines[98765] == '98765'
|
2019-11-11 06:04:37 +01:00
|
|
|
println(sb2.len)
|
2021-01-25 12:08:43 +01:00
|
|
|
assert sb2.len == maxn
|
2019-12-08 12:34:51 +01:00
|
|
|
}
|
2019-11-10 01:08:53 +01:00
|
|
|
|
2019-12-06 21:02:09 +01:00
|
|
|
fn test_byte_write() {
|
|
|
|
mut sb := strings.new_builder(100)
|
2020-12-21 21:00:32 +01:00
|
|
|
temp_str := 'byte testing'
|
2019-12-06 21:02:09 +01:00
|
|
|
mut count := 0
|
|
|
|
for word in temp_str {
|
2022-01-28 19:34:44 +01:00
|
|
|
sb.write_byte(word)
|
2019-12-08 12:34:51 +01:00
|
|
|
count++
|
2019-12-06 21:02:09 +01:00
|
|
|
assert count == sb.len
|
|
|
|
}
|
2020-07-01 00:53:53 +02:00
|
|
|
sb_final := sb.str()
|
|
|
|
assert sb_final == temp_str
|
2019-12-06 21:02:09 +01:00
|
|
|
}
|
2021-02-21 12:32:18 +01:00
|
|
|
|
|
|
|
fn test_strings_builder_reuse() {
|
|
|
|
mut sb := strings.new_builder(256)
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('world')
|
2021-02-21 12:32:18 +01:00
|
|
|
assert sb.str() == 'world'
|
2021-02-22 12:18:11 +01:00
|
|
|
sb.write_string('hello')
|
2021-02-21 12:32:18 +01:00
|
|
|
assert sb.str() == 'hello'
|
|
|
|
}
|
2021-05-07 18:41:27 +02:00
|
|
|
|
|
|
|
fn test_cut_to() {
|
|
|
|
mut sb := strings.new_builder(16)
|
|
|
|
sb.write_string('hello')
|
|
|
|
assert sb.cut_to(3) == 'lo'
|
|
|
|
assert sb.len == 3
|
|
|
|
assert sb.cut_to(3) == ''
|
|
|
|
assert sb.len == 3
|
|
|
|
assert sb.cut_to(0) == 'hel'
|
|
|
|
assert sb.cut_to(32) == ''
|
|
|
|
assert sb.len == 0
|
|
|
|
}
|
2021-08-19 06:14:20 +02:00
|
|
|
|
|
|
|
fn test_write_rune() {
|
|
|
|
mut sb := strings.new_builder(10)
|
|
|
|
sb.write_rune(`h`)
|
|
|
|
sb.write_rune(`e`)
|
|
|
|
sb.write_rune(`l`)
|
|
|
|
sb.write_rune(`l`)
|
|
|
|
sb.write_rune(`o`)
|
|
|
|
x := sb.str()
|
|
|
|
assert x == 'hello'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_write_runes() {
|
|
|
|
mut sb := strings.new_builder(20)
|
|
|
|
sb.write_runes([`h`, `e`, `l`, `l`, `o`])
|
|
|
|
sb.write_rune(` `)
|
|
|
|
sb.write_runes([`w`, `o`, `r`, `l`, `d`])
|
|
|
|
x := sb.str()
|
|
|
|
assert x == 'hello world'
|
|
|
|
}
|
2022-03-18 09:36:53 +01:00
|
|
|
|
|
|
|
fn test_ensure_cap() {
|
|
|
|
mut sb := strings.new_builder(0)
|
|
|
|
assert sb.cap == 0
|
|
|
|
sb.ensure_cap(10)
|
|
|
|
assert sb.cap == 10
|
|
|
|
sb.ensure_cap(10)
|
|
|
|
assert sb.cap == 10
|
|
|
|
sb.ensure_cap(15)
|
|
|
|
assert sb.cap == 15
|
|
|
|
sb.ensure_cap(10)
|
|
|
|
assert sb.cap == 15
|
|
|
|
sb.ensure_cap(-1)
|
|
|
|
assert sb.cap == 15
|
|
|
|
}
|