#!/usr/bin/env python

import os
import sys
sys.path.insert(0, os.path.expanduser('~/lib'))
sys.path.insert(0, os.path.expanduser('.'))

import bufsock
import odirect
import readline0

underlying_chunk_size=1<<19
toplevel_chunk_size=12345

arrow="===============>"

found_error = 0
for filename in readline0.readline0():
	sys.stderr.write("comparing filename %s's data via odirect+bufsock and via normal python file I/O ...\n" % filename)
	odirect_file = odirect.odirect(filename, 'rbd', underlying_chunk_size)
	bufsock_file = bufsock.bufsock(odirect_file, chunk_len=underlying_chunk_size)
	regular_file = open(filename, 'r')
	while 1:
		# Note: assumption here is that both file I/O methods are going to return all that they can - EG, if we ask for
		# 512K, we're going to get 512K until we hit EOF.  If the two methods return short blocks inconsistently, this
		# test may appear to have failed, when it wouldn't if we were being careful about potential blocking
		# differences
		odirect_block = bufsock_file.read(toplevel_chunk_size)
		regular_block = regular_file.read(toplevel_chunk_size)
		if odirect_block != regular_block:
			sys.stderr.write('Woah!  %s Error on file %s, lengths are %d for odirect, %d for regular python I/O and %d for the requested transfer size\n' % \
				(arrow, filename, len(odirect_block), len(regular_block), toplevel_chunk_size))
			found_error = 1
		if not odirect_block:
			sys.stdout.write('Empty block')
			sys.stdout.flush()
			break
		#hex_dump(block)
		sys.stdout.write('%d ' % len(odirect_block))
		sys.stdout.flush()
	print
	del odirect_file
	regular_file.close()

sys.exit(found_error)