예제 #1
0
파일: test_adders.py 프로젝트: xjtian/lcsim
    def test_function(self):
        # Test for all inputs
        it = itertools.product([0, 1], [0, 1], [0, 1])

        for x in it:
            src = sources.digital_source_circuit(x)

            a = adders.full_adder_circuit()
            circuit.connect_circuits(src, a, {0: 0, 1: 1, 2: 2})

            num = int("".join(map(str, a.evaluate())), 2)
            self.assertEqual(sum(x), num)
예제 #2
0
    def test_function_in_place(self):
        # Test all possible inputs up to 4 bits.
        for size in xrange(2, 5):
            space = [[0, 1] for _ in xrange(0, size)]

            for output in itertools.product(*space):
                for shift in xrange(0, 2 * size):
                    c = sources.digital_source_circuit(output)

                    # It's the same algorithm as the code being tested...
                    shifted = output[shift % size:] + output[:shift % size]
                    shifted = list(shifted)

                    # Also make sure it's in place
                    self.assertIs(c, shifters.left_rotate_in_place(c, shift))
                    self.assertEqual(shifted, c.evaluate())
예제 #3
0
    def test_function(self):
        # Test all possible operations up to 8 bits
        for l in xrange(2, 9):
            space = [[0, 1] for _ in xrange(0, l)]
            it1 = itertools.product(*space)

            for x in it1:
                c = bitwise.bitwise_not_circuit(l)

                n = int(''.join(map(lambda y: '0' if y else '1', x)), 2)

                source_c = sources.digital_source_circuit(x)
                circuit.connect_circuits(source_c, c,
                                         {i: i for i in xrange(0, l)})

                result = int(''.join(map(str, c.evaluate())), 2)
                self.assertEqual(n, result)
예제 #4
0
    def test_function(self):
        # Test all possible inputs up to 4 bits.
        for size in xrange(2, 5):
            space = [[0, 1] for _ in xrange(0, size)]

            for output in itertools.product(*space):
                for shift in xrange(0, 2 * size):
                    c = sources.digital_source_circuit(output)

                    # It's the same algorithm as the code being tested...
                    i = size - (shift % size)
                    shifted = output[i:] + output[:i]
                    shifted = list(shifted)

                    # Also make sure it's not in place
                    result = shifters.right_rotate(c, shift)
                    self.assertIsNot(c, result)
                    self.assertEqual(shifted, result.evaluate())
예제 #5
0
파일: test_adders.py 프로젝트: xjtian/lcsim
    def test_specific(self):
        a = 0xE8A4602C
        b = 0x98BADCFE

        al = map(int, list(bin(a)[2:]))
        bl = map(int, list(bin(b)[2:]))

        while len(al) < 32:
            al.insert(0, 0)
        while len(bl) < 32:
            bl.insert(0, 0)

        src = sources.digital_source_circuit(al + bl)

        adder = adders.ripple_adder_no_carry(32)
        circuit.connect_circuits(src, adder, {i: i for i in xrange(0, 64)})

        num = int("".join(map(str, adder.evaluate())), 2)
        self.assertEqual((a + b) % (1 << 32), num)
예제 #6
0
    def test_function(self):
        # Test all possible operations up to 8 bits
        for l in xrange(2, 9):

            space = [[0, 1] for _ in xrange(0, l)]
            it1 = itertools.product(*space)
            it2 = itertools.product(*space)

            for x in it1:
                for y in it2:
                    c = bitwise.bitwise_and_circuit(l)

                    n1 = int(''.join(map(str, x)), 2)
                    n2 = int(''.join(map(str, y)), 2)

                    source_c = sources.digital_source_circuit(x + y)
                    circuit.connect_circuits(source_c, c,
                                             {i: i for i in xrange(0, 2 * l)})

                    result = int(''.join(map(str, c.evaluate())), 2)
                    self.assertEqual(n1 & n2, result)
예제 #7
0
파일: test_adders.py 프로젝트: xjtian/lcsim
    def test_function(self):
        # Test for all inputs up to 8 bits
        for l in xrange(2, 9):
            space = [[0, 1] for _ in xrange(0, l)]

            it1 = itertools.product(*space)
            it2 = itertools.product(*space)

            for x in it1:
                for y in it2:
                    n1 = int("".join(map(str, x)), 2)
                    n2 = int("".join(map(str, y)), 2)
                    mapping = {i: i for i in xrange(0, 2 * l)}

                    src = sources.digital_source_circuit(x + y)

                    a = adders.ripple_adder_no_carry(l)
                    circuit.connect_circuits(src, a, mapping)

                    num = int("".join(map(str, a.evaluate())), 2)
                    self.assertEqual("%dAdd" % l, a.name)
                    self.assertEqual((n1 + n2) % (2 ** l), num)
예제 #8
0
    def test_function(self):
        c = sources.digital_source_circuit([0, 1, 1, 0, 0])

        self.assertEqual('dSrc', c.name)
        self.assertEqual([0, 1, 1, 0, 0], c.evaluate())
        self.assertEqual(0, len(c._inputs))