import pickle
from generation_code import serial_filename
import serial_output_code
import numpy as np
from shutil import copyfile
from os import remove

# This file is only temporary, mostly to be used when updating the
# reference output from a regression test, to ensure that, in all
# aspects that are in common with the previosu regression test, the new
# solution is the same.

# It is largely the same as test_serial_code.py

temp_filename = 'temp.pickle'

try:
    # Copy reference output to temporary location
    copyfile(serial_filename(),temp_filename)

    # Run serial code
    serial_output_code.serial_output_code()

    with open(serial_filename(),'rb') as f:
        qmc_out = pickle.load(f)

    with open(temp_filename,'rb') as f:
        old_out = pickle.load(f)
finally:
    # Copy reference output back
    copyfile(temp_filename,serial_filename())

    # Remove temporary file
    remove(temp_filename)


assert qmc_out[0] == old_out[0] # should be a float

print(len(qmc_out))

print(len(old_out))

assert len(qmc_out) == (len(old_out) + 1) # Because we've added in a new output

for ii in range(1,len(old_out)):
    assert(len(old_out[ii])==len(qmc_out[ii]))
    for jj in range(len(qmc_out[1])):
        # For some reason, the sizes of these variables (in
        # bytes) aren't always the same. I've no idea why.
        # Hence, this assertion is commented out.
        #assert getsizeof(qmc_out[ii][jj]) == getsizeof(old_out[ii][jj]) 
        #assert np.all(np.isclose(qmc_out[ii][jj],old_out[ii][jj]))
        assert np.all(np.isclose(qmc_out[ii][jj],old_out[ii][jj]))

