001: /*
002: * Copyright 2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.random;
017:
018: import junit.framework.Test;
019: import junit.framework.TestSuite;
020: import java.util.Random;
021:
022: /**
023: * Test cases for the RandomAdaptor class
024: *
025: * @version $Revision:$ $Date$
026: */
027:
028: public class RandomAdaptorTest extends RandomDataTest {
029:
030: public RandomAdaptorTest(String name) {
031: super (name);
032: }
033:
034: public static Test suite() {
035: TestSuite suite = new TestSuite(RandomAdaptorTest.class);
036: suite.setName("RandomAdaptor Tests");
037: return suite;
038: }
039:
040: public void testAdaptor() {
041: ConstantGenerator generator = new ConstantGenerator();
042: Random random = RandomAdaptor.createAdaptor(generator);
043: checkConstant(random);
044: RandomAdaptor randomAdaptor = new RandomAdaptor(generator);
045: checkConstant(randomAdaptor);
046: }
047:
048: private void checkConstant(Random random) {
049: byte[] bytes = new byte[] { 0 };
050: random.nextBytes(bytes);
051: assertEquals(0, bytes[0]);
052: assertEquals(false, random.nextBoolean());
053: assertEquals(0, random.nextDouble(), 0);
054: assertEquals(0, random.nextFloat(), 0);
055: assertEquals(0, random.nextGaussian(), 0);
056: assertEquals(0, random.nextInt());
057: assertEquals(0, random.nextInt(1));
058: assertEquals(0, random.nextLong());
059: random.setSeed(100);
060: assertEquals(0, random.nextDouble(), 0);
061: }
062:
063: /*
064: * "Constant" generator to test Adaptor delegation.
065: * "Powered by Eclipse ;-)"
066: *
067: */
068: private class ConstantGenerator implements RandomGenerator {
069:
070: public boolean nextBoolean() {
071: return false;
072: }
073:
074: public void nextBytes(byte[] bytes) {
075: }
076:
077: public double nextDouble() {
078: return 0;
079: }
080:
081: public float nextFloat() {
082: return 0;
083: }
084:
085: public double nextGaussian() {
086: return 0;
087: }
088:
089: public int nextInt() {
090: return 0;
091: }
092:
093: public int nextInt(int n) {
094: return 0;
095: }
096:
097: public long nextLong() {
098: return 0;
099: }
100:
101: public void setSeed(long seed) {
102: }
103: }
104: }
|