示例#1
0
 def test_len(self):
     """Bitvector len should match initialized length"""
     self.assertEqual(len(Bitvector()), 0)
     self.assertEqual(len(Bitvector('010')), 3)
     self.assertEqual(len(Bitvector(1024, 5)), 5)
     self.assertEqual(len(Bitvector(1024, 0)), 0)
     self.assertEqual(len(Bitvector('1' * 1000)), 1000)
示例#2
0
 def test_thaw(self):
     """Bitvector thaw should return mutable bitvector with same data"""
     b = Bitvector('111')
     c = b.thaw()
     self.assertEqual(c, Bitvector('111'))
     c[1] = 0
     self.assertEqual(c, Bitvector('101'))
     self.assertNotEqual(Bitvector('111'), Bitvector('101'))
示例#3
0
    def test_str(self):
        """Bitvector str should match expected results"""
        vecs = [Bitvector(i, 0) for i in [0, 1, 2, 7, 8, 1024, 814715L]]
        for v in vecs:
            self.assertEqual(str(v), '')

        vecs = [
            Bitvector(i, 1) for i in [0, 1, 2, 7, 8, 1024, 814715L, '1' * 50]
        ]
示例#4
0
 def test_type(self):
     """VectorFromMatches should return correct type of vector"""
     v = VectorFromMatches('a', 'a')
     self.assertEqual(v, Bitvector('1'))
     self.assertTrue(isinstance(v, ImmutableBitvector))
     v = VectorFromMatches('a', 'a', constructor=MutableBitvector)
     self.assertEqual(v, Bitvector('1'))
     v[0] = 0
     self.assertEqual(v, Bitvector('0'))
     self.assertTrue(isinstance(v, MutableBitvector))
示例#5
0
    def test_getitem(self):
        """Bitvector getitem should return states at specified position(s)"""

        vec_strings = ['', '0', '1', '10', '10001101', '101' * 50]
        vecs = map(Bitvector, vec_strings)
        for vec_string, vec in zip(vec_strings, vecs):
            for char, item in zip(vec_string, vec):
                self.assertEqual(char, str(item))
        #test some 2- and 3-item slices as well
        vec = Bitvector('1001000101001')
        self.assertEqual(vec[3:7], Bitvector('1000'))
        self.assertEqual(vec[:4], Bitvector('1001'))
        self.assertEqual(vec[7:], Bitvector('101001'))
        self.assertEqual(vec[1:11:2], Bitvector('01011'))
示例#6
0
    def test_xor(self):
        """Bitvector A^B should return 0 for each identical position in A and B"""

        results = [
            ['', '', '', '', '', '', ''],  #'' xor x
            ['', '0', '1', '0', '0', '1', '1'],  #'0' xor x
            ['', '1', '0', '1', '1', '0', '0'],  #'1' xor x
            ['', '0', '1', '00', '01', '10', '11'],  #'00' xor x
            ['', '0', '1', '01', '00', '11', '10'],  #'01' xor x
            ['', '1', '0', '10', '11', '00', '01'],  #'10' xor x
            ['', '1', '0', '11', '10', '01', '00'],  #'11' xor x
        ]
        vectors = self.vectors

        for first_pos, first in enumerate(vectors):
            for second_pos, second in enumerate(vectors):
                self.assertEqual(str(first ^ second),
                                 results[first_pos][second_pos])

        #test chaining
        expected = Bitvector('0110')
        observed = Bitvector('1111') ^ Bitvector('0110') ^ Bitvector('1111')

        #test long
        self.assertEqual(Bitvector('11'*50) ^ Bitvector('01'*50), \
            Bitvector('10'*50))
