/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.
 */

// Note that $opt$ is a marker for the optimizing compiler to test
// it does compile the method.
public class SubTest {

  public static void expectEquals(int expected, int result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectEquals(long expected, long result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectEquals(float expected, float result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectEquals(double expected, double result) {
    if (expected != result) {
      throw new Error("Expected: " + expected + ", found: " + result);
    }
  }

  public static void expectApproxEquals(float a, float b) {
    float maxDelta = 0.0001F;
    boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta);
    if (!aproxEquals) {
      throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta + " " + (a - b));
    }
  }

  public static void expectApproxEquals(double a, double b) {
    double maxDelta = 0.00001D;
    boolean aproxEquals = (a > b) ? ((a - b) < maxDelta) : ((b - a) < maxDelta);
    if (!aproxEquals) {
      throw new Error("Expected: " + a + ", found: " + b + ", with delta: " + maxDelta + " " + (a - b));
    }
  }

  public static void expectNaN(float a) {
    if (a == a) {
      throw new Error("Expected NaN: " + a);
    }
  }

  public static void expectNaN(double a) {
    if (a == a) {
      throw new Error("Expected NaN: " + a);
    }
  }

  public static void main() {
    subInt();
    subLong();
    subFloat();
    subDouble();
  }

  private static void subInt() {
    expectEquals(2, $opt$Sub(5, 3));
    expectEquals(0, $opt$Sub(0, 0));
    expectEquals(-3, $opt$Sub(0, 3));
    expectEquals(3, $opt$Sub(3, 0));
    expectEquals(4, $opt$Sub(1, -3));
    expectEquals(-9, $opt$Sub(-12, -3));
    expectEquals(134217724, $opt$Sub(134217729, 5)); // (2^27 + 1) - 5
  }

  private static void subLong() {
    expectEquals(2L, $opt$Sub(5L, 3L));
    expectEquals(0L, $opt$Sub(0L, 0L));
    expectEquals(-3L, $opt$Sub(0L, 3L));
    expectEquals(3L, $opt$Sub(3L, 0L));
    expectEquals(4L, $opt$Sub(1L, -3L));
    expectEquals(-9L, $opt$Sub(-12L, -3L));
    expectEquals(134217724L, $opt$Sub(134217729L, 5L)); // (2^27 + 1) - 5
    expectEquals(34359738362L, $opt$Sub(34359738369L, 7L)); // (2^35 + 1) - 7
  }

  private static void subFloat() {
    expectApproxEquals(2F, $opt$Sub(5F, 3F));
    expectApproxEquals(0F, $opt$Sub(0F, 0F));
    expectApproxEquals(-3F, $opt$Sub(0F, 3F));
    expectApproxEquals(3F, $opt$Sub(3F, 0F));
    expectApproxEquals(4F, $opt$Sub(1F, -3F));
    expectApproxEquals(-9F, $opt$Sub(-12F, -3F));
    expectApproxEquals(34359738362F, $opt$Sub(34359738369F, 7F)); // (2^35 + 1) - 7
    expectApproxEquals(-0.1F, $opt$Sub(0.1F, 0.2F));
    expectApproxEquals(0.2F, $opt$Sub(-0.5F, -0.7F));

    expectNaN($opt$Sub(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY));
    expectNaN($opt$Sub(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY));
    expectNaN($opt$Sub(Float.NaN, 11F));
    expectNaN($opt$Sub(Float.NaN, -11F));
    expectNaN($opt$Sub(Float.NaN, Float.NEGATIVE_INFINITY));
    expectNaN($opt$Sub(Float.NaN, Float.POSITIVE_INFINITY));

    expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(-Float.MAX_VALUE, Float.MAX_VALUE));
    expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(2F, Float.POSITIVE_INFINITY));
    expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(Float.MAX_VALUE, -Float.MAX_VALUE));
    expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(2F, Float.NEGATIVE_INFINITY));
    expectEquals(Float.POSITIVE_INFINITY, $opt$Sub(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY));
    expectEquals(Float.NEGATIVE_INFINITY, $opt$Sub(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY));
  }

  private static void subDouble() {
    expectApproxEquals(2D, $opt$Sub(5D, 3D));
    expectApproxEquals(0D, $opt$Sub(0D, 0D));
    expectApproxEquals(-3D, $opt$Sub(0D, 3D));
    expectApproxEquals(3D, $opt$Sub(3D, 0D));
    expectApproxEquals(4D, $opt$Sub(1D, -3D));
    expectApproxEquals(-9D, $opt$Sub(-12D, -3D));
    expectApproxEquals(134217724D, $opt$Sub(134217729D, 5D)); // (2^27 + 1) - 5
    expectApproxEquals(34359738362D, $opt$Sub(34359738369D, 7D)); // (2^35 + 1) - 7
    expectApproxEquals(-0.1D, $opt$Sub(0.1D, 0.2D));
    expectApproxEquals(0.2D, $opt$Sub(-0.5D, -0.7D));

    expectNaN($opt$Sub(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY));
    expectNaN($opt$Sub(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY));
    expectNaN($opt$Sub(Double.NaN, 11D));
    expectNaN($opt$Sub(Double.NaN, -11D));
    expectNaN($opt$Sub(Double.NaN, Double.NEGATIVE_INFINITY));
    expectNaN($opt$Sub(Double.NaN, Double.POSITIVE_INFINITY));

    expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(-Double.MAX_VALUE, Double.MAX_VALUE));
    expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(2D, Double.POSITIVE_INFINITY));
    expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(Double.MAX_VALUE, -Double.MAX_VALUE));
    expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(2D, Double.NEGATIVE_INFINITY));
    expectEquals(Double.POSITIVE_INFINITY, $opt$Sub(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY));
    expectEquals(Double.NEGATIVE_INFINITY, $opt$Sub(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY));
  }

  static int $opt$Sub(int a, int b) {
    return a - b;
  }

  static long $opt$Sub(long a, long b) {
    return a - b;
  }

  static float $opt$Sub(float a, float b) {
    return a - b;
  }

  static double $opt$Sub(double a, double b) {
    return a - b;
  }

}
