~pieq/checkbox/fix-1576570-stress-test-progress-log

« back to all changes in this revision

Viewing changes to checkbox-support/checkbox_support/lib/bit.py

  • Committer: Sylvain Pineau
  • Date: 2014-01-07 13:39:38 UTC
  • mto: This revision was merged to the branch mainline in revision 2588.
  • Revision ID: sylvain.pineau@canonical.com-20140107133938-46v5ehofwa9whl1e
checkbox-support: Copy required modules from checkbox-old/checkbox

and their corresponding tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This file is part of Checkbox.
 
3
#
 
4
# Copyright 2008 Canonical Ltd.
 
5
#
 
6
# Checkbox is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License version 3,
 
8
# as published by the Free Software Foundation.
 
9
 
 
10
#
 
11
# Checkbox is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
from struct import calcsize
 
20
 
 
21
 
 
22
def get_bitmask(key):
 
23
    bitmask = []
 
24
    for value in reversed(key.split()):
 
25
        value = int(value, 16)
 
26
        bitmask.append(value)
 
27
 
 
28
    return bitmask
 
29
 
 
30
def get_bitcount(bitmask):
 
31
    bitcount = 0
 
32
    for value in bitmask:
 
33
        while value:
 
34
            bitcount += 1
 
35
            value &= (value - 1)
 
36
 
 
37
    return bitcount
 
38
 
 
39
def test_bit(bit, bitmask, bits=None):
 
40
    if bits is None:
 
41
        bits = calcsize("l") * 8
 
42
    offset = bit % bits
 
43
    long = int(bit / bits)
 
44
    if long >= len(bitmask):
 
45
        return 0
 
46
    return (bitmask[long] >> offset) & 1