Last active
March 27, 2019 15:51
-
-
Save antmdvs/b8c77a583284f72eb439c2da0a1c37bb to your computer and use it in GitHub Desktop.
Palindromes in C# 7. Test-driven using xUnit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is API-compatible with .NET Standard 1.2. | |
// https://docs.microsoft.com/en-us/dotnet/standard/net-standard#which-net-standard-version-to-target | |
using System; | |
using System.Linq; | |
namespace Palindrome | |
{ | |
public class PalindromeChecker : IPalindromeChecker | |
{ | |
public bool IsPalindrome(string input) | |
{ | |
if (string.IsNullOrWhiteSpace(input)) | |
{ | |
return false; | |
} | |
var loweredLettersAndNumbers = input | |
.Where(char.IsLetterOrDigit) | |
.Select(char.ToLower); | |
if (!loweredLettersAndNumbers.Any()) | |
{ | |
// we need letters or numbers to qualify as a palindrome | |
return false; | |
} | |
// do the reversed characters match? if so, we have a palindrome. | |
return loweredLettersAndNumbers | |
.Reverse() | |
.SequenceEqual(loweredLettersAndNumbers); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
namespace XUnitTestProject1 | |
{ | |
public class PalindromeTests | |
{ | |
[Theory] | |
[InlineData("madam", true)] | |
[InlineData("nurses run", true)] | |
[InlineData("Nurses run", true)] | |
[InlineData("nur sesrun", true /*?*/)] // needs requirements clarification - this is nonsensical, but is it a palindrome? | |
[InlineData("Madam, I'm Adam.", true)] // from https://www.dictionary.com/browse/palindrome | |
[InlineData("12321", true)] | |
[InlineData("12320", false)] | |
[InlineData("a", true)] // single-letter word | |
[InlineData("b", true /*?*/)] // needs requirements clarification. - While "a" is a single-letter word, "b" is NOT a word by itself so it doesn't really "read" backwords nor forwards according to the definition of palindrome. | |
[InlineData("ab", false)] | |
[InlineData("", false)] | |
[InlineData(" ", false)] | |
[InlineData(" , ", false)] | |
[InlineData("_", false)] // _ is part of the \w regex character class (https://gist.github.com/antmdvs/b8c77a583284f72eb439c2da0a1c37bb/revisions#diff-7c21bc6041149a1858ff9271bd964b41L21) | |
[InlineData(null, false)] // since we don't have non-nullable reference types (yet) :) | |
public void ShouldCorrectlyIdentifyPalindromes(string input, bool expected) | |
{ | |
var sut = new PalindromeChecker(); | |
var result = sut.IsPalindrome(input); | |
Assert.Equal(expected, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment