#!/usr/bin/env python

# (c) Copyright 2007 The Board of Trustees of the University of Illinois.

import sys
import itertools

import filecompare as fc
import textfilecompare as tfc

def read_record(f):
	# Read a record.  Each record consists of one integer and one
	# floating-point number.

	words = f.readline().split()

	try:
		(ix, flt) = words
	except ValueError:
		raise ValueError, "Malformed line in output file"

	return (int(ix), float(flt))

def compare_record((ref_n, ref_f), (cmp_n, cmp_f)):
	# The first number should match exactly
	if ref_n != cmp_n: return False

	# The second number should be equal with a tolerance of (1e-9 + 2%).
	diff = abs(ref_f - cmp_f)
	if not (diff < 1e-9 + 0.02 * abs(ref_f)):
		return False

	# All numbers are within tolerance
	return True

err = "Computed output does not match the expected values\n"

comparison = fc.Then(
	fc.Sequence_(itertools.repeat(fc.Compare(read_record, equal=compare_record, message=err), 20000)),
	fc.Compare(tfc.eof)
	)

fc.default_main(comparison)
