1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/strings/builder_test.v

69 lines
1.2 KiB
V
Raw Normal View History

import strings
2019-11-10 03:08:53 +03:00
fn test_sb() {
mut sb := strings.Builder{}
sb.write('hi')
sb.write('!')
sb.write('hello')
assert sb.len == 8
assert sb.str() == 'hi!hello'
assert sb.len == 0
2020-06-17 01:59:33 +03:00
///
sb = strings.new_builder(10)
sb.write('a')
sb.write('b')
assert sb.len == 2
assert sb.str() == 'ab'
2020-06-17 01:59:33 +03:00
///
2020-06-17 05:05:13 +03:00
$if !windows {
// TODO msvc bug
sb = strings.new_builder(10)
sb.write('123456')
assert sb.cut_last(2) == '56'
assert sb.str() == '1234'
}
2020-06-17 01:59:33 +03:00
///
/*
sb = strings.new_builder(10)
sb.write('123456')
x := sb.cut_to(2)
assert x == '456'
assert sb.str() == '123'
*/
}
2019-11-10 03:08:53 +03:00
const (
n = 100000
)
fn test_big_sb() {
mut sb := strings.new_builder(100)
mut sb2 := strings.new_builder(10000)
2019-11-10 03:08:53 +03:00
for i in 0..n {
sb.writeln(i.str())
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()
assert lines.len == n
assert lines[0] == '0'
assert lines[1] == '1'
assert lines[777] == '777'
assert lines[98765] == '98765'
println(sb2.len)
assert sb2.len == n
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)
temp_str := "byte testing"
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
}
assert sb.str() == temp_str
}