KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > MathTrigFunctionsTest


1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  contributor license agreements. See the NOTICE file distributed with
3  this work for additional information regarding copyright ownership.
4  The ASF licenses this file to You under the Apache License, Version 2.0
5  (the "License"); you may not use this file except in compliance with
6  the License. You may obtain a copy of the License at
7
8  http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */

16
17 package org.apache.derbyTesting.functionTests.tests.lang;
18
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Statement JavaDoc;
22 import java.util.Random JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
28 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
29
30 public class MathTrigFunctionsTest extends BaseJDBCTestCase {
31
32     private static final boolean debugFlag = false;
33
34     private static final double SMALLEST_NEG_DERBY_DOUBLE = -1.79769E+308;
35
36     private static final double SMALL_NEG_DOUBLE = -1.79768E+308;
37
38     private static final double SMALLEST_POS_DERBY_DOUBLE = 2.225E-307;
39
40     private static final double LARGEST_POS_DERBY_DOUBLE = 1.79769E+308;
41
42     private static final double LARGEST_NEG_DERBY_DOUBLE = -2.225E-307;
43
44     private static final double[] testRadians = { -0.000000001, -0.25,
45             0.000000001, 0.25, 0.5, 0.0, 1.0, 2.0, java.lang.StrictMath.PI,
46             java.lang.StrictMath.PI };
47
48     private static final double[] testArcValues = { 0.000000001, -0.000000001,
49             0.25, -0.25, 0.5, 0.0, -0.0, 1.0, -1.0 };
50
51     private static final double[] logValues = { 0.000000001, 0.25, 0.5, 1.0,
52             45.0, 90.0, 135.0, 180.0, 270, SMALLEST_POS_DERBY_DOUBLE,
53             LARGEST_POS_DERBY_DOUBLE };
54
55     private static final double[] testValues = { SMALLEST_NEG_DERBY_DOUBLE,
56             SMALL_NEG_DOUBLE, SMALLEST_POS_DERBY_DOUBLE,
57             LARGEST_POS_DERBY_DOUBLE, LARGEST_NEG_DERBY_DOUBLE, 0.000000001,
58             -0.000000001, 0.25, -0.25, 0.5, 0.0, -0.0, 1.0, -1.0, 2.0, 3.0,
59             java.lang.StrictMath.PI, 2 * java.lang.StrictMath.PI, 4.0, 45.0,
60             90.0, 135.0, 180.0, 270 };
61
62     private static final double[] testValuesTwo = { SMALLEST_NEG_DERBY_DOUBLE,
63             SMALL_NEG_DOUBLE, SMALLEST_POS_DERBY_DOUBLE,
64             LARGEST_NEG_DERBY_DOUBLE, 0.000000001, -0.000000001, 0.25, -0.25,
65             0.5, 0.0, -0.0, 1.0, -1.0, 2.0, 3.0, java.lang.StrictMath.PI,
66             2 * java.lang.StrictMath.PI, 4.0, 45.0, 90.0, 135.0, 180.0, 270 };
67
68     /**
69      * Tests the ACOS function which returns the arc cosine of a specified
70      * number.
71      * <p>
72      * Acceptable input values to the ACOS function are DOUBLE PRECISION values
73      * from -1 to 1. NULL returns NULL and if the absolute value of the input is
74      * greater than 1 an SQL state of 22003 is returned.
75      * <p>
76      * The return value from the ACOS function is a DOUBLE PRECISION number in
77      * radians with a range of zero to PI.
78      *
79      */

80     public void testAcos() throws SQLException JavaDoc {
81         // test the case where the input value is null
82
executeNullValues("ACOS");
83         executeNullFn("ACOS");
84         debug();
85         for (int i = 0; i < testArcValues.length; i++) {
86             double expected = java.lang.StrictMath.acos(testArcValues[i]);
87             double rValue = executeValues("ACOS", testArcValues[i]);
88             debug("ACOS: input value: " + testArcValues[i]
89                     + " expected value: " + expected + " return value: "
90                     + rValue);
91             assertEquals(expected, rValue, 0.0);
92             double fValue = executeFn("ACOS", testArcValues[i]);
93             assertEquals(expected, fValue, 0.0);
94
95         }
96         Random JavaDoc rand = new java.util.Random JavaDoc();
97         for (int i = 0; i < 100; i++) {
98             double randD = rand.nextDouble();
99             double expect = java.lang.StrictMath.acos(randD);
100             double rVal = executeValues("ACOS", randD);
101             assertEquals(expect, rVal, 0.0);
102             double fVal = executeFn("ACOS", randD);
103             assertEquals(expect, fVal, 0.0);
104
105         }
106
107         /* test the case where the input value is out of range */
108         try {
109             executeValues("ACOS", 2.0);
110             fail("ACOS: Out of range test failed, input value is: " + 2.0);
111         } catch (SQLException JavaDoc sqlE) {
112             // "ERROR 22003: The resulting value is outside the range for the
113
// data type DOUBLE.";
114
assertSQLState(
115                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
116                     sqlE);
117         }
118
119         /* test the case where the input value is out of range */
120         try {
121             executeFn("ACOS", 2.0);
122             fail("ACOS: Out of range test failed, input value is: " + 2.0);
123         } catch (SQLException JavaDoc sqlE) {
124             // "ERROR 22003: The resulting value is outside the range for the
125
// data type DOUBLE.";
126
assertSQLState(
127                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
128                     sqlE);
129         }
130
131     }
132
133     /**
134      * Tests the ASIN function which returns the arc sine of a specified number.
135      * <p>
136      * Acceptable input values to the ASIN function are DOUBLE PRECISION values
137      * from -1 to 1.
138      * <p>
139      * If the specified number is zero (0), the result of this function is zero.
140      * Note: Derby does not support negative zero.
141      * <p>
142      * An input value of NULL returns NULL.
143      * <p>
144      * If the absolute value of the input is greater than 1 an SQL state of
145      * 22003 is returned.
146      * <p>
147      * The return value from the ASIN function is a DOUBLE PRECISION number in
148      * radians with a range of -PI/2 to PI/2.
149      *
150      */

151     public void testAsin() throws SQLException JavaDoc {
152         executeNullValues("ASIN");
153         executeNullFn("ASIN");
154         debug();
155         for (int i = 0; i < testArcValues.length; i++) {
156             double expected = java.lang.StrictMath.asin(testArcValues[i]);
157             double rValue = executeValues("ASIN", testArcValues[i]);
158             debug("ASIN: input value: " + testArcValues[i]
159                     + " expected value: " + expected + " return value: "
160                     + rValue);
161             assertEquals(expected, rValue, 0.0);
162             double fValue = executeFn("ASIN", testArcValues[i]);
163             assertEquals(expected, fValue, 0.0);
164         }
165
166         Random JavaDoc rand = new java.util.Random JavaDoc();
167         for (int i = 0; i < 100; i++) {
168             double randD = rand.nextDouble();
169             double expect = java.lang.StrictMath.asin(randD);
170             double rVal = executeValues("ASIN", randD);
171             assertEquals(expect, rVal, 0.0);
172             double fVal = executeFn("ASIN", randD);
173             assertEquals(expect, fVal, 0.0);
174
175         }
176
177         try {
178             executeValues("ASIN", 2.0);
179             fail("ASIN: Out of range test failed, input value is: " + 2.0);
180         } catch (SQLException JavaDoc sqlE) {
181             // "ERROR 22003: The resulting value is outside the range for the
182
// data type DOUBLE.";
183
assertSQLState(
184                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
185                     sqlE);
186         }
187         try {
188             executeFn("ASIN", 2.0);
189             fail("ASIN: Out of range test failed, input value is: " + 2.0);
190         } catch (SQLException JavaDoc sqlE) {
191             // "ERROR 22003: The resulting value is outside the range for the
192
// data type DOUBLE.";
193
assertSQLState(
194                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
195                     sqlE);
196         }
197
198     }
199
200     /**
201      * Tests the ATAN function which returns the arc tangent of a specified
202      * number. Acceptable input values to the ATAN function are DOUBLE PRECISION
203      * values.
204      * <p>
205      * If the specified number is zero (0), the result of this function is zero.
206      * An input value of NULL returns NULL.
207      * <p>
208      * The return value from the ATAN function is a DOUBLE PRECISION number in
209      * radians with a range of -PI/2 to PI/2.
210      *
211      */

212     public void testAtan() throws SQLException JavaDoc {
213         executeNullValues("ATAN");
214         executeNullFn("ATAN");
215
216         debug();
217         for (int i = 0; i < testValues.length; i++) {
218             double expected = java.lang.StrictMath.atan(testValues[i]);
219             double rValue = executeValues("ATAN", testValues[i]);
220             debug("ATAN: input value: " + testValues[i] + " expected value: "
221                     + expected + " return value: " + rValue);
222             assertEquals(expected, rValue, 0.0);
223             double fValue = executeFn("ATAN", testValues[i]);
224             assertEquals(expected, fValue, 0.0);
225         }
226
227         Random JavaDoc rand = new java.util.Random JavaDoc();
228         for (int i = 0; i < 100; i++) {
229             double randD = rand.nextDouble();
230             double expect = java.lang.StrictMath.atan(randD);
231             double rVal = executeValues("ATAN", randD);
232             assertEquals(expect, rVal, 0.0);
233             double fVal = executeFn("ATAN", randD);
234             assertEquals(expect, fVal, 0.0);
235
236         }
237
238     }
239
240     /**
241      * Tests the COS function which returns the cosine of a specified number.
242      * <p>
243      * Acceptable input values to the COS function are DOUBLE PRECISION values.
244      * <p>
245      * An input value of NULL returns NULL.
246      */

247     public void testCos() throws SQLException JavaDoc {
248         executeNullValues("COS");
249         executeNullFn("COS");
250         debug();
251         for (int i = 0; i < testValues.length; i++) {
252             double expected = java.lang.StrictMath.cos(testValues[i]);
253             double rValue = executeValues("COS", testValues[i]);
254             debug("COS: input value: " + testValues[i] + " expected value: "
255                     + expected + " return value: " + rValue);
256             assertEquals(expected, rValue, 0.0);
257             double fValue = executeFn("COS", testValues[i]);
258             assertEquals(expected, fValue, 0.0);
259         }
260
261         Random JavaDoc rand = new java.util.Random JavaDoc();
262         for (int i = 0; i < 100; i++) {
263             double randD = rand.nextDouble();
264             double expect = java.lang.StrictMath.cos(randD);
265             double rVal = executeValues("COS", randD);
266             assertEquals(expect, rVal, 0.0);
267             double fVal = executeFn("COS", randD);
268             assertEquals(expect, fVal, 0.0);
269
270         }
271
272     }
273
274     /**
275      * Tests the SIN function which returns the sine of a specified number.
276      * <p>
277      * Acceptable input values to the SIN function are DOUBLE PRECISION values.
278      * <p>
279      * An input value of NULL returns NULL.
280      * <p>
281      * If the argument is zero, then the result is zero.
282      * <p>
283      * The data type of the returned value is a DOUBLE PRECISION number.
284      */

285     public void testSin() throws SQLException JavaDoc {
286         executeNullValues("SIN");
287         executeNullFn("SIN");
288
289         debug();
290         for (int i = 0; i < testValues.length; i++) {
291             double expected = java.lang.StrictMath.sin(testValues[i]);
292             double rValue = executeValues("SIN", testValues[i]);
293             debug("SIN: input value: " + testValues[i] + " expected value: "
294                     + expected + " return value: " + rValue);
295             assertEquals(expected, rValue, 0.0);
296             double fValue = executeFn("SIN", testValues[i]);
297             assertEquals(expected, fValue, 0.0);
298         }
299
300         Random JavaDoc rand = new java.util.Random JavaDoc();
301         for (int i = 0; i < 100; i++) {
302             double randD = rand.nextDouble();
303             double expect = java.lang.StrictMath.sin(randD);
304             double rVal = executeValues("SIN", randD);
305             assertEquals(expect, rVal, 0.0);
306             double fVal = executeFn("SIN", randD);
307             assertEquals(expect, fVal, 0.0);
308
309         }
310
311     }
312
313     /**
314      * Tests the TAN function which returns the tangent of a specified number.
315      * <p>
316      * Acceptable input values to the TAN function are DOUBLE PRECISION values.
317      * <p>
318      * An input value of NULL returns NULL.
319      * <p>
320      * If the argument is zero, then the result is zero.
321      * <p>
322      * The data type of the returned value is a DOUBLE PRECISION number.
323      */

324     public void testTan() throws SQLException JavaDoc {
325         executeNullValues("TAN");
326
327         executeNullFn("TAN");
328
329         debug();
330         for (int i = 0; i < testValues.length; i++) {
331             double expected = java.lang.StrictMath.tan(testValues[i]);
332             double rValue = executeValues("TAN", testValues[i]);
333             debug("TAN: input value: " + testValues[i] + " expected value: "
334                     + expected + " return value: " + rValue);
335             assertEquals(expected, rValue, 0.0);
336             double fValue = executeFn("TAN", testValues[i]);
337             assertEquals(expected, fValue, 0.0);
338         }
339
340         Random JavaDoc rand = new java.util.Random JavaDoc();
341         for (int i = 0; i < 100; i++) {
342             double randD = rand.nextDouble();
343             double expect = java.lang.StrictMath.tan(randD);
344             double rVal = executeValues("TAN", randD);
345             assertEquals(expect, rVal, 0.0);
346             double fVal = executeFn("TAN", randD);
347             assertEquals(expect, fVal, 0.0);
348
349         }
350
351     }
352
353     /**
354      * Tests the PI function which returns a value that is closer than any other
355      * value to pi.
356      * <p>
357      * The constant pi is the ratio of the circumference of a circle to the
358      * diameter of a circle.
359      * <p>
360      * No input values are allowed for the PI function.
361      */

362
363     public void testPI() throws SQLException JavaDoc {
364         double value = executeValues("PI");
365         assertEquals(java.lang.StrictMath.PI, value, 0.0);
366         double fValue = executeFn("PI");
367         assertEquals(java.lang.StrictMath.PI, fValue, 0.0);
368
369         try {
370             executeValues("PI", 2.0);
371             fail("PI: Out of range test failed, input value is: " + 2.0);
372         } catch (SQLException JavaDoc sqlE) {
373             // '<statement>' is not recognized as a function or procedure.
374
assertSQLState("42Y03", sqlE);
375         }
376
377         try {
378             executeFn("PI", 2.0);
379             fail("PI: Out of range test failed, input value is: " + 2.0);
380         } catch (SQLException JavaDoc sqlE) {
381             // '<statement>' is not recognized as a function or procedure.
382
assertSQLState("42Y03", sqlE);
383         }
384
385     }
386
387     /**
388      * Tests the DEGREES function which converts a DOUBLE PRECISION number from
389      * radians to degrees.
390      * <p>
391      * The input is an angle measured in radians, which is converted to an
392      * approximately equivalent angle measured in degrees.
393      * <p>
394      * The conversion from radians to degrees is not exact. You should not
395      * expect that the COS(DEGREES(PI/2)) to exactly equal 0.0.
396      * <p>
397      * The return value is a DOUBLE PRECISION number.
398      */

399     public void testDegrees() throws SQLException JavaDoc {
400         executeNullValues("DEGREES");
401         executeNullFn("DEGREES");
402
403         debug();
404         for (int i = 0; i < testRadians.length; i++) {
405             double expected = java.lang.StrictMath.toDegrees(testRadians[i]);
406             double rValue = executeValues("DEGREES", testRadians[i]);
407             // rValue = executeValues("DEGREES", SMALL_NEG_DOUBLE);
408
debug("DEGREES: input value: " + testRadians[i]
409                     + " expected value: " + expected + " return value: "
410                     + rValue);
411             assertEquals(expected, rValue, 0.0);
412             double fValue = executeFn("DEGREES", testRadians[i]);
413             assertEquals(expected, fValue, 0.0);
414
415         }
416
417         Random JavaDoc rand = new java.util.Random JavaDoc();
418         for (int i = 0; i < 100; i++) {
419             double randD = rand.nextDouble();
420             double expect = java.lang.StrictMath.toDegrees(randD);
421             double rVal = executeValues("DEGREES", randD);
422             assertEquals(expect, rVal, 0.0);
423             double fVal = executeFn("DEGREES", randD);
424             assertEquals(expect, fVal, 0.0);
425
426         }
427
428         try {
429             executeValues("DEGREES", SMALLEST_NEG_DERBY_DOUBLE);
430             fail("DEGREES: Out of range test failed, input value is: "
431                     + SMALLEST_NEG_DERBY_DOUBLE);
432         } catch (SQLException JavaDoc sqlE) {
433             // "ERROR 22003: The resulting value is outside the range for the
434
// data type DOUBLE.";
435
assertSQLState(
436                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
437                     sqlE);
438         }
439         try {
440             executeFn("DEGREES", SMALLEST_NEG_DERBY_DOUBLE);
441             fail("DEGREES: Out of range test failed, input value is: "
442                     + SMALLEST_NEG_DERBY_DOUBLE);
443         } catch (SQLException JavaDoc sqlE) {
444             // "ERROR 22003: The resulting value is outside the range for the
445
// data type DOUBLE.";
446
assertSQLState(
447                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
448                     sqlE);
449         }
450     }
451
452     /**
453      * Tests the RADIANS function which converts a DOUBLE PRECISION number from
454      * degrees to radians.
455      * <p>
456      * The input is an angle measured in degrees, which is converted to an
457      * approximately equivalent angle measured in radians.
458      * <p>
459      * The conversion from radians to degrees is not exact. You should not
460      * expect that the COS(RADIANS(90.0)) to exactly equal 0.0.
461      * <p>
462      * The return value is a DOUBLE PRECISION number.
463      */

464     public void testRadians() throws SQLException JavaDoc {
465         executeNullValues("RADIANS");
466
467         executeNullFn("RADIANS");
468
469         debug();
470         for (int i = 0; i < testArcValues.length; i++) {
471             double expected = java.lang.StrictMath.toRadians(testArcValues[i]);
472             double rValue = executeValues("RADIANS", testArcValues[i]);
473             debug("RADIANS: input value: " + testArcValues[i]
474                     + " expected value: " + expected + " return value: "
475                     + rValue);
476             assertEquals(expected, rValue, 0.0);
477             double fValue = executeFn("RADIANS", testArcValues[i]);
478             assertEquals(expected, fValue, 0.0);
479
480         }
481
482         Random JavaDoc rand = new java.util.Random JavaDoc();
483         for (int i = 0; i < 100; i++) {
484             double randD = rand.nextDouble();
485             double expect = java.lang.StrictMath.toRadians(randD);
486             double rVal = executeValues("RADIANS", randD);
487             assertEquals(expect, rVal, 0.0);
488             double fVal = executeFn("RADIANS", randD);
489             assertEquals(expect, fVal, 0.0);
490
491         }
492
493         try {
494             executeValues("RADIANS", SMALLEST_POS_DERBY_DOUBLE);
495             fail("RADIANS: Out of range test failed, input value is: "
496                     + SMALLEST_NEG_DERBY_DOUBLE);
497         } catch (SQLException JavaDoc sqlE) {
498             // "ERROR 22003: The resulting value is outside the range for the
499
// data type DOUBLE.";
500
assertSQLState(
501                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
502                     sqlE);
503         }
504         try {
505             executeFn("RADIANS", SMALLEST_POS_DERBY_DOUBLE);
506             fail("RADIANS: Out of range test failed, input value is: "
507                     + SMALLEST_NEG_DERBY_DOUBLE);
508         } catch (SQLException JavaDoc sqlE) {
509             // "ERROR 22003: The resulting value is outside the range for the
510
// data type DOUBLE.";
511
assertSQLState(
512                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
513                     sqlE);
514         }
515     }
516
517     /**
518      * Tests the EXP function which returns e raised to the power of the input
519      * DOUBLE PRECISION number. The input number is the exponent to raise e to.
520      * <p>
521      * The constant e is the base of the natural logarithms.
522      * <p>
523      * The return value is a DOUBLE PRECISION number.
524      *
525      * @throws SQLException
526      */

527     public void testExp() throws SQLException JavaDoc {
528         executeNullValues("EXP");
529         executeNullFn("EXP");
530
531         debug();
532         for (int i = 0; i < testValuesTwo.length; i++) {
533             double expected = java.lang.StrictMath.exp(testValuesTwo[i]);
534             double rValue = executeValues("EXP", testValuesTwo[i]);
535             debug("EXP: input value: " + testValuesTwo[i] + " expected value: "
536                     + expected + " return value: " + rValue);
537             assertEquals(expected, rValue, 0.0);
538             double fValue = executeFn("EXP", testValuesTwo[i]);
539             assertEquals(expected, fValue, 0.0);
540         }
541
542         Random JavaDoc rand = new java.util.Random JavaDoc();
543         for (int i = 0; i < 100; i++) {
544             double randD = rand.nextDouble();
545             double expect = java.lang.StrictMath.exp(randD);
546             double rVal = executeValues("EXP", randD);
547             assertEquals(expect, rVal, 0.0);
548             double fVal = executeFn("EXP", randD);
549             assertEquals(expect, fVal, 0.0);
550
551         }
552
553         try {
554             executeValues("EXP", LARGEST_POS_DERBY_DOUBLE);
555             fail("EXP: Out of range test failed, input value is: "
556                     + LARGEST_POS_DERBY_DOUBLE);
557         } catch (SQLException JavaDoc sqlE) {
558             // "ERROR 22003: The resulting value is outside the range for the
559
// data type DOUBLE.";
560
assertSQLState(
561                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
562                     sqlE);
563         }
564         try {
565             executeFn("EXP", LARGEST_POS_DERBY_DOUBLE);
566             fail("EXP: Out of range test failed, input value is: "
567                     + LARGEST_POS_DERBY_DOUBLE);
568         } catch (SQLException JavaDoc sqlE) {
569             // "ERROR 22003: The resulting value is outside the range for the
570
// data type DOUBLE.";
571
assertSQLState(
572                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
573                     sqlE);
574         }
575
576     }
577
578     /**
579      * Tests the LOG10 function which returns the base-10 logarithm of a DOUBLE
580      * PRECISION number that is greater than zero.
581      * <p>
582      * If the input value is NULL, the result of this function is NULL.
583      * <p>
584      * If the input value is zero or a negative number, an exception is returned
585      * that indicates that the value is out of range (SQL state 22003).
586      * <p>
587      * The return type is a DOUBLE PRECISION number.
588      */

589
590     public void testLog10() throws SQLException JavaDoc {
591         executeNullValues("LOG10");
592         executeNullFn("LOG10");
593
594         debug();
595         for (int i = 0; i < logValues.length; i++) {
596             // ln 10 = y * (log base 10 (10))
597
// 2.3025850929940456840179914546844 = y * 1
598
double expected = java.lang.StrictMath.log(logValues[i]) / 2.3025850929940456840179914546844;
599             double rValue = executeValues("LOG10", logValues[i]);
600             debug("LOG10: input value: " + logValues[i] + " expected value: "
601                     + expected + " return value: " + rValue);
602             assertEquals(expected, rValue, 0.0);
603             double fValue = executeFn("LOG10", logValues[i]);
604             assertEquals(expected, fValue, 0.0);
605         }
606
607         Random JavaDoc rand = new java.util.Random JavaDoc();
608         for (int i = 0; i < 100; i++) {
609             double randD = rand.nextDouble();
610             double expect = java.lang.StrictMath.log(randD) / 2.3025850929940456840179914546844;
611             double rVal = executeValues("LOG10", randD);
612             assertEquals(expect, rVal, 0.0);
613             double fVal = executeFn("LOG10", randD);
614             assertEquals(expect, fVal, 0.0);
615
616         }
617
618         try {
619             executeValues("LOG10", 0.0);
620             fail("LOG10: Out of range test failed, input value is: " + 0.0);
621         } catch (SQLException JavaDoc sqlE) {
622             // "ERROR 22003: The resulting value is outside the range for the
623
// data type DOUBLE.";
624
assertSQLState(
625                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
626                     sqlE);
627         }
628         try {
629             executeValues("LOG10", -1.0);
630             fail("LOG10: Out of range test failed, input value is: " + -1.0);
631         } catch (SQLException JavaDoc sqlE) {
632             // "ERROR 22003: The resulting value is outside the range for the
633
// data type DOUBLE.";
634
assertSQLState(
635                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
636                     sqlE);
637         }
638
639         try {
640             executeFn("LOG10", 0.0);
641             fail("LOG10: Out of range test failed, input value is: " + 0.0);
642         } catch (SQLException JavaDoc sqlE) {
643             // "ERROR 22003: The resulting value is outside the range for the
644
// data type DOUBLE.";
645
assertSQLState(
646                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
647                     sqlE);
648         }
649         try {
650             executeFn("LOG10", -1.0);
651             fail("LOG10: Out of range test failed, input value is: " + -1.0);
652         } catch (SQLException JavaDoc sqlE) {
653             // "ERROR 22003: The resulting value is outside the range for the
654
// data type DOUBLE.";
655
assertSQLState(
656                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
657                     sqlE);
658         }
659
660     }
661
662     /**
663      * Tests the LOG function which returns the natural logarithm (base e) of a
664      * DOUBLE PRECISION number that is greater than zero (0).
665      * <p>
666      * If the specified number is NULL, the result of these functions is NULL.
667      * If the specified number is zero or a negative number, an exception is
668      * returned that indicates that the value is out of range (SQL state 22003).
669      * <p>
670      * The data type of the returned value is a DOUBLE PRECISION number.
671      */

672     public void testLog() throws SQLException JavaDoc {
673         executeNullValues("LOG");
674         executeNullFn("LOG");
675
676         debug();
677         for (int i = 0; i < logValues.length; i++) {
678             double expected = java.lang.StrictMath.log(logValues[i]);
679             double rValue = executeValues("LOG", logValues[i]);
680             debug("LOG: input value: " + logValues[i] + " expected value: "
681                     + expected + " return value: " + rValue);
682             assertEquals(expected, rValue, 0.0);
683             double fValue = executeFn("LOG", logValues[i]);
684             assertEquals(expected, fValue, 0.0);
685         }
686
687         Random JavaDoc rand = new java.util.Random JavaDoc();
688         for (int i = 0; i < 100; i++) {
689             double randD = rand.nextDouble();
690             double expect = java.lang.StrictMath.log(randD);
691             double rVal = executeValues("LOG", randD);
692             assertEquals(expect, rVal, 0.0);
693             double fVal = executeFn("LOG", randD);
694             assertEquals(expect, fVal, 0.0);
695
696         }
697
698         try {
699             executeValues("LOG", 0.0);
700             fail("LOG: Out of range test failed, input value is: " + 0.0);
701         } catch (SQLException JavaDoc sqlE) {
702             // "ERROR 22003: The resulting value is outside the range for the
703
// data type DOUBLE.";
704
assertSQLState(
705                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
706                     sqlE);
707         }
708         try {
709             executeFn("LOG", 0.0);
710             fail("LOG: Out of range test failed, input value is: " + 0.0);
711         } catch (SQLException JavaDoc sqlE) {
712             // "ERROR 22003: The resulting value is outside the range for the
713
// data type DOUBLE.";
714
assertSQLState(
715                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
716                     sqlE);
717         }
718
719     }
720
721     /**
722      * Tests the LN function which returns the natural logarithm (base e) of a
723      * DOUBLE PRECISION number that is greater than zero (0).
724      * <p>
725      * If the specified number is NULL, the result of these functions is NULL.
726      * If the specified number is zero or a negative number, an exception is
727      * returned that indicates that the value is out of range (SQL state 22003).
728      * <p>
729      * The data type of the returned value is a DOUBLE PRECISION number.
730      */

731     public void testLn() throws SQLException JavaDoc {
732         executeNullValues("LN");
733         // Note: the syntax 'values {fn ln(value)}' is not supported
734
// Object fnVal = executeNullFn("LN");
735
debug();
736         for (int i = 0; i < logValues.length; i++) {
737             double expected = java.lang.StrictMath.log(logValues[i]);
738             double rValue = executeValues("LN", logValues[i]);
739             debug("LOG: input value: " + logValues[i] + " expected value: "
740                     + expected + " return value: " + rValue);
741             assertEquals(expected, rValue, 0.0);
742         }
743
744         Random JavaDoc rand = new java.util.Random JavaDoc();
745         for (int i = 0; i < 100; i++) {
746             double randD = rand.nextDouble();
747             double expect = java.lang.StrictMath.log(randD);
748             double rVal = executeValues("LN", randD);
749             assertEquals(expect, rVal, 0.0);
750         }
751
752         try {
753             executeValues("LN", 0.0);
754             fail("LOG: Out of range test failed, input value is: " + 0.0);
755         } catch (SQLException JavaDoc sqlE) {
756             // "ERROR 22003: The resulting value is outside the range for the
757
// data type DOUBLE.";
758
assertSQLState(
759                     SQLStateConstants.DATA_EXCEPTION_NUMERIC_VALUE_OUT_OF_RANGE,
760                     sqlE);
761         }
762
763     }
764
765     /**
766      * Tests the CEIL function which rounds a DOUBLE PRECISION number up, and
767      * return the smallest number that is greater than or equal to the input
768      * number.
769      * <p>
770      * If the input number is NULL, the result of these functions is NULL. If
771      * the input number is equal to a mathematical integer, the result of these
772      * functions is the same as the input number. If the input number is zero
773      * (0), the result of these functions is zero. If the input number is less
774      * than zero but greater than -1.0, then the result of these functions is
775      * zero.
776      * <p>
777      * The returned value is the smallest (closest to negative infinity) double
778      * floating point value that is greater than or equal to the specified
779      * number. The returned value is equal to a mathematical integer.
780      * <p>
781      * The data type of the returned value is a DOUBLE PRECISION number.
782      */

783
784     public void testCeil() throws SQLException JavaDoc {
785         executeNullValues("CEIL");
786
787         // Note: the syntax 'values {fn CEIL(value)}' is not supported
788
// Object fnVal = executeNullFn("CEIL");
789

790         debug();
791         for (int i = 0; i < testValues.length; i++) {
792             double expected = java.lang.StrictMath.ceil(testValues[i]);
793             double rValue = executeValues("CEIL", testValues[i]);
794             debug("CEIL: input value: " + testValues[i] + " expected value: "
795                     + expected + " return value: " + rValue);
796             assertEquals(expected, rValue, 0.0);
797         }
798
799         Random JavaDoc rand = new java.util.Random JavaDoc();
800         for (int i = 0; i < 100; i++) {
801             double randD = rand.nextDouble();
802             double expect = java.lang.StrictMath.ceil(randD);
803             double rVal = executeValues("CEIL", randD);
804             assertEquals(expect, rVal, 0.0);
805         }
806
807     }
808
809     /**
810      * Tests the CEILING function which rounds a DOUBLE PRECISION number up, and
811      * return the smallest number that is greater than or equal to the input
812      * number.
813      * <p>
814      * If the input number is NULL, the result of these functions is NULL. If
815      * the input number is equal to a mathematical integer, the result of these
816      * functions is the same as the input number. If the input number is zero
817      * (0), the result of these functions is zero. If the input number is less
818      * than zero but greater than -1.0, then the result of these functions is
819      * zero.
820      * <p>
821      * The returned value is the smallest (closest to negative infinity) double
822      * floating point value that is greater than or equal to the specified
823      * number. The returned value is equal to a mathematical integer.
824      * <p>
825      * The data type of the returned value is a DOUBLE PRECISION number.
826      */

827     public void testCeiling() throws SQLException JavaDoc {
828         executeNullValues("CEILING");
829
830         executeNullFn("CEILING");
831
832         debug();
833         for (int i = 0; i < testValues.length; i++) {
834             double expected = java.lang.StrictMath.ceil(testValues[i]);
835             double rValue = executeValues("CEILING", testValues[i]);
836             debug("CEILING: input value: " + testValues[i]
837                     + " expected value: " + expected + " return value: "
838                     + rValue);
839             assertEquals(expected, rValue, 0.0);
840             double fValue = executeFn("CEILING", testValues[i]);
841             assertEquals(expected, fValue, 0.0);
842         }
843
844         Random JavaDoc rand = new java.util.Random JavaDoc();
845         for (int i = 0; i < 100; i++) {
846             double randD = rand.nextDouble();
847             double expect = java.lang.StrictMath.ceil(randD);
848             double rVal = executeValues("CEILING", randD);
849             assertEquals(expect, rVal, 0.0);
850             double fVal = executeFn("CEILING", randD);
851             assertEquals(expect, fVal, 0.0);
852
853         }
854
855     }
856
857     /**
858      * Tests the FLOOR function which rounds the input value which must be a
859      * DOUBLE PRECISION number down, and returns the largest number that is less
860      * than or equal to the input value.
861      * <p>
862      * If the input value is NULL, the result of this function is NULL. If the
863      * input value is equal to a mathematical integer, the result of this
864      * function is the same as the input number. If the input value is zero (0),
865      * the result of this function is zero.
866      * <p>
867      * The returned value is the largest (closest to positive infinity) double
868      * floating point value that is less than or equal to the input value. The
869      * returned value is equal to a mathematical integer. The data type of the
870      * returned value is a DOUBLE PRECISION number.
871      *
872      * @throws SQLException
873      */

874
875     public void testFloor() throws SQLException JavaDoc {
876         executeNullValues("FLOOR");
877
878         executeNullFn("FLOOR");
879
880         debug();
881         for (int i = 0; i < testValues.length; i++) {
882             double expected = java.lang.StrictMath.floor(testValues[i]);
883             double rValue = executeValues("FLOOR", testValues[i]);
884             debug("FLOOR: input value: " + testValues[i] + " expected value: "
885                     + expected + " return value: " + rValue);
886             assertEquals(expected, rValue, 0.0);
887             double fValue = executeFn("FLOOR", testValues[i]);
888             assertEquals(expected, fValue, 0.0);
889         }
890
891         Random JavaDoc rand = new java.util.Random JavaDoc();
892         for (int i = 0; i < 100; i++) {
893             double randD = rand.nextDouble();
894             double expect = java.lang.StrictMath.floor(randD);
895             double rVal = executeValues("FLOOR", randD);
896             assertEquals(expect, rVal, 0.0);
897             double fVal = executeFn("FLOOR", randD);
898             assertEquals(expect, fVal, 0.0);
899
900         }
901     }
902
903     private double executeValues(String JavaDoc functionName) throws SQLException JavaDoc {
904         Statement JavaDoc stmt = createStatement();
905         ResultSet JavaDoc rs = stmt.executeQuery("values " + functionName + "()");
906         double rValue = 0.0;
907         while (rs.next()) {
908             rValue = rs.getDouble(1);
909         }
910         rs.close();
911         stmt.close();
912         return rValue;
913     }
914
915     private double executeValues(String JavaDoc functionName, double value)
916             throws SQLException JavaDoc {
917         Statement JavaDoc stmt = createStatement();
918         ResultSet JavaDoc rs = stmt.executeQuery("values " + functionName + "(" + value
919                 + ")");
920         double rValue = 0.0;
921         while (rs.next()) {
922             rValue = rs.getDouble(1);
923         }
924         rs.close();
925         stmt.close();
926         return rValue;
927     }
928
929     private void executeNullValues(String JavaDoc functionName) throws SQLException JavaDoc {
930         Statement JavaDoc stmt = createStatement();
931         ResultSet JavaDoc rs = stmt.executeQuery("values " + functionName + "(null)");
932         Object JavaDoc rValue = new Object JavaDoc();
933         while (rs.next()) {
934             rValue = rs.getObject(1);
935         }
936         rs.close();
937         stmt.close();
938         assertNull(rValue);
939     }
940
941     private double executeFn(String JavaDoc functionName) throws SQLException JavaDoc {
942         Statement JavaDoc stmt = createStatement();
943         ResultSet JavaDoc rs = stmt.executeQuery("values {fn " + functionName + "()}");
944         double rValue = 0.0;
945         while (rs.next()) {
946             rValue = rs.getDouble(1);
947         }
948         rs.close();
949         stmt.close();
950         return rValue;
951     }
952
953     private double executeFn(String JavaDoc functionName, double value)
954             throws SQLException JavaDoc {
955         Statement JavaDoc stmt = createStatement();
956         ResultSet JavaDoc rs = stmt.executeQuery("values {fn " + functionName + "("
957                 + value + ")}");
958         double rValue = 0.0;
959         while (rs.next()) {
960             rValue = rs.getDouble(1);
961         }
962         rs.close();
963         stmt.close();
964         return rValue;
965     }
966
967     private void executeNullFn(String JavaDoc functionName) throws SQLException JavaDoc {
968         Statement JavaDoc stmt = createStatement();
969         ResultSet JavaDoc rs = stmt.executeQuery("values {fn " + functionName
970                 + "(null)}");
971         Object JavaDoc rValue = new Object JavaDoc();
972         while (rs.next()) {
973             rValue = rs.getObject(1);
974         }
975         rs.close();
976         stmt.close();
977         assertNull(rValue);
978     }
979
980     private void debug(String JavaDoc message) {
981         if (debugFlag) {
982             System.out.println(message);
983         }
984     }
985
986     private void debug() {
987         if (debugFlag) {
988             System.out.println();
989         }
990     }
991
992     public MathTrigFunctionsTest(String JavaDoc name) {
993         super(name);
994     }
995
996     public static Test suite() {
997         TestSuite suite = new TestSuite();
998         suite.addTestSuite(MathTrigFunctionsTest.class);
999         return suite;
1000    }
1001
1002}
1003
Popular Tags