Id DecodeId() { Id id = null; byte prefix1 = Scan(2); switch (prefix1) { case 0: // 0 | 1 byte v = Scan(1); id = new Id.Leaf(v); break; case 1: // (0,1) id = new Id.Node(0, DecodeId()); break; case 2: // (i,0) id = new Id.Node(DecodeId(), 0); break; case 3: // (l,r) id = new Id.Node(DecodeId(), DecodeId()); break; default: ThrowUnexpected(prefix1); break; } return(id); }
public void EqualsShouldReturnFalseWhenComparingComplexNodesWithDifferentValues() { // Arrange Id i1 = new Id.Node(new Id.Node(0, new Id.Node(1, 0)), new Id.Node(1, 0)); // ((0,(1,0)),(1,0)) Id i2 = new Id.Node(new Id.Node(0, new Id.Node(1, 1)), new Id.Node(1, 0)); // ((0,(1,1)),(1,0)) // Act & Assert i1.Equals(i2).Should().BeFalse(); }
public void EqualsShouldReturnTrueWhenComparingComplexNodesWithSameValues() { // Arrange Id i1 = new Id.Node(new Id.Node(0, new Id.Node(1, 0)), new Id.Node(1, 0)); // ((0,(1,0)),(1,0)) Id i2 = new Id.Node(new Id.Node(0, new Id.Node(1, 0)), new Id.Node(1, 0)); // ((0,(1,0)),(1,0)) // Act & Assert i1.Equals(i2).Should().BeTrue(); }
public void ImplicitConversionOperatorShouldReturnNodeWhenTuple() { // Arrange Id i1 = new Id.Node(new Id.Node(0, new Id.Node(1, 0)), new Id.Node(1, 0)); // ((0,(1,0)),(1,0)) Id i2 = ((0, (1, 0)), (1, 0)); // combo of C#7 tuples and implicit conversion operator is wow! // Act & Assert i1.Equals(i2).Should().BeTrue(); }
public void EqualsShouldReturnFalseWhenComparingNodesWithDifferentValues() { // Arrange Id i1 = new Id.Node(0, 0); Id i2 = new Id.Node(0, 1); // Act & Assert i1.Equals(i2).Should().BeFalse(); }
public void EqualsShouldReturnTrueWhenComparingNodesWithSameValues() { // Arrange Id i1 = new Id.Node(0, 1); Id i2 = new Id.Node(0, 1); // Act & Assert i1.Equals(i2).Should().BeTrue(); }
public void NormalizeShouldReturn0WhenNodeIs0and0() { // Arrange Id id = new Id.Node(0, 0); // Act Id normalized = id.Normalize(); // Assert normalized.Should().Be((Id)0); }
public void NormalizeShouldReturnUnchangedWhenNodeCannotBeSimplifiedLike1and0() { // Arrange Id id = new Id.Node(1, 0); // Act Id normalized = id.Normalize(); // Assert normalized.Should().Be(id); }
public void NormalizeShouldReturn1WhenNodeIs1and1() { // Arrange Id id = new Id.Node(1, 1); // Act Id normalized = id.Normalize(); // Assert normalized.Should().Be((Id)1); }
public void SumShouldReturnNode10WhenLeftNode100AndRightNode010() { // Arrange Id i1 = new Id.Node(new Id.Node(1, 0), 0); Id i2 = new Id.Node(new Id.Node(0, 1), 0); // Act Id sum = i1.Sum(i2); // Assert sum.Should().Be(new Id.Node(1, 0)); }
public void SplitShouldReturnNodei00iWhenLeftAndRightNodes() { // split(((0,1),(1,0))) = (((0,1),0),((0,(1,0))) Id id = new Id.Node(new Id.Node(0, 1), new Id.Node(1, 0)); id.Split().Should().Be( new Id.Node( new Id.Node(new Id.Node(0, 1), 0), new Id.Node(0, new Id.Node(1, 0)) ) ); }
public void GetHashCodeShouldNotMatchWhenComplexNodesHaveDifferentValues() { // Arrange Id i1 = new Id.Node(new Id.Node(0, new Id.Node(1, 0)), new Id.Node(1, 0)); // ((0,(1,0)),(1,0)) Id i2 = new Id.Node(new Id.Node(0, new Id.Node(1, 1)), new Id.Node(1, 0)); // ((0,(1,1)),(1,0)) // Act int hash1 = i1.GetHashCode(); int hash2 = i2.GetHashCode(); // Assert hash1.Should().NotBe(hash2); }
public void GetHashCodeShouldNotMatchWhenNodesHaveDifferentValues() { // Arrange Id i1 = new Id.Node(0, 0); Id i2 = new Id.Node(0, 1); // Act int hash1 = i1.GetHashCode(); int hash2 = i2.GetHashCode(); // Assert hash1.Should().NotBe(hash2); }
public void GetHashCodeShouldMatchWhenNodesHaveSameValues() { // Arrange Id i1 = new Id.Node(0, 1); Id i2 = new Id.Node(0, 1); // Act int hash1 = i1.GetHashCode(); int hash2 = i2.GetHashCode(); // Assert hash1.Should().Be(hash2); }
public void MatchShouldInvokeNodeActionWhenIdRepresentsNode() { // Arrange Id id = new Id.Node(0, 1); Action <int> leafAction = Substitute.For <Action <int> >(); Action <Id, Id> nodeAction = Substitute.For <Action <Id, Id> >(); // Act id.Match(leafAction, nodeAction); // Assert leafAction.DidNotReceiveWithAnyArgs(); nodeAction.Received().Invoke(0, 1); }
public void MatchShouldInvokeNodeFuncWhenIdRepresentsNode() { // Arrange Id id = new Id.Node(0, 1); Func <int, object> leafFn = Substitute.For <Func <int, object> >(); Func <Id, Id, object> nodeFn = Substitute.For <Func <Id, Id, object> >(); // Act id.Match(leafFn, nodeFn); // Assert leafFn.DidNotReceiveWithAnyArgs(); nodeFn.Received().Invoke(0, 1); }
public void SplitShouldReturnNode0i0iWhenLeftLeaf0() { // split((0,1)) = ((0,(1,0)),(0,(0,1))) Id id1 = new Id.Node(0, 1); id1.Split().Should().Be( new Id.Node( new Id.Node(0, new Id.Node(1, 0)), new Id.Node(0, new Id.Node(0, 1)) ) ); // split((0,(0,1))) = ((0,(0,(1,0))),(0,(0,(0,1)))) Id id2 = new Id.Node(0, new Id.Node(0, 1)); id2.Split().Should().Be( new Id.Node( new Id.Node(0, new Id.Node(0, new Id.Node(1, 0))), new Id.Node(0, new Id.Node(0, new Id.Node(0, 1))) )); }
public void SplitShouldReturnNodei0i0WhenRightLeaf0() { // split((1,0)) = (((1,0),0),((0,1),0)) Id id1 = new Id.Node(1, 0); id1.Split().Should().Be( new Id.Node( new Id.Node(new Id.Node(1, 0), 0), new Id.Node(new Id.Node(0, 1), 0) ) ); // split(((1,0),0)) = ((((1,0),0),0),(((0,1),0),0)) Id id2 = new Id.Node(new Id.Node(1, 0), 0); id2.Split().Should().Be( new Id.Node( new Id.Node(new Id.Node(new Id.Node(1, 0), 0), 0), new Id.Node(new Id.Node(new Id.Node(0, 1), 0), 0) ) ); }
public void ToStringShouldCorrectlyRepresentNode() { var id = new Id.Node(1, new Id.Node(0, 1)); id.ToString().Should().Be("(1,(0,1))"); }