def test_resolution_pathsuccess_1(self): """Should return new BraidWord with manually set generator cut out""" bw = BraidWord([1, 2, 3]) newbraidword = bw.resolution(random_index=False, index=1) # Check that word length shortened by one self.assertEqual(newbraidword.word, [1, 3])
def test_resolution_pathsuccess_0(self): """Should return new BraidWord with random generator cut out""" bw = BraidWord([1, 2, 3]) newbraidword = bw.resolution() # Check that word length shortened by one self.assertEqual(newbraidword.length(), 2)
def test_canDestabilize_pathfail_1(self): """Should return False if more than one largestGenerator exists""" # More than one largest generator exists bw = BraidWord([1, 2, 3, 3]) # Execution path False self.assertFalse(bw.canDestabilize())
def test_conjugate(self): """Should conjugate word @ idx 2)""" bw = BraidWord([1, 2, 3, 4]) # Execution path True self.assertTrue(bw.conjugate(2)) # Word modification self.assertEqual(bw.word, [3, 4, 1, 2])
def test_canDestabilize_pathfail_0(self): """Should return False if largestGenerator is not @end""" # largest generator not at end bw = BraidWord([1, 2, 5, 3]) # Execution path False self.assertFalse(bw.canDestabilize())
def test_canFlip_pathsuccess_1(self): """Should return True when conditions met and testing for absolute values of generators""" # Testing negative generators bw = BraidWord([-1, -2, -1]) # Execution path True self.assertTrue(bw.canFlip(0))
def test_crossing_change_pathsuccess_2(self): """Should return new BraidWord with manually set generator inverted""" bw = BraidWord([1, 2, 3]) newbraidword = bw.crossing_change(random_index=False, index=1) # Check that manually set generator has been inverted self.assertEqual(newbraidword.word, [1, -2, 3])
def test_crossing_change_pathfail_0(self): """Should raise error if randon_index set to False and index not set or not set to int""" bw = BraidWord([1, 2, 3]) with self.assertRaises(ValueError) as te: bw.crossing_change(random_index=False)
def test_canFlip_pathsuccess_0(self): """Should return True when conditions met and testing only positive generators Refer to test_canFlip_pathfail_{0, 1}""" # Testing positive generators bw = BraidWord([1, 2, 1]) # Execution path True self.assertTrue(bw.canFlip(0))
def test_stabilize(self): """Should stabilize the word and genCount.""" bw = BraidWord([1, 2, 3]) self.assertTrue(bw.stabilize()) # Word modification self.assertEqual(bw.word, [1, 2, 3, 4]) # genCount modification self.assertEqual(bw.genCount, [1, 1, 1, 1])
def test_crossing_change_pathsuccess_0(self): """Should return new BraidWord with random generator inverted""" bw = BraidWord([1, 2, 3]) newbraidword = bw.crossing_change() # Check that one generator has been inverted x = list((i == -abs(i) for i in newbraidword.word)) self.assertTrue(any(x) and not all(x))
def test_cancel_pathsuccess(self): """Should successfully cancel gen @idx 3 and should modify word""" bw = BraidWord([-1, 2, 3, 1]) # Execution path True self.assertTrue(bw.cancel(3)) # Word modification self.assertEqual(bw.word, [2, 3])
def test_transpose_pathfail(self): """Should return False if two adjacent generators have absolute values differing by 1 or less and should not transpose word.""" bw = BraidWord([1, 2, 3, -4]) # Checking absval with -4 self.assertFalse(bw.transpose(2)) # (No) Word modification self.assertEqual(bw.word, [1, 2, 3, -4])
def test_canFlip_pathfail_1(self): """Should return False if ~condP2: abs(l[index] - l[(index+1) % len(l)]) != 1; and should not modify word. """ bw = BraidWord([1, 3, 1]) # Execution path False self.assertFalse(bw.canFlip(0))
def test_cancel_pathfail(self): """Should return False and fail to cancel gen @idx 1 and not modify word""" bw = BraidWord([-1, 2, 3, 1]) # Execution path False self.assertFalse(bw.cancel(1)) # (No) Word modification self.assertEqual(bw.word, [-1, 2, 3, 1])
def test_canFlip_pathfail_0(self): """Should return False if ~condP1: ( l[index] != l[(index+2) % len(l)] ) and should not modify word. """ bw = BraidWord([1, 2, 3]) # Execution path False self.assertFalse(bw.canFlip(0))
def test_insert_pathfail(self): """Should raise ValueError when given generator (5) > self.largestGenerator (4) and should not modify word.""" bw = BraidWord([1, 2, 3, 4]) with self.assertRaises(ValueError) as cm: bw.insert(2, 5) # (No) Word modification self.assertEqual(bw.word, [1, 2, 3, 4])
def test_transpose_pathsuccess(self): """Should return True if two adjacent generators have absolute values differing by more than 1 and should successfully transpose word.""" bw = BraidWord([1, 2, 3, 4]) self.assertTrue( bw.transpose(3)) # Checking wrapping around with @idx 3 # Word modification self.assertEqual(bw.word, [4, 2, 3, 1])
def test_init_pathsuccess_2(self): """ Should create MarkovChain with only BraidWord argument given (kwargs) """ bw = MarkovChain(braidword=BraidWord([1, 2, 3])) # Check types self.assertEqual(type(bw.braid), BraidWord) # Check words self.assertEqual(bw.braid.word, BraidWord([1, 2, 3]).word)
def test_destabilize_pathsuccess_0(self): """Should return True if Cond1 and Cond2 are satisfied and should modifiy word and genCount""" bw = BraidWord([-1, 2, 3]) # Check absval of genCount with -1 self.assertTrue(bw.destabilize()) # Word modifications self.assertEqual(bw.word, [-1, 2]) # genCount modification self.assertEqual(bw.genCount, [1, 1])
def test_destabilize_pathfail_1(self): """Should return False if Cond2: one largestGenerator exists but not at end and should not modify word or genCount""" bw = BraidWord([1, 3, 2]) self.assertFalse(bw.destabilize()) # (No) Word modification self.assertEqual(bw.word, [1, 3, 2]) # (No) genCount modification self.assertEqual(bw.genCount, [1, 1, 1])
def test_init_pathsuccess_1(self): """ Should create MarkovChain if all keyword arguments are valid (kwargs) """ bw = MarkovChain(braidword=BraidWord([1, 2, 3]), maxgen=1, maxlen=1) # Check types self.assertEqual(type(bw.braid), BraidWord) # Check words self.assertEqual(bw.braid.word, BraidWord([1, 2, 3]).word) self.assertEqual(bw.maxgen, 1) self.assertEqual(bw.maxlen, 1)
def test_insert_pathsuccess(self): """Should successfully insert generator (3) @idx 2 and should modify word and genCount.""" bw = BraidWord([1, 2, 3, 4]) # Execution path True self.assertTrue(bw.insert(2, 3)) # Word modification self.assertEqual(bw.word, [1, 2, -3, 3, 3, 4]) # genCount modification self.assertEqual(bw.genCount, [1, 1, 3, 1])
def test_flip_pathfail_0(self): """Should return False if ~condP1: ( l[index] != l[(index+2) % len(l)] ) and should not modify word. """ bw = BraidWord([1, 2, 3]) # NOTE condP2 holds (refer to test_flip_pathfail_1) self.assertFalse(bw.flip(0)) # (No) Word modification self.assertEqual(bw.word, [1, 2, 3]) # (No) genCount modification self.assertEqual(bw.genCount, [1, 1, 1])
def test_flip_pathfail_1(self): """Should return False if ~condP2: abs(l[index] - l[(index+1) % len(l)]) != 1 and should not modify word. """ bw = BraidWord([1, 3, 1]) # NOTE condP1 holds (refer to test_flip_pathfail_0) self.assertFalse(bw.flip(0)) # (No) Word modification self.assertEqual(bw.word, [1, 3, 1]) # (No) genCount modification self.assertEqual(bw.genCount, [2, 0, 1])
def test_init_success_0(self): ''' Should successfully create BraidWord (kwargs) ''' bw = BraidWord(initword=[1, 2, 3]) self.assertEqual(bw.word, [1, 2, 3])
def test_init_pathfail_1(self): ''' Should raise ValueError if initword contains any zeros (kwargs) ''' with self.assertRaises(ValueError) as te: bw = BraidWord(initword=[0])
def test_init_pathfail_0(self): ''' Should raise TypeError if initword is not a list (args) ''' with self.assertRaises(TypeError) as te: bw = BraidWord(1)
def new_braidword(braidword: BraidWord): r""" Method to set a new BraidWord. Parameters ---------- braidword : :obj:`BraidWord` The new BraidWord to replace the old BraidWord. Returns ------- Replaces self.braid with braidword. """ # Check if is braidword if not isinstance(braidword, BraidWord): # Check if is list if not isinstance(braidword, list): msg = 'First argument must be BraidWord or list.' raise ValueError(msg) else: self.braid = BraidWord(deepcopy(braidword)) else: self.braid = deepcopy(braidword)
def test_aggregate(self): ''' Should return non-empty self.braidagg ''' mc = MarkovChain(BraidWord([1, 2, 3])) mc.model(num_braidreps=1, msteps=10) # Check if non-empty self.assertTrue(mc.aggregate())