LogarithmicAxisTest.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * LogarithmicAxisTest.java
  29. * ------------------------
  30. * (C) Copyright 2003-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. * 26-Mar-2003 : Version 1 (DG);
  38. * 02-Mar-2007 : Added tests from bug report 880597 (DG);
  39. *
  40. */
  41. package org.jfree.chart.axis;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertFalse;
  44. import java.awt.geom.Rectangle2D;
  45. import org.jfree.chart.TestUtilities;
  46. import org.jfree.ui.RectangleEdge;
  47. import org.junit.Before;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link LogarithmicAxis} class.
  51. */
  52. public class LogarithmicAxisTest {
  53. static class MyLogarithmicAxis extends LogarithmicAxis {
  54. /**
  55. * Creates a new instance.
  56. *
  57. * @param label the label.
  58. */
  59. public MyLogarithmicAxis(String label) {
  60. super(label);
  61. }
  62. /* (non-Javadoc)
  63. * @see org.jfree.chart.axis.LogarithmicAxis#switchedLog10(double)
  64. */
  65. @Override
  66. protected double switchedLog10(double val) {
  67. return super.switchedLog10(val);
  68. }
  69. }
  70. /** Tolerance for floating point comparisons */
  71. public static double EPSILON = 0.000001;
  72. MyLogarithmicAxis axis = null;
  73. /**
  74. * Sets up a new axis.
  75. *
  76. * @throws Exception
  77. */
  78. @Before
  79. public void setUp() throws Exception {
  80. this.axis = new MyLogarithmicAxis("Value (log)");
  81. this.axis.setAllowNegativesFlag(false);
  82. this.axis.setLog10TickLabelsFlag(false);
  83. this.axis.setLowerMargin(0.0);
  84. this.axis.setUpperMargin(0.0);
  85. this.axis.setLowerBound(0.2);
  86. this.axis.setUpperBound(100.0);
  87. }
  88. /**
  89. * Serialize an instance, restore it, and check for equality.
  90. */
  91. @Test
  92. public void testSerialization() {
  93. LogarithmicAxis a1 = new LogarithmicAxis("Test Axis");
  94. LogarithmicAxis a2 = (LogarithmicAxis) TestUtilities.serialised(a1);
  95. assertEquals(a1, a2);
  96. }
  97. /**
  98. * Test if adjustedLog10 and adjustedPow10 are inverses of each other
  99. */
  100. @Test
  101. public void testAdjustedLog10() {
  102. checkLogPowRoundTrip(20);
  103. checkLogPowRoundTrip(10);
  104. checkLogPowRoundTrip(5);
  105. checkLogPowRoundTrip(2);
  106. checkLogPowRoundTrip(1);
  107. checkLogPowRoundTrip(0.5);
  108. checkLogPowRoundTrip(0.2);
  109. checkLogPowRoundTrip(0.0001);
  110. }
  111. private void checkLogPowRoundTrip(double value) {
  112. assertEquals("log(pow(x)) = x", value, this.axis.adjustedLog10(
  113. this.axis.adjustedPow10(value)), EPSILON);
  114. assertEquals("pow(log(x)) = x", value, this.axis.adjustedPow10(
  115. this.axis.adjustedLog10(value)), EPSILON);
  116. }
  117. /**
  118. * Test if switchedLog10 and switchedPow10 are inverses of each other
  119. */
  120. @Test
  121. public void testSwitchedLog10() {
  122. assertFalse("Axis should not allow negative values",
  123. this.axis.getAllowNegativesFlag());
  124. assertEquals(Math.log(0.5) / LogarithmicAxis.LOG10_VALUE,
  125. this.axis.switchedLog10(0.5), EPSILON);
  126. checkSwitchedLogPowRoundTrip(20);
  127. checkSwitchedLogPowRoundTrip(10);
  128. checkSwitchedLogPowRoundTrip(5);
  129. checkSwitchedLogPowRoundTrip(2);
  130. checkSwitchedLogPowRoundTrip(1);
  131. checkSwitchedLogPowRoundTrip(0.5);
  132. checkSwitchedLogPowRoundTrip(0.2);
  133. checkSwitchedLogPowRoundTrip(0.0001);
  134. }
  135. private void checkSwitchedLogPowRoundTrip(double value) {
  136. assertEquals("log(pow(x)) = x", value, this.axis.switchedLog10(
  137. this.axis.switchedPow10(value)), EPSILON);
  138. assertEquals("pow(log(x)) = x", value, this.axis.switchedPow10(
  139. this.axis.switchedLog10(value)), EPSILON);
  140. }
  141. /**
  142. * Test of java2DToValue method.
  143. */
  144. @Test
  145. public void testJava2DToValue() {
  146. Rectangle2D plotArea = new Rectangle2D.Double(22, 33, 500, 500);
  147. RectangleEdge edge = RectangleEdge.BOTTOM;
  148. // set axis bounds to be both greater than 1
  149. this.axis.setRange(10, 20);
  150. checkPointsToValue(edge, plotArea);
  151. // check for bounds interval that includes 1
  152. this.axis.setRange(0.5, 10);
  153. checkPointsToValue(edge, plotArea);
  154. // check for bounds interval that includes 1
  155. this.axis.setRange(0.2, 20);
  156. checkPointsToValue(edge, plotArea);
  157. // check for both bounds smaller than 1
  158. this.axis.setRange(0.2, 0.7);
  159. checkPointsToValue(edge, plotArea);
  160. }
  161. /**
  162. * Test of valueToJava2D method.
  163. */
  164. @Test
  165. public void testValueToJava2D() {
  166. Rectangle2D plotArea = new Rectangle2D.Double(22, 33, 500, 500);
  167. RectangleEdge edge = RectangleEdge.BOTTOM;
  168. // set axis bounds to be both greater than 1
  169. this.axis.setRange(10, 20);
  170. checkPointsToJava2D(edge, plotArea);
  171. // check for bounds interval that includes 1
  172. this.axis.setRange(0.5, 10);
  173. checkPointsToJava2D(edge, plotArea);
  174. // check for bounds interval that includes 1
  175. this.axis.setRange(0.2, 20);
  176. checkPointsToJava2D(edge, plotArea);
  177. // check for both bounds smaller than 1
  178. this.axis.setRange(0.2, 0.7);
  179. checkPointsToJava2D(edge, plotArea);
  180. }
  181. private void checkPointsToJava2D(RectangleEdge edge,
  182. Rectangle2D plotArea) {
  183. assertEquals("Left most point on the axis should be beginning of "
  184. + "range.", plotArea.getX(), this.axis.valueToJava2D(
  185. this.axis.getLowerBound(), plotArea, edge), EPSILON);
  186. assertEquals("Right most point on the axis should be end of range.",
  187. plotArea.getX() + plotArea.getWidth(),
  188. this.axis.valueToJava2D(this.axis.getUpperBound(),
  189. plotArea, edge), EPSILON);
  190. assertEquals("Center point on the axis should geometric mean of the bounds.",
  191. plotArea.getX() + (plotArea.getWidth() / 2),
  192. this.axis.valueToJava2D(Math.sqrt(this.axis.getLowerBound()
  193. * this.axis.getUpperBound()), plotArea, edge), EPSILON);
  194. }
  195. /**
  196. * Check the translation java2D to value for left, right, and center point.
  197. *
  198. * @param edge the edge.
  199. * @param plotArea the plot area.
  200. */
  201. private void checkPointsToValue(RectangleEdge edge, Rectangle2D plotArea) {
  202. assertEquals("Right most point on the axis should be end of range.",
  203. this.axis.getUpperBound(), this.axis.java2DToValue(
  204. plotArea.getX() + plotArea.getWidth(), plotArea, edge),
  205. EPSILON);
  206. assertEquals("Left most point on the axis should be beginning of "
  207. + "range.", this.axis.getLowerBound(),
  208. this.axis.java2DToValue(plotArea.getX(), plotArea, edge),
  209. EPSILON);
  210. assertEquals("Center point on the axis should geometric mean of the "
  211. + "bounds.", Math.sqrt(this.axis.getUpperBound()
  212. * this.axis.getLowerBound()), this.axis.java2DToValue(
  213. plotArea.getX() + (plotArea.getWidth() / 2), plotArea, edge),
  214. EPSILON);
  215. }
  216. }