|
| 1 | +using Algorithms.Numeric; |
| 2 | +using NUnit.Framework; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace Algorithms.Tests.Numeric |
| 7 | +{ |
| 8 | + public class AutomorphicNumberTests |
| 9 | + { |
| 10 | + [TestCase(1)] |
| 11 | + [TestCase(5)] |
| 12 | + [TestCase(6)] |
| 13 | + [TestCase(25)] |
| 14 | + [TestCase(76)] |
| 15 | + [TestCase(376)] |
| 16 | + [TestCase(625)] |
| 17 | + [TestCase(9376)] |
| 18 | + [TestCase(90625)] |
| 19 | + [TestCase(109376)] |
| 20 | + |
| 21 | + public void TestAutomorphicNumbers(int number) |
| 22 | + { |
| 23 | + Assert.That(AutomorphicNumber.IsAutomorphic(number), Is.True); |
| 24 | + } |
| 25 | + |
| 26 | + [TestCase(2)] |
| 27 | + [TestCase(3)] |
| 28 | + [TestCase(7)] |
| 29 | + [TestCase(18)] |
| 30 | + [TestCase(79)] |
| 31 | + [TestCase(356)] |
| 32 | + [TestCase(623)] |
| 33 | + [TestCase(9876)] |
| 34 | + [TestCase(90635)] |
| 35 | + [TestCase(119376)] |
| 36 | + [TestCase(891625)] |
| 37 | + [TestCase(2990625)] |
| 38 | + [TestCase(7209376)] |
| 39 | + [TestCase(12891625)] |
| 40 | + [TestCase(87129396)] |
| 41 | + public void TestNonAutomorphicNumbers(int number) |
| 42 | + { |
| 43 | + Assert.That(AutomorphicNumber.IsAutomorphic(number), Is.False); |
| 44 | + } |
| 45 | + |
| 46 | + [TestCase(0)] |
| 47 | + [TestCase(-1)] |
| 48 | + public void TestInvalidAutomorphicNumbers(int number) |
| 49 | + { |
| 50 | + Assert.Throws(Is.TypeOf<ArgumentException>() |
| 51 | + .And.Message.EqualTo($"An automorphic number must always be positive."), |
| 52 | + delegate |
| 53 | + { |
| 54 | + AutomorphicNumber.IsAutomorphic(number); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + [TestCase(1, 100)] |
| 59 | + public void TestAutomorphicNumberSequence(int lower, int upper) |
| 60 | + { |
| 61 | + List<long> automorphicList = new() { 1, 5, 6, 25, 76 }; |
| 62 | + Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList)); |
| 63 | + } |
| 64 | + |
| 65 | + [TestCase(8, 12)] |
| 66 | + public void TestNoAutomorphicNumberInTheSequence(int lower, int upper) |
| 67 | + { |
| 68 | + List<long> automorphicList = new(); |
| 69 | + Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList)); |
| 70 | + } |
| 71 | + |
| 72 | + [TestCase(25,25)] |
| 73 | + public void TestAutomorphicNumberSequenceSameBounds(int lower, int upper) |
| 74 | + { |
| 75 | + List<long> automorphicList = new() { 25 }; |
| 76 | + Assert.That(AutomorphicNumber.GetAutomorphicNumbers(lower, upper), Is.EqualTo(automorphicList)); |
| 77 | + } |
| 78 | + |
| 79 | + [TestCase(-1,1)] |
| 80 | + [TestCase(0, 1)] |
| 81 | + public void TestAutomorphicNumberSequenceInvalidLowerBound(int lower, int upper) |
| 82 | + { |
| 83 | + Assert.Throws(Is.TypeOf<ArgumentException>() |
| 84 | + .And.Message.EqualTo($"Lower bound must be greater than 0."), |
| 85 | + delegate |
| 86 | + { |
| 87 | + AutomorphicNumber.GetAutomorphicNumbers(lower, upper); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + [TestCase(1, -1)] |
| 92 | + [TestCase(10, -1)] |
| 93 | + public void TestAutomorphicNumberSequenceInvalidUpperBound(int lower, int upper) |
| 94 | + { |
| 95 | + Assert.Throws(Is.TypeOf<ArgumentException>() |
| 96 | + .And.Message.EqualTo($"Upper bound must be greater than 0."), |
| 97 | + delegate |
| 98 | + { |
| 99 | + AutomorphicNumber.GetAutomorphicNumbers(lower, upper); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + [TestCase(25, 2)] |
| 104 | + public void TestAutomorphicNumberSequenceReversedBounds(int lower, int upper) |
| 105 | + { |
| 106 | + Assert.Throws(Is.TypeOf<ArgumentException>() |
| 107 | + .And.Message.EqualTo($"The lower bound must be less than or equal to the upper bound."), |
| 108 | + delegate |
| 109 | + { |
| 110 | + AutomorphicNumber.GetAutomorphicNumbers(lower, upper); |
| 111 | + }); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments