001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.random;
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: 796543 $ $Date: 2009-07-21 17:32:38 -0400 (Tue, 21 Jul 2009) $
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 static 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(int seed) {
102            }
103            
104            public void setSeed(int[] seed) {
105            }
106    
107            public void setSeed(long seed) {
108            }
109    
110        }
111    }