Пример #1
0
 def test_endianess(self):
     """Test, if right functions are assigned."""
     assert self.memory.encode == big_endian_encode
     assert self.memory.decode == big_endian_decode
     self.memory = AbstractMemory(BYTE_SIZE, endianess="big")
     assert self.memory.encode == big_endian_encode
     assert self.memory.decode == big_endian_decode
     self.memory = AbstractMemory(BYTE_SIZE, endianess="little")
     assert self.memory.encode == little_endian_encode
     assert self.memory.decode == little_endian_decode
     with raises(ValueError):
         AbstractMemory(BYTE_SIZE, endianess="wrong_endianess")
Пример #2
0
 def test_init(self):
     """Test if we can predefine addresses."""
     self.memory = AbstractMemory(BYTE_SIZE, addresses={0: 5, 'R1': 6})
     assert 0 in self.memory
     assert 1 not in self.memory
     assert -1 not in self.memory
     assert 'R1' in self.memory
     assert 'R2' not in self.memory
Пример #3
0
 def setup(self):
     """Init state."""
     self.memory = AbstractMemory(BYTE_SIZE)
     assert self.memory.word_size == BYTE_SIZE
Пример #4
0
class TestAbstractMemory:

    """Test case for abstract memory class."""

    memory = None

    def setup(self):
        """Init state."""
        self.memory = AbstractMemory(BYTE_SIZE)
        assert self.memory.word_size == BYTE_SIZE

    def test_check_word_size(self):
        """Word size check is a first part of information protection."""
        for i in range(2 ** self.memory.word_size):
            self.memory.check_word_size(i)
        for i in range(2 ** self.memory.word_size, 2 * 2 ** self.memory.word_size):
            with raises(ValueError):
                self.memory.check_word_size(i)
        for i in range(-2 ** self.memory.word_size, 0):
            with raises(ValueError):
                self.memory.check_word_size(i)

    def test_check_bits_count(self):
        """Should runs without exception, when size % word_size == 0."""
        for i in range(1, self.memory.word_size * 10):
            if i % self.memory.word_size == 0:
                self.memory.check_bits_count(0, i)
            else:
                with raises(KeyError):
                    self.memory.check_bits_count(0, i)

    def test_setitem(self):
        """Test that setitem is not implemented."""
        with raises(NotImplementedError):
            self.memory[0] = 0
        with raises(NotImplementedError):
            self.memory['R1'] = 0

    def test_init(self):
        """Test if we can predefine addresses."""
        self.memory = AbstractMemory(BYTE_SIZE, addresses={0: 5, 'R1': 6})
        assert 0 in self.memory
        assert 1 not in self.memory
        assert -1 not in self.memory
        assert 'R1' in self.memory
        assert 'R2' not in self.memory

    def test_fetch(self):
        """Test that fetch is defined."""
        with raises(NotImplementedError):
            self.memory.fetch(0, BYTE_SIZE)

    def test_put(self):
        """Test that put is defined."""
        with raises(NotImplementedError):
            self.memory.put(0, 15, BYTE_SIZE)

    def test_endianess(self):
        """Test, if right functions are assigned."""
        assert self.memory.encode == big_endian_encode
        assert self.memory.decode == big_endian_decode
        self.memory = AbstractMemory(BYTE_SIZE, endianess="big")
        assert self.memory.encode == big_endian_encode
        assert self.memory.decode == big_endian_decode
        self.memory = AbstractMemory(BYTE_SIZE, endianess="little")
        assert self.memory.encode == little_endian_encode
        assert self.memory.decode == little_endian_decode
        with raises(ValueError):
            AbstractMemory(BYTE_SIZE, endianess="wrong_endianess")