Skip to content

Commit

Permalink
Added support for multiplication functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tacosontitan committed Jul 21, 2024
1 parent d25f741 commit 3bdca00
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
139 changes: 139 additions & 0 deletions src/Hussy.Net/Math/Multiplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2024 tacosontitan and contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System.Numerics;

namespace Hussy.Net;

public static partial class Hussy
{
/// <summary>
/// Multiplies <c>2</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>2</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>2</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult2<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 2.To<T>();

/// <summary>
/// Multiplies <c>3</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>3</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>3</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult3<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 3.To<T>();

/// <summary>
/// Multiplies <c>4</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>4</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>4</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult4<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 4.To<T>();

/// <summary>
/// Multiplies <c>5</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>5</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>5</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult5<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 5.To<T>();

/// <summary>
/// Multiplies <c>6</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>6</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>6</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult6<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 6.To<T>();

/// <summary>
/// Multiplies <c>7</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>7</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>7</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult7<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 7.To<T>();

/// <summary>
/// Multiplies <c>8</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>1</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>8</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult8<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 8.To<T>();

/// <summary>
/// Multiplies <c>9</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>9</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>9</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult9<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 9.To<T>();

/// <summary>
/// Multiplies <c>10</c> by the specified <paramref name="input"/>.
/// </summary>
/// <param name="input">The input to multiply <c>10</c> by.</param>
/// <typeparam name="T">Specifies the type of <paramref name="input"/>.</typeparam>
/// <returns>
/// The specified <paramref name="input"/> decremented by <c>10</c>.
/// </returns>
/// <remarks>This method is designed to shorten functions being passed as delegates.</remarks>
public static T Mult10<T>(T input)
where T : IMultiplyOperators<T, T, T> =>
input * 10.To<T>();
}
58 changes: 58 additions & 0 deletions test/Hussy.Net.Tests/Math/MultiplicationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2024 tacosontitan and contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace Hussy.Net.Tests.Math;

public class MultiplicationTests
{
private const double TestValue = 2;

[Theory]
[InlineData(2)]
[InlineData(3)]
[InlineData(4)]
[InlineData(5)]
[InlineData(6)]
[InlineData(7)]
[InlineData(8)]
[InlineData(9)]
[InlineData(10)]
public void Multiplication_ProducesCorrectResult(double target)
{
var expectedResult = TestValue * target;
var testFunction = GetFunction(target);
var actualResult = testFunction(TestValue);

Assert.Equal(expectedResult, actualResult);

#pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).

static Func<double, double> GetFunction(double target) => target switch
{
2 => Mult2,
3 => Mult3,
4 => Mult4,
5 => Mult5,
6 => Mult6,
7 => Mult7,
8 => Mult8,
9 => Mult9,
10 => Mult10
};

#pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive).
}
}

0 comments on commit 3bdca00

Please sign in to comment.