コード例 #1
0
    def test_setitem_invalid_key_stochastic_player(self):
        invalid_key = (Random(), TitForTat(), 2)
        with self.assertRaises(ValueError):
            self.cache[invalid_key] = self.test_value

        invalid_key = (TitForTat(), Random(), 2)
        with self.assertRaises(ValueError):
            self.cache[invalid_key] = self.test_value
コード例 #2
0
    def test_setitem_invalid_key_first_two_elements_not_player(self):
        invalid_key = ('test', 'test', 2)
        with self.assertRaises(ValueError):
            self.cache[invalid_key] = self.test_value

        invalid_key = (TitForTat(), 'test', 2)
        with self.assertRaises(ValueError):
            self.cache[invalid_key] = self.test_value

        invalid_key = ('test', TitForTat(), 2)
        with self.assertRaises(ValueError):
            self.cache[invalid_key] = self.test_value
コード例 #3
0
 def setUpClass(cls):
     cls.test_key = (TitForTat(), Defector(), 3)
     cls.test_value = [(C, D), (D, D), (D, D)]
     cls.test_save_file = 'test_cache_save.txt'
     cls.test_load_file = 'test_cache_load.txt'
     cls.test_pickle = b'\x80\x03}q\x00X\x0b\x00\x00\x00Tit For Tatq\x01X\x08\x00\x00\x00Defectorq\x02K\x03\x87q\x03]q\x04(X\x01\x00\x00\x00Cq\x05X\x01\x00\x00\x00Dq\x06\x86q\x07h\x06h\x06\x86q\x08h\x06h\x06\x86q\tes.'
     with open(cls.test_load_file, 'wb') as f:
         f.write(cls.test_pickle)
コード例 #4
0
    def setUpClass(cls):
        cls.test_key = (TitForTat(), Defector(), 3)
        cls.test_value = [(C, D), (D, D), (D, D)]
        cls.test_save_file = 'test_cache_save.txt'
        cls.test_load_file = 'test_cache_load.txt'
        test_data_to_pickle = {
            ('Tit For Tat', 'Defector', 3): [(C, D), (D, D), (D, D)]
        }
        cls.test_pickle = pickle.dumps(test_data_to_pickle)

        with open(cls.test_load_file, 'wb') as f:
            f.write(cls.test_pickle)
コード例 #5
0
    def setUpClass(cls):
        cls.test_key = (TitForTat(), Defector(), 3)
        cls.test_value = [(C, D), (D, D), (D, D)]
        cls.test_save_file = "test_cache_save.txt"
        cls.test_load_file = "test_cache_load.txt"
        test_data_to_pickle = {
            ("Tit For Tat", "Defector", 3): [(C, D), (D, D), (D, D)]
        }
        cls.test_pickle = pickle.dumps(test_data_to_pickle)

        with open(cls.test_load_file, "wb") as f:
            f.write(cls.test_pickle)
コード例 #6
0
 def test_is_valid_key(self):
     cache = DeterministicCache()
     self.assertTrue(cache._is_valid_key(self.test_key))
     # Should return false if key is not a tuple
     self.assertFalse(cache._is_valid_key('test'))
     # Should return false if tuple is not a triplet
     self.assertFalse(cache._is_valid_key(('test', 'test')))
     # Should return false if contents of tuple are not axelrod Players
     # and an integer
     self.assertFalse(cache._is_valid_key(('test', 'test', 'test')))
     self.assertFalse(cache._is_valid_key((TitForTat(), 'test', 2)))
     self.assertFalse(cache._is_valid_key(('test', TitForTat(), 2)))
     self.assertFalse(
         cache._is_valid_key((TitForTat(), TitForTat(), TitForTat())))
     # Should return false if either player class is stochastic
     self.assertFalse(cache._is_valid_key((Random(), TitForTat(), 2)))
     self.assertFalse(cache._is_valid_key((TitForTat(), Random(), 2)))
コード例 #7
0
 def test_setitem_invalid_key_last_element_not_integer(self):
     invalid_key = (TitForTat(), TitForTat(), TitForTat())
     with self.assertRaises(ValueError):
         self.cache[invalid_key] = self.test_value
コード例 #8
0
 def test_setitem_invalid_key_too_many_players(self):
     invalid_key = (TitForTat(), TitForTat(), TitForTat())
     with self.assertRaises(ValueError):
         self.cache[invalid_key] = self.test_value