1 /* ===========================================================
2 * JFreeChart : a free chart library for the Java(tm) platform
3 * ===========================================================
4 *
5 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6 *
7 * Project Info: http://www.jfree.org/jfreechart/index.html
8 *
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 *
23 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24 * in the United States and other countries.]
25 *
26 * ----------------------
27 * MatrixSeriesTests.java
28 * ----------------------
29 * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
30 *
31 * Original Author: David Gilbert (for Object Refinery Limited);
32 * Contributor(s): -;
33 *
34 * $Id: MatrixSeriesTests.java,v 1.5 2005/05/19 10:35:27 mungady Exp $
35 *
36 * Changes
37 * -------
38 * 21-May-2004 : Version 1 (DG);
39 *
40 */
41
42 package org.jfree.data.xy.junit;
43
44 import java.io.ByteArrayInputStream;
45 import java.io.ByteArrayOutputStream;
46 import java.io.ObjectInput;
47 import java.io.ObjectInputStream;
48 import java.io.ObjectOutput;
49 import java.io.ObjectOutputStream;
50
51 import junit.framework.Test;
52 import junit.framework.TestCase;
53 import junit.framework.TestSuite;
54
55 import org.jfree.data.xy.MatrixSeries;
56
57 /**
58 * Tests for the {@link MatrixSeries} class.
59 */
60 public class MatrixSeriesTests extends TestCase {
61
62 /**
63 * Returns the tests as a test suite.
64 *
65 * @return The test suite.
66 */
67 public static Test suite() {
68 return new TestSuite(MatrixSeriesTests.class);
69 }
70
71 /**
72 * Constructs a new set of tests.
73 *
74 * @param name the name of the tests.
75 */
76 public MatrixSeriesTests(String name) {
77 super(name);
78 }
79
80 /**
81 * Confirm that the equals method can distinguish all the required fields.
82 */
83 public void testEquals() {
84
85 MatrixSeries m1 = new MatrixSeries("Test", 8, 3);
86 m1.update(0, 0, 11.0);
87 m1.update(7, 2, 22.0);
88 MatrixSeries m2 = new MatrixSeries("Test", 8, 3);
89 m2.update(0, 0, 11.0);
90 m2.update(7, 2, 22.0);
91 assertTrue(m1.equals(m2));
92 assertTrue(m2.equals(m1));
93
94 m1 = new MatrixSeries("Test 2", 8, 3);
95 assertFalse(m1.equals(m2));
96 m2 = new MatrixSeries("Test 2", 8, 3);
97 assertTrue(m1.equals(m2));
98
99 m1 = new MatrixSeries("Test 2", 10, 3);
100 assertFalse(m1.equals(m2));
101 m2 = new MatrixSeries("Test 2", 10, 3);
102 assertTrue(m1.equals(m2));
103
104 m1 = new MatrixSeries("Test 2", 10, 5);
105 assertFalse(m1.equals(m2));
106 m2 = new MatrixSeries("Test 2", 10, 5);
107 assertTrue(m1.equals(m2));
108
109 }
110
111 /**
112 * Confirm that cloning works.
113 */
114 public void testCloning() {
115 MatrixSeries m1 = new MatrixSeries("Test", 8, 3);
116 m1.update(0, 0, 11.0);
117 m1.update(7, 2, 22.0);
118 MatrixSeries m2 = null;
119 try {
120 m2 = (MatrixSeries) m1.clone();
121 }
122 catch (CloneNotSupportedException e) {
123 System.err.println("Failed to clone.");
124 }
125 assertTrue(m1 != m2);
126 assertTrue(m1.getClass() == m2.getClass());
127 assertTrue(m1.equals(m2));
128 }
129
130 /**
131 * Serialize an instance, restore it, and check for equality.
132 */
133 public void testSerialization() {
134
135 MatrixSeries m1 = new MatrixSeries("Test", 8, 3);
136 m1.update(0, 0, 11.0);
137 m1.update(7, 2, 22.0);
138 MatrixSeries m2 = null;
139
140 try {
141 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
142 ObjectOutput out = new ObjectOutputStream(buffer);
143 out.writeObject(m1);
144 out.close();
145
146 ObjectInput in = new ObjectInputStream(
147 new ByteArrayInputStream(buffer.toByteArray())
148 );
149 m2 = (MatrixSeries) in.readObject();
150 in.close();
151 }
152 catch (Exception e) {
153 fail(e.toString());
154 }
155 assertEquals(m1, m2);
156
157 }
158
159 /**
160 * Tests the getItemColumn() method.
161 */
162 public void testGetItemColumn() {
163 MatrixSeries m = new MatrixSeries("Test", 3, 2);
164 assertEquals(0, m.getItemColumn(0));
165 assertEquals(1, m.getItemColumn(1));
166 assertEquals(0, m.getItemColumn(2));
167 assertEquals(1, m.getItemColumn(3));
168 assertEquals(0, m.getItemColumn(4));
169 assertEquals(1, m.getItemColumn(5));
170 }
171
172 /**
173 * Tests the getItemRow() method.
174 */
175 public void testGetItemRow() {
176 MatrixSeries m = new MatrixSeries("Test", 3, 2);
177 assertEquals(0, m.getItemRow(0));
178 assertEquals(0, m.getItemRow(1));
179 assertEquals(1, m.getItemRow(2));
180 assertEquals(1, m.getItemRow(3));
181 assertEquals(2, m.getItemRow(4));
182 assertEquals(2, m.getItemRow(5));
183 }
184
185 /**
186 * Tests the getItem() method.
187 */
188 public void testGetItem() {
189 MatrixSeries m = new MatrixSeries("Test", 3, 2);
190 m.update(0, 0, 0.0);
191 m.update(0, 1, 1.0);
192 m.update(1, 0, 2.0);
193 m.update(1, 1, 3.0);
194 m.update(2, 0, 4.0);
195 m.update(2, 1, 5.0);
196 assertEquals(0.0, m.getItem(0).doubleValue(), 0.001);
197 assertEquals(1.0, m.getItem(1).doubleValue(), 0.001);
198 assertEquals(2.0, m.getItem(2).doubleValue(), 0.001);
199 assertEquals(3.0, m.getItem(3).doubleValue(), 0.001);
200 assertEquals(4.0, m.getItem(4).doubleValue(), 0.001);
201 assertEquals(5.0, m.getItem(5).doubleValue(), 0.001);
202 }
203
204 }