Source Code Cross Referenced for ResizableDoubleArrayTest.java in  » Science » Apache-commons-math-1.1 » org » apache » commons » math » 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 » Science » Apache commons math 1.1 » org.apache.commons.math.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2003-2004 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.util;
017:
018:        import org.apache.commons.math.random.RandomDataImpl;
019:        import org.apache.commons.math.random.RandomData;
020:
021:        /**
022:         * This class contains test cases for the ResizableDoubleArray.
023:         * 
024:         * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
025:         */
026:        public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest {
027:
028:            public ResizableDoubleArrayTest(String name) {
029:                super (name);
030:            }
031:
032:            protected void tearDown() throws Exception {
033:                da = null;
034:                ra = null;
035:            }
036:
037:            protected void setUp() throws Exception {
038:                da = new ResizableDoubleArray();
039:                ra = new ResizableDoubleArray();
040:            }
041:
042:            public void testConstructors() {
043:                float defaultExpansionFactor = 2.0f;
044:                float defaultContractionCriteria = 2.5f;
045:                int defaultMode = ResizableDoubleArray.MULTIPLICATIVE_MODE;
046:
047:                ResizableDoubleArray testDa = new ResizableDoubleArray(2);
048:                assertEquals(0, testDa.getNumElements());
049:                assertEquals(2, testDa.getInternalLength());
050:                assertEquals(defaultExpansionFactor, testDa
051:                        .getExpansionFactor(), 0);
052:                assertEquals(defaultContractionCriteria, testDa
053:                        .getContractionCriteria(), 0);
054:                assertEquals(defaultMode, testDa.getExpansionMode());
055:                try {
056:                    da = new ResizableDoubleArray(-1);
057:                    fail("Expecting IllegalArgumentException");
058:                } catch (IllegalArgumentException ex) {
059:                    // expected
060:                }
061:
062:                testDa = new ResizableDoubleArray(2, 2.0f);
063:                assertEquals(0, testDa.getNumElements());
064:                assertEquals(2, testDa.getInternalLength());
065:                assertEquals(defaultExpansionFactor, testDa
066:                        .getExpansionFactor(), 0);
067:                assertEquals(defaultContractionCriteria, testDa
068:                        .getContractionCriteria(), 0);
069:                assertEquals(defaultMode, testDa.getExpansionMode());
070:
071:                try {
072:                    da = new ResizableDoubleArray(2, 0.5f);
073:                    fail("Expecting IllegalArgumentException");
074:                } catch (IllegalArgumentException ex) {
075:                    // expected
076:                }
077:
078:                testDa = new ResizableDoubleArray(2, 3.0f);
079:                assertEquals(3.0f, testDa.getExpansionFactor(), 0);
080:                assertEquals(3.5f, testDa.getContractionCriteria(), 0);
081:
082:                testDa = new ResizableDoubleArray(2, 2.0f, 3.0f);
083:                assertEquals(0, testDa.getNumElements());
084:                assertEquals(2, testDa.getInternalLength());
085:                assertEquals(defaultExpansionFactor, testDa
086:                        .getExpansionFactor(), 0);
087:                assertEquals(3.0f, testDa.getContractionCriteria(), 0);
088:                assertEquals(defaultMode, testDa.getExpansionMode());
089:
090:                try {
091:                    da = new ResizableDoubleArray(2, 2.0f, 1.5f);
092:                    fail("Expecting IllegalArgumentException");
093:                } catch (IllegalArgumentException ex) {
094:                    // expected
095:                }
096:
097:                testDa = new ResizableDoubleArray(2, 2.0f, 3.0f,
098:                        ResizableDoubleArray.ADDITIVE_MODE);
099:                assertEquals(0, testDa.getNumElements());
100:                assertEquals(2, testDa.getInternalLength());
101:                assertEquals(defaultExpansionFactor, testDa
102:                        .getExpansionFactor(), 0);
103:                assertEquals(3.0f, testDa.getContractionCriteria(), 0);
104:                assertEquals(ResizableDoubleArray.ADDITIVE_MODE, testDa
105:                        .getExpansionMode());
106:
107:                try {
108:                    da = new ResizableDoubleArray(2, 2.0f, 2.5f, -1);
109:                    fail("Expecting IllegalArgumentException");
110:                } catch (IllegalArgumentException ex) {
111:                    // expected
112:                }
113:
114:            }
115:
116:            public void testSetElementArbitraryExpansion() {
117:
118:                // MULTIPLICATIVE_MODE 
119:                da.addElement(2.0);
120:                da.addElement(4.0);
121:                da.addElement(6.0);
122:                da.setElement(1, 3.0);
123:
124:                // Expand the array arbitrarily to 1000 items
125:                da.setElement(1000, 3.4);
126:
127:                assertEquals(
128:                        "The number of elements should now be 1001, it isn't",
129:                        da.getNumElements(), 1001);
130:
131:                assertEquals(
132:                        "Uninitialized Elements are default value of 0.0, index 766 wasn't",
133:                        0.0, da.getElement(760), Double.MIN_VALUE);
134:
135:                assertEquals("The 1000th index should be 3.4, it isn't", 3.4,
136:                        da.getElement(1000), Double.MIN_VALUE);
137:                assertEquals("The 0th index should be 2.0, it isn't", 2.0, da
138:                        .getElement(0), Double.MIN_VALUE);
139:
140:                // Make sure numElements and expansion work correctly for expansion boundary cases
141:                da.clear();
142:                da.addElement(2.0);
143:                da.addElement(4.0);
144:                da.addElement(6.0);
145:                assertEquals(4, ((ResizableDoubleArray) da).getInternalLength());
146:                assertEquals(3, da.getNumElements());
147:                da.setElement(3, 7.0);
148:                assertEquals(4, ((ResizableDoubleArray) da).getInternalLength());
149:                assertEquals(4, da.getNumElements());
150:                da.setElement(10, 10.0);
151:                assertEquals(11, ((ResizableDoubleArray) da)
152:                        .getInternalLength());
153:                assertEquals(11, da.getNumElements());
154:                da.setElement(9, 10.0);
155:                assertEquals(11, ((ResizableDoubleArray) da)
156:                        .getInternalLength());
157:                assertEquals(11, da.getNumElements());
158:
159:                try {
160:                    da.setElement(-2, 3);
161:                    fail("Expecting ArrayIndexOutOfBoundsException for negative index");
162:                } catch (ArrayIndexOutOfBoundsException ex) {
163:                    // expected
164:                }
165:
166:                // ADDITIVE_MODE
167:
168:                ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f,
169:                        3.0f, ResizableDoubleArray.ADDITIVE_MODE);
170:                assertEquals(2, testDa.getInternalLength());
171:                testDa.addElement(1d);
172:                testDa.addElement(1d);
173:                assertEquals(2, testDa.getInternalLength());
174:                testDa.addElement(1d);
175:                assertEquals(4, testDa.getInternalLength());
176:            }
177:
178:            public void testAdd1000() {
179:                super .testAdd1000();
180:                assertEquals(
181:                        "Internal Storage length should be 1024 if we started out with initial capacity of "
182:                                + "16 and an expansion factor of 2.0", 1024,
183:                        ((ResizableDoubleArray) da).getInternalLength());
184:            }
185:
186:            public void testAddElementRolling() {
187:                super .testAddElementRolling();
188:
189:                // MULTIPLICATIVE_MODE
190:                da.clear();
191:                da.addElement(1);
192:                da.addElement(2);
193:                da.addElementRolling(3);
194:                assertEquals(3, da.getElement(1), 0);
195:                da.addElementRolling(4);
196:                assertEquals(3, da.getElement(0), 0);
197:                assertEquals(4, da.getElement(1), 0);
198:                da.addElement(5);
199:                assertEquals(5, da.getElement(2), 0);
200:                da.addElementRolling(6);
201:                assertEquals(4, da.getElement(0), 0);
202:                assertEquals(5, da.getElement(1), 0);
203:                assertEquals(6, da.getElement(2), 0);
204:
205:                // ADDITIVE_MODE  (x's are occupied storage locations, 0's are open)
206:                ResizableDoubleArray testDa = new ResizableDoubleArray(2, 2.0f,
207:                        2.5f, ResizableDoubleArray.ADDITIVE_MODE);
208:                assertEquals(2, testDa.getInternalLength());
209:                testDa.addElement(1d); // x,0
210:                testDa.addElement(2d); // x,x
211:                testDa.addElement(3d); // x,x,x,0 -- expanded
212:                assertEquals(1d, testDa.getElement(0), 0);
213:                assertEquals(2d, testDa.getElement(1), 0);
214:                assertEquals(3d, testDa.getElement(2), 0);
215:                assertEquals(4, testDa.getInternalLength()); // x,x,x,0 
216:                assertEquals(3, testDa.getNumElements());
217:                testDa.addElementRolling(4d);
218:                assertEquals(2d, testDa.getElement(0), 0);
219:                assertEquals(3d, testDa.getElement(1), 0);
220:                assertEquals(4d, testDa.getElement(2), 0);
221:                assertEquals(4, testDa.getInternalLength()); // 0,x,x,x
222:                assertEquals(3, testDa.getNumElements());
223:                testDa.addElementRolling(5d); // 0,0,x,x,x,0 -- time to contract
224:                assertEquals(3d, testDa.getElement(0), 0);
225:                assertEquals(4d, testDa.getElement(1), 0);
226:                assertEquals(5d, testDa.getElement(2), 0);
227:                assertEquals(4, testDa.getInternalLength()); // contracted -- x,x,x,0     
228:                assertEquals(3, testDa.getNumElements());
229:                try {
230:                    testDa.getElement(4);
231:                    fail("Expecting ArrayIndexOutOfBoundsException");
232:                } catch (ArrayIndexOutOfBoundsException ex) {
233:                    // expected
234:                }
235:                try {
236:                    testDa.getElement(-1);
237:                    fail("Expecting ArrayIndexOutOfBoundsException");
238:                } catch (ArrayIndexOutOfBoundsException ex) {
239:                    // expected
240:                }
241:            }
242:
243:            public void testSetNumberOfElements() {
244:                da.addElement(1.0);
245:                da.addElement(1.0);
246:                da.addElement(1.0);
247:                da.addElement(1.0);
248:                da.addElement(1.0);
249:                da.addElement(1.0);
250:                assertEquals("Number of elements should equal 6", da
251:                        .getNumElements(), 6);
252:
253:                ((ResizableDoubleArray) da).setNumElements(3);
254:                assertEquals("Number of elements should equal 3", da
255:                        .getNumElements(), 3);
256:
257:                try {
258:                    ((ResizableDoubleArray) da).setNumElements(-3);
259:                    fail("Setting number of elements to negative should've thrown an exception");
260:                } catch (IllegalArgumentException iae) {
261:                }
262:
263:                ((ResizableDoubleArray) da).setNumElements(1024);
264:                assertEquals("Number of elements should now be 1024", da
265:                        .getNumElements(), 1024);
266:                assertEquals("Element 453 should be a default double", da
267:                        .getElement(453), 0.0, Double.MIN_VALUE);
268:
269:            }
270:
271:            public void testWithInitialCapacity() {
272:
273:                ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
274:                assertEquals("Initial number of elements should be 0", 0, eDA2
275:                        .getNumElements());
276:
277:                RandomData randomData = new RandomDataImpl();
278:                int iterations = randomData.nextInt(100, 1000);
279:
280:                for (int i = 0; i < iterations; i++) {
281:                    eDA2.addElement(i);
282:                }
283:
284:                assertEquals("Number of elements should be equal to "
285:                        + iterations, iterations, eDA2.getNumElements());
286:
287:                eDA2.addElement(2.0);
288:
289:                assertEquals("Number of elements should be equals to "
290:                        + (iterations + 1), iterations + 1, eDA2
291:                        .getNumElements());
292:            }
293:
294:            public void testWithInitialCapacityAndExpansionFactor() {
295:
296:                ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f,
297:                        3.5f);
298:                assertEquals("Initial number of elements should be 0", 0, eDA3
299:                        .getNumElements());
300:
301:                RandomData randomData = new RandomDataImpl();
302:                int iterations = randomData.nextInt(100, 3000);
303:
304:                for (int i = 0; i < iterations; i++) {
305:                    eDA3.addElement(i);
306:                }
307:
308:                assertEquals("Number of elements should be equal to "
309:                        + iterations, iterations, eDA3.getNumElements());
310:
311:                eDA3.addElement(2.0);
312:
313:                assertEquals("Number of elements should be equals to "
314:                        + (iterations + 1), iterations + 1, eDA3
315:                        .getNumElements());
316:
317:                assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3
318:                        .getExpansionFactor(), Double.MIN_VALUE);
319:            }
320:
321:            public void testDiscard() {
322:                da.addElement(2.0);
323:                da.addElement(2.0);
324:                da.addElement(2.0);
325:                da.addElement(2.0);
326:                da.addElement(2.0);
327:                da.addElement(2.0);
328:                da.addElement(2.0);
329:                da.addElement(2.0);
330:                da.addElement(2.0);
331:                da.addElement(2.0);
332:                da.addElement(2.0);
333:                assertEquals("Number of elements should be 11", 11, da
334:                        .getNumElements());
335:
336:                ((ResizableDoubleArray) da).discardFrontElements(5);
337:                assertEquals("Number of elements should be 6", 6, da
338:                        .getNumElements());
339:
340:                try {
341:                    ((ResizableDoubleArray) da).discardFrontElements(-1);
342:                    fail("Trying to discard a negative number of element is not allowed");
343:                } catch (Exception e) {
344:                }
345:
346:                try {
347:                    ((ResizableDoubleArray) da).discardFrontElements(10000);
348:                    fail("You can't discard more elements than the array contains");
349:                } catch (Exception e) {
350:                }
351:            }
352:
353:            public void testMutators() {
354:                ((ResizableDoubleArray) da).setContractionCriteria(10f);
355:                assertEquals(10f, ((ResizableDoubleArray) da)
356:                        .getContractionCriteria(), 0);
357:                ((ResizableDoubleArray) da).setExpansionFactor(8f);
358:                assertEquals(8f, ((ResizableDoubleArray) da)
359:                        .getExpansionFactor(), 0);
360:                try {
361:                    ((ResizableDoubleArray) da).setExpansionFactor(11f); // greater than contractionCriteria
362:                    fail("Expecting IllegalArgumentException");
363:                } catch (IllegalArgumentException ex) {
364:                    // expected
365:                }
366:                ((ResizableDoubleArray) da)
367:                        .setExpansionMode(ResizableDoubleArray.ADDITIVE_MODE);
368:                assertEquals(ResizableDoubleArray.ADDITIVE_MODE,
369:                        ((ResizableDoubleArray) da).getExpansionMode());
370:                try {
371:                    ((ResizableDoubleArray) da).setExpansionMode(-1);
372:                    fail("Expecting IllegalArgumentException");
373:                } catch (IllegalArgumentException ex) {
374:                    // expected
375:                }
376:            }
377:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.