// File: /workspace/github_cpp_makefile_star2_O2/ashlall[P]StreamStats/Test/Chi_Square_test.cpp
// Function: test_chi_square_1st()
void test_chi_square_1st() //One-sample test
{
 	int sketch_method = 1;
	cout << "Enter the number 1-4 to choose sketch method that you want to use: "<<endl;
	cout << "1. Greenwald sketch/default." <<endl;
	cout << "2. Q-Digest sketch." <<endl;
	cout << "3. Reservoir Sampling sketch." <<endl;
	cout << "4. Count-Min Sketch." <<endl;
	cin >> sketch_method;

 	int stream_size;
 	cout << "Enter the stream size: "<<endl;
	cin >> stream_size;

	int sample_size;
	cout << "Enter the memory capacity for the sample size: "<<endl;
 	cin >> sample_size;
  
	int k;
 	cout << "Enter the number of bins you want to use: " <<endl;
 	cin >> k;

	double mean, SD;
	cout << "Enter the mean of the normal distribution you want to compare against: "<<endl;
	cin >> mean;
	cout << "Enter the standard deviation of the normal distribution you want to compare against: "<<endl;
	cin >> SD;

	ChiSquareContinuous b(sample_size, sketch_method); //ChiSquare b(memory, sketch method);
	ChiSquareContinuous b1(sample_size, sketch_method);
	ChiSquareContinuous b2(sample_size, sketch_method);

 	double item;
	double chiEstimated;
  
	double N=0;
	double *items=new double[stream_size];
	default_random_engine generator(5);
	normal_distribution<double> distribution(mean,SD);
	for (int i=0; i<stream_size; i++) 
	{
    		item = distribution(generator);
    		N++;
    		items[i]=item;
  		b.insert(item);
  		b1.insert(item);
  		b2.insert(item);
    	}
    	chiEstimated = b.calculate_statistic_ifNormal(k,mean,SD);
    
    
    	double *Upper=b.get_upper();
    	double *Lower=b.get_lower();
    	double E=N/k;
    	double chiActual=0, chiActual2 = 0;
    /*timeval timeBefore, timeAfter;
    long diffSeconds, diffUSeconds;
    gettimeofday(&timeBefore, NULL);*/
    	for (int i=1;i<=k;i++)
   	{		
      		double O=0;
      		for(int j=0;j<stream_size;j++)
    		{
    	    	 	if(( items[j]<=Upper[i]) && (items[j]>=Lower[i]))
		        	O++;
    	    	}
      	    	double lambda= fabs(O-E);
    	    	chiActual=chiActual+((lambda*lambda)/E);
    	}

    /*gettimeofday(&timeAfter, NULL);
    diffSeconds = timeAfter.tv_sec - timeBefore.tv_sec;
    diffUSeconds = timeAfter.tv_usec - timeBefore.tv_usec;
    cout << "1st way: " << diffSeconds + diffUSeconds/1000000.0 << endl;

    gettimeofday(&timeBefore, NULL);
    int *frequencies = get_frequencies(Upper, Lower, items, k, stream_size);
    for (int i = 1; i <= k; i++)
      { 
	chiActual2 += (frequencies[i] - E) * (frequencies[i] - E)/E;
	}*/
    /*gettimeofday(&timeAfter, NULL);
    diffSeconds= timeAfter.tv_sec - timeBefore.tv_sec;
    diffUSeconds = timeAfter.tv_usec - timeBefore.tv_usec;
    cout << "2nd way: "<< diffSeconds + diffUSeconds/1000000.0<<" seconds\n";*/

   	cout << "chiActual: "<< chiActual << endl;
   	
  	cout << "chiEstimated: " << chiEstimated << endl;
  	

  	cout << "Estimated p-value by calling calculate_pvalue_ifNormal(num_bins, mean, SD): " << b1.calculate_pvalue_ifNormal(k, mean, SD) <<endl;

  	cout << "Estimated final decision by calling final_decision_ifNormal(num_bins, mean, SD, com_p	): "<<b2.final_decision_ifNormal(k, mean, SD, 0.05) <<endl;

  	delete []items;
	//delete [] frequencies;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
leaq -0x60(%rbp), %rbx
movl $0x1, (%rbx)
movq 0x89d7(%rip), %r14      # 0xcfc8
leaq 0x3c84(%rip), %rsi      # 0x827c
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq 0x3cb1(%rip), %rsi      # 0x82c0
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq 0x3cb7(%rip), %rsi      # 0x82dd
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq 0x3cb4(%rip), %rsi      # 0x82f1
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq 0x3cbb(%rip), %rsi      # 0x830f
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
movq 0x896d(%rip), %r15      # 0xcfd8
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2100
leaq 0x3ca7(%rip), %rsi      # 0x8324
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x30(%rbp), %rsi
movq %r15, %rdi
callq 0x2100
leaq 0x3c9c(%rip), %rsi      # 0x833c
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x5c(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2100
leaq 0x3ca6(%rip), %rsi      # 0x836c
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x2c(%rbp), %rsi
movq %r15, %rdi
callq 0x2100
leaq 0x3cae(%rip), %rsi      # 0x8397
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x3cd3(%rip), %rsi      # 0x83df
movq %r14, %rdi
callq 0x2180
movq %rax, %rdi
callq 0x20b0
leaq -0x48(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
cvtsi2sdl (%r12), %xmm0
movl (%rbx), %esi
leaq -0x90(%rbp), %rdi
callq 0x3bbc
cvtsi2sdl (%r12), %xmm0
movl (%rbx), %esi
leaq -0xe0(%rbp), %rdi
callq 0x3bbc
cvtsi2sdl -0x5c(%rbp), %xmm0
movl -0x60(%rbp), %esi
leaq -0xb8(%rbp), %rdi
callq 0x3bbc
movslq -0x30(%rbp), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x2030
movq %rax, %rbx
leaq -0x68(%rbp), %rax
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0x100(%rbp), %rcx
movsd %xmm0, (%rcx)
movsd %xmm1, 0x8(%rcx)
andq $0x0, 0x10(%rcx)
movq $0x5, (%rax)
movb $0x0, 0x18(%rcx)
movq -0x70(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0xc0(%rbp), %r13
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x98(%rbp), %r14
xorl %r15d, %r15d
movslq %r12d, %rax
cmpq %rax, %r15
jge 0x483c
leaq -0x100(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x535c
movapd %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
addsd 0x3815(%rip), %xmm0     # 0x8010
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, (%rbx,%r15,8)
movq -0x58(%rbp), %rdi
movq (%rdi), %rax
movapd %xmm1, %xmm0
movsd %xmm1, -0x38(%rbp)
callq *(%rax)
movsd -0x38(%rbp), %xmm0
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
movsd -0x38(%rbp), %xmm0
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
incq %r15
movl -0x30(%rbp), %r12d
jmp 0x47d2
movl -0x2c(%rbp), %esi
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0x90(%rbp), %rdi
callq 0x3cee
movsd %xmm0, -0x58(%rbp)
movq -0x80(%rbp), %rax
movl -0x2c(%rbp), %ecx
cvtsi2sd %ecx, %xmm0
movl -0x30(%rbp), %edx
xorl %edi, %edi
testl %edx, %edx
cmovlel %edi, %edx
movq -0x78(%rbp), %rsi
testl %ecx, %ecx
cmovlel %edi, %ecx
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
incl %ecx
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
pushq $0x1
popq %rdi
cmpq %rcx, %rdi
je 0x48ee
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
cmpq %r8, %rdx
je 0x48ca
movsd (%rbx,%r8,8), %xmm1
movsd (%rax,%rdi,8), %xmm2
ucomisd %xmm1, %xmm2
jb 0x48c5
ucomisd (%rsi,%rdi,8), %xmm1
jb 0x48c5
addsd 0x374b(%rip), %xmm0     # 0x8010
incq %r8
jmp 0x48a0
movsd -0x40(%rbp), %xmm1
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
divsd %xmm1, %xmm0
movsd -0x38(%rbp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, -0x38(%rbp)
incq %rdi
jmp 0x4894
movq 0x86d3(%rip), %rdi      # 0xcfc8
leaq 0x3b39(%rip), %rsi      # 0x8435
callq 0x2180
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x21f0
movq %rax, %rdi
callq 0x20b0
movq 0x86ab(%rip), %rdi      # 0xcfc8
leaq 0x3b1d(%rip), %rsi      # 0x8441
callq 0x2180
movq %rax, %rdi
movsd -0x58(%rbp), %xmm0
callq 0x21f0
movq %rax, %rdi
callq 0x20b0
movq 0x8683(%rip), %rdi      # 0xcfc8
leaq 0x3b04(%rip), %rsi      # 0x8450
callq 0x2180
movq %rax, %r14
movl -0x2c(%rbp), %esi
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0xe0(%rbp), %rdi
callq 0x3fd4
movq %r14, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x20b0
movq 0x8644(%rip), %rdi      # 0xcfc8
leaq 0x3b12(%rip), %rsi      # 0x849d
callq 0x2180
movq %rax, %r14
movl -0x2c(%rbp), %esi
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0xb8(%rbp), %rdi
callq 0x3fd4
movsd 0x37b4(%rip), %xmm1     # 0x8168
xorl %esi, %esi
ucomisd %xmm0, %xmm1
seta %sil
movq %r14, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x20b0
movq %rbx, %rdi
callq 0x21b0
leaq -0xb8(%rbp), %rdi
callq 0x3cb2
leaq -0xe0(%rbp), %rdi
callq 0x3cb2
leaq -0x90(%rbp), %rdi
callq 0x3cb2
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4a1c
jmp 0x4a1c
movq %rax, %rbx
jmp 0x4a2b
movq %rax, %rbx
jmp 0x4a37
jmp 0x4a1c
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x3cb2
leaq -0xe0(%rbp), %rdi
callq 0x3cb2
leaq -0x90(%rbp), %rdi
callq 0x3cb2
movq %rbx, %rdi
callq 0x2280