Skip to content

Commit

Permalink
Merge pull request #5 from markcastle/feature/benchmarking_and_improving
Browse files Browse the repository at this point in the history
Feature/benchmarking and improving
  • Loading branch information
markcastle committed Apr 22, 2023
2 parents 5f1388e + 8059c5a commit 06285ac
Show file tree
Hide file tree
Showing 15 changed files with 904 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2019
image: Visual Studio 2022

before_build:
- nuget restore
18 changes: 18 additions & 0 deletions CaseConverter.Benchmarks/CaseConverter.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CaseConverter\CaseConverter.csproj" />
</ItemGroup>

</Project>
62 changes: 62 additions & 0 deletions CaseConverter.Benchmarks/CaseConverterBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using BenchmarkDotNet.Attributes;

namespace CaseConverter.Benchmarks;

[MemoryDiagnoser]
public class CaseConverterBenchmarks
{
private const string TestString = "ThisIsATestString";
private const string TestString2 = "thisisateststring";

private const string LongTestString =
"This method appears to be efficient already as it utilizes regular expressions, which are highly performant for string operations such as this. However, if you would like an alternative approach that remains compatible with .NET Standard 2.0 and 2.1, you can use a StringBuilder to build a new string while iterating over the input string's characters:";

[Benchmark]
public string ToSnakeCaseBenchmark()
{
return TestString.ToSnakeCase();
}

[Benchmark]
public string ToCamelCaseBenchmark()
{
return TestString.ToCamelCase();
}

[Benchmark]
public string ToKebabCaseBenchmark()
{
return TestString.ToKebabCase();
}

[Benchmark]
public string ToPascalCaseBenchmark()
{
return TestString2.ToPascalCase();
}

[Benchmark]
public string ToTitleCaseBenchmark()
{
return TestString.ToTitleCase();
}

[Benchmark]
public string ToTrainCaseBenchmark()
{
return TestString.ToTrainCase();
}

[Benchmark]
public bool IsAllUpperBenchmark()
{
return TestString.IsAllUpper();
}

[Benchmark]
public string ReplaceWhitespaceBenchmark()
{
return LongTestString.ReplaceWhitespace(".");
}

}
5 changes: 5 additions & 0 deletions CaseConverter.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using CaseConverter.Benchmarks;

Summary summary = BenchmarkRunner.Run<CaseConverterBenchmarks>();
6 changes: 3 additions & 3 deletions CaseConverter.Tests/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ public void Test_GeneralTests()
Assert.IsTrue("THIS-IS-AN-UPPER-CASE-STRING".IsAllUpper());
Assert.IsTrue("THIS_IS_AN_UPPER_CASE_STRING".IsAllUpper());

Assert.IsTrue("word".PascalCaseSingleWord() == "Word");
Assert.IsTrue("pseudopseudohypoparathyroidism".PascalCaseSingleWord() == "Pseudopseudohypoparathyroidism");
Assert.IsTrue("PSEUDOPSEUDOHYPOPARATHYROIDISM".PascalCaseSingleWord() == "Pseudopseudohypoparathyroidism");
Assert.IsTrue("word".ToPascalCase() == "Word");
Assert.IsTrue("pseudopseudohypoparathyroidism".ToPascalCase() == "Pseudopseudohypoparathyroidism");
Assert.IsTrue("PSEUDOPSEUDOHYPOPARATHYROIDISM".ToPascalCase() == "Pseudopseudohypoparathyroidism");

Assert.IsTrue("WordsAreAllYouNeed".InsertCharacterBeforeUpperCase() == "Words Are All You Need");
Assert.IsTrue("WordsAreAllYouNeed".InsertCharacterBeforeUpperCase('+') == "Words+Are+All+You+Need");
Expand Down
53 changes: 53 additions & 0 deletions CaseConverter.Tests/InsertCharacterBeforeUpperCaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CaseConverter.Tests
{
[TestClass]
public class InsertCharacterBeforeUpperCaseTests
{
[TestMethod]
public void TestInsertSpaceBeforeUpperCase()
{
string input = "InsertCharacterBeforeUpperCase";
string expectedOutput = "Insert Character Before Upper Case";
string actualOutput = input.InsertCharacterBeforeUpperCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestInsertUnderscoreBeforeUpperCase()
{
string input = "InsertCharacterBeforeUpperCase";
string expectedOutput = "Insert_Character_Before_Upper_Case";
string actualOutput = input.InsertCharacterBeforeUpperCase('_');
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestEmptyString()
{
string input = "";
string expectedOutput = "";
string actualOutput = input.InsertCharacterBeforeUpperCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestAllLowerCase()
{
string input = "insertcharacterbeforeuppercase";
string expectedOutput = "insertcharacterbeforeuppercase";
string actualOutput = input.InsertCharacterBeforeUpperCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestAllUpperCase()
{
string input = "INSERTCHARACTERBEFOREUPPERCASE";
string expectedOutput = "I N S E R T C H A R A C T E R B E F O R E U P P E R C A S E";
string actualOutput = input.InsertCharacterBeforeUpperCase();
Assert.AreEqual(expectedOutput, actualOutput);
}
}
}
80 changes: 80 additions & 0 deletions CaseConverter.Tests/ToCamelCaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CaseConverter.Tests
{
[TestClass]
public class ToCamelCaseTests
{
[TestMethod]
public void TestBasicString()
{
string input = "hello world";
string expectedOutput = "helloWorld";
string actualOutput = input.ToCamelCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestHyphenatedString()
{
string input = "hello-world";
string expectedOutput = "helloWorld";
string actualOutput = input.ToCamelCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestUnderscoreString()
{
string input = "hello_world";
string expectedOutput = "helloWorld";
string actualOutput = input.ToCamelCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestPreserveLeadingUnderscore()
{
string input = "_hello_world";
string expectedOutput = "_helloWorld";
string actualOutput = input.ToCamelCase(preserveLeadingUnderscore: true);
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestRemoveWhitespace()
{
string input = "hello world";
string expectedOutput = "helloWorld";
string actualOutput = input.ToCamelCase(removeWhitespace: true);
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestPreserveWhitespace()
{
string input = "hello world";
string expectedOutput = "hello world";
string actualOutput = input.ToCamelCase(removeWhitespace: false);
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestEmptyString()
{
string input = "";
string expectedOutput = "";
string actualOutput = input.ToCamelCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestAllUpperCase()
{
string input = "HELLO_WORLD";
string expectedOutput = "helloWorld";
string actualOutput = input.ToCamelCase();
Assert.AreEqual(expectedOutput, actualOutput);
}
}
}
92 changes: 92 additions & 0 deletions CaseConverter.Tests/ToKebabCaseTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CaseConverter.Tests
{
[TestClass]
public class ToKebabCaseTests
{
[TestMethod]
public void TestToKebabCase()
{
string input = "This is a test";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestToKebabCaseWithSpaces()
{
string input = "This is a test";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestToKebabCaseWithUnderscores()
{
string input = "this_is_a_test";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestToKebabCaseWithHyphens()
{
string input = "this-is-a-test";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestEmptyString()
{
string input = "";
string expectedOutput = "";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestAllUpperCase()
{
string input = "THIS IS A TEST";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestAllLowerCase()
{
string input = "this is a test";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestPascalCase()
{
string input = "ThisIsATest";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}

[TestMethod]
public void TestCamelCase()
{
string input = "thisIsATest";
string expectedOutput = "this-is-a-test";
string actualOutput = input.ToKebabCase();
Assert.AreEqual(expectedOutput, actualOutput);
}
}
}
Loading

0 comments on commit 06285ac

Please sign in to comment.