// File: 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
subq $0x1d0, %rsp            # imm = 0x1D0
movl $0x1, -0x4(%rbp)
movq 0x12bcf(%rip), %rdi     # 0x18fc8
movq %rdi, -0x158(%rbp)
leaq 0xae39(%rip), %rsi      # 0x11240
callq 0x2170
movq %rax, %rdi
movq 0x12ba2(%rip), %rsi     # 0x18fb8
movq %rsi, -0x150(%rbp)
callq 0x2190
movq -0x158(%rbp), %rdi
leaq 0xae54(%rip), %rsi      # 0x11284
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x158(%rbp), %rdi
leaq 0xae4f(%rip), %rsi      # 0x112a1
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x158(%rbp), %rdi
leaq 0xae41(%rip), %rsi      # 0x112b5
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x158(%rbp), %rdi
leaq 0xae3d(%rip), %rsi      # 0x112d3
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq 0x12b27(%rip), %rdi     # 0x18fd8
movq %rdi, -0x148(%rbp)
leaq -0x4(%rbp), %rsi
callq 0x20f0
movq -0x158(%rbp), %rdi
leaq 0xae19(%rip), %rsi      # 0x112e8
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x148(%rbp), %rdi
leaq -0x8(%rbp), %rsi
callq 0x20f0
movq -0x158(%rbp), %rdi
leaq 0xadff(%rip), %rsi      # 0x11300
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x148(%rbp), %rdi
leaq -0xc(%rbp), %rsi
callq 0x20f0
movq -0x158(%rbp), %rdi
leaq 0xadfd(%rip), %rsi      # 0x11330
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x148(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x20f0
movq -0x158(%rbp), %rdi
leaq 0xadf6(%rip), %rsi      # 0x1135b
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x148(%rbp), %rdi
leaq -0x18(%rbp), %rsi
callq 0x2140
movq -0x158(%rbp), %rdi
leaq 0xae0c(%rip), %rsi      # 0x113a3
callq 0x2170
movq -0x150(%rbp), %rsi
movq %rax, %rdi
callq 0x2190
movq -0x148(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x2140
cvtsi2sdl -0xc(%rbp), %xmm0
movl -0x4(%rbp), %esi
leaq -0x48(%rbp), %rdi
callq 0x5190
cvtsi2sdl -0xc(%rbp), %xmm0
movl -0x4(%rbp), %esi
leaq -0x70(%rbp), %rdi
callq 0x5190
jmp 0x65df
cvtsi2sdl -0xc(%rbp), %xmm0
movl -0x4(%rbp), %esi
leaq -0xa8(%rbp), %rdi
callq 0x5190
jmp 0x65f5
movq $0x0, -0xc0(%rbp)
movslq -0x8(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x2030
movq %rax, -0x160(%rbp)
jmp 0x6628
movq -0x160(%rbp), %rax
movq %rax, -0xc8(%rbp)
leaq -0xd0(%rbp), %rdi
movl $0x5, %esi
callq 0x83d0
jmp 0x6649
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
leaq -0xf0(%rbp), %rdi
callq 0x8400
jmp 0x6661
movl $0x0, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x676f
leaq -0xf0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x8450
movsd %xmm0, -0x168(%rbp)
jmp 0x6697
movsd -0x168(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsd -0xc0(%rbp), %xmm0
movsd 0xa951(%rip), %xmm1     # 0x11008
addsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movsd -0xb0(%rbp), %xmm0
movq -0xc8(%rbp), %rax
movslq -0xf4(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd -0xb0(%rbp), %xmm0
leaq -0x48(%rbp), %rdi
callq 0x53b0
jmp 0x66f1
movsd -0xb0(%rbp), %xmm0
leaq -0x70(%rbp), %rdi
callq 0x53b0
jmp 0x6704
movsd -0xb0(%rbp), %xmm0
leaq -0xa8(%rbp), %rdi
callq 0x53b0
jmp 0x671a
jmp 0x671c
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
jmp 0x666b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x6ae5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0x6adc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x5340
jmp 0x6adc
movl -0x10(%rbp), %esi
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
leaq -0x48(%rbp), %rdi
callq 0x53e0
movsd %xmm0, -0x170(%rbp)
jmp 0x678f
movsd -0x170(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x62f0
movq %rax, -0x100(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x62d0
movq %rax, -0x108(%rbp)
movsd -0xc0(%rbp), %xmm0
cvtsi2sdl -0x10(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x118(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x120(%rbp)
movl $0x1, -0x124(%rbp)
movl -0x124(%rbp), %eax
cmpl -0x10(%rbp), %eax
jg 0x6910
xorps %xmm0, %xmm0
movsd %xmm0, -0x130(%rbp)
movl $0x0, -0x134(%rbp)
movl -0x134(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x68ad
movq -0xc8(%rbp), %rax
movslq -0x134(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x100(%rbp), %rax
movslq -0x124(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
jb 0x6897
movq -0xc8(%rbp), %rax
movslq -0x134(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x108(%rbp), %rax
movslq -0x124(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jb 0x6897
movsd 0xa781(%rip), %xmm0     # 0x11008
addsd -0x130(%rbp), %xmm0
movsd %xmm0, -0x130(%rbp)
jmp 0x6899
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x681c
movsd -0x130(%rbp), %xmm0
subsd -0x110(%rbp), %xmm0
movaps 0xa8bc(%rip), %xmm1     # 0x11180
pand %xmm1, %xmm0
movsd %xmm0, -0x140(%rbp)
movsd -0x118(%rbp), %xmm0
movsd -0x140(%rbp), %xmm1
mulsd -0x140(%rbp), %xmm1
divsd -0x110(%rbp), %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x118(%rbp)
movl -0x124(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x124(%rbp)
jmp 0x67f8
movq 0x126b1(%rip), %rdi     # 0x18fc8
leaq 0xaadb(%rip), %rsi      # 0x113f9
callq 0x2170
movq %rax, -0x178(%rbp)
jmp 0x692c
movq -0x178(%rbp), %rdi
movsd -0x118(%rbp), %xmm0
callq 0x2290
movq %rax, -0x180(%rbp)
jmp 0x6949
movq -0x180(%rbp), %rdi
movq 0x12661(%rip), %rsi     # 0x18fb8
callq 0x2190
jmp 0x695e
movq 0x12663(%rip), %rdi     # 0x18fc8
leaq 0xaa99(%rip), %rsi      # 0x11405
callq 0x2170
movq %rax, -0x188(%rbp)
jmp 0x697a
movq -0x188(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x2290
movq %rax, -0x190(%rbp)
jmp 0x6997
movq -0x190(%rbp), %rdi
movq 0x12613(%rip), %rsi     # 0x18fb8
callq 0x2190
jmp 0x69ac
movq 0x12615(%rip), %rdi     # 0x18fc8
leaq 0xaa5a(%rip), %rsi      # 0x11414
callq 0x2170
movq %rax, -0x198(%rbp)
jmp 0x69c8
movl -0x10(%rbp), %esi
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
leaq -0x70(%rbp), %rdi
callq 0x5760
movsd %xmm0, -0x1a0(%rbp)
jmp 0x69e8
movsd -0x1a0(%rbp), %xmm0
movq -0x198(%rbp), %rdi
callq 0x2290
movq %rax, -0x1a8(%rbp)
jmp 0x6a05
movq -0x1a8(%rbp), %rdi
movq 0x125a5(%rip), %rsi     # 0x18fb8
callq 0x2190
jmp 0x6a1a
movq 0x125a7(%rip), %rdi     # 0x18fc8
leaq 0xaa39(%rip), %rsi      # 0x11461
callq 0x2170
movq %rax, -0x1b0(%rbp)
jmp 0x6a36
movl -0x10(%rbp), %esi
movsd -0x18(%rbp), %xmm0
movsd -0x20(%rbp), %xmm1
leaq -0xa8(%rbp), %rdi
movsd 0xa6fe(%rip), %xmm2     # 0x11150
callq 0x57c0
movb %al, -0x1b1(%rbp)
jmp 0x6a5f
movq -0x1b0(%rbp), %rdi
movb -0x1b1(%rbp), %al
movzbl %al, %esi
andl $0x1, %esi
callq 0x2270
movq %rax, -0x1c0(%rbp)
jmp 0x6a80
movq -0x1c0(%rbp), %rdi
movq 0x1252a(%rip), %rsi     # 0x18fb8
callq 0x2190
jmp 0x6a95
movq -0xc8(%rbp), %rax
movq %rax, -0x1c8(%rbp)
cmpq $0x0, %rax
je 0x6ab5
movq -0x1c8(%rbp), %rdi
callq 0x21b0
leaq -0xa8(%rbp), %rdi
callq 0x5340
leaq -0x70(%rbp), %rdi
callq 0x5340
leaq -0x48(%rbp), %rdi
callq 0x5340
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x70(%rbp), %rdi
callq 0x5340
leaq -0x48(%rbp), %rdi
callq 0x5340
movq -0x78(%rbp), %rdi
callq 0x2260
nopw (%rax,%rax)