/*
 * LIBOIL - Library of Optimized Inner Loops
 * Copyright (c) 2004 David A. Schleef <ds@schleef.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#include <stdio.h>
#include <liboil/liboil.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <liboil/liboilprototype.h>
#include <liboil/liboiltest.h>
#include <liboil/liboilcpu.h>
#define LOG_FILE "c:\\logs\\testsuite_test1_log1.txt"
#include "std_log_result.h"
#define LOG_FILENAME_LINE __FILE__, __LINE__

void create_xml(int result)
{
    if(result)
        assert_failed = 1;
    
    testResultXml("testsuite_test1");
    close_log_file();
}

void hist(OilTest *Test);

int main (int argc, char *argv[])
{
  OilFunctionClass *klass;
  OilFunctionImpl *impl;
  OilTest *test;
  int i;
  int n;
  //int j;
  int ret;
  unsigned int cpu_flags;
  //xmlfile = "test1";
  std_log(LOG_FILENAME_LINE, "Test Started testsuite_test1");
  
  oil_init ();

  cpu_flags = oil_cpu_get_flags ();     //Returns a bitmask containing the available CPU features
  n = oil_class_get_n_classes ();       
  for (i=0;i<n; i++ ){
    klass = oil_class_get_by_index(i);

    printf("%s\n", klass->name);
    std_log(LOG_FILENAME_LINE, "class name = %s\n",klass->name);

    test = oil_test_new (klass);        //Creates a new OilTest(structure describing how to test an OilFunctionImpl for an OilFunctionClass) for the OilFunctionClass represented by klass
    if (test == NULL) {
      std_log(LOG_FILENAME_LINE, "bad prototype");
      printf("  bad prototype\n");
      continue;
    }

    for (impl = klass->first_impl; impl; impl = impl->next) 
    {
        printf("  %s\n", impl->name);
        if ((impl->flags & OIL_CPU_FLAG_MASK) & ~cpu_flags) 
        {
            std_log(LOG_FILENAME_LINE, "not supported");
            printf("not supported\n");
        } 
        else 
        {
            test->n = 1600;
            ret = oil_test_check_impl (test, impl);     //Runs the testing procedure described by test on the implementation impl
            if (ret) 
            {
                #if 0
                printf("    %lu %g\n",test->prof.min,
                (double)test->prof.total/test->prof.n);
                for(j=0;j<test->prof.hist_n;j++){
                printf("    %lu %d\n",test->prof.hist_time[j],test->prof.hist_count[j]);
                }
                #endif
                printf("    ave=%g std=%g\n", impl->profile_ave, impl->profile_std);
                //hist(test);
            } 
            else 
            {
                printf("  not tested\n");
                oil_test_free (test);
                std_log(LOG_FILENAME_LINE, "Test Failed");
                create_xml(1);
                return 1;
            }
        }
    }//for loop

    oil_test_free (test);   //Frees memory associated with test(OilTest)
  }
  std_log(LOG_FILENAME_LINE, "Test Successful");
  create_xml(0);
  return 0;
}


void
hist(OilTest *test)
{
  double ave;
  double std;
  int max_i;
  double off;
  double s;
  double s2;
  int i;
  int n;
  double x;

  do {
    s = s2 = 0;
    n = 0;
    max_i = -1;
    for(i=0;i<10;i++){
      x = test->prof.hist_time[i];
      s2 += x * x * test->prof.hist_count[i];
      s += x * test->prof.hist_count[i];
      n += test->prof.hist_count[i];
      if (test->prof.hist_count[i] > 0) {
        if (max_i == -1 || test->prof.hist_time[i] > test->prof.hist_time[max_i]) {
          max_i = i;
        }
      }
    }

    ave = s / n;
    std = sqrt (s2 - s * s / n + n*n) / (n-1);
    off = (test->prof.hist_time[max_i] - ave)/std;

    printf("    ave=%g std=%g max=%ld off=%g %s\n",
        ave, std, test->prof.hist_time[max_i], off, (off>4.0)?"yes":"no");

    if (off > 4.0) {
      test->prof.hist_count[max_i] = 0;
    }
  } while (off > 4.0);
  printf("    ave=%g std=%g\n", ave, std);
}


