# Python Fights online AI creation game
# Copyright (C) 2009  Maxim Sloyko
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest

from core import MemoryCell, MemorySlot
import core
from serialize import serializer

class TestMemSlotSaver(unittest.TestCase):
    def testSaveLoad(self):
        slot1 = MemorySlot()
        cell1 = MemoryCell(kind = core.MC_OWN_HEAD)
        cell2 = MemoryCell(kind = core.MC_ENEMY_HEAD, trait = core.MCT_NOT)
        cell3 = MemoryCell(kind = core.MC_ENEMY_BODY)

        slot1.set_cell(3,3, cell1)
        slot1.set_cell(3,5, cell2)
        slot1.set_cell(0,2, cell3)

        sstr = serializer.save_slots([slot1])
        slot_list = serializer.load_slots(sstr)

        self.assertEqual(len(slot_list), 1)

        s1 = slot_list[0]
        self.assertEqual(len(s1.cell_map.values()), 3)
        self.assertEqual(s1.get_cell(3,3), cell1)
        self.assertEqual(s1.get_cell(3,5), cell2)
        self.assertEqual(s1.get_cell(0,2), cell3)

    def testMultiSaveLoad(self):
        slot1 = MemorySlot()
        cell1 = MemoryCell(kind = core.MC_OWN_HEAD)
        cell2 = MemoryCell(kind = core.MC_ENEMY_HEAD, trait = core.MCT_NOT)
        cell3 = MemoryCell(kind = core.MC_ENEMY_BODY)

        slot1.set_cell(3,3, cell1)
        slot1.set_cell(3,5, cell2)
        slot1.set_cell(0,2, cell3)

        slot2 = MemorySlot()
        cell21 = MemoryCell(kind = core.MC_OWN_HEAD)
        cell22 = MemoryCell(kind = core.MC_ENEMY_TAIL, trait = core.MCT_NOT)

        slot2.set_cell(3,3, cell21)
        slot2.set_cell(3,6, cell22)

        sstr = serializer.save_slots([slot1, slot2])
        slot_list = serializer.load_slots(sstr)

        self.assertEqual(len(slot_list), 2)

        s1 = slot_list[0]
        self.assertEqual(len(s1.cell_map.values()), 3)
        self.assertEqual(s1.get_cell(3,3), cell1)
        self.assertEqual(s1.get_cell(3,5), cell2)
        self.assertEqual(s1.get_cell(0,2), cell3)

        s2 = slot_list[1]
        self.assertEqual(len(s2.cell_map.values()), 2)
        self.assertEqual(s2.get_cell(3,3), cell21)
        self.assertEqual(s2.get_cell(3,6), cell22)


if '__main__' == __name__:
    unittest.main()
