Exemplo n.º 1
0
    def test_store_and_retrieve_in_3d_grid(self):
        grid = MultiDimensionalGrid(grid_shape=(2, 4, 3))
        pos = (1, 2, 2)

        item = 35
        self.assertEqual(0, grid.get_item(pos))
        grid.put_item(pos, item)
        self.assertEqual(item, grid.get_item(pos))
Exemplo n.º 2
0
    def test_retrieve_from_2d_grid(self):
        grid = MultiDimensionalGrid(grid_shape=(2, 3))

        a = [[1, 2, 3], [4, 5, 6]]
        grid.set_grid(a)

        self.assertEqual(4, grid.get_item((1, 0)))
        self.assertEqual(3, grid.get_item((0, 2)))
Exemplo n.º 3
0
    def test_all_retrievals_from_1d_grid_return_correct_items(self):
        grid = MultiDimensionalGrid(grid_shape=(3, ))

        a = [1, 2, 3]
        grid.set_grid(a)

        for i in range(3):
            self.assertEqual(a[i], grid.get_item((i, )))
Exemplo n.º 4
0
    def test_all_retrievals_from_2d_grid_return_correct_items(self):
        grid = MultiDimensionalGrid(grid_shape=(2, 3))

        a = [[1, 2, 3], [4, 5, 6]]
        grid.set_grid(a)

        for i in range(2):
            for j in range(3):
                self.assertEqual(a[i][j], grid.get_item((i, j)))
Exemplo n.º 5
0
class ExceptionsTests(TestCase):
    def setUp(self):
        self.grid = MultiDimensionalGrid(grid_shape=(4, 2))

    def test_get_tensor_after_initialization_returns_zeros(self):
        self.grid = MultiDimensionalGrid(grid_shape=(2, ))

        self.assertEqual(0, self.grid.get_item((0, )))

    def test_get_item_using_negative_integers(self):
        self.assertRaises(PositionOutOfBoundsError, lambda: self.grid.get_item(
            (2, -1)))

    def test_get_item_using_out_of_bound_position(self):
        self.assertRaises(PositionOutOfBoundsError, lambda: self.grid.get_item(
            (2, 12)))

        self.assertRaises(PositionOutOfBoundsError, lambda: self.grid.get_item(
            (4, 0)))

    def test_get_item_when_passing_less_than_required_position_components(
            self):
        self.assertRaises(InvalidPositionError, lambda: self.grid.get_item(
            (2, )))

    def test_get_item_when_passing_more_than_required_position_components(
            self):
        self.assertRaises(InvalidPositionError, lambda: self.grid.get_item(
            (2, 1, 0, 0)))
Exemplo n.º 6
0
    def test_1d(self):
        grid = MultiDimensionalGrid(grid_shape=(3, ))
        grid.set_grid([1, 2, 3])

        self.assertEqual(2, grid.get_item((1, )))
Exemplo n.º 7
0
class StoringTests(TestCase):
    def assert_retrieved_items_match_stored_ones(self, grid, a, shape):
        for position in grid.get_positions():
            inner_list = get_inner_most_list(a, position)
            item = inner_list[position[-1]]
            grid.put_item(position, item)

        for position in grid.get_positions():
            inner_list = get_inner_most_list(a, position)
            expected_item = inner_list[position[-1]]
            item = grid.get_item(position)
            self.assertEqual(expected_item, item)

    def setUp(self):
        self.grid = MultiDimensionalGrid(grid_shape=(4, 2))

    def test_store_and_retrieve_item(self):
        position = (2, 1)

        t = 2

        self.grid.put_item(position, t)

        actual = self.grid.get_item(position)
        self.assertEqual(t, actual)

    def test_store_and_retrieve_two_items(self):
        pos1 = (2, 1)
        pos2 = (1, 0)

        t1 = 4
        t2 = 7

        self.grid.put_item(pos1, t1)
        self.grid.put_item(pos2, t2)

        actual = self.grid.get_item(pos1)
        self.assertEqual(t1, actual)

        actual = self.grid.get_item(pos2)
        self.assertEqual(t2, actual)

    def test_filling_2d_grid_completely_and_retrieving_all_entries(self):
        a = np.arange(4 * 2).reshape(4, 2)
        self.assert_retrieved_items_match_stored_ones(self.grid, a, (4, 2))

    def test_store_and_retrieve_in_3d_grid(self):
        grid = MultiDimensionalGrid(grid_shape=(2, 4, 3))
        pos = (1, 2, 2)

        item = 35
        self.assertEqual(0, grid.get_item(pos))
        grid.put_item(pos, item)
        self.assertEqual(item, grid.get_item(pos))

    def test_filling_10d_grid_and_retrieving_all_entries_back(self):
        shape = tuple([2] * 10)
        grid = MultiDimensionalGrid(shape)
        a = np.arange(2**10).reshape(shape).tolist()

        self.assert_retrieved_items_match_stored_ones(grid, a, shape)
Exemplo n.º 8
0
    def test_store_and_retrieve_in_3d_grid_after_initialization(self):
        grid = MultiDimensionalGrid(grid_shape=(2, 4, 3))
        pos = (1, 2, 2)

        self.assertEqual(0, grid.get_item(pos))