using LinearAlgebra
using CVChannel
using Test

#This script looks at the werner-holevo channel communication value for d=3
#It uses the general linear program to generate the constraints
println("\n Here we will see the Werner-Holevo channels are multiplicative")
println("for p less than 0.3, by searching over p in {0,0.01,...,0.35}.")

@testset "Non-multiplicativity of WH channels for p<0.35" begin
    λ_vals = [0:0.001:0.35;];
    ctr = 1;
    results = zeros(length(λ_vals),5)
    println("\nBeginning to get the data.")
    for λ_id in λ_vals
        if ctr == Int(round(length(λ_vals)-1)/2)
            println("Now halfway through.")
        end
        #Gets single copy cv
        A,B,g,a = generalWHLPConstraints(1,3,λ_id*ones(1))
        cv1, v1 =wernerHolevoCVPPT(1,3,A,B,g,a)
        #Gets parallely copy cv
        A,B,g,a = generalWHLPConstraints(2,3,λ_id*ones(2))
        cv2, v2 =wernerHolevoCVPPT(2,3,A,B,g,a)

        results[ctr,:] = [λ_id, cv1, cv2, cv1^2, cv2-cv1^2]
        ctr = ctr + 1
    end
    printed_30 = false
    print_array = zeros(1,5)
    for i in [1:Int(round(length(λ_vals)/10)):length(λ_vals);]
        if results[i,1]>0.3 && printed_30 == false
            print_array = [print_array; results[301,:]']
            printed_30 = true
        end
        print_array = [print_array; results[i,:]']
    end
    print_array = print_array[2:end,:]
    println("\n Here is a subset of the data. Column labels at bottom.")
    show(stdout, "text/plain", print_array)
    println("\n λ      cv(N)    cv(N^2)   diff")
    println("\nThus by looking at the diff column, we see what we were verifying.")
    info_vec = Vector{Union{Nothing,String}}(nothing, size(results)[1] + 1)
    info_vec[1] = "INFO:"
    info_vec[2] = "Generated by werner-holevo-non-multiplicativity.jl"
    header = ["λ" "cv(N)" "cv(N^2)" "cv(N)^2" "non-mult"]

    println("Please name the file you'd like to write the results to: \n")
    data_to_save = hcat(vcat(header,results),info_vec)
    file_name = readline()
    file_to_open = string(file_name,".csv")
    writedlm(file_to_open, data_to_save, ',')

    @test all(result -> result > 0 || isapprox(result, 0, atol=2e-6), results[:,5])
end
