def test_full_integral_image_correctness():
	''' Test generated full integral image correctness,
	    note that this relies on the corectness of interpreter and reference.py '''

	block_size = (20, 20)
	size = tuple(x*3 for x in block_size)

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)

	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	pe_dim = [s//b for s, b in zip(size, block_size)]
	def code_gen(code, block_size, args):
		return gen_code.gen_full_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, pe_dim, block_size)
	code = Code()
	code.set_generator(optimiser_wrapper(code_gen), block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	if not ((integral_err < err_eps) and (sq_integral_err < err_eps)):
		print 'integral comp:', integral_err
		print 'squared integral comp:', sq_integral_err
		
		print 'rendering instruction stream to file, can take a while'
		try:
			f = open('unoptimised_full_integral_image_trace.txt', 'w')
			def tag_str(instr): return ', '.join(instr.tag) if hasattr(instr, 'tag') else ''
			f.write('\n'.join(str(x).ljust(40) + ' tags: ' + tag_str(x) for x in code_gen(Code())))
			f.close()

			optim_gen = optimiser_wrapper(code_gen, block_size, {})
			f = open('bad_full_integral_image_trace.txt', 'w')
			def tag_str(instr): return ', '.join(instr.tag) if hasattr(instr, 'tag') else ''
			f.write('\n'.join(str(x).ljust(40) + ' tags: ' + tag_str(x) for x in optim_gen(Code())))
			f.close()
		except Exception, e:
			print 'could render instruction stream to file'
			print 'err: ' + str(e)
		
		assert False
def gen_integral_image_correctness():
	''' test if generated integral image is correct, 
	    note that this relies on the corectness of interpreter and reference.py '''

#		size = (120, 80)
#		block_size = (40, 40)
	size = (80, 80)
	block_size = size 

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)
	
	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	def code_gen(code, block_size, args):
		return gen_code.gen_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, block_size)
	code = Code()
	code.set_generator(optimiser_wrapper(code_gen), block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	if not ((integral_err < err_eps) and (sq_integral_err < err_eps)):
		print 'integral comp:', integral_err
		print 'squared integral comp:', sq_integral_err 
		assert False
def test_full_integral_image_correctness():
	''' Test generated full integral image correctness,
	    note that this relies on the correctness of interpreter and reference.py '''

	block_size = (20, 20)
	size = tuple(x*3 for x in block_size)

	# generate random test image
	test_image = [[float(random.randint(0, 255)) for i in xrange(size[0])] for j in xrange(size[1])]

	# reference implementation
	integral_ref = reference.gen_integral_image(test_image)
	sq_integral_ref = reference.gen_integral_squared_image(test_image)

	# pointer config
	buffer_size = block_size[0]*block_size[1]
	src_ptr = 0
	integral_ptr = buffer_size
	sq_integral_ptr = 2*buffer_size

	# set up interpreter for integral image calculation
	pe_dim = [s//b for s, b in zip(size, block_size)]
	code = Code()
	def code_gen(code, block_size, args): 
		return gen_code.gen_full_integral_image(code, src_ptr, integral_ptr, sq_integral_ptr, pe_dim, block_size)
	code.set_generator(code_gen, block_size)

	sim = Interpreter(code, test_image, block_size)
	sim.run()

	# get result of simulator with scaling, truncation turned off and float output
	integral_test = sim.gen_output_image(1, False, False, True)
	sq_integral_test = sim.gen_output_image(2, False, False, True)

	# comparison of reference with blip sim
	integral_err = compare_images(integral_ref, integral_test)
	sq_integral_err = compare_images(sq_integral_ref, sq_integral_test)

	err_eps = 0.001
	assert (integral_err < err_eps) and (sq_integral_err < err_eps)