public void PacksBitsCorrectly() { var packed = Packed.Create(new[] { true, false, true }); Assert.AreEqual(1, packed.PackedBytes.Count); Assert.AreEqual(5, packed.PackedBytes[0]); }
public void GetSameBitsAsWerePutIn() { var packed = Packed.Create(new[] { true, false, true, false }); Assert.IsTrue(packed.GetBit(1)); Assert.IsTrue(packed.GetBit(3)); }
public void ClonesCorrectly() { var packed = Packed.Create(new byte[] { 0x10, 0x01, 0x02, 0xF3 }); var cloned = packed.Clone(); Assert.IsTrue(packed.GetBytes().SequenceEqual(cloned.GetBytes())); }
public void XorsCorrectly() { var packed1 = Packed.Create(new byte[] { 0x00, 0x01, 0x02, 0x03 }); var packed2 = Packed.Create(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }); packed1.Xor(packed2); Assert.IsTrue(packed1.GetBytes().SequenceEqual(new byte[] { 0xFF, 0xFE, 0xFD, 0xFC }.Concat(Enumerable.Repeat(default(byte), 4)))); }
public void CanGetBackSameBytes() { var bytes = new byte[] { 0x01, 0x02, 0xF3, 0xE4 }; var packed = Packed.Create(bytes); var newBytes = packed.GetBytes().Take(4); Assert.IsTrue(newBytes.SequenceEqual(bytes)); }
/// <summary> /// Creates a new <see cref="Slice"/> from the given <paramref name="coefficients"/> and /// <paramref name="data"/>. /// </summary> public static Slice Create( IReadOnlyCollection <bool> coefficients, IReadOnlyCollection <byte> data) => new Slice( numCoefficients: coefficients.Count, numData: data.Count, packedCoefficients: Packed.Create(coefficients.ToBytes()), packedData: Packed.Create(data) );
public void DoesNotSolveUnsolvableSystem() { var coefficients = new List <Packed> { Packed.Create(new[] { true, false, false }) }; var solution = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); if (solution.Count > 0) { Assert.IsTrue(solution[solution.Count].Operation != Operation.Complete); } }
public void SolvesComplicatedSystem() { var coefficients = new List <Packed> { Packed.Create(new[] { true, false, true }), Packed.Create(new[] { true, true, true }), Packed.Create(new[] { false, false, true }) }; var steps = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); Assert.IsTrue(steps.Count > 0); Assert.AreEqual(Operation.Complete, steps[steps.Count - 1].Operation); AssertIsSolved(coefficients, 3); }
public void ProducesStepsWhichResultInIdenticalChangesAndASolution() { var coefficients = new List <Packed> { Packed.Create(new [] { true, true, true }), Packed.Create(new [] { false, true, true }), Packed.Create(new [] { false, true, false }) }; var copy = coefficients.Select(x => x.Clone()).ToList(); var steps = GaussianEliminationHelpers.Solve(coefficients, 3).ToList(); // Assert that it has been solved Assert.IsTrue(steps.Count > 0); Assert.AreEqual(Operation.Complete, steps[steps.Count - 1].Operation); AssertIsSolved(coefficients, 3); // Perform the steps on the copy var mappedOperations = new Dictionary <Operation, Action <int, int, IList <Packed> > > { { Operation.Swap, (from, to, list) => { var temp = list[to]; list[to] = list[from]; list[from] = temp; } }, { Operation.Xor, (from, to, list) => list[to].Xor(list[from]) } }; foreach (var step in steps) { if (!mappedOperations.TryGetValue(step.Operation, out var operation)) { continue; } operation.Invoke(step.From, step.To, copy); } // Assert that the copy has been solved AssertIsSolved(copy, 3); }
public void GetSameBitsAsBytes() { var packed = Packed.Create(new byte[] { 0x55, 0xFF }); var bits = Enumerable.Range( 0, 16) .Select(x => packed.GetBit(x)) .ToList(); var expectedSequence = new[] { true, false, true, false, true, false, true, false, true, true, true, true, true, true, true, true, }; Assert.IsTrue(expectedSequence.SequenceEqual(bits)); }