Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builtin/moon.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ options(
"stringbuilder_buffer.mbt": [ "not", "js" ],
"stringbuilder_buffer_wbtest.mbt": [ "not", "js" ],
"stringbuilder_concat.mbt": [ "js" ],
"stringbuilder_grow_wbtest.mbt": [ "not", "js" ],
},
)
12 changes: 12 additions & 0 deletions builtin/stringbuilder_bench_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ test "bench StringBuilder::write_char non-BMP n=4096" (it : @bench.T) {
})
}

///|
test "bench StringBuilder growth from minimum capacity n=4096" (it : @bench.T) {
let piece = stringbuilder_bench_piece
it.bench(fn() {
let builder = StringBuilder(size_hint=1)
for _ in 0..<stringbuilder_bench_repeats {
builder.write_string(piece)
}
it.keep(builder.to_string().length())
})
}

///|
test "bench StringBuilder::reset reuse spare capacity n=4096" (it : @bench.T) {
let piece = stringbuilder_bench_piece
Expand Down
36 changes: 36 additions & 0 deletions builtin/stringbuilder_grow_wbtest.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "StringBuilder growth_capacity doubles until the requirement is covered" {
inspect(stringbuilder_growth_capacity(1, 0, 100), content="128")
inspect(stringbuilder_growth_capacity(3, 0, 24), content="24")
inspect(stringbuilder_growth_capacity(16, 0, 16), content="16")
inspect(stringbuilder_growth_capacity(16, 0, 17), content="32")
}

///|
test "StringBuilder growth_capacity survives Int overflow" {
// 2^30 doubles to INT_MIN; the old loop then went 0 -> 0 -> ... forever.
let big = 1 << 30
inspect(
stringbuilder_growth_capacity(big, 0, big + 1) == big + 1,
content="true",
)
// A non-power-of-two start can overflow to a negative value on the way up.
inspect(
stringbuilder_growth_capacity(11, 0, 1_500_000_000),
content="1500000000",
)
}
Loading