|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Numerics; |
| 4 | +using Algorithms.Sequences; |
| 5 | +using FluentAssertions; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +namespace Algorithms.Tests.Sequences; |
| 9 | + |
| 10 | +[TestFixture] |
| 11 | +public class OnesCountingSequenceTest |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// <para> |
| 15 | + /// Values taken from http://oeis.org/A000120/b000120.txt. |
| 16 | + /// </para> |
| 17 | + /// <para> |
| 18 | + /// While the file contains 10,000 values, this only tests 1000. |
| 19 | + /// </para> |
| 20 | + /// </summary> |
| 21 | + private readonly BigInteger[] oeisValues = { |
| 22 | + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, |
| 23 | + 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, |
| 24 | + 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, |
| 25 | + 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, |
| 26 | + 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, |
| 27 | + 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, |
| 28 | + 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, |
| 29 | + 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4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, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, |
| 55 | + 6, 6, 7, 6, 7, 7, 8, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, |
| 56 | + 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, |
| 57 | + 7, 6, 7, 7, 8, 5, 6, 6, 7, 6, 7, 7, 8, 6, 7, 7, 8, 7, 8, 8, 9, 3, 4, 4, 5, |
| 58 | + 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, |
| 59 | + 7, 7, 8, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8, 5, 6, 6, 7, 6, 7, |
| 60 | + 7, 8, 6, 7, 7, 8, 7, 8, 8, 9, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, |
| 61 | + 8, 5, 6, 6, 7, 6, 7, 7, 8, 6, 7, 7, 8, 7, 8, 8, 9, 5, 6, 6, 7, 6, 7, 7, 8, |
| 62 | + }; |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// <para> |
| 66 | + /// Performs number of ones in the binary representation of a BigInteger. |
| 67 | + /// </para> |
| 68 | + /// <para> |
| 69 | + /// This is used as a check to compare the provided values from OEIS. |
| 70 | + /// </para> |
| 71 | + /// </summary> |
| 72 | + /// <param name="i">BigInteger value to count 1s in</param> |
| 73 | + /// <returns>Number of 1s in binary representation of number.</returns> |
| 74 | + private int CountOnes(BigInteger i) |
| 75 | + { |
| 76 | + var temp = i; |
| 77 | + BigInteger remainder = 0; |
| 78 | + var result = 0; |
| 79 | + |
| 80 | + while (temp != BigInteger.Zero) { |
| 81 | + temp = BigInteger.DivRem(temp, 2, out remainder); |
| 82 | + result += remainder.IsOne ? 1 : 0; |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + [Test] |
| 90 | + public void Count1000() |
| 91 | + { |
| 92 | + // Compare generated sequence against provided data |
| 93 | + var sequence = new OnesCountingSequence().Sequence.Take(oeisValues.Length); |
| 94 | + sequence.SequenceEqual(oeisValues).Should().BeTrue(); |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void CompareAgainstCalculated() |
| 99 | + { |
| 100 | + // Calculate 1s in binary value the old fashioned way. |
| 101 | + var calculated = new List<BigInteger>(); |
| 102 | + for (var i = 0; i < oeisValues.Length; i++) { |
| 103 | + calculated.Add(CountOnes(new BigInteger(i))); |
| 104 | + } |
| 105 | + |
| 106 | + calculated.SequenceEqual(oeisValues).Should().BeTrue(); |
| 107 | + } |
| 108 | +} |
0 commit comments