|
| 1 | +using Algorithms.Numeric; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace Algorithms.Tests.Numeric; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Tests for the Sigmoid class, which implements the sigmoid activation function. |
| 9 | +/// </summary> |
| 10 | +public static class SigmoidTests |
| 11 | +{ |
| 12 | + // Standard tolerance for floating-point comparisons. |
| 13 | + private const double Tolerance = 1e-15; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Tests that the sigmoid function correctly calculates the center point (x=0). |
| 17 | + /// Sigmoid(0) should equal 0.5. |
| 18 | + /// </summary> |
| 19 | + [Test] |
| 20 | + public static void GetsCenterValue() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + double x = 0.0; |
| 24 | + double expected = 0.5; |
| 25 | + |
| 26 | + // Act |
| 27 | + var result = Sigmoid.Calculate(x); |
| 28 | + |
| 29 | + // Assert |
| 30 | + Assert.That(result, Is.EqualTo(expected).Within(Tolerance)); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Tests that the sigmoid function approaches 1 for large positive inputs (asymptotic behavior). |
| 35 | + /// </summary> |
| 36 | + [Test] |
| 37 | + public static void GetsAsymptoticValueForLargePositiveX() |
| 38 | + { |
| 39 | + // Arrange |
| 40 | + double x = 100.0; |
| 41 | + double expected = 1.0; |
| 42 | + |
| 43 | + // Act |
| 44 | + var result = Sigmoid.Calculate(x); |
| 45 | + |
| 46 | + // Assert |
| 47 | + // The result should be extremely close to 1.0. |
| 48 | + Assert.That(result, Is.EqualTo(expected).Within(1e-10)); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Tests that the sigmoid function approaches 0 for large negative inputs (asymptotic behavior). |
| 53 | + /// </summary> |
| 54 | + [Test] |
| 55 | + public static void GetsAsymptoticValueForLargeNegativeX() |
| 56 | + { |
| 57 | + // Arrange |
| 58 | + double x = -100.0; |
| 59 | + double expected = 0.0; |
| 60 | + |
| 61 | + // Act |
| 62 | + var result = Sigmoid.Calculate(x); |
| 63 | + |
| 64 | + // Assert |
| 65 | + // The result should be extremely close to 0.0. |
| 66 | + Assert.That(result, Is.EqualTo(expected).Within(1e-10)); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Tests the sigmoid calculation for various general positive and negative values. |
| 71 | + /// Values are confirmed against a reference calculation (or manually verified). |
| 72 | + /// </summary> |
| 73 | + /// <param name="input">The input value.</param> |
| 74 | + /// <param name="expected">The expected sigmoid output.</param> |
| 75 | + [TestCase(1.0, 0.7310585786300049)] |
| 76 | + [TestCase(5.0, 0.9933071490757153)] |
| 77 | + [TestCase(-1.0, 0.2689414213699951)] |
| 78 | + [TestCase(-5.0, 0.006692850924284855)] |
| 79 | + [TestCase(0.5, 0.6224593312018546)] |
| 80 | + [TestCase(-0.5, 0.3775406687981454)] |
| 81 | + public static void GetsStandardSigmoidValues(double input, double expected) |
| 82 | + { |
| 83 | + // Act |
| 84 | + var result = Sigmoid.Calculate(input); |
| 85 | + |
| 86 | + // Assert |
| 87 | + Assert.That(result, Is.EqualTo(expected).Within(Tolerance)); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Tests that the calculation correctly handles floating-point values and large numbers. |
| 92 | + /// </summary> |
| 93 | + [Test] |
| 94 | + public static void HandlesFractionalAndLargeInput() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + double x1 = 3.14159; // PI approximation |
| 98 | + // Corrected expected value: 1 / (1 + e^-3.14159) |
| 99 | + double expected1 = 0.9585760624650355; |
| 100 | + |
| 101 | + double x2 = -20.0; |
| 102 | + // Expected = 1 / (1 + e^20) - Should be very close to 0 |
| 103 | + double expected2 = 2.0611536224385583E-9; |
| 104 | + |
| 105 | + // Act & Assert 1 |
| 106 | + var result1 = Sigmoid.Calculate(x1); |
| 107 | + Assert.That(result1, Is.EqualTo(expected1).Within(Tolerance)); |
| 108 | + |
| 109 | + // Act & Assert 2 |
| 110 | + var result2 = Sigmoid.Calculate(x2); |
| 111 | + Assert.That(result2, Is.EqualTo(expected2).Within(Tolerance)); |
| 112 | + } |
| 113 | +} |
0 commit comments