#!/usr/bin/python

import re
import subprocess
import os.path
import sys

# Input file
hbfile = "../graphs/cage4.rb"
hbfile_solution = 12.37
result_error = 0.01


# Check the input file
if not os.path.exists(hbfile):
	print "Error: input file not found"
	sys.exit(0)


def check_program(program,parallel,nprocs):

	# Message
	print "Checking:" , program
	print "------------------------"

	if not os.path.exists(program):
		print "Error: Program not found"
		sys.exit(0)

	# Loop for the processes
	for p in nprocs:
	
		# Create the execution line
		exelist = []
		if parallel:
			exelist = ["mpiexec"]
			exelist.append("-n")
			exelist.append(str(p))
		exelist.append("./"+program)
		exelist.append("-n")
		exelist.append(str(100))
		exelist.append(hbfile)

		# Execute the program
		success = False
		subp = subprocess.Popen(exelist, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
		for line in subp.stdout.readlines():
			result = re.search('# Result norm: ([0-9\.]+)',line)
			if result:
				norm = float(result.group(1))
				error = abs(norm-hbfile_solution)
				if error < result_error:
					success = True
		retval = subp.wait()


		# Print the message
		print "[Procs " + str(p).rjust(2) + "] ",
		if success:
			print "Ok"
		else:
			print "WRONG !!!"

	print 


check_program("mmult_bit",True,[1,2,3,5,7,9,10,13,25])
check_program("mmult_csr",True,[1,2,3,5,7,9,10,13,25])





