Source Code Cross Referenced for RandomTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:
018:        package org.apache.harmony.luni.tests.java.util;
019:
020:        import java.util.Random;
021:
022:        public class RandomTest extends junit.framework.TestCase {
023:
024:            Random r;
025:
026:            /**
027:             * @tests java.util.Random#Random()
028:             */
029:            public void test_Constructor() {
030:                // Test for method java.util.Random()
031:                assertTrue("Used to test", true);
032:            }
033:
034:            /**
035:             * @tests java.util.Random#Random(long)
036:             */
037:            public void test_ConstructorJ() {
038:                Random r = new Random(8409238L);
039:                Random r2 = new Random(8409238L);
040:                for (int i = 0; i < 100; i++)
041:                    assertTrue(
042:                            "Values from randoms with same seed don't match", r
043:                                    .nextInt() == r2.nextInt());
044:            }
045:
046:            /**
047:             * @tests java.util.Random#nextBoolean()
048:             */
049:            public void test_nextBoolean() {
050:                // Test for method boolean java.util.Random.nextBoolean()
051:                boolean falseAppeared = false, trueAppeared = false;
052:                for (int counter = 0; counter < 100; counter++)
053:                    if (r.nextBoolean())
054:                        trueAppeared = true;
055:                    else
056:                        falseAppeared = true;
057:                assertTrue(
058:                        "Calling nextBoolean() 100 times resulted in all trues",
059:                        falseAppeared);
060:                assertTrue(
061:                        "Calling nextBoolean() 100 times resulted in all falses",
062:                        trueAppeared);
063:            }
064:
065:            /**
066:             * @tests java.util.Random#nextBytes(byte[])
067:             */
068:            public void test_nextBytes$B() {
069:                // Test for method void java.util.Random.nextBytes(byte [])
070:                boolean someDifferent = false;
071:                byte[] randomBytes = new byte[100];
072:                r.nextBytes(randomBytes);
073:                byte firstByte = randomBytes[0];
074:                for (int counter = 1; counter < randomBytes.length; counter++)
075:                    if (randomBytes[counter] != firstByte)
076:                        someDifferent = true;
077:                assertTrue(
078:                        "nextBytes() returned an array of length 100 of the same byte",
079:                        someDifferent);
080:            }
081:
082:            /**
083:             * @tests java.util.Random#nextDouble()
084:             */
085:            public void test_nextDouble() {
086:                // Test for method double java.util.Random.nextDouble()
087:                double lastNum = r.nextDouble();
088:                double nextNum;
089:                boolean someDifferent = false;
090:                boolean inRange = true;
091:                for (int counter = 0; counter < 100; counter++) {
092:                    nextNum = r.nextDouble();
093:                    if (nextNum != lastNum)
094:                        someDifferent = true;
095:                    if (!(0 <= nextNum && nextNum < 1.0))
096:                        inRange = false;
097:                    lastNum = nextNum;
098:                }
099:                assertTrue(
100:                        "Calling nextDouble 100 times resulted in same number",
101:                        someDifferent);
102:                assertTrue(
103:                        "Calling nextDouble resulted in a number out of range [0,1)",
104:                        inRange);
105:            }
106:
107:            /**
108:             * @tests java.util.Random#nextFloat()
109:             */
110:            public void test_nextFloat() {
111:                // Test for method float java.util.Random.nextFloat()
112:                float lastNum = r.nextFloat();
113:                float nextNum;
114:                boolean someDifferent = false;
115:                boolean inRange = true;
116:                for (int counter = 0; counter < 100; counter++) {
117:                    nextNum = r.nextFloat();
118:                    if (nextNum != lastNum)
119:                        someDifferent = true;
120:                    if (!(0 <= nextNum && nextNum < 1.0))
121:                        inRange = false;
122:                    lastNum = nextNum;
123:                }
124:                assertTrue(
125:                        "Calling nextFloat 100 times resulted in same number",
126:                        someDifferent);
127:                assertTrue(
128:                        "Calling nextFloat resulted in a number out of range [0,1)",
129:                        inRange);
130:            }
131:
132:            /**
133:             * @tests java.util.Random#nextGaussian()
134:             */
135:            public void test_nextGaussian() {
136:                // Test for method double java.util.Random.nextGaussian()
137:                double lastNum = r.nextGaussian();
138:                double nextNum;
139:                boolean someDifferent = false;
140:                boolean someInsideStd = false;
141:                for (int counter = 0; counter < 100; counter++) {
142:                    nextNum = r.nextGaussian();
143:                    if (nextNum != lastNum)
144:                        someDifferent = true;
145:                    if (-1.0 <= nextNum && nextNum <= 1.0)
146:                        someInsideStd = true;
147:                    lastNum = nextNum;
148:                }
149:                assertTrue(
150:                        "Calling nextGaussian 100 times resulted in same number",
151:                        someDifferent);
152:                assertTrue(
153:                        "Calling nextGaussian 100 times resulted in no number within 1 std. deviation of mean",
154:                        someInsideStd);
155:            }
156:
157:            /**
158:             * @tests java.util.Random#nextInt()
159:             */
160:            public void test_nextInt() {
161:                // Test for method int java.util.Random.nextInt()
162:                int lastNum = r.nextInt();
163:                int nextNum;
164:                boolean someDifferent = false;
165:                for (int counter = 0; counter < 100; counter++) {
166:                    nextNum = r.nextInt();
167:                    if (nextNum != lastNum)
168:                        someDifferent = true;
169:                    lastNum = nextNum;
170:                }
171:                assertTrue("Calling nextInt 100 times resulted in same number",
172:                        someDifferent);
173:            }
174:
175:            /**
176:             * @tests java.util.Random#nextInt(int)
177:             */
178:            public void test_nextIntI() {
179:                // Test for method int java.util.Random.nextInt(int)
180:                final int range = 10;
181:                int lastNum = r.nextInt(range);
182:                int nextNum;
183:                boolean someDifferent = false;
184:                boolean inRange = true;
185:                for (int counter = 0; counter < 100; counter++) {
186:                    nextNum = r.nextInt(range);
187:                    if (nextNum != lastNum)
188:                        someDifferent = true;
189:                    if (!(0 <= nextNum && nextNum < range))
190:                        inRange = false;
191:                    lastNum = nextNum;
192:                }
193:                assertTrue(
194:                        "Calling nextInt (range) 100 times resulted in same number",
195:                        someDifferent);
196:                assertTrue(
197:                        "Calling nextInt (range) resulted in a number outside of [0, range)",
198:                        inRange);
199:
200:            }
201:
202:            /**
203:             * @tests java.util.Random#nextLong()
204:             */
205:            public void test_nextLong() {
206:                // Test for method long java.util.Random.nextLong()
207:                long lastNum = r.nextLong();
208:                long nextNum;
209:                boolean someDifferent = false;
210:                for (int counter = 0; counter < 100; counter++) {
211:                    nextNum = r.nextLong();
212:                    if (nextNum != lastNum)
213:                        someDifferent = true;
214:                    lastNum = nextNum;
215:                }
216:                assertTrue(
217:                        "Calling nextLong 100 times resulted in same number",
218:                        someDifferent);
219:            }
220:
221:            /**
222:             * @tests java.util.Random#setSeed(long)
223:             */
224:            public void test_setSeedJ() {
225:                // Test for method void java.util.Random.setSeed(long)
226:                long[] randomArray = new long[100];
227:                boolean someDifferent = false;
228:                final long firstSeed = 1000;
229:                long aLong, anotherLong, yetAnotherLong;
230:                Random aRandom = new Random();
231:                Random anotherRandom = new Random();
232:                Random yetAnotherRandom = new Random();
233:                aRandom.setSeed(firstSeed);
234:                anotherRandom.setSeed(firstSeed);
235:                for (int counter = 0; counter < randomArray.length; counter++) {
236:                    aLong = aRandom.nextLong();
237:                    anotherLong = anotherRandom.nextLong();
238:                    assertTrue(
239:                            "Two randoms with same seeds gave differing nextLong values",
240:                            aLong == anotherLong);
241:                    yetAnotherLong = yetAnotherRandom.nextLong();
242:                    randomArray[counter] = aLong;
243:                    if (aLong != yetAnotherLong)
244:                        someDifferent = true;
245:                }
246:                assertTrue(
247:                        "Two randoms with the different seeds gave the same chain of values",
248:                        someDifferent);
249:                aRandom.setSeed(firstSeed);
250:                for (int counter = 0; counter < randomArray.length; counter++)
251:                    assertTrue(
252:                            "Reseting a random to its old seed did not result in the same chain of values as it gave before",
253:                            aRandom.nextLong() == randomArray[counter]);
254:            }
255:
256:            // two random create at a time should also generated different results
257:            // regression test for Harmony 4616
258:            public void test_random_generate() throws Exception {
259:                for (int i = 0; i < 100; i++) {
260:                    Random random1 = new Random();
261:                    Random random2 = new Random();
262:                    assertFalse(random1.nextLong() == random2.nextLong());
263:                }
264:            }
265:
266:            /**
267:             * Sets up the fixture, for example, open a network connection. This method
268:             * is called before a test is executed.
269:             */
270:            protected void setUp() {
271:                r = new Random();
272:            }
273:
274:            /**
275:             * Tears down the fixture, for example, close a network connection. This
276:             * method is called after a test is executed.
277:             */
278:            protected void tearDown() {
279:            }
280:        }
ww_w.__j_a__v___a___2_s.___c___o___m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.