Example #1
0
def test(base, exp):
	size =  base ** exp
	## print out header
	s = [
		"Size / Divisor",
		"Number Bits Set",
		"Count Result",
		"Percent Bits Set"
	]
	print "".join(map(lambda x: x.rjust(25), s))
	print "="*100

	for i in range(exp):
		v = BitVector()
		divisor = base ** (exp-i)
		set = size/divisor
		for i in range(set):
			v[i*divisor] = 1
		count = v.count()
		pct = (100 * float(set) / size)

		### print out status...
		s = [
			"%d / %d" % (size, divisor),
			"%d" % (set,),
			"%d" % (count,),
			"%.08f" % (pct,)
		]
		print "".join(map(lambda x: x.rjust(25), s))
		assert count == set
Example #2
0
	def test_size(self):
		v = BitVector()
		v.resize(10)
		for i in range(5):
			v[i*2] = True
		assert len(v) == 10
		assert v.count() == 5
Example #3
0
	def test_serialize(self):
		v = self.random_vector()
		u = BitVector()
		assert u != v
		s = v.serialize()
		u.deserialize(s)
		assert u == v
Example #4
0
	def test_setget(self):
		v = BitVector()
		assert v.any() == False
		assert v.none() == True
		v[0] = 1
		v[2] = True
		assert v.any() == True
		assert v.none() == False
		assert v[0] == True
		assert v[1] == False
		assert v[2] == True
		assert v[3] == False
Example #5
0
	def test_count(self):
		base = 2
		exp = 18
		size = base ** exp

		for e in range(exp+1):
			v = BitVector()
			div = base ** (exp-e)
			nbits = size / div

			stdout.write("%d/%d... " % (nbits, size))
			stdout.flush()

			for i in range(nbits):
				v[i*div] = True

			assert v.count() == nbits
Example #6
0
	def random_vector(self, quiet=True):
		from random import random
		from bm import BitVector

		v = BitVector()
		def f():
			for i in range(int(self.size * self.chance)):
				j = int(self.size * random())
				if j >= self.size:
					continue
				v[j] = 1
		if not quiet:
			print
			print "="*50
			self.time(f, "Allocate Random Vector")
			v.print_stats()
		else:
			f()
		return v
Example #7
0
	def test_setclear(self):
		v = BitVector()
		v.resize(100)

		assert v.any() == False
		assert v.none() == True

		v.set()

		assert v.any() == True
		assert v.none() == False
		assert v.count() == len(v)

		v.clear()

		assert v.any() == False
		assert v.none() == True
		assert v.count() == 0