DataUtilitiesTest.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, 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
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * ----------------------
  28. * DataUtilitiesTest.java
  29. * ----------------------
  30. * (C) Copyright 2005-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 03-Mar-2005 : Version 1 (DG);
  38. * 28-Jan-2009 : Added tests for equal(double[][], double[][]) method (DG);
  39. * 28-Jan-2009 : Added tests for clone(double[][]) (DG);
  40. * 04-Feb-2009 : Added tests for new calculateColumnTotal/RowTotal methods (DG);
  41. *
  42. */
  43. package org.jfree.data;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertTrue;
  46. import static org.junit.Assert.assertFalse;
  47. import org.junit.Test;
  48. /**
  49. * Some tests for the {@link DataUtilities} class.
  50. */
  51. public class DataUtilitiesTest {
  52. /**
  53. * Tests the createNumberArray2D() method.
  54. */
  55. @Test
  56. public void testCreateNumberArray2D() {
  57. double[][] d = new double[2][];
  58. d[0] = new double[] {1.1, 2.2, 3.3, 4.4};
  59. d[1] = new double[] {1.1, 2.2, 3.3, 4.4, 5.5};
  60. Number[][] n = DataUtilities.createNumberArray2D(d);
  61. assertEquals(2, n.length);
  62. assertEquals(4, n[0].length);
  63. assertEquals(5, n[1].length);
  64. }
  65. private static final double EPSILON = 0.000000001;
  66. /**
  67. * Some checks for the calculateColumnTotal() method.
  68. */
  69. @Test
  70. public void testCalculateColumnTotal() {
  71. DefaultKeyedValues2D table = new DefaultKeyedValues2D();
  72. table.addValue(new Double(1.0), "R0", "C0");
  73. table.addValue(new Double(2.0), "R0", "C1");
  74. table.addValue(new Double(3.0), "R1", "C0");
  75. table.addValue(new Double(4.0), "R1", "C1");
  76. assertEquals(4.0, DataUtilities.calculateColumnTotal(table, 0), EPSILON);
  77. assertEquals(6.0, DataUtilities.calculateColumnTotal(table, 1), EPSILON);
  78. table.setValue(null, "R1", "C1");
  79. assertEquals(2.0, DataUtilities.calculateColumnTotal(table, 1), EPSILON);
  80. }
  81. /**
  82. * Some checks for the calculateColumnTotal() method.
  83. */
  84. @Test
  85. public void testCalculateColumnTotal2() {
  86. DefaultKeyedValues2D table = new DefaultKeyedValues2D();
  87. table.addValue(new Double(1.0), "R0", "C0");
  88. table.addValue(new Double(2.0), "R0", "C1");
  89. table.addValue(new Double(3.0), "R1", "C0");
  90. table.addValue(new Double(4.0), "R1", "C1");
  91. assertEquals(4.0, DataUtilities.calculateColumnTotal(table, 0,
  92. new int[] {0, 1}), EPSILON);
  93. assertEquals(1.0, DataUtilities.calculateColumnTotal(table, 0,
  94. new int[] {0}), EPSILON);
  95. assertEquals(3.0, DataUtilities.calculateColumnTotal(table, 0,
  96. new int[] {1}), EPSILON);
  97. assertEquals(0.0, DataUtilities.calculateColumnTotal(table, 0,
  98. new int[] {}), EPSILON);
  99. assertEquals(6.0, DataUtilities.calculateColumnTotal(table, 1,
  100. new int[] {0, 1}), EPSILON);
  101. assertEquals(2.0, DataUtilities.calculateColumnTotal(table, 1,
  102. new int[] {0}), EPSILON);
  103. assertEquals(4.0, DataUtilities.calculateColumnTotal(table, 1,
  104. new int[] {1}), EPSILON);
  105. table.setValue(null, "R1", "C1");
  106. assertEquals(2.0, DataUtilities.calculateColumnTotal(table, 1,
  107. new int[] {0, 1}), EPSILON);
  108. assertEquals(0.0, DataUtilities.calculateColumnTotal(table, 1,
  109. new int[] {1}), EPSILON);
  110. }
  111. /**
  112. * Some checks for the calculateRowTotal() method.
  113. */
  114. @Test
  115. public void testCalculateRowTotal() {
  116. DefaultKeyedValues2D table = new DefaultKeyedValues2D();
  117. table.addValue(new Double(1.0), "R0", "C0");
  118. table.addValue(new Double(2.0), "R0", "C1");
  119. table.addValue(new Double(3.0), "R1", "C0");
  120. table.addValue(new Double(4.0), "R1", "C1");
  121. assertEquals(3.0, DataUtilities.calculateRowTotal(table, 0), EPSILON);
  122. assertEquals(7.0, DataUtilities.calculateRowTotal(table, 1), EPSILON);
  123. table.setValue(null, "R1", "C1");
  124. assertEquals(3.0, DataUtilities.calculateRowTotal(table, 1), EPSILON);
  125. }
  126. /**
  127. * Some checks for the calculateRowTotal() method.
  128. */
  129. @Test
  130. public void testCalculateRowTotal2() {
  131. DefaultKeyedValues2D table = new DefaultKeyedValues2D();
  132. table.addValue(new Double(1.0), "R0", "C0");
  133. table.addValue(new Double(2.0), "R0", "C1");
  134. table.addValue(new Double(3.0), "R1", "C0");
  135. table.addValue(new Double(4.0), "R1", "C1");
  136. assertEquals(3.0, DataUtilities.calculateRowTotal(table, 0,
  137. new int[] {0, 1}), EPSILON);
  138. assertEquals(1.0, DataUtilities.calculateRowTotal(table, 0,
  139. new int[] {0}), EPSILON);
  140. assertEquals(2.0, DataUtilities.calculateRowTotal(table, 0,
  141. new int[] {1}), EPSILON);
  142. assertEquals(0.0, DataUtilities.calculateRowTotal(table, 0,
  143. new int[] {}), EPSILON);
  144. assertEquals(7.0, DataUtilities.calculateRowTotal(table, 1,
  145. new int[] {0, 1}), EPSILON);
  146. assertEquals(3.0, DataUtilities.calculateRowTotal(table, 1,
  147. new int[] {0}), EPSILON);
  148. assertEquals(4.0, DataUtilities.calculateRowTotal(table, 1,
  149. new int[] {1}), EPSILON);
  150. assertEquals(0.0, DataUtilities.calculateRowTotal(table, 1,
  151. new int[] {}), EPSILON);
  152. table.setValue(null, "R1", "C1");
  153. assertEquals(3.0, DataUtilities.calculateRowTotal(table, 1,
  154. new int[] {0, 1}), EPSILON);
  155. assertEquals(0.0, DataUtilities.calculateRowTotal(table, 1,
  156. new int[] {1}), EPSILON);
  157. }
  158. /**
  159. * Some tests for the equal(double[][], double[][]) method.
  160. */
  161. @Test
  162. public void testEqual() {
  163. assertTrue(DataUtilities.equal(null, null));
  164. double[][] a = new double[5][];
  165. double[][] b = new double[5][];
  166. assertTrue(DataUtilities.equal(a, b));
  167. a = new double[4][];
  168. assertFalse(DataUtilities.equal(a, b));
  169. b = new double[4][];
  170. assertTrue(DataUtilities.equal(a, b));
  171. a[0] = new double[6];
  172. assertFalse(DataUtilities.equal(a, b));
  173. b[0] = new double[6];
  174. assertTrue(DataUtilities.equal(a, b));
  175. a[0][0] = 1.0;
  176. assertFalse(DataUtilities.equal(a, b));
  177. b[0][0] = 1.0;
  178. assertTrue(DataUtilities.equal(a, b));
  179. a[0][1] = Double.NaN;
  180. assertFalse(DataUtilities.equal(a, b));
  181. b[0][1] = Double.NaN;
  182. assertTrue(DataUtilities.equal(a, b));
  183. a[0][2] = Double.NEGATIVE_INFINITY;
  184. assertFalse(DataUtilities.equal(a, b));
  185. b[0][2] = Double.NEGATIVE_INFINITY;
  186. assertTrue(DataUtilities.equal(a, b));
  187. a[0][3] = Double.POSITIVE_INFINITY;
  188. assertFalse(DataUtilities.equal(a, b));
  189. b[0][3] = Double.POSITIVE_INFINITY;
  190. assertTrue(DataUtilities.equal(a, b));
  191. a[0][4] = Double.POSITIVE_INFINITY;
  192. assertFalse(DataUtilities.equal(a, b));
  193. b[0][4] = Double.NEGATIVE_INFINITY;
  194. assertFalse(DataUtilities.equal(a, b));
  195. b[0][4] = Double.POSITIVE_INFINITY;
  196. assertTrue(DataUtilities.equal(a, b));
  197. }
  198. /**
  199. * Some tests for the clone() method.
  200. */
  201. @Test
  202. public void testClone() {
  203. double[][] a = new double[1][];
  204. double[][] b = DataUtilities.clone(a);
  205. assertTrue(DataUtilities.equal(a, b));
  206. a[0] = new double[] { 3.0, 4.0 };
  207. assertFalse(DataUtilities.equal(a, b));
  208. b[0] = new double[] { 3.0, 4.0 };
  209. assertTrue(DataUtilities.equal(a, b));
  210. a = new double[2][3];
  211. a[0][0] = 1.23;
  212. a[1][1] = Double.NaN;
  213. b = DataUtilities.clone(a);
  214. assertTrue(DataUtilities.equal(a, b));
  215. a[0][0] = 99.9;
  216. assertFalse(DataUtilities.equal(a, b));
  217. b[0][0] = 99.9;
  218. assertTrue(DataUtilities.equal(a, b));
  219. }
  220. }