/*
 * Copyright 2010 Google Inc.
 * 
 * Licensed 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 com.google.synthesizer.core.model.test;

import com.google.synthesizer.core.model.SynthesisTime;

import junit.framework.TestCase;

public class SynthesisTimeTest extends TestCase {
  private static final double TOLERANCE = 0.0000001;

  public void testZeroRate() {
    SynthesisTime time = new SynthesisTime();
    assertEquals(0.0, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.0, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
  }
    
  public void testAdvance() {
    SynthesisTime time = new SynthesisTime();
    time.setSampleRate(10.0);  // 10 Hz
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.1, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.2, time.getAbsoluteTime(), TOLERANCE);
  }

  public void testChangeSampleRate() {
    // Set one sample rate.
    SynthesisTime time = new SynthesisTime();
    time.setSampleRate(10.0);  // 10 Hz
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.1, time.getAbsoluteTime(), TOLERANCE);

    // Change the sample rate.
    time.setSampleRate(5.0);  // 5 Hz
    assertEquals(0.2, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.1, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.2, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.3, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.2, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.5, time.getAbsoluteTime(), TOLERANCE);
  }

  public void testReset() {
    // Get it out of the default state.
    SynthesisTime time = new SynthesisTime();
    time.setSampleRate(10.0);  // 10 Hz
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.1, time.getAbsoluteTime(), TOLERANCE);

    // Reset.
    time.reset();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.0, time.getAbsoluteTime(), TOLERANCE);
    time.advance();
    assertEquals(0.1, time.getDeltaTime(), TOLERANCE);
    assertEquals(0.1, time.getAbsoluteTime(), TOLERANCE);
  }
}
