def test_get_min_complexity_1(self):
        gate = SeqGate()
        gate.parse(
            'and(and(seq({h}lop({e}{f}))xor(and(and(seq({a}{g}){d}){g})and(xor({g}{d}){b})))and({e}opt({c})))'
        )

        self.assertEqual(512, gate.complexity)
    def test_get_next_possible_states_3(self):
        gate = SeqGate()
        gate.parse('{a}lop(opt({b}{c}{d}{e}{f}))xor({g}{h})')
        e1 = gate.elements[0]

        actual = set(list(gate.get_next_possible_states((), None, e1)))
        self.assertEqual(1, len(actual))
    def test_eq3(self):
        gate1 = SeqGate()
        gate1.parse('and(xor({b}{c}){d}){e})')
        gate2 = AndGate()
        gate2.parse('and(xor({b}{c}){d}){e})')

        self.assertFalse(gate1 == gate2)
 def test_96(self):
     gate = SeqGate()
     gate.parse(
         'and(and({f}opt(and({f}opt(and({e}{d})))))xor(opt({d})xor({b}xor({c}{a}{b}))))'
         'lop(xor({a}{c}opt(xor({f}{d}))xor({b}xor({c}{a}{b}))))')
     all_length_8_routes = gate.get_all_n_length_routes(8)
     self.assertEqual(500, len(all_length_8_routes))
    def test_count_repeating_if_seq_parent_4(self):
        gate = SeqGate()
        gate.parse(
            '{a}and({d}xor({c}{b})){e}lop({f}and(xor({b}{c}){d}){e})xor({g}{h})'
        )

        self.assertEqual(4, gate.elements[3].count_repeating_if_seq_parent())
    def test_get_min_complexity_legend4(self):
        gate = SeqGate()
        gate.parse('xor(seq({a}{c}{d}{e}{h})'
                   'seq({a}{b}{d}{e}{g})'
                   'seq({a}{d}{c}{e}{h})'
                   'seq({a}{b}{d}{e}{h})'
                   'seq({a}{c}{d}{e}{g})'
                   'seq({a}{d}{c}{e}{g})'
                   'seq({a}{d}{b}{e}{h})'
                   'seq({a}{c}{d}{e}{f}{d}{b}{e}{h})'
                   'seq({a}{d}{b}{e}{g})'
                   'seq({a}{c}{d}{e}{f}{b}{d}{e}{h})'
                   'seq({a}{c}{d}{e}{f}{b}{d}{e}{g})'
                   'seq({a}{c}{d}{e}{f}{d}{b}{e}{g})'
                   'seq({a}{d}{c}{e}{f}{c}{d}{e}{h})'
                   'seq({a}{d}{c}{e}{f}{d}{b}{e}{h})'
                   'seq({a}{d}{c}{e}{f}{b}{d}{e}{g})'
                   'seq({a}{c}{d}{e}{f}{b}{d}{e}{f}{d}{b}{e}{g})'
                   'seq({a}{d}{c}{e}{f}{d}{b}{e}{g})'
                   'seq({a}{d}{c}{e}{f}{b}{d}{e}{f}{b}{d}{e}{g})'
                   'seq({a}{d}{c}{e}{f}{d}{b}{e}{f}{b}{d}{e}{h})'
                   'seq({a}{d}{b}{e}{f}{b}{d}{e}{f}{d}{b}{e}{g})'
                   'seq({a}{d}{c}{e}{f}{d}{b}{e}{f}{c}{d}{e}{f}{d}{b}{e}{g}))')

        self.assertEqual(21, gate.complexity)
    def test_get_min_complexity_legend(self):
        gate = SeqGate()
        gate.parse(
            '{a}and(xor({b}{c}){d}){e}lop({f}and(xor({b}{c}){d}){e})xor({g}{h})'
        )

        self.assertEqual(85 * 8, gate.complexity)
        self.assertEqual(5 * 8, gate.complexity_for_metric)
 def test_legend_1(self):
     gate = SeqGate()
     gate.parse(
         '{a}and(xor({b}{c}){d}){e}lop({f}and(xor({b}{c}){d}){e})xor({g}{h})'
     )
     all_length_9_routes = gate.get_all_n_length_routes(
         9, ('a', 'c', 'd', 'e', 'f', 'd', 'b', 'e', 'h'))
     self.assertEqual(8, len(all_length_9_routes))
 def test_98(self):
     gate = SeqGate()
     gate.parse(
         '{a}{b}{c}osq(opt(lop(opt({d}{e}{f}))seq(lo1({f}{g}{h}{m}){i}))xor({j}{l}{n}{o}{p}){k})'
     )
     all_length_5_routes = gate.get_all_n_length_routes(
         7, ('a', 'd', 'b', 'e', 'h'))
     self.assertEqual(500, len(all_length_5_routes))
 def test_97(self):
     gate = SeqGate()
     gate.parse(
         'xor(and(seq({f}{c})seq({a}{d}{b}))lop({a}))xor(seq(opt({d}){e}lop({g}))seq(xor({a}opt({e})){h})and({h}and({b}{d})))'
     )
     all_length_5_routes = gate.get_all_n_length_routes(
         5, ('a', 'd', 'b', 'e', 'h'))
     self.assertEqual(500, len(all_length_5_routes))
 def test_number_of_combos_lop_opt(self):
     # assuming max lop gate length = 3
     gate = SeqGate()
     gate.parse('lop({b})opt({c}{d})lop(xor({e}{b}))')
     all_length_5_routes = gate.get_all_n_length_routes(5)
     # (0,2,3) 2^3 + (1,1,3) 2 * 2^3 + (1,2,2) 2^2 + (2,0,3) 2^3 + (2,1,2) 2 * 2^2 + (2,2,1) 2 +
     # (3,0,2) 2^2 + (3,1,1) 2 * 2 + (3,2,0) 1
     # 8 + 16 + 4 + 8 + 8 + 2 + 4 + 4 + 1
     self.assertEqual(55, len(all_length_5_routes))
    def test_get_min_complexity_legend3(self):
        gate = SeqGate()
        gate.parse('{a}lop(opt({b}{c}{d}{e}{f}))xor({g}{h})')

        self.assertEqual(
            sum(pow(326, i) for i in range(3 + 1)) * 2, gate.complexity)
        self.assertEqual(
            sum(pow(326, i) for i in range(2 + 1)) * 2,
            gate.complexity_for_metric)
 def test_legend_3_2(self):
     gate = SeqGate()
     gate.parse('{a}lop(opt({b}{c}{d}{e}{f}))xor({g}{h})')
     all_length_11_routes = gate.get_all_n_length_routes(
         15, ('a', 'd', 'c', 'e', 'f', 'd', 'b', 'e', 'f', 'c', 'd', 'e',
              'f', 'd', 'b', 'e', 'g'))
     # count = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0}
     # for x in all_length_11_routes:
     #     for y in get_event_names(x):
     #         count[y] += 1
     self.assertEqual(19100, len(all_length_11_routes))
    def test_get_next_possible_states_2(self):
        gate = SeqGate()
        gate.parse('{a}lop(opt({b}{c}{d}{e}{f}))xor({g}{h})')
        events_with_parents = gate.get_all_child_events_with_parents()
        e1 = gate.elements[0]
        keys = [x for x in events_with_parents.keys()]
        e2 = keys[1]

        actual = set(
            list(events_with_parents[e1].get_next_possible_states((e1, ), e1,
                                                                  e2)))
        self.assertEqual(7, len(actual))
    def test_get_next_possible_states(self):
        gate = SeqGate()
        gate.parse('{a}lop(opt({b}{c}{d}{e}{f}))xor({g}{h})')
        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]
        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[2]
        e4 = keys[3]

        actual = set(
            list(events_with_parents[e3].get_next_possible_states((e1, e2, e3),
                                                                  e3, e4)))
        self.assertEqual(7, len(actual))
    def test_eq2(self):
        gate1 = SeqGate()
        gate1.parse('and({d}xor({b}{c})){e})')
        gate2 = SeqGate()
        gate2.parse('and(xor({b}{c}){d}){e})')

        self.assertTrue(gate1 == gate2)
    def test_get_next_possible_states_9_5(self):
        gate = SeqGate()
        gate.parse('{a}lo3(lo3(opt({d}))and({d}))opt({c}{b}){e}')

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[2]

        actual = set(
            list(events_with_parents[e2].get_next_possible_states((e1, e2), e2,
                                                                  e3)))
        self.assertEqual(2, len(actual))
    def test_get_next_possible_states_4(self):
        gate = SeqGate()
        gate.parse(
            '{a}and(seq(xor(seq(lop({c})opt({c})){b}){d})lop(seq({f}{d})))xor({e}seq({e}{g}))'
        )
        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]
        e1 = keys[0]
        e2 = keys[2]
        e4 = keys[4]
        e8 = keys[8]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states((e1, e2, e4),
                                                                  e4, e8)))
        self.assertEqual(3, len(actual))
 def test_92(self):
     gate = SeqGate()
     gate.parse('and({a}{f}opt(and({b}{e}lop({c}))){d})')
     all_length_6_routes = gate.get_all_n_length_routes(6)
     expected = [
         EventGroupMatcher(
             EventGroup([
                 EventGroupParallel([
                     Event('a'),
                     Event('f'),
                     EventGroupParallel(string_to_events('bec')),
                     Event('d')
                 ])
             ]))
     ]
     self.assertCountEqual(expected, all_length_6_routes)
    def test_get_next_possible_states_8(self):
        gate = SeqGate()
        gate.parse('{a}{c}and({d})and(lo1({f}{d}{b}{e}){e})xor(xor({g}){h})')

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[2]
        e4 = keys[7]
        e5 = keys[9]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(3, len(actual))
 def test_9(self):
     gate = SeqGate()
     gate.parse('and({c}and({a}lop({e}opt({d}))seq({c}{b})))')
     all_length_5_routes = gate.get_all_n_length_routes(5)
     expected = [
         EventGroupMatcher(
             EventGroup([
                 EventGroupParallel([
                     Event('c'),
                     EventGroupParallel([
                         Event('a'),
                         Event('e'),
                         EventGroup(string_to_events('cb'))
                     ])
                 ])
             ]))
     ]
     self.assertCountEqual(expected, all_length_5_routes)
    def test_get_next_possible_states_9_6(self):
        gate = SeqGate()
        gate.parse(
            'and(xor({d}{a})and({d}{c}))xor(lop({g})seq({e}{f}{d}{b}))and(opt({h}){e})'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[1]
        e2 = keys[3]
        e3 = keys[2]
        e4 = keys[5]

        actual = set(
            list(events_with_parents[e3].get_next_possible_states((e1, e2, e3),
                                                                  e3, e4)))
        self.assertEqual(4, len(actual))
 def test_8(self):
     gate = SeqGate()
     gate.parse(
         'xor({f}{d}and({e}xor(lop(xor({f}{d}))lop({a}))))and({b}{a})')
     all_length_3_routes = gate.get_all_n_length_routes(3)
     self.assertCountEqual([
         EventGroupMatcher(
             EventGroup(
                 [Event('f'),
                  EventGroupParallel(string_to_events('ba'))])),
         EventGroupMatcher(
             EventGroup(
                 [Event('d'),
                  EventGroupParallel(string_to_events('ba'))])),
         EventGroupMatcher(
             EventGroup(
                 [Event('e'),
                  EventGroupParallel(string_to_events('ba'))]))
     ], all_length_3_routes)
    def test_get_next_possible_states_9_1(self):
        gate = SeqGate()
        gate.parse(
            '{a}seq({b}and(opt(seq({d}){e})){h})xor({g}and(opt({a}))opt(opt(and({d}))xor(seq({c}){e}seq({e}){a})))'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[2]
        e4 = keys[8]
        e5 = keys[10]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(1, len(actual))
    def test_get_next_possible_states_9_2(self):
        gate = SeqGate()
        gate.parse(
            '{a}xor(xor(xor({b})lo2({b}){b}){c}seq(and(and({b})){e})){d}opt(xor(and({e}opt(xor({h}{g})))){h}'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[2]
        e4 = keys[7]
        e5 = keys[9]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(4, len(actual))
    def test_get_next_possible_states_9_7(self):
        gate = SeqGate()
        gate.parse(
            '{a}and(and(opt({e})seq(xor({d}{e}){b}))xor(opt({g})xor(xor({h}xor({f}{h}))xor({f}and(and({c}{h}){a})))))'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[2]
        e3 = keys[4]
        e4 = keys[1]
        e5 = keys[6]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(8, len(actual))
    def test_get_next_possible_states_9_4(self):
        gate = SeqGate()
        gate.parse(
            'seq({a})xor({b}{f}{c}){d}and(xor({h}xor(and(xor(and({c})))lo0(xor({b}{f}{d}{g})))){e})opt({h})'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[0]
        e2 = keys[1]
        e3 = keys[4]
        e4 = keys[11]
        e5 = keys[12]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(7, len(actual))
    def test_get_next_possible_states_6_6(self):
        gate = SeqGate()
        gate.parse(
            'and(xor(opt(lop({f}))xor(and({e}{a}seq({c}{d})opt({g}{a}))and({b}{h}){g})'
            'xor(xor(and({e}{a}seq({c}{d}))opt({b}))xor(xor(opt({c})lop({c}))seq({b}{d}))))opt({h}))'
        )

        events_with_parents = gate.get_all_child_events_with_parents()
        keys = [x for x in events_with_parents.keys()]

        e1 = keys[2]
        e2 = keys[3]
        e3 = keys[4]
        e4 = keys[1]
        e5 = keys[18]

        actual = set(
            list(events_with_parents[e4].get_next_possible_states(
                (e1, e2, e3, e4), e4, e5)))
        self.assertEqual(3, len(actual))
 def test_7(self):
     gate = SeqGate()
     gate.parse('lop({c})')
     self.assertEqual(None, gate.get_all_n_length_routes(6))
     self.assertEqual([
         EventGroupMatcher(
             EventGroup([EventGroupParallel(string_to_events('ccc'))]))
     ], gate.get_all_n_length_routes(3))
 def test_6(self):
     gate = SeqGate()
     gate.parse('xor(and({c}and(and({c}{b}and({d}{f})){b}{e})){e}{e})')
     self.assertEqual([
         EventGroup(
             [EventGroupParallel(string_to_events('abcdefghijklmn'))])
     ], gate.get_all_n_length_routes(7))
     self.assertEqual([], gate.get_all_n_length_routes(6))