aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/strings/builder_test.v
blob: 9180c09af036bd8375fcd06c7f7c9c08feb1ff7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import strings

type MyInt = int

const maxn = 100000

fn test_sb() {
	mut sb := strings.new_builder(100)
	sb.write_string('hi')
	sb.write_string('!')
	sb.write_string('hello')
	assert sb.len == 8
	sb_end := sb.str()
	assert sb_end == 'hi!hello'
	assert sb.len == 0
	///
	sb = strings.new_builder(10)
	sb.write_string('a')
	sb.write_string('b')
	assert sb.len == 2
	assert sb.str() == 'ab'
	// Test interpolation optimization
	sb = strings.new_builder(10)
	x := 10
	y := MyInt(20)
	sb.writeln('x = $x y = $y')
	res := sb.str()
	assert res[res.len - 1] == `\n`
	println('"$res"')
	assert res.trim_space() == 'x = 10 y = 20'
	//
	sb = strings.new_builder(10)
	sb.write_string('x = $x y = $y')
	assert sb.str() == 'x = 10 y = 20'
	//$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'
	//}
}

fn test_big_sb() {
	mut sb := strings.new_builder(100)
	mut sb2 := strings.new_builder(10000)
	for i in 0 .. maxn {
		sb.writeln(i.str())
		sb2.write_string('+')
	}
	s := sb.str()
	lines := s.split_into_lines()
	assert lines.len == maxn
	assert lines[0] == '0'
	assert lines[1] == '1'
	assert lines[777] == '777'
	assert lines[98765] == '98765'
	println(sb2.len)
	assert sb2.len == maxn
}

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)
		count++
		assert count == sb.len
	}
	sb_final := sb.str()
	assert sb_final == temp_str
}

fn test_strings_builder_reuse() {
	mut sb := strings.new_builder(256)
	sb.write_string('world')
	assert sb.str() == 'world'
	sb.write_string('hello')
	assert sb.str() == 'hello'
}

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
}

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'
}