示例#7
0
    def test_str(self):
        """Bitvector str should match expected results"""
        vecs = [Bitvector(i, 0) for i in [0, 1, 2, 7, 8, 1024, 814715]]
        for v in vecs:
            self.assertEqual(str(v), '')

        vecs = [
            Bitvector(i, 1) for i in [0, 1, 2, 7, 8, 1024, 814715, '1' * 50]
        ]
        single_results = list('01010011')
        for exp, vec in zip(single_results, vecs):
            self.assertEqual(str(vec), exp)

        vecs = [
            Bitvector(i, 3) for i in [0, 1, 2, 7, 8, 1024, 814715, '1' * 50]
        ]
        three_results = [
            '000', '001', '010', '111', '000', '000', '011', '111'
        ]
        for exp, vec in zip(three_results, vecs):
            self.assertEqual(str(vec), exp)

        #should pad or truncate to the correct length
        self.assertEqual(str(Bitvector(814715, 20)), '11000110111001111011')
        self.assertEqual(str(Bitvector(814715, 10)), '1001111011')
        self.assertEqual(str(Bitvector(8, 10)), '0000001000')
        self.assertEqual(str(Bitvector('1' * 50)), '1' * 50)
示例#8
0
    def test_and(self):
        """Bitvector A&B should return 0 for each position that is 0 in A and B"""

        results = [
            ['', '', '', '', '', '', ''],  #'' and x
            ['', '0', '0', '0', '0', '0', '0'],  #'0' and x
            ['', '0', '1', '0', '0', '1', '1'],  #'1' and x
            ['', '0', '0', '00', '00', '00', '00'],  #'00' and x
            ['', '0', '0', '00', '01', '00', '01'],  #'01' and x
            ['', '0', '1', '00', '00', '10', '10'],  #'10' and x
            ['', '0', '1', '00', '01', '10', '11'],  #'11' and x
        ]
        vectors = self.vectors

        for first_pos, first in enumerate(vectors):
            for second_pos, second in enumerate(vectors):
                self.assertEqual(str(first & second),
                                 results[first_pos][second_pos])
        #test chaining
        expected = Bitvector('0110')
        observed = Bitvector('1110') & Bitvector('1111') & Bitvector('0111')
        self.assertEqual(observed, expected)

        #test long
        self.assertEqual(Bitvector('10'*50) & Bitvector('11'*50), \
            Bitvector('10'*50))
示例#9
0
    def test_or(self):
        """Bitvector A|B should return 1 for each position that is 1 in A or B"""

        results = [
            ['', '', '', '', '', '', ''],  #'' or x
            ['', '0', '1', '0', '0', '1', '1'],  #'0' or x
            ['', '1', '1', '1', '1', '1', '1'],  #'1' or x
            ['', '0', '1', '00', '01', '10', '11'],  #'00' or x
            ['', '0', '1', '01', '01', '11', '11'],  #'01' or x
            ['', '1', '1', '10', '11', '10', '11'],  #'10' or x
            ['', '1', '1', '11', '11', '11', '11'],  #'11' or x
        ]
        vectors = self.vectors

        for first_pos, first in enumerate(vectors):
            for second_pos, second in enumerate(vectors):
                self.assertEqual(str(first | second),
                                 results[first_pos][second_pos])
        #test chaining
        expected = Bitvector('1110')
        observed = Bitvector('1000') | Bitvector('0100') | Bitvector('0110')
        self.assertEqual(observed, expected)

        #test long
        self.assertEqual(Bitvector('10'*50) | Bitvector('01'*50), \
            Bitvector('11'*50))
示例#10
0
    def test_init(self):
        """VectorFromRuns should return vector with 1 where string is ucase"""
        empty = []
        empty_run = [[5, 0]]
        one_run = [[5, 3]]
        two_runs = [[5, 1], [11, 4]]
        overlap = [[5, 3], [6, 4]]

        tests = [empty, empty_run, one_run, two_runs, overlap]
        exp_15 = [
            '0' * 15, '0' * 15, '0' * 5 + '1' * 3 + 7 * '0',
            '0' * 5 + '1' + '0' * 5 + '1' * 4, '0' * 5 + '1' * 5 + '0' * 5
        ]
        for test, exp in zip(tests, exp_15):
            self.assertEqual(VectorFromRuns(test, 15), Bitvector(exp))
            self.assertEqual(VectorFromRuns(test, 0), Bitvector(''))
        #should fail if vec too short
        self.assertRaises(IndexError, VectorFromRuns, two_runs, 5)
