// File: /workspace/github_cpp_makefile_star2_O1/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 -0x58(%rbp), %rbx
movl $0x1, (%rbx)
movq 0x7247(%rip), %r14      # 0xbfc8
leaq 0x34f4(%rip), %rsi      # 0x827c
movl $0x43, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x34fd(%rip), %rsi      # 0x82c0
movl $0x1c, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x34df(%rip), %rsi      # 0x82dd
movl $0x13, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x34b8(%rip), %rsi      # 0x82f1
movl $0x1d, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq 0x349b(%rip), %rsi      # 0x830f
movl $0x14, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x7129(%rip), %r15      # 0xbfd8
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2110
leaq 0x3463(%rip), %rsi      # 0x8324
movl $0x17, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x30(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x3434(%rip), %rsi      # 0x833c
movl $0x2f, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x54(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x2110
leaq 0x341a(%rip), %rsi      # 0x836c
movl $0x2a, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x2c(%rbp), %rsi
movq %r15, %rdi
callq 0x2110
leaq 0x33fe(%rip), %rsi      # 0x8397
movl $0x47, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x50(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
leaq 0x33ff(%rip), %rsi      # 0x83df
movl $0x55, %edx
movq %r14, %rdi
callq 0x21c0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
leaq -0x48(%rbp), %rsi
movq %r15, %rdi
callq 0x2120
cvtsi2sdl (%r12), %xmm0
movl (%rbx), %esi
leaq -0x90(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl (%r12), %xmm0
movl (%rbx), %esi
leaq -0x100(%rbp), %rdi
callq 0x41aa
xorps %xmm0, %xmm0
cvtsi2sdl -0x54(%rbp), %xmm0
movl -0x58(%rbp), %esi
leaq -0xd8(%rbp), %rdi
callq 0x41aa
movslq -0x30(%rbp), %rax
leaq (,%rax,8), %rcx
testq %rax, %rax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0x2030
movq %rax, %rbx
movq $0x5, -0x68(%rbp)
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
movsd %xmm0, -0xb0(%rbp)
movsd %xmm1, -0xa8(%rbp)
movq $0x0, -0xa0(%rbp)
movb $0x0, -0x98(%rbp)
cmpl $0x0, -0x30(%rbp)
jle 0x5149
movq -0x70(%rbp), %r14
movq -0xe0(%rbp), %r15
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0xb8(%rbp), %r12
xorl %r13d, %r13d
leaq -0xb0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
movq %rdi, %rdx
callq 0x7780
movapd %xmm0, %xmm1
movsd -0x40(%rbp), %xmm0
addsd 0x2f0c(%rip), %xmm0     # 0x8010
movsd %xmm0, -0x40(%rbp)
movsd %xmm1, (%rbx,%r13,8)
movq (%r14), %rax
movq %r14, %rdi
movapd %xmm1, %xmm0
movsd %xmm1, -0x38(%rbp)
callq *(%rax)
movsd -0x38(%rbp), %xmm0
movq (%r15), %rax
movq %r15, %rdi
callq *(%rax)
movsd -0x38(%rbp), %xmm0
movq (%r12), %rax
movq %r12, %rdi
callq *(%rax)
incq %r13
movslq -0x30(%rbp), %rax
cmpq %rax, %r13
jl 0x50e0
jmp 0x5152
xorpd %xmm0, %xmm0
movsd %xmm0, -0x40(%rbp)
movl -0x2c(%rbp), %esi
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0x90(%rbp), %rdi
callq 0x4398
movsd %xmm0, -0x60(%rbp)
movl -0x2c(%rbp), %eax
testl %eax, %eax
jle 0x520c
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %rdx
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd -0x40(%rbp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, -0x40(%rbp)
movl -0x30(%rbp), %esi
incq %rax
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movl $0x1, %edi
movsd 0x2e5c(%rip), %xmm0     # 0x8010
xorpd %xmm1, %xmm1
testl %esi, %esi
jle 0x51e3
movsd (%rcx,%rdi,8), %xmm2
xorl %r8d, %r8d
movsd (%rbx,%r8,8), %xmm3
ucomisd %xmm3, %xmm2
jb 0x51db
ucomisd (%rdx,%rdi,8), %xmm3
jb 0x51db
addsd %xmm0, %xmm1
incq %r8
cmpq %r8, %rsi
jne 0x51c4
movsd -0x40(%rbp), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm1, %xmm1
divsd %xmm2, %xmm1
movsd -0x38(%rbp), %xmm2
addsd %xmm1, %xmm2
movsd %xmm2, -0x38(%rbp)
incq %rdi
cmpq %rax, %rdi
jne 0x51b4
jmp 0x5215
xorpd %xmm0, %xmm0
movsd %xmm0, -0x38(%rbp)
movq 0x6dac(%rip), %rdi      # 0xbfc8
leaq 0x3212(%rip), %rsi      # 0x8435
movl $0xb, %edx
callq 0x21c0
movq 0x6d94(%rip), %rdi      # 0xbfc8
movsd -0x38(%rbp), %xmm0
callq 0x2220
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x6d59(%rip), %rdi      # 0xbfc8
leaq 0x31cb(%rip), %rsi      # 0x8441
movl $0xe, %edx
callq 0x21c0
movq 0x6d41(%rip), %rdi      # 0xbfc8
movsd -0x60(%rbp), %xmm0
callq 0x2220
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x6d06(%rip), %rdi      # 0xbfc8
leaq 0x3187(%rip), %rsi      # 0x8450
movl $0x4c, %edx
callq 0x21c0
movl -0x2c(%rbp), %r14d
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0x100(%rbp), %rdi
movl %r14d, %esi
callq 0x4398
decl %r14d
movl %r14d, %edi
callq 0x3d24
movq 0x6cc6(%rip), %rdi      # 0xbfc8
callq 0x2220
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq 0x6c90(%rip), %rdi      # 0xbfc8
leaq 0x315e(%rip), %rsi      # 0x849d
movl $0x59, %edx
callq 0x21c0
movl -0x2c(%rbp), %r14d
movsd -0x50(%rbp), %xmm0
movsd -0x48(%rbp), %xmm1
leaq -0xd8(%rbp), %rdi
movl %r14d, %esi
callq 0x4398
decl %r14d
movl %r14d, %edi
callq 0x3d24
movsd 0x2def(%rip), %xmm1     # 0x8168
xorl %esi, %esi
ucomisd %xmm0, %xmm1
seta %sil
movq 0x6c3e(%rip), %rdi      # 0xbfc8
callq 0x2260
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2130
movq %rbx, %rdi
callq 0x21e0
leaq -0xd8(%rbp), %rdi
callq 0x434a
leaq -0x100(%rbp), %rdi
callq 0x434a
leaq -0x90(%rbp), %rdi
callq 0x434a
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5407
jmp 0x5407
movq %rax, %rbx
jmp 0x5416
movq %rax, %rbx
jmp 0x5422
jmp 0x5407
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x434a
leaq -0x100(%rbp), %rdi
callq 0x434a
leaq -0x90(%rbp), %rdi
callq 0x434a
movq %rbx, %rdi
callq 0x22b0