Files
coding-practice/problems/3227-vowels-game-in-a-string/test/SolutionTests.cs

168 lines
4.8 KiB
C#
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 單元測試xUnit
// 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.
using Xunit;
public class SolutionTests
{
private readonly Solution _s = new Solution();
[Fact]
public void Example1_StringWithVowels_AliceWins()
{
// Arrange
var input = "leetcode"; // contains vowels: e,e,o,e
var expected = true; // Alice wins
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void Example2_StringWithoutVowels_BobWins()
{
// Arrange
var input = "bcdf"; // no vowels
var expected = false; // Bob wins (Alice can't make first move)
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void AllVowels_AliceWins()
{
// Arrange
var input = "aeiou"; // all vowels (odd count = 5)
var expected = true; // Alice removes entire string
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void SingleVowel_AliceWins()
{
// Arrange
var input = "a"; // single vowel
var expected = true; // Alice removes it immediately
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void SingleConsonant_BobWins()
{
// Arrange
var input = "b"; // single consonant, no vowels
var expected = false; // Alice can't move
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void MixedString_EvenVowelCount_AliceWins()
{
// Arrange
var input = "programming"; // vowels: o,a,i (3 vowels, odd)
var expected = true; // Alice can remove all vowels at once
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void LongStringNoVowels_BobWins()
{
// Arrange
var input = "bcdfghjklmnpqrstvwxyz"; // all consonants
var expected = false; // No vowels = Bob wins
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void RepeatedVowels_AliceWins()
{
// Arrange
var input = "aaaa"; // 4 vowels (even count)
var expected = true; // Alice can remove 3, leaving 1 (Bob can't move)
// Act
var got = _s.DoesAliceWin(input);
// Assert
Assert.Equal(expected, got);
}
[Fact]
public void EdgeCases()
{
// Single character tests
Assert.True(_s.DoesAliceWin("e")); // vowel
Assert.False(_s.DoesAliceWin("x")); // consonant
// Two character tests
Assert.True(_s.DoesAliceWin("ab")); // has vowel 'a'
Assert.False(_s.DoesAliceWin("xy")); // no vowels
// Vowel at different positions
Assert.True(_s.DoesAliceWin("xabc")); // vowel at start-middle
Assert.True(_s.DoesAliceWin("abcx")); // vowel in middle
Assert.True(_s.DoesAliceWin("xbca")); // vowel at end
}
[Fact]
public void GameTheoryValidation()
{
// Test cases that validate the game theory logic:
// If total vowels = 0: Alice loses (can't make first move)
Assert.False(_s.DoesAliceWin("bcdfg"));
// If total vowels = odd: Alice wins (removes all vowels in one move)
Assert.True(_s.DoesAliceWin("hello")); // e,o = 2 vowels (even), but Alice still wins
Assert.True(_s.DoesAliceWin("beautiful")); // e,a,u,i,u = 5 vowels (odd)
// If total vowels = even > 0: Alice wins (leaves exactly 1 vowel for Bob)
Assert.True(_s.DoesAliceWin("code")); // o,e = 2 vowels (even)
Assert.True(_s.DoesAliceWin("education")); // e,u,a,i,o = 5 vowels, but any vowels = Alice wins
}
[Theory]
[InlineData("", false)] // empty string - no vowels
[InlineData("aeiou", true)] // all vowels
[InlineData("bcdfg", false)] // all consonants
[InlineData("hello", true)] // mixed with vowels
[InlineData("rhythm", false)] // no vowels (y not considered vowel)
[InlineData("queue", true)] // multiple same vowels
public void ParameterizedTests(string input, bool expected)
{
// Act & Assert
Assert.Equal(expected, _s.DoesAliceWin(input));
}
}