0001: /* Licensed to the Apache Software Foundation (ASF) under one or more
0002: contributor license agreements. See the NOTICE file distributed with
0003: this work for additional information regarding copyright ownership.
0004: The ASF licenses this file to You under the Apache License, Version 2.0
0005: (the "License"); you may not use this file except in compliance with
0006: the License. You may obtain a copy of the License at
0007:
0008: http://www.apache.org/licenses/LICENSE-2.0
0009:
0010: Unless required by applicable law or agreed to in writing, software
0011: distributed under the License is distributed on an "AS IS" BASIS,
0012: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: See the License for the specific language governing permissions and
0014: limitations under the License.
0015: */
0016:
0017: package org.apache.derbyTesting.functionTests.tests.lang;
0018:
0019: import java.sql.ResultSet;
0020: import java.sql.SQLException;
0021: import java.sql.Statement;
0022: import java.util.Random;
0023:
0024: import junit.framework.Test;
0025: import junit.framework.TestSuite;
0026:
0027: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
0028: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
0029:
0030: public class MathTrigFunctionsTest extends BaseJDBCTestCase {
0031:
0032: private static final boolean debugFlag = false;
0033:
0034: private static final double SMALLEST_NEG_DERBY_DOUBLE = -1.79769E+308;
0035:
0036: private static final double SMALL_NEG_DOUBLE = -1.79768E+308;
0037:
0038: private static final double SMALLEST_POS_DERBY_DOUBLE = 2.225E-307;
0039:
0040: private static final double LARGEST_POS_DERBY_DOUBLE = 1.79769E+308;
0041:
0042: private static final double LARGEST_NEG_DERBY_DOUBLE = -2.225E-307;
0043:
0044: private static final double[] testRadians = { -0.000000001, -0.25,
0045: 0.000000001, 0.25, 0.5, 0.0, 1.0, 2.0,
0046: java.lang.StrictMath.PI, java.lang.StrictMath.PI };
0047:
0048: private static final double[] testArcValues = { 0.000000001,
0049: -0.000000001, 0.25, -0.25, 0.5, 0.0, -0.0, 1.0, -1.0 };
0050:
0051: private static final double[] logValues = { 0.000000001, 0.25, 0.5,
0052: 1.0, 45.0, 90.0, 135.0, 180.0, 270,
0053: SMALLEST_POS_DERBY_DOUBLE, LARGEST_POS_DERBY_DOUBLE };
0054:
0055: private static final double[] testValues = {
0056: SMALLEST_NEG_DERBY_DOUBLE, SMALL_NEG_DOUBLE,
0057: SMALLEST_POS_DERBY_DOUBLE, LARGEST_POS_DERBY_DOUBLE,
0058: LARGEST_NEG_DERBY_DOUBLE, 0.000000001, -0.000000001, 0.25,
0059: -0.25, 0.5, 0.0, -0.0, 1.0, -1.0, 2.0, 3.0,
0060: java.lang.StrictMath.PI, 2 * java.lang.StrictMath.PI, 4.0,
0061: 45.0, 90.0, 135.0, 180.0, 270 };
0062:
0063: private static final double[] testValuesTwo = {
0064: SMALLEST_NEG_DERBY_DOUBLE, SMALL_NEG_DOUBLE,
0065: SMALLEST_POS_DERBY_DOUBLE, LARGEST_NEG_DERBY_DOUBLE,
0066: 0.000000001, -0.000000001, 0.25, -0.25, 0.5, 0.0, -0.0,
0067: 1.0, -1.0, 2.0, 3.0, java.lang.StrictMath.PI,
0068: 2 * java.lang.StrictMath.PI, 4.0, 45.0, 90.0, 135.0, 180.0,
0069: 270 };
0070:
0071: /**
0072: * Tests the ACOS function which returns the arc cosine of a specified
0073: * number.
0074: * <p>
0075: * Acceptable input values to the ACOS function are DOUBLE PRECISION values
0076: * from -1 to 1. NULL returns NULL and if the absolute value of the input is
0077: * greater than 1 an SQL state of 22003 is returned.
0078: * <p>
0079: * The return value from the ACOS function is a DOUBLE PRECISION number in
0080: * radians with a range of zero to PI.
0081: *
0082: */
0083: public void testAcos() throws SQLException {
0084: // test the case where the input value is null
0085: executeNullValues("ACOS");
0086: executeNullFn("ACOS");
0087: debug();
0088: for (int i = 0; i < testArcValues.length; i++) {
0089: double expected = java.lang.StrictMath
0090: .acos(testArcValues[i]);
0091: double rValue = executeValues("ACOS", testArcValues[i]);
0092: debug("ACOS: input value: " + testArcValues[i]
0093: + " expected value: " + expected
0094: + " return value: " + rValue);
0095: assertEquals(expected, rValue, 0.0);
0096: double fValue = executeFn("ACOS", testArcValues[i]);
0097: assertEquals(expected, fValue, 0.0);
0098:
0099: }
0100: Random rand = new java.util.Random();
0101: for (int i = 0; i < 100; i++) {
0102: double randD = rand.nextDouble();
0103: double expect = java.lang.StrictMath.acos(randD);
0104: double rVal = executeValues("ACOS", randD);
0105: assertEquals(expect, rVal, 0.0);
0106: double fVal = executeFn("ACOS", randD);
0107: assertEquals(expect, fVal, 0.0);
0108:
0109: }
0110:
0111: /* test the case where the input value is out of range */
0112: try {
0113: executeValues("ACOS", 2.0);
0114: fail("ACOS: Out of range test failed, input value is: " + 2.0);
0115: } catch (SQLException sqlE) {
0116: // "ERROR 22003: The resulting value is outside the range for the
0117: // data type DOUBLE.";
0118: assertSQLState(
0119: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0120: sqlE);
0121: }
0122:
0123: /* test the case where the input value is out of range */
0124: try {
0125: executeFn("ACOS", 2.0);
0126: fail("ACOS: Out of range test failed, input value is: " + 2.0);
0127: } catch (SQLException sqlE) {
0128: // "ERROR 22003: The resulting value is outside the range for the
0129: // data type DOUBLE.";
0130: assertSQLState(
0131: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0132: sqlE);
0133: }
0134:
0135: }
0136:
0137: /**
0138: * Tests the ASIN function which returns the arc sine of a specified number.
0139: * <p>
0140: * Acceptable input values to the ASIN function are DOUBLE PRECISION values
0141: * from -1 to 1.
0142: * <p>
0143: * If the specified number is zero (0), the result of this function is zero.
0144: * Note: Derby does not support negative zero.
0145: * <p>
0146: * An input value of NULL returns NULL.
0147: * <p>
0148: * If the absolute value of the input is greater than 1 an SQL state of
0149: * 22003 is returned.
0150: * <p>
0151: * The return value from the ASIN function is a DOUBLE PRECISION number in
0152: * radians with a range of -PI/2 to PI/2.
0153: *
0154: */
0155: public void testAsin() throws SQLException {
0156: executeNullValues("ASIN");
0157: executeNullFn("ASIN");
0158: debug();
0159: for (int i = 0; i < testArcValues.length; i++) {
0160: double expected = java.lang.StrictMath
0161: .asin(testArcValues[i]);
0162: double rValue = executeValues("ASIN", testArcValues[i]);
0163: debug("ASIN: input value: " + testArcValues[i]
0164: + " expected value: " + expected
0165: + " return value: " + rValue);
0166: assertEquals(expected, rValue, 0.0);
0167: double fValue = executeFn("ASIN", testArcValues[i]);
0168: assertEquals(expected, fValue, 0.0);
0169: }
0170:
0171: Random rand = new java.util.Random();
0172: for (int i = 0; i < 100; i++) {
0173: double randD = rand.nextDouble();
0174: double expect = java.lang.StrictMath.asin(randD);
0175: double rVal = executeValues("ASIN", randD);
0176: assertEquals(expect, rVal, 0.0);
0177: double fVal = executeFn("ASIN", randD);
0178: assertEquals(expect, fVal, 0.0);
0179:
0180: }
0181:
0182: try {
0183: executeValues("ASIN", 2.0);
0184: fail("ASIN: Out of range test failed, input value is: " + 2.0);
0185: } catch (SQLException sqlE) {
0186: // "ERROR 22003: The resulting value is outside the range for the
0187: // data type DOUBLE.";
0188: assertSQLState(
0189: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0190: sqlE);
0191: }
0192: try {
0193: executeFn("ASIN", 2.0);
0194: fail("ASIN: Out of range test failed, input value is: " + 2.0);
0195: } catch (SQLException sqlE) {
0196: // "ERROR 22003: The resulting value is outside the range for the
0197: // data type DOUBLE.";
0198: assertSQLState(
0199: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0200: sqlE);
0201: }
0202:
0203: }
0204:
0205: /**
0206: * Tests the ATAN function which returns the arc tangent of a specified
0207: * number. Acceptable input values to the ATAN function are DOUBLE PRECISION
0208: * values.
0209: * <p>
0210: * If the specified number is zero (0), the result of this function is zero.
0211: * An input value of NULL returns NULL.
0212: * <p>
0213: * The return value from the ATAN function is a DOUBLE PRECISION number in
0214: * radians with a range of -PI/2 to PI/2.
0215: *
0216: */
0217: public void testAtan() throws SQLException {
0218: executeNullValues("ATAN");
0219: executeNullFn("ATAN");
0220:
0221: debug();
0222: for (int i = 0; i < testValues.length; i++) {
0223: double expected = java.lang.StrictMath.atan(testValues[i]);
0224: double rValue = executeValues("ATAN", testValues[i]);
0225: debug("ATAN: input value: " + testValues[i]
0226: + " expected value: " + expected
0227: + " return value: " + rValue);
0228: assertEquals(expected, rValue, 0.0);
0229: double fValue = executeFn("ATAN", testValues[i]);
0230: assertEquals(expected, fValue, 0.0);
0231: }
0232:
0233: Random rand = new java.util.Random();
0234: for (int i = 0; i < 100; i++) {
0235: double randD = rand.nextDouble();
0236: double expect = java.lang.StrictMath.atan(randD);
0237: double rVal = executeValues("ATAN", randD);
0238: assertEquals(expect, rVal, 0.0);
0239: double fVal = executeFn("ATAN", randD);
0240: assertEquals(expect, fVal, 0.0);
0241:
0242: }
0243:
0244: }
0245:
0246: /**
0247: * Tests the COS function which returns the cosine of a specified number.
0248: * <p>
0249: * Acceptable input values to the COS function are DOUBLE PRECISION values.
0250: * <p>
0251: * An input value of NULL returns NULL.
0252: */
0253: public void testCos() throws SQLException {
0254: executeNullValues("COS");
0255: executeNullFn("COS");
0256: debug();
0257: for (int i = 0; i < testValues.length; i++) {
0258: double expected = java.lang.StrictMath.cos(testValues[i]);
0259: double rValue = executeValues("COS", testValues[i]);
0260: debug("COS: input value: " + testValues[i]
0261: + " expected value: " + expected
0262: + " return value: " + rValue);
0263: assertEquals(expected, rValue, 0.0);
0264: double fValue = executeFn("COS", testValues[i]);
0265: assertEquals(expected, fValue, 0.0);
0266: }
0267:
0268: Random rand = new java.util.Random();
0269: for (int i = 0; i < 100; i++) {
0270: double randD = rand.nextDouble();
0271: double expect = java.lang.StrictMath.cos(randD);
0272: double rVal = executeValues("COS", randD);
0273: assertEquals(expect, rVal, 0.0);
0274: double fVal = executeFn("COS", randD);
0275: assertEquals(expect, fVal, 0.0);
0276:
0277: }
0278:
0279: }
0280:
0281: /**
0282: * Tests the SIN function which returns the sine of a specified number.
0283: * <p>
0284: * Acceptable input values to the SIN function are DOUBLE PRECISION values.
0285: * <p>
0286: * An input value of NULL returns NULL.
0287: * <p>
0288: * If the argument is zero, then the result is zero.
0289: * <p>
0290: * The data type of the returned value is a DOUBLE PRECISION number.
0291: */
0292: public void testSin() throws SQLException {
0293: executeNullValues("SIN");
0294: executeNullFn("SIN");
0295:
0296: debug();
0297: for (int i = 0; i < testValues.length; i++) {
0298: double expected = java.lang.StrictMath.sin(testValues[i]);
0299: double rValue = executeValues("SIN", testValues[i]);
0300: debug("SIN: input value: " + testValues[i]
0301: + " expected value: " + expected
0302: + " return value: " + rValue);
0303: assertEquals(expected, rValue, 0.0);
0304: double fValue = executeFn("SIN", testValues[i]);
0305: assertEquals(expected, fValue, 0.0);
0306: }
0307:
0308: Random rand = new java.util.Random();
0309: for (int i = 0; i < 100; i++) {
0310: double randD = rand.nextDouble();
0311: double expect = java.lang.StrictMath.sin(randD);
0312: double rVal = executeValues("SIN", randD);
0313: assertEquals(expect, rVal, 0.0);
0314: double fVal = executeFn("SIN", randD);
0315: assertEquals(expect, fVal, 0.0);
0316:
0317: }
0318:
0319: }
0320:
0321: /**
0322: * Tests the TAN function which returns the tangent of a specified number.
0323: * <p>
0324: * Acceptable input values to the TAN function are DOUBLE PRECISION values.
0325: * <p>
0326: * An input value of NULL returns NULL.
0327: * <p>
0328: * If the argument is zero, then the result is zero.
0329: * <p>
0330: * The data type of the returned value is a DOUBLE PRECISION number.
0331: */
0332: public void testTan() throws SQLException {
0333: executeNullValues("TAN");
0334:
0335: executeNullFn("TAN");
0336:
0337: debug();
0338: for (int i = 0; i < testValues.length; i++) {
0339: double expected = java.lang.StrictMath.tan(testValues[i]);
0340: double rValue = executeValues("TAN", testValues[i]);
0341: debug("TAN: input value: " + testValues[i]
0342: + " expected value: " + expected
0343: + " return value: " + rValue);
0344: assertEquals(expected, rValue, 0.0);
0345: double fValue = executeFn("TAN", testValues[i]);
0346: assertEquals(expected, fValue, 0.0);
0347: }
0348:
0349: Random rand = new java.util.Random();
0350: for (int i = 0; i < 100; i++) {
0351: double randD = rand.nextDouble();
0352: double expect = java.lang.StrictMath.tan(randD);
0353: double rVal = executeValues("TAN", randD);
0354: assertEquals(expect, rVal, 0.0);
0355: double fVal = executeFn("TAN", randD);
0356: assertEquals(expect, fVal, 0.0);
0357:
0358: }
0359:
0360: }
0361:
0362: /**
0363: * Tests the PI function which returns a value that is closer than any other
0364: * value to pi.
0365: * <p>
0366: * The constant pi is the ratio of the circumference of a circle to the
0367: * diameter of a circle.
0368: * <p>
0369: * No input values are allowed for the PI function.
0370: */
0371:
0372: public void testPI() throws SQLException {
0373: double value = executeValues("PI");
0374: assertEquals(java.lang.StrictMath.PI, value, 0.0);
0375: double fValue = executeFn("PI");
0376: assertEquals(java.lang.StrictMath.PI, fValue, 0.0);
0377:
0378: try {
0379: executeValues("PI", 2.0);
0380: fail("PI: Out of range test failed, input value is: " + 2.0);
0381: } catch (SQLException sqlE) {
0382: // '<statement>' is not recognized as a function or procedure.
0383: assertSQLState("42Y03", sqlE);
0384: }
0385:
0386: try {
0387: executeFn("PI", 2.0);
0388: fail("PI: Out of range test failed, input value is: " + 2.0);
0389: } catch (SQLException sqlE) {
0390: // '<statement>' is not recognized as a function or procedure.
0391: assertSQLState("42Y03", sqlE);
0392: }
0393:
0394: }
0395:
0396: /**
0397: * Tests the DEGREES function which converts a DOUBLE PRECISION number from
0398: * radians to degrees.
0399: * <p>
0400: * The input is an angle measured in radians, which is converted to an
0401: * approximately equivalent angle measured in degrees.
0402: * <p>
0403: * The conversion from radians to degrees is not exact. You should not
0404: * expect that the COS(DEGREES(PI/2)) to exactly equal 0.0.
0405: * <p>
0406: * The return value is a DOUBLE PRECISION number.
0407: */
0408: public void testDegrees() throws SQLException {
0409: executeNullValues("DEGREES");
0410: executeNullFn("DEGREES");
0411:
0412: debug();
0413: for (int i = 0; i < testRadians.length; i++) {
0414: double expected = java.lang.StrictMath
0415: .toDegrees(testRadians[i]);
0416: double rValue = executeValues("DEGREES", testRadians[i]);
0417: // rValue = executeValues("DEGREES", SMALL_NEG_DOUBLE);
0418: debug("DEGREES: input value: " + testRadians[i]
0419: + " expected value: " + expected
0420: + " return value: " + rValue);
0421: assertEquals(expected, rValue, 0.0);
0422: double fValue = executeFn("DEGREES", testRadians[i]);
0423: assertEquals(expected, fValue, 0.0);
0424:
0425: }
0426:
0427: Random rand = new java.util.Random();
0428: for (int i = 0; i < 100; i++) {
0429: double randD = rand.nextDouble();
0430: double expect = java.lang.StrictMath.toDegrees(randD);
0431: double rVal = executeValues("DEGREES", randD);
0432: assertEquals(expect, rVal, 0.0);
0433: double fVal = executeFn("DEGREES", randD);
0434: assertEquals(expect, fVal, 0.0);
0435:
0436: }
0437:
0438: try {
0439: executeValues("DEGREES", SMALLEST_NEG_DERBY_DOUBLE);
0440: fail("DEGREES: Out of range test failed, input value is: "
0441: + SMALLEST_NEG_DERBY_DOUBLE);
0442: } catch (SQLException sqlE) {
0443: // "ERROR 22003: The resulting value is outside the range for the
0444: // data type DOUBLE.";
0445: assertSQLState(
0446: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0447: sqlE);
0448: }
0449: try {
0450: executeFn("DEGREES", SMALLEST_NEG_DERBY_DOUBLE);
0451: fail("DEGREES: Out of range test failed, input value is: "
0452: + SMALLEST_NEG_DERBY_DOUBLE);
0453: } catch (SQLException sqlE) {
0454: // "ERROR 22003: The resulting value is outside the range for the
0455: // data type DOUBLE.";
0456: assertSQLState(
0457: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0458: sqlE);
0459: }
0460: }
0461:
0462: /**
0463: * Tests the RADIANS function which converts a DOUBLE PRECISION number from
0464: * degrees to radians.
0465: * <p>
0466: * The input is an angle measured in degrees, which is converted to an
0467: * approximately equivalent angle measured in radians.
0468: * <p>
0469: * The conversion from radians to degrees is not exact. You should not
0470: * expect that the COS(RADIANS(90.0)) to exactly equal 0.0.
0471: * <p>
0472: * The return value is a DOUBLE PRECISION number.
0473: */
0474: public void testRadians() throws SQLException {
0475: executeNullValues("RADIANS");
0476:
0477: executeNullFn("RADIANS");
0478:
0479: debug();
0480: for (int i = 0; i < testArcValues.length; i++) {
0481: double expected = java.lang.StrictMath
0482: .toRadians(testArcValues[i]);
0483: double rValue = executeValues("RADIANS", testArcValues[i]);
0484: debug("RADIANS: input value: " + testArcValues[i]
0485: + " expected value: " + expected
0486: + " return value: " + rValue);
0487: assertEquals(expected, rValue, 0.0);
0488: double fValue = executeFn("RADIANS", testArcValues[i]);
0489: assertEquals(expected, fValue, 0.0);
0490:
0491: }
0492:
0493: Random rand = new java.util.Random();
0494: for (int i = 0; i < 100; i++) {
0495: double randD = rand.nextDouble();
0496: double expect = java.lang.StrictMath.toRadians(randD);
0497: double rVal = executeValues("RADIANS", randD);
0498: assertEquals(expect, rVal, 0.0);
0499: double fVal = executeFn("RADIANS", randD);
0500: assertEquals(expect, fVal, 0.0);
0501:
0502: }
0503:
0504: try {
0505: executeValues("RADIANS", SMALLEST_POS_DERBY_DOUBLE);
0506: fail("RADIANS: Out of range test failed, input value is: "
0507: + SMALLEST_NEG_DERBY_DOUBLE);
0508: } catch (SQLException sqlE) {
0509: // "ERROR 22003: The resulting value is outside the range for the
0510: // data type DOUBLE.";
0511: assertSQLState(
0512: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0513: sqlE);
0514: }
0515: try {
0516: executeFn("RADIANS", SMALLEST_POS_DERBY_DOUBLE);
0517: fail("RADIANS: Out of range test failed, input value is: "
0518: + SMALLEST_NEG_DERBY_DOUBLE);
0519: } catch (SQLException sqlE) {
0520: // "ERROR 22003: The resulting value is outside the range for the
0521: // data type DOUBLE.";
0522: assertSQLState(
0523: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0524: sqlE);
0525: }
0526: }
0527:
0528: /**
0529: * Tests the EXP function which returns e raised to the power of the input
0530: * DOUBLE PRECISION number. The input number is the exponent to raise e to.
0531: * <p>
0532: * The constant e is the base of the natural logarithms.
0533: * <p>
0534: * The return value is a DOUBLE PRECISION number.
0535: *
0536: * @throws SQLException
0537: */
0538: public void testExp() throws SQLException {
0539: executeNullValues("EXP");
0540: executeNullFn("EXP");
0541:
0542: debug();
0543: for (int i = 0; i < testValuesTwo.length; i++) {
0544: double expected = java.lang.StrictMath
0545: .exp(testValuesTwo[i]);
0546: double rValue = executeValues("EXP", testValuesTwo[i]);
0547: debug("EXP: input value: " + testValuesTwo[i]
0548: + " expected value: " + expected
0549: + " return value: " + rValue);
0550: assertEquals(expected, rValue, 0.0);
0551: double fValue = executeFn("EXP", testValuesTwo[i]);
0552: assertEquals(expected, fValue, 0.0);
0553: }
0554:
0555: Random rand = new java.util.Random();
0556: for (int i = 0; i < 100; i++) {
0557: double randD = rand.nextDouble();
0558: double expect = java.lang.StrictMath.exp(randD);
0559: double rVal = executeValues("EXP", randD);
0560: assertEquals(expect, rVal, 0.0);
0561: double fVal = executeFn("EXP", randD);
0562: assertEquals(expect, fVal, 0.0);
0563:
0564: }
0565:
0566: try {
0567: executeValues("EXP", LARGEST_POS_DERBY_DOUBLE);
0568: fail("EXP: Out of range test failed, input value is: "
0569: + LARGEST_POS_DERBY_DOUBLE);
0570: } catch (SQLException sqlE) {
0571: // "ERROR 22003: The resulting value is outside the range for the
0572: // data type DOUBLE.";
0573: assertSQLState(
0574: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0575: sqlE);
0576: }
0577: try {
0578: executeFn("EXP", LARGEST_POS_DERBY_DOUBLE);
0579: fail("EXP: Out of range test failed, input value is: "
0580: + LARGEST_POS_DERBY_DOUBLE);
0581: } catch (SQLException sqlE) {
0582: // "ERROR 22003: The resulting value is outside the range for the
0583: // data type DOUBLE.";
0584: assertSQLState(
0585: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0586: sqlE);
0587: }
0588:
0589: }
0590:
0591: /**
0592: * Tests the LOG10 function which returns the base-10 logarithm of a DOUBLE
0593: * PRECISION number that is greater than zero.
0594: * <p>
0595: * If the input value is NULL, the result of this function is NULL.
0596: * <p>
0597: * If the input value is zero or a negative number, an exception is returned
0598: * that indicates that the value is out of range (SQL state 22003).
0599: * <p>
0600: * The return type is a DOUBLE PRECISION number.
0601: */
0602:
0603: public void testLog10() throws SQLException {
0604: executeNullValues("LOG10");
0605: executeNullFn("LOG10");
0606:
0607: debug();
0608: for (int i = 0; i < logValues.length; i++) {
0609: // ln 10 = y * (log base 10 (10))
0610: // 2.3025850929940456840179914546844 = y * 1
0611: double expected = java.lang.StrictMath.log(logValues[i]) / 2.3025850929940456840179914546844;
0612: double rValue = executeValues("LOG10", logValues[i]);
0613: debug("LOG10: input value: " + logValues[i]
0614: + " expected value: " + expected
0615: + " return value: " + rValue);
0616: assertEquals(expected, rValue, 0.0);
0617: double fValue = executeFn("LOG10", logValues[i]);
0618: assertEquals(expected, fValue, 0.0);
0619: }
0620:
0621: Random rand = new java.util.Random();
0622: for (int i = 0; i < 100; i++) {
0623: double randD = rand.nextDouble();
0624: double expect = java.lang.StrictMath.log(randD) / 2.3025850929940456840179914546844;
0625: double rVal = executeValues("LOG10", randD);
0626: assertEquals(expect, rVal, 0.0);
0627: double fVal = executeFn("LOG10", randD);
0628: assertEquals(expect, fVal, 0.0);
0629:
0630: }
0631:
0632: try {
0633: executeValues("LOG10", 0.0);
0634: fail("LOG10: Out of range test failed, input value is: " + 0.0);
0635: } catch (SQLException sqlE) {
0636: // "ERROR 22003: The resulting value is outside the range for the
0637: // data type DOUBLE.";
0638: assertSQLState(
0639: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0640: sqlE);
0641: }
0642: try {
0643: executeValues("LOG10", -1.0);
0644: fail("LOG10: Out of range test failed, input value is: "
0645: + -1.0);
0646: } catch (SQLException sqlE) {
0647: // "ERROR 22003: The resulting value is outside the range for the
0648: // data type DOUBLE.";
0649: assertSQLState(
0650: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0651: sqlE);
0652: }
0653:
0654: try {
0655: executeFn("LOG10", 0.0);
0656: fail("LOG10: Out of range test failed, input value is: " + 0.0);
0657: } catch (SQLException sqlE) {
0658: // "ERROR 22003: The resulting value is outside the range for the
0659: // data type DOUBLE.";
0660: assertSQLState(
0661: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0662: sqlE);
0663: }
0664: try {
0665: executeFn("LOG10", -1.0);
0666: fail("LOG10: Out of range test failed, input value is: "
0667: + -1.0);
0668: } catch (SQLException sqlE) {
0669: // "ERROR 22003: The resulting value is outside the range for the
0670: // data type DOUBLE.";
0671: assertSQLState(
0672: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0673: sqlE);
0674: }
0675:
0676: }
0677:
0678: /**
0679: * Tests the LOG function which returns the natural logarithm (base e) of a
0680: * DOUBLE PRECISION number that is greater than zero (0).
0681: * <p>
0682: * If the specified number is NULL, the result of these functions is NULL.
0683: * If the specified number is zero or a negative number, an exception is
0684: * returned that indicates that the value is out of range (SQL state 22003).
0685: * <p>
0686: * The data type of the returned value is a DOUBLE PRECISION number.
0687: */
0688: public void testLog() throws SQLException {
0689: executeNullValues("LOG");
0690: executeNullFn("LOG");
0691:
0692: debug();
0693: for (int i = 0; i < logValues.length; i++) {
0694: double expected = java.lang.StrictMath.log(logValues[i]);
0695: double rValue = executeValues("LOG", logValues[i]);
0696: debug("LOG: input value: " + logValues[i]
0697: + " expected value: " + expected
0698: + " return value: " + rValue);
0699: assertEquals(expected, rValue, 0.0);
0700: double fValue = executeFn("LOG", logValues[i]);
0701: assertEquals(expected, fValue, 0.0);
0702: }
0703:
0704: Random rand = new java.util.Random();
0705: for (int i = 0; i < 100; i++) {
0706: double randD = rand.nextDouble();
0707: double expect = java.lang.StrictMath.log(randD);
0708: double rVal = executeValues("LOG", randD);
0709: assertEquals(expect, rVal, 0.0);
0710: double fVal = executeFn("LOG", randD);
0711: assertEquals(expect, fVal, 0.0);
0712:
0713: }
0714:
0715: try {
0716: executeValues("LOG", 0.0);
0717: fail("LOG: Out of range test failed, input value is: " + 0.0);
0718: } catch (SQLException sqlE) {
0719: // "ERROR 22003: The resulting value is outside the range for the
0720: // data type DOUBLE.";
0721: assertSQLState(
0722: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0723: sqlE);
0724: }
0725: try {
0726: executeFn("LOG", 0.0);
0727: fail("LOG: Out of range test failed, input value is: " + 0.0);
0728: } catch (SQLException sqlE) {
0729: // "ERROR 22003: The resulting value is outside the range for the
0730: // data type DOUBLE.";
0731: assertSQLState(
0732: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0733: sqlE);
0734: }
0735:
0736: }
0737:
0738: /**
0739: * Tests the LN function which returns the natural logarithm (base e) of a
0740: * DOUBLE PRECISION number that is greater than zero (0).
0741: * <p>
0742: * If the specified number is NULL, the result of these functions is NULL.
0743: * If the specified number is zero or a negative number, an exception is
0744: * returned that indicates that the value is out of range (SQL state 22003).
0745: * <p>
0746: * The data type of the returned value is a DOUBLE PRECISION number.
0747: */
0748: public void testLn() throws SQLException {
0749: executeNullValues("LN");
0750: // Note: the syntax 'values {fn ln(value)}' is not supported
0751: // Object fnVal = executeNullFn("LN");
0752: debug();
0753: for (int i = 0; i < logValues.length; i++) {
0754: double expected = java.lang.StrictMath.log(logValues[i]);
0755: double rValue = executeValues("LN", logValues[i]);
0756: debug("LOG: input value: " + logValues[i]
0757: + " expected value: " + expected
0758: + " return value: " + rValue);
0759: assertEquals(expected, rValue, 0.0);
0760: }
0761:
0762: Random rand = new java.util.Random();
0763: for (int i = 0; i < 100; i++) {
0764: double randD = rand.nextDouble();
0765: double expect = java.lang.StrictMath.log(randD);
0766: double rVal = executeValues("LN", randD);
0767: assertEquals(expect, rVal, 0.0);
0768: }
0769:
0770: try {
0771: executeValues("LN", 0.0);
0772: fail("LOG: Out of range test failed, input value is: " + 0.0);
0773: } catch (SQLException sqlE) {
0774: // "ERROR 22003: The resulting value is outside the range for the
0775: // data type DOUBLE.";
0776: assertSQLState(
0777: SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
0778: sqlE);
0779: }
0780:
0781: }
0782:
0783: /**
0784: * Tests the CEIL function which rounds a DOUBLE PRECISION number up, and
0785: * return the smallest number that is greater than or equal to the input
0786: * number.
0787: * <p>
0788: * If the input number is NULL, the result of these functions is NULL. If
0789: * the input number is equal to a mathematical integer, the result of these
0790: * functions is the same as the input number. If the input number is zero
0791: * (0), the result of these functions is zero. If the input number is less
0792: * than zero but greater than -1.0, then the result of these functions is
0793: * zero.
0794: * <p>
0795: * The returned value is the smallest (closest to negative infinity) double
0796: * floating point value that is greater than or equal to the specified
0797: * number. The returned value is equal to a mathematical integer.
0798: * <p>
0799: * The data type of the returned value is a DOUBLE PRECISION number.
0800: */
0801:
0802: public void testCeil() throws SQLException {
0803: executeNullValues("CEIL");
0804:
0805: // Note: the syntax 'values {fn CEIL(value)}' is not supported
0806: // Object fnVal = executeNullFn("CEIL");
0807:
0808: debug();
0809: for (int i = 0; i < testValues.length; i++) {
0810: double expected = java.lang.StrictMath.ceil(testValues[i]);
0811: double rValue = executeValues("CEIL", testValues[i]);
0812: debug("CEIL: input value: " + testValues[i]
0813: + " expected value: " + expected
0814: + " return value: " + rValue);
0815: assertEquals(expected, rValue, 0.0);
0816: }
0817:
0818: Random rand = new java.util.Random();
0819: for (int i = 0; i < 100; i++) {
0820: double randD = rand.nextDouble();
0821: double expect = java.lang.StrictMath.ceil(randD);
0822: double rVal = executeValues("CEIL", randD);
0823: assertEquals(expect, rVal, 0.0);
0824: }
0825:
0826: }
0827:
0828: /**
0829: * Tests the CEILING function which rounds a DOUBLE PRECISION number up, and
0830: * return the smallest number that is greater than or equal to the input
0831: * number.
0832: * <p>
0833: * If the input number is NULL, the result of these functions is NULL. If
0834: * the input number is equal to a mathematical integer, the result of these
0835: * functions is the same as the input number. If the input number is zero
0836: * (0), the result of these functions is zero. If the input number is less
0837: * than zero but greater than -1.0, then the result of these functions is
0838: * zero.
0839: * <p>
0840: * The returned value is the smallest (closest to negative infinity) double
0841: * floating point value that is greater than or equal to the specified
0842: * number. The returned value is equal to a mathematical integer.
0843: * <p>
0844: * The data type of the returned value is a DOUBLE PRECISION number.
0845: */
0846: public void testCeiling() throws SQLException {
0847: executeNullValues("CEILING");
0848:
0849: executeNullFn("CEILING");
0850:
0851: debug();
0852: for (int i = 0; i < testValues.length; i++) {
0853: double expected = java.lang.StrictMath.ceil(testValues[i]);
0854: double rValue = executeValues("CEILING", testValues[i]);
0855: debug("CEILING: input value: " + testValues[i]
0856: + " expected value: " + expected
0857: + " return value: " + rValue);
0858: assertEquals(expected, rValue, 0.0);
0859: double fValue = executeFn("CEILING", testValues[i]);
0860: assertEquals(expected, fValue, 0.0);
0861: }
0862:
0863: Random rand = new java.util.Random();
0864: for (int i = 0; i < 100; i++) {
0865: double randD = rand.nextDouble();
0866: double expect = java.lang.StrictMath.ceil(randD);
0867: double rVal = executeValues("CEILING", randD);
0868: assertEquals(expect, rVal, 0.0);
0869: double fVal = executeFn("CEILING", randD);
0870: assertEquals(expect, fVal, 0.0);
0871:
0872: }
0873:
0874: }
0875:
0876: /**
0877: * Tests the FLOOR function which rounds the input value which must be a
0878: * DOUBLE PRECISION number down, and returns the largest number that is less
0879: * than or equal to the input value.
0880: * <p>
0881: * If the input value is NULL, the result of this function is NULL. If the
0882: * input value is equal to a mathematical integer, the result of this
0883: * function is the same as the input number. If the input value is zero (0),
0884: * the result of this function is zero.
0885: * <p>
0886: * The returned value is the largest (closest to positive infinity) double
0887: * floating point value that is less than or equal to the input value. The
0888: * returned value is equal to a mathematical integer. The data type of the
0889: * returned value is a DOUBLE PRECISION number.
0890: *
0891: * @throws SQLException
0892: */
0893:
0894: public void testFloor() throws SQLException {
0895: executeNullValues("FLOOR");
0896:
0897: executeNullFn("FLOOR");
0898:
0899: debug();
0900: for (int i = 0; i < testValues.length; i++) {
0901: double expected = java.lang.StrictMath.floor(testValues[i]);
0902: double rValue = executeValues("FLOOR", testValues[i]);
0903: debug("FLOOR: input value: " + testValues[i]
0904: + " expected value: " + expected
0905: + " return value: " + rValue);
0906: assertEquals(expected, rValue, 0.0);
0907: double fValue = executeFn("FLOOR", testValues[i]);
0908: assertEquals(expected, fValue, 0.0);
0909: }
0910:
0911: Random rand = new java.util.Random();
0912: for (int i = 0; i < 100; i++) {
0913: double randD = rand.nextDouble();
0914: double expect = java.lang.StrictMath.floor(randD);
0915: double rVal = executeValues("FLOOR", randD);
0916: assertEquals(expect, rVal, 0.0);
0917: double fVal = executeFn("FLOOR", randD);
0918: assertEquals(expect, fVal, 0.0);
0919:
0920: }
0921: }
0922:
0923: private double executeValues(String functionName)
0924: throws SQLException {
0925: Statement stmt = createStatement();
0926: ResultSet rs = stmt.executeQuery("values " + functionName
0927: + "()");
0928: double rValue = 0.0;
0929: while (rs.next()) {
0930: rValue = rs.getDouble(1);
0931: }
0932: rs.close();
0933: stmt.close();
0934: return rValue;
0935: }
0936:
0937: private double executeValues(String functionName, double value)
0938: throws SQLException {
0939: Statement stmt = createStatement();
0940: ResultSet rs = stmt.executeQuery("values " + functionName + "("
0941: + value + ")");
0942: double rValue = 0.0;
0943: while (rs.next()) {
0944: rValue = rs.getDouble(1);
0945: }
0946: rs.close();
0947: stmt.close();
0948: return rValue;
0949: }
0950:
0951: private void executeNullValues(String functionName)
0952: throws SQLException {
0953: Statement stmt = createStatement();
0954: ResultSet rs = stmt.executeQuery("values " + functionName
0955: + "(null)");
0956: Object rValue = new Object();
0957: while (rs.next()) {
0958: rValue = rs.getObject(1);
0959: }
0960: rs.close();
0961: stmt.close();
0962: assertNull(rValue);
0963: }
0964:
0965: private double executeFn(String functionName) throws SQLException {
0966: Statement stmt = createStatement();
0967: ResultSet rs = stmt.executeQuery("values {fn " + functionName
0968: + "()}");
0969: double rValue = 0.0;
0970: while (rs.next()) {
0971: rValue = rs.getDouble(1);
0972: }
0973: rs.close();
0974: stmt.close();
0975: return rValue;
0976: }
0977:
0978: private double executeFn(String functionName, double value)
0979: throws SQLException {
0980: Statement stmt = createStatement();
0981: ResultSet rs = stmt.executeQuery("values {fn " + functionName
0982: + "(" + value + ")}");
0983: double rValue = 0.0;
0984: while (rs.next()) {
0985: rValue = rs.getDouble(1);
0986: }
0987: rs.close();
0988: stmt.close();
0989: return rValue;
0990: }
0991:
0992: private void executeNullFn(String functionName) throws SQLException {
0993: Statement stmt = createStatement();
0994: ResultSet rs = stmt.executeQuery("values {fn " + functionName
0995: + "(null)}");
0996: Object rValue = new Object();
0997: while (rs.next()) {
0998: rValue = rs.getObject(1);
0999: }
1000: rs.close();
1001: stmt.close();
1002: assertNull(rValue);
1003: }
1004:
1005: private void debug(String message) {
1006: if (debugFlag) {
1007: System.out.println(message);
1008: }
1009: }
1010:
1011: private void debug() {
1012: if (debugFlag) {
1013: System.out.println();
1014: }
1015: }
1016:
1017: public MathTrigFunctionsTest(String name) {
1018: super (name);
1019: }
1020:
1021: public static Test suite() {
1022: TestSuite suite = new TestSuite();
1023: suite.addTestSuite(MathTrigFunctionsTest.class);
1024: return suite;
1025: }
1026:
1027: }
|