예제 #1
0
    public void Capacity()
    {
        // Arrange
        var memory = new BrainfMemory(10);

        // Act
        var capacity = memory.Capacity;

        for (var i = 0; i < 11; i++)
        {
            memory.CellValue = i;
            memory.Pointer++;
        }

        // Assert
        Assert.Equal(10, capacity);
        Assert.Equal(20, memory.Capacity);
    }
예제 #2
0
    public void Negative_Pointer()
    {
        // Arrange
        var memory = new BrainfMemory();
        var sum    = 0;

        // Act
        for (var i = -1; i >= -10; i--)
        {
            memory.CellValue = -(i + 1);
            memory.Pointer   = i;
        }

        for (var i = -10; i <= -1; i++)
        {
            memory.Pointer = i;
            sum           += memory.CellValue;
        }

        // Assert
        Assert.Equal(45, sum);
    }
예제 #3
0
    public void Compile()
    {
        // Arrange
        const string sourceCode = @"
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]";

        var parser   = new BrainfParser();
        var compiler = new BrainfCompiler();

        var memory = new BrainfMemory();
        var io     = BrainfIO.CreateString();

        // Act
        var program = parser.Parse(sourceCode);
        var func    = compiler.Compile(program);

        func(memory, io);

        // Assert
        Assert.Equal("ZYXWVUTSRQPONMLKJIHGFEDCBA", io.GetString());
    }
예제 #4
0
    public void Positive_Pointer()
    {
        // Arrange
        var memory = new BrainfMemory();
        var sum    = 0;

        // Act
        for (var i = 0; i < 10; i++)
        {
            memory.Pointer   = i;
            memory.CellValue = i;
        }

        for (var i = 9; i >= 0; i--)
        {
            memory.Pointer = i;
            sum           += memory.CellValue;
        }

        // Assert
        Assert.Equal(45, sum);
        Assert.Equal(0, memory.Pointer);
        Assert.Equal(0, memory.CellValue);
    }