示例#11
0
    def test_init(self):
        """VectorFromSpans hould return vector with 1 in correct spans"""
        empty = []
        empty_run = [[5, 5]]
        one_run = [[5, 8]]
        two_runs = [[5, 6], [11, 15]]
        overlap = [[5, 8], [6, 10]]

        tests = [empty, empty_run, one_run, two_runs, overlap]
        exp_15 = [
            '0' * 15, '0' * 15, '0' * 5 + '1' * 3 + 7 * '0',
            '0' * 5 + '1' + '0' * 5 + '1' * 4, '0' * 5 + '1' * 5 + '0' * 5
        ]
        for test, exp in zip(tests, exp_15):
            self.assertEqual(VectorFromSpans(test, 15), Bitvector(exp))
            self.assertEqual(VectorFromSpans(test, 0), Bitvector(''))
        #should fail if vec too short
        self.assertRaises(IndexError, VectorFromSpans, two_runs, 5)
示例#12
0
 def test_thaw(self):
     """Bitvector thaw should return mutable bitvector with same data"""
     b = Bitvector('111')
     c = b.thaw()
     self.assertEqual(c, Bitvector('111'))
     c[1] = 0
     self.assertEqual(c, Bitvector('101'))
     self.assertNotEqual(Bitvector('111'), Bitvector('101'))
示例#13
0
    def test_invert(self):
        """Bitvector ~A should return a vector exchanging 1's for 0's"""
        results = map(Bitvector, ['', '1', '0', '11', '10', '01', '00'])
        for data, result in zip(self.vectors, results):
            self.assertEqual(~data, result)

            if len(data):
                self.assertNotEqual(data, result)
            else:
                self.assertEqual(data, result)

            #test chaining
            self.assertEqual(~~data,
                             data)  #inverting twice should give original
            self.assertEqual(~~~data, ~data)

            #test long
            self.assertEqual(~Bitvector('10' * 50), Bitvector('01' * 50))
            self.assertEqual(str(~Bitvector('10' * 50)),
                             str(Bitvector('01' * 50)))
示例#14
0
    def test_init(self):
        """VectorFromPositions should return correct vector"""
        empty = []
        first = [0]
        fifth = [4]
        several = [1, 4, 6, 9]

        tests = [empty, first, fifth, several]
        exp_0 = [''] * 4
        for test, exp in zip(tests, exp_0):
            self.assertEqual(VectorFromPositions(test, 0), Bitvector(exp))

        exp_5 = ['00000', '10000', '00001']
        for test, exp in zip(tests, exp_5):
            self.assertEqual(VectorFromPositions(test, 5), Bitvector(exp))

        exp_10 = ['0' * 10, '1' + '0' * 9, '00001' + '0' * 5, '0100101001']
        for test, exp in zip(tests, exp_10):
            self.assertEqual(VectorFromPositions(test, 10), Bitvector(exp))
        self.assertRaises(IndexError, VectorFromPositions, [10], 5)
示例#15
0
    def test_init(self):
        """VectorFromCases should return vector with 1 where string is ucase"""
        valid_strings = ['', 'a', 'X', 'aBc', 'Acb', 'abC', 'AAA', 'aaa', '@']
        results = ['', '0', '1', '010', '100', '001', '111', '000', '0']

        for data, result in zip(valid_strings, results):
            self.assertEqual(VectorFromCases(data), LongBitvector(result))
            self.assertTrue(isinstance(VectorFromCases(data), LongBitvector))
            self.assertEqual(VectorFromCases(data, ShortBitvector), \
                ShortBitvector(result))
            self.assertTrue(isinstance(VectorFromCases(data, ShortBitvector), \
                                       ShortBitvector))

        v = VectorFromCases('aBC')
        self.assertTrue(isinstance(v, ImmutableBitvector))

        w = VectorFromCases('aBC', MutableBitvector)
        self.assertTrue(isinstance(w, MutableBitvector))
        self.assertEqual(w, Bitvector('011'))
        w[0] = 1
        self.assertEqual(w, Bitvector('111'))
示例#16
0
 def test_freeze(self):
     """MutableBitvector freeze should return immutable bv with same data"""
     b = MutableBitvector('111')
     b[1] = 0
     c = b.freeze()
     self.assertSameObj(c, b._handler)
     self.assertEqual(c, Bitvector('101'))
     try:
         c[1] = 1
     except TypeError:
         pass
     else:
         raise AssertionError, \
             "MutableBitvector.freeze() returned mutable object."
示例#17
0
 def test_init(self):
     """Bitvector init should give expected results."""
     self.assertEqual(Bitvector(), 0)
     self.assertEqual(Bitvector('1001'), 9)
     self.assertEqual(Bitvector(['1', '0', '0', '0']), 8)
     self.assertEqual(Bitvector([]), 0)
     #if passing in non-sequence, must specify length
     self.assertRaises(TypeError, Bitvector, 1024)
     self.assertEqual(Bitvector(1024, 10), 1024)
     bv = Bitvector(10, 3)
     self.assertEqual(bv, 10)
     self.assertEqual(len(bv), 3)
     self.assertEqual(len(Bitvector('1' * 1000)), 1000)
     #check that initializing a bv from itself preserves length
     bv2 = Bitvector(bv)
     self.assertEqual(bv2, 10)
     self.assertEqual(len(bv2), 3)
示例#18
0
    def test_setitem(self):
        """MutableBitvector setitem should change positions correctly"""
        self.assertRaises(IndexError, MutableBitvector().__setitem__, 1, 1)
        vec_strings = ['0', '1', '10', '10001101', '101' * 50]
        vecs = map(MutableBitvector, vec_strings)
        results_1 = ['1', '1', '10', '10001101', '101' * 50]
        for vec, res in zip(vecs, results_1):
            vec[0] = 1
            self.assertEqual(vec, Bitvector(res))

        results_0 = ['0', '0', '10', '10001100', '101' * 49 + '100']
        for vec, res in zip(vecs, results_0):
            vec[-1] = 0
            self.assertEqual(vec, Bitvector(res))

        vec = MutableBitvector('1001000101001')
        vec[3:7] = '1111'
        self.assertEqual(vec, Bitvector('1001111101001'))
        vec[:4] = '0101'
        self.assertEqual(vec, Bitvector('0101111101001'))
        vec[7:] = '000111'
        self.assertEqual(vec, Bitvector('0101111000111'))
        vec[1:11:2] = '11011'
        self.assertEqual(vec, Bitvector('0101101101111'))
示例#19
0
    def test_str(self):
        """Bitvector str should match expected results"""
        vecs = [Bitvector(i, 0) for i in [0, 1, 2, 7, 8, 1024, 814715L]]
        for v in vecs:
            self.assertEqual(str(v), '')

        vecs = [
            Bitvector(i, 1) for i in [0, 1, 2, 7, 8, 1024, 814715L, '1' * 50]
        ]
        single_results = list('01010011')
        for exp, vec in zip(single_results, vecs):
            self.assertEqual(str(vec), exp)

        vecs = [
            Bitvector(i, 3) for i in [0, 1, 2, 7, 8, 1024, 814715L, '1' * 50]
        ]
        three_results = [
            '000', '001', '010', '111', '000', '000', '011', '111'
        ]
        for exp, vec in zip(three_results, vecs):
            self.assertEqual(str(vec), exp)

        #should pad or truncate to the correct length
        self.assertEqual(str(Bitvector(814715, 20)), '11000110111001111011')
        self.assertEqual(str(Bitvector(814715, 10)), '1001111011')
        self.assertEqual(str(Bitvector(8, 10)), '0000001000')
        self.assertEqual(str(Bitvector('1' * 50)), '1' * 50)

    def test_or(self):
        """Bitvector A|B should return 1 for each position that is 1 in A or B"""
示例#20
0
 def test_freeze(self):
     """Bitvector freeze should return same object"""
     v = Bitvector()
     self.assertSameObj(v.freeze(), v)
示例#21
0
 def test_repr(self):
     """Bitvector repr should look like a normal object"""
     v = Bitvector(3, 10)
     v_id = str(hex(id(v)))
     expected = '<cogent.core.bitvector.ShortBitvector object at'
     self.assertTrue( ` v `.startswith(expected))
示例#22
0
 def test_freeze(self):
     """Bitvector freeze should return same object"""
     v = Bitvector()
     self.assertSameObj(v.freeze(), v)