Skip to content

Commit eaa18be

Browse files
varunu28linkdotnet
authored andcommitted
Adding more tests
1 parent ca1154d commit eaa18be

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

stringbuilder_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,27 @@ func TestReplaceRune(t *testing.T) {
430430
}
431431
}
432432

433+
func TestReplaceRuneConcurrent(t *testing.T) {
434+
sb := NewStringBuilderFromString("Hello")
435+
436+
var wg sync.WaitGroup
437+
wg.Add(NumOfThreads)
438+
for i := 0; i < NumOfThreads; i++ {
439+
go func() {
440+
defer wg.Done()
441+
for j := 0; j < NumOfIterations; j++ {
442+
sb.ReplaceRune('l', 'm')
443+
}
444+
}()
445+
}
446+
447+
wg.Wait()
448+
449+
if got := sb.ToString(); got != "Hemmo" {
450+
t.Errorf("StringBuilder.ReplaceRune() = %v, want %v", got, "Hemmo")
451+
}
452+
}
453+
433454
func TestReplace(t *testing.T) {
434455
tests := []struct {
435456
name string
@@ -546,6 +567,28 @@ func TestReuseReversedStringBuilder(t *testing.T) {
546567
}
547568
}
548569

570+
func TestReverseStringBuilderConcurrent(t *testing.T) {
571+
sb := NewStringBuilderFromString("ABC")
572+
573+
var wg sync.WaitGroup
574+
wg.Add(NumOfThreads)
575+
for i := 0; i < NumOfThreads; i++ {
576+
go func() {
577+
defer wg.Done()
578+
for j := 0; j < NumOfIterations; j++ {
579+
sb = sb.Reverse()
580+
}
581+
}()
582+
}
583+
584+
wg.Wait()
585+
586+
expected := "ABC" // We expect the original string as we have reversed NumOfThreads * NumOfIterations times which is even
587+
if got := sb.ToString(); got != expected {
588+
t.Errorf("StringBuilder.Reverse() = %v, want %v", got, expected)
589+
}
590+
}
591+
549592
func TestStringBuilderSubstring(t *testing.T) {
550593
tests := []struct {
551594
name string

0 commit comments

Comments
 (0)