View Javadoc

1   // Copyright (C) 2000 Paco Gomez
2   // Copyright (C) 2000 - 2011 Philip Aston
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.util;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.ByteArrayOutputStream;
27  import java.io.IOException;
28  import java.io.ObjectInputStream;
29  import java.io.ObjectOutputStream;
30  import java.util.Random;
31  
32  import junit.framework.TestCase;
33  
34  
35  /**
36   * Unit test case for {@link Serialiser}.
37   *
38   * @author Philip Aston
39   */
40  public class TestSerialiser extends TestCase
41  {
42      private final Random m_random = new Random();
43  
44      public void testUnsignedLongs() throws Exception
45      {
46  	final ByteArrayOutputStream byteArrayOutputStream =
47  	    new ByteArrayOutputStream();
48  
49  	final ObjectOutputStream objectOutputStream =
50  	    new ObjectOutputStream(byteArrayOutputStream);
51  
52  	final long[] longs = new long[10000];
53  
54  	final Serialiser serialiser = new Serialiser();
55  
56  	for (int i=0; i<longs.length; i++) {
57  	    if (i < 1000) {
58  		longs[i] = i;
59  	    }
60  	    else {
61  		longs[i] = Math.abs(m_random.nextLong() & 0xFFF);
62  	    }
63  
64  	    serialiser.writeUnsignedLong(objectOutputStream, longs[i]);
65  	}
66  
67  	try {
68  	    serialiser.writeUnsignedLong(objectOutputStream, -1);
69  	    fail("Should not reach");
70  	}
71  	catch (IOException e) {
72  	}
73  
74  	objectOutputStream.close();
75  
76  	final byte[] bytes = byteArrayOutputStream.toByteArray();
77  
78  	assertTrue("We should compress", bytes.length < 8 * longs.length);
79  
80  	final ObjectInputStream objectInputStream =
81  	    new ObjectInputStream(new ByteArrayInputStream(bytes));
82  
83  	for (int i=0; i<longs.length; i++) {
84  	    assertEquals(longs[i],
85  			 serialiser.readUnsignedLong(objectInputStream));
86  	}
87      }
88  
89      public void testLongs() throws Exception
90      {
91  	final ByteArrayOutputStream byteArrayOutputStream =
92  	    new ByteArrayOutputStream();
93  
94  	final ObjectOutputStream objectOutputStream =
95  	    new ObjectOutputStream(byteArrayOutputStream);
96  
97  	final long[] longs = new long[3002];
98  
99  	final Serialiser serialiser = new Serialiser();
100 
101 	for (int i=0; i<longs.length; i++) {
102 	    if (i < 1000) {
103 		longs[i] = i;
104 	    }
105 	    else if (i <2000) {
106 		longs[i] = i - 2000;
107 	    }
108 	    else {
109 		longs[i] = m_random.nextLong();
110 	    }
111 
112 	    longs[3000] = Long.MIN_VALUE;
113 	    longs[3001] = Long.MAX_VALUE;
114 
115 	    serialiser.writeLong(objectOutputStream, longs[i]);
116 	}
117 
118 	objectOutputStream.close();
119 
120 	final byte[] bytes = byteArrayOutputStream.toByteArray();
121 
122 	assertTrue("We should compress", bytes.length < 8 * longs.length);
123 
124 	final ObjectInputStream objectInputStream =
125 	    new ObjectInputStream(new ByteArrayInputStream(bytes));
126 
127 	for (int i=0; i<longs.length; i++) {
128 	    assertEquals(longs[i], serialiser.readLong(objectInputStream));
129 	}
130     }
131 
132     public void testDoubles() throws Exception
133     {
134 	final ByteArrayOutputStream byteArrayOutputStream =
135 	    new ByteArrayOutputStream();
136 
137 	final ObjectOutputStream objectOutputStream =
138 	    new ObjectOutputStream(byteArrayOutputStream);
139 
140 	final double[] doubles = new double[10000];
141 
142 	final Serialiser serialiser = new Serialiser();
143 
144 	for (int i=0; i<doubles.length; i++) {
145 	    if (i < 1000) {
146 		doubles[i] = i;
147 	    }
148 	    else {
149 		doubles[i] = m_random.nextDouble();
150 	    }
151 
152 	    serialiser.writeDouble(objectOutputStream, doubles[i]);
153 	}
154 
155 	objectOutputStream.close();
156 
157 	final byte[] bytes = byteArrayOutputStream.toByteArray();
158 
159 	// To do, make this work.
160 	//assertTrue("We should compress", bytes.length < 8 * doubles.length);
161 
162 	final ObjectInputStream objectInputStream =
163 	    new ObjectInputStream(new ByteArrayInputStream(bytes));
164 
165 	for (int i=0; i<doubles.length; i++) {
166 	    assertEquals(doubles[i], serialiser.readDouble(objectInputStream),
167 			 0.00001);
168 	}
169     }
170 }