import matplotlib.pyplot as plt
from matplotlib import rc
import numpy as np
import sys

#setting parameters
our_results_filename = sys.argv[1]
afl_results_filename = sys.argv[2]
rad_results_filename = sys.argv[3]
interval = int(sys.argv[4])
legend_location = sys.argv[5]
figure_filename = sys.argv[6]
large_fontsize = 20
small_fontsize = 20

our_results_file = open(our_results_filename, "r")
afl_results_file = open(afl_results_filename, "r")
rad_results_file = open(rad_results_filename, "r")

#initializing data structures
our_results_set = set()
afl_results_set = set()
rad_results_set = set()

our_results_plot_data = [0]
afl_results_plot_data = [0]
rad_results_plot_data = [0]
x_axis_points = [0]

#obtaining plot data
count = 0
for line in our_results_file:
    #addinng our result to plot data
    line_split = line.split()
    our_results_set.add(line_split[len(line_split) - 1])
    our_results_plot_data.append(len(our_results_set))

    #addinng AFL's result to plot data
    afl_result = int(afl_results_file.readline())
    if afl_result != -1: #-1 output is generated when the test generated by AFL was invalid
        afl_results_set.add(afl_result)
    afl_results_plot_data.append(len(afl_results_set))

    #addinng Radamsa's result to plot data
    rad_result = int(rad_results_file.readline())
    if rad_result != -1: #-1 output is generated when the test generated by Radamsa was invalid
        rad_results_set.add(rad_result)
    rad_results_plot_data.append(len(rad_results_set))

    #adding number of tests to x-axis plot data
    count += 1
    x_axis_points.append(count)

#setting fonts
rc('font', **{'family':'serif','serif':['Times']})
rc('text', usetex=True)

#setting image boundary color
rc('axes',edgecolor='grey')

#setting figure size
plt.figure(figsize=(10,6))

#plotting the data
plt.plot(x_axis_points, our_results_plot_data, 'k-', linewidth='3', label='Our test generation tool')
plt.plot(x_axis_points, afl_results_plot_data, 'k--', linewidth='4', label='AFL')
plt.plot(x_axis_points, rad_results_plot_data, 'k-.', linewidth='5', label='Radamsa')

#adding labels to the plot
plt.xlabel("Number of tests", fontsize=large_fontsize)
plt.ylabel("Number of unique cache misses observed", fontsize=large_fontsize)
plt.legend(loc=legend_location, fontsize=large_fontsize)

#adding grid lines
ticks = np.arange(start=0, stop=count, step=interval)
plt.xticks(ticks)
# plt.grid()

#setting fontsizes for axes ticks
plt.setp(plt.xticks()[1], fontsize=small_fontsize)
plt.setp(plt.yticks()[1], fontsize=small_fontsize)

#setting plot margin
plt.margins(0, 0.01)
plt.tight_layout()

#saving figure and closing figure
plt.savefig(figure_filename)
# plt.show()
plt.close()
