PolynomialFunction2DTest.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * PolynomialFunction2DTests.java
  29. * ------------------------------
  30. * (C) Copyright 2009, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 28-May-2009 : Version 1 (DG);
  38. *
  39. */
  40. package org.jfree.data.function;
  41. import java.util.Arrays;
  42. import org.jfree.chart.TestUtilities;
  43. import org.junit.Test;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertTrue;
  47. /**
  48. * Tests for the {@link PolynomialFunction2D} class.
  49. */
  50. public class PolynomialFunction2DTest {
  51. /**
  52. * Some tests for the constructor.
  53. */
  54. @Test
  55. public void testConstructor() {
  56. PolynomialFunction2D f = new PolynomialFunction2D(new double[] {1.0,
  57. 2.0});
  58. assertTrue(Arrays.equals(new double[] {1.0, 2.0}, f.getCoefficients()));
  59. boolean pass = false;
  60. try {
  61. f = new PolynomialFunction2D(null);
  62. }
  63. catch (IllegalArgumentException e) {
  64. pass = true;
  65. }
  66. assertTrue(pass);
  67. }
  68. /**
  69. * Some checks for the getCoefficients() method.
  70. */
  71. @Test
  72. public void testGetCoefficients() {
  73. PolynomialFunction2D f = new PolynomialFunction2D(new double[] {1.0,
  74. 2.0});
  75. double[] c = f.getCoefficients();
  76. assertTrue(Arrays.equals(new double[] {1.0, 2.0}, c));
  77. // make sure that modifying the returned array doesn't change the
  78. // function
  79. c[0] = 99.9;
  80. assertTrue(Arrays.equals(new double[] {1.0, 2.0}, f.getCoefficients()));
  81. }
  82. /**
  83. * Some checks for the getOrder() method.
  84. */
  85. @Test
  86. public void testGetOrder() {
  87. PolynomialFunction2D f = new PolynomialFunction2D(new double[] {1.0,
  88. 2.0});
  89. assertEquals(1, f.getOrder());
  90. }
  91. /**
  92. * For datasets, the equals() method just checks keys and values.
  93. */
  94. @Test
  95. public void testEquals() {
  96. PolynomialFunction2D f1 = new PolynomialFunction2D(new double[] {1.0,
  97. 2.0});
  98. PolynomialFunction2D f2 = new PolynomialFunction2D(new double[] {1.0,
  99. 2.0});
  100. assertTrue(f1.equals(f2));
  101. f1 = new PolynomialFunction2D(new double[] {2.0, 3.0});
  102. assertFalse(f1.equals(f2));
  103. f2 = new PolynomialFunction2D(new double[] {2.0, 3.0});
  104. assertTrue(f1.equals(f2));
  105. }
  106. /**
  107. * Serialize an instance, restore it, and check for equality.
  108. */
  109. @Test
  110. public void testSerialization() {
  111. PolynomialFunction2D f1 = new PolynomialFunction2D(new double[] {1.0,
  112. 2.0});
  113. PolynomialFunction2D f2 = (PolynomialFunction2D)
  114. TestUtilities.serialised(f1);
  115. assertEquals(f1, f2);
  116. }
  117. /**
  118. * Objects that are equal should have the same hash code otherwise FindBugs
  119. * will tell on us...
  120. */
  121. @Test
  122. public void testHashCode() {
  123. PolynomialFunction2D f1 = new PolynomialFunction2D(new double[] {1.0,
  124. 2.0});
  125. PolynomialFunction2D f2 = new PolynomialFunction2D(new double[] {1.0,
  126. 2.0});
  127. assertEquals(f1.hashCode(), f2.hashCode());
  128. }
  129. }