Пример #1
0
        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);
        }
Пример #2
0
        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();
        }
Пример #3
0
        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();
        }
Пример #4
0
        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();
        }
Пример #5
0
        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();
        }
Пример #6
0
        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();
        }
Пример #7
0
        public void NormalizeShouldReturn0WhenNodeIs0and0()
        {
            // Arrange
            Id id = new Id.Node(0, 0);

            // Act
            Id normalized = id.Normalize();

            // Assert
            normalized.Should().Be((Id)0);
        }
Пример #8
0
        public void NormalizeShouldReturnUnchangedWhenNodeCannotBeSimplifiedLike1and0()
        {
            // Arrange
            Id id = new Id.Node(1, 0);

            // Act
            Id normalized = id.Normalize();

            // Assert
            normalized.Should().Be(id);
        }
Пример #9
0
        public void NormalizeShouldReturn1WhenNodeIs1and1()
        {
            // Arrange
            Id id = new Id.Node(1, 1);

            // Act
            Id normalized = id.Normalize();

            // Assert
            normalized.Should().Be((Id)1);
        }
Пример #10
0
        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));
        }
Пример #11
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))
                    )
                );
        }
Пример #12
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);
        }
Пример #13
0
        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);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        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);
        }
Пример #17
0
        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)))
                    ));
        }
Пример #18
0
        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)
                    )
                );
        }
Пример #19
0
        public void ToStringShouldCorrectlyRepresentNode()
        {
            var id = new Id.Node(1, new Id.Node(0, 1));

            id.ToString().Should().Be("(1,(0,1))");
        }