Files
coding-practice/problems/3227-vowels-game-in-a-string/test/main_test.go

189 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// LeetCode 3227: Vowels Game in a String 單元測試Go testing
// Problem: Alice and Bob play a game. Alice removes substrings with odd vowels,
// Bob removes substrings with even vowels. Alice wins if string has any vowels.
package vowels_test
import (
"fmt"
"testing"
vowels "leetcode/3227-vowels-game-in-a-string/go"
)
func TestExample1(t *testing.T) {
// "leetcode" contains vowels: e,e,o,e
input := "leetcode"
got := vowels.DoesAliceWin(input)
want := true // Alice wins
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestExample2(t *testing.T) {
// "bcdf" has no vowels
input := "bcdf"
got := vowels.DoesAliceWin(input)
want := false // Bob wins (Alice can't make first move)
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestAllVowels(t *testing.T) {
// All vowels string - Alice removes entire string
input := "aeiou"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestSingleVowel(t *testing.T) {
// Single vowel - Alice removes it immediately
input := "a"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestSingleConsonant(t *testing.T) {
// Single consonant - Alice can't move
input := "b"
got := vowels.DoesAliceWin(input)
want := false
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestMixedString(t *testing.T) {
// "programming" has vowels: o,a,i
input := "programming"
got := vowels.DoesAliceWin(input)
want := true // Alice can remove vowels
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestLongStringNoVowels(t *testing.T) {
// All consonants - no vowels means Bob wins
input := "bcdfghjklmnpqrstvwxyz"
got := vowels.DoesAliceWin(input)
want := false
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestRepeatedVowels(t *testing.T) {
// Even number of same vowels - Alice can leave 1 for Bob
input := "aaaa"
got := vowels.DoesAliceWin(input)
want := true
if got != want {
t.Fatalf("input %q: want %v got %v", input, want, got)
}
}
func TestEdgeCases(t *testing.T) {
testCases := []struct {
input string
want bool
desc string
}{
{"e", true, "single vowel"},
{"x", false, "single consonant"},
{"ab", true, "has vowel 'a'"},
{"xy", false, "no vowels"},
{"xabc", true, "vowel in middle"},
{"abcx", true, "vowel at start"},
{"xbca", true, "vowel at end"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Fatalf("input %q (%s): want %v got %v", tc.input, tc.desc, tc.want, got)
}
})
}
}
func TestGameTheoryValidation(t *testing.T) {
testCases := []struct {
input string
want bool
desc string
}{
// If total vowels = 0: Alice loses
{"bcdfg", false, "no vowels - Alice can't move"},
{"rhythm", false, "no vowels (y not counted)"},
// Any vowels > 0: Alice wins
{"hello", true, "e,o vowels - Alice wins"},
{"beautiful", true, "e,a,u,i,u vowels - Alice wins"},
{"code", true, "o,e vowels - Alice wins"},
{"education", true, "e,u,a,i,o vowels - Alice wins"},
{"queue", true, "u,e,u,e vowels - Alice wins"},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Fatalf("input %q (%s): want %v got %v", tc.input, tc.desc, tc.want, got)
}
})
}
}
func TestParameterized(t *testing.T) {
testCases := []struct {
input string
want bool
}{
{"", false}, // empty string
{"aeiou", true}, // all vowels
{"bcdfg", false}, // all consonants
{"hello", true}, // mixed with vowels
{"rhythm", false}, // no vowels
{"queue", true}, // multiple same vowels
{"xyz", false}, // short no vowels
{"programming", true}, // longer mixed
}
for _, tc := range testCases {
t.Run(tc.input, func(t *testing.T) {
got := vowels.DoesAliceWin(tc.input)
if got != tc.want {
t.Errorf("doesAliceWin(%q) = %v, want %v", tc.input, got, tc.want)
}
})
}
}
// Benchmark test
func BenchmarkDoesAliceWin(b *testing.B) {
testString := "abcdefghijklmnopqrstuvwxyz"
b.ResetTimer()
for i := 0; i < b.N; i++ {
vowels.DoesAliceWin(testString)
}
}
// Example test for documentation
func ExampleDoesAliceWin() {
result1 := vowels.DoesAliceWin("leetcode")
result2 := vowels.DoesAliceWin("bcdf")
fmt.Println(result1)
fmt.Println(result2)
// Output:
// true
// false
}