/*
   Derby - Class org.apache.derbyTesting.functionTests.tests.memorydb.MogTest

   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.
 */

package org.apache.derbyTesting.functionTests.tests.memorydb;

import junit.framework.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.TestConfiguration;

/**
 * Test consistency among (GenMog), (SampMog), and (ClusMog).
 */
public class MogTest extends BaseJDBCTestCase {

  public MogTest(String name) { super(name); }
	
  public static Test suite() {
    return TestConfiguration.defaultSuite(MogTest.class);
  }

  /** Dispose of objects after testing. */
  protected void tearDown() throws Exception
  {
    super.tearDown();
  }

  /**
   * Calculates by using the default directory/disk storage back end.
   *
   * @throws SQLException if the test fails
   */
  public void testClusMogOnDisk()
          throws SQLException {
      long start = System.currentTimeMillis();
      doTestClusMog(getConnection());
      println("duration-on-disk: " + (System.currentTimeMillis() - start));
  }

  /**
   * Calculates by using the in-memory storage back end.
   *
   * @throws SQLException if the test fails
   */
  public void testClusMogInMemory()
          throws SQLException {
      long start = System.currentTimeMillis();
      // Close the connection explicitly here, since the test framework won't
      // do it for us when we create it manually.
      Connection conn = obtainConnection();
      try {
          doTestClusMog(conn);
      } finally {
          try {
              conn.rollback();
              conn.close();
          } catch (SQLException sqle) {
              // Ignore exceptions during close.
          }
      }
      println("duration-in-memory: " + (System.currentTimeMillis() - start));
  }

  /**
   * Test consistency between (ClusMog) and (ClusMogSQL).
   * @throws SQLException if something goes wrong
   */
  public void doTestClusMog(Connection conn)
          throws SQLException {

    // Initialize test objects.
    long _seed = System.currentTimeMillis();
    java.util.Random rng = new java.util.Random(_seed);
    /** MOG generator being tested */
    GenMog genMog = new GenMog(rng);
    /** MOG sampler being tested */
    SampMog sampMog = new SampMog(rng);
    /** clustering object being tested */
    ClusMog clusMog = new ClusMog();
    /** clustering object being tested */
    ClusMogSQL clusMogSql = new ClusMogSQL(conn);
    clusMogSql.setUnique(rng.nextInt());

    println(getName() + " using random seed: " + _seed);
    final int max_ns = 10 * ClusMog.min_sample_size_per_cluster * GenMog.max_n;
    final double sample[] = new double[max_ns];
    final double center[] = new double[GenMog.max_n];
    final int niter = 1;
    for (int i=niter; i>0; --i) {
      // Generate a MOG configuration.
      genMog.generate();
      // Compute a sample size.
      final int min_ns = ClusMog.min_sample_size_per_cluster * genMog.n;
      final int ns = min_ns + rng.nextInt(max_ns - min_ns);
      println("ns = " + ns);
      // Generate samples from the MOG distribution.
      sampMog.set(genMog.n, genMog.weight, genMog.mean, genMog.var);
      sampMog.generate(ns, sample);

      // Produce an initial cluster center configuration.
      ClusMog.uniform(genMog.n, center, ns, sample);
      // Cluster the samples to recover the MOG configuration.
      clusMog.cluster(genMog.n, center, ns, sample);
      // Cluster the samples again, using SQL.
      clusMogSql.clusterSQL(genMog.n, center, ns, sample);
      // Compare the computed MOG configurations.
      assertEquals(clusMog.n, clusMogSql.n);
      compare(clusMog.n, clusMog.weight, clusMogSql.weight);
      compare(clusMog.n, clusMog.mean, clusMogSql.mean);
      compare(clusMog.n, clusMog.var, clusMogSql.var);

      // Produce another initial cluster center configuration.
      ClusMog.random(genMog.n, center, ns, sample, rng);
      // Cluster the samples to recover the MOG configuration.
      clusMog.cluster(genMog.n, center, ns, sample);
      // Cluster the samples again, using SQL.
      clusMogSql.clusterSQL(genMog.n, center, ns, sample);
      // Compare the computed MOG configurations.
      assertEquals(clusMog.n, clusMogSql.n);
      compare(clusMog.n, clusMog.weight, clusMogSql.weight);
      compare(clusMog.n, clusMog.mean, clusMogSql.mean);
      compare(clusMog.n, clusMog.var, clusMogSql.var);
    }
  }

  /** Compare two floating-point arrays, with tolerance. */
  private void compare(int n, double ones[], double oths[])
  {
    final double thresh = 1.0e-6;
    for (int i=0; i<n; ++i) {
      final double one = ones[i];
      final double oth = oths[i];
      final double dif = Math.abs(one - oth);
      final double err = dif / (1.0 + Math.abs(one));
      assertTrue(err < thresh);
    }
  }

  /**
   * Obtains a connection to an in-memory database.
   *
   * @return A connection to an in-memory database.
   * @throws SQLException if obtaining the connection fails
   */
  private Connection obtainConnection()
        throws SQLException {
    try {
        if (usingDerbyNetClient()) {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } else {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        }
    } catch (Exception e) {
        SQLException sqle =  new SQLException(e.getMessage());
        sqle.initCause(e);
        throw sqle;
    }
    StringBuffer sb = new StringBuffer("jdbc:derby:");
    if (usingEmbedded()) {
        sb.append("memory:");
    } else {
        // This is a hack. Change this when proper support for the in-memory
        // back end has been implemented.
        sb.append("//");
        sb.append(TestConfiguration.getCurrent().getHostName());
        sb.append(':');
        sb.append(TestConfiguration.getCurrent().getPort());
        sb.append('/');
        sb.append("memory:");
    }
    sb.append("MogTestDb;create=true");
    return DriverManager.getConnection(sb.toString());
  }
}

@@ -5646,24 +5646,134 @@
     
-assertTrue(err %3C
+// Use if to avoid unnecessary string concatenation.%0A      if (err %3E= thresh) %7B%0A        fail(%22Error too big;%22 + err + %22 %3E= %22 +
  thr
@@ -5774,24 +5774,32 @@
  + thresh);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

Improve testing of the in-memory back end. Added better error reporting to see if we have to increase the tolerance or if there is something else wrong with the test.