|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2020 iText Group NV |
| 4 | + Authors: iText Software. |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License version 3 |
| 8 | + as published by the Free Software Foundation with the addition of the |
| 9 | + following permission added to Section 15 as permitted in Section 7(a): |
| 10 | + FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY |
| 11 | + ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT |
| 12 | + OF THIRD PARTY RIGHTS |
| 13 | +
|
| 14 | + This program 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. |
| 17 | + See the GNU Affero General Public License for more details. |
| 18 | + You should have received a copy of the GNU Affero General Public License |
| 19 | + along with this program; if not, see http://www.gnu.org/licenses or write to |
| 20 | + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 21 | + Boston, MA, 02110-1301 USA, or download the license from the following URL: |
| 22 | + http://itextpdf.com/terms-of-use/ |
| 23 | +
|
| 24 | + The interactive user interfaces in modified source and object code versions |
| 25 | + of this program must display Appropriate Legal Notices, as required under |
| 26 | + Section 5 of the GNU Affero General Public License. |
| 27 | +
|
| 28 | + In accordance with Section 7(b) of the GNU Affero General Public License, |
| 29 | + a covered work must retain the producer line in every PDF that is created |
| 30 | + or manipulated using iText. |
| 31 | +
|
| 32 | + You can be released from the requirements of the license by purchasing |
| 33 | + a commercial license. Buying such a license is mandatory as soon as you |
| 34 | + develop commercial activities involving the iText software without |
| 35 | + disclosing the source code of your own applications. |
| 36 | + These activities include: offering paid services to customers as an ASP, |
| 37 | + serving PDFs on the fly in a web application, shipping iText with a closed |
| 38 | + source product. |
| 39 | +
|
| 40 | + For more information, please contact iText Software Corp. at this |
| 41 | + |
| 42 | + */ |
| 43 | +package com.itextpdf.kernel.geom; |
| 44 | + |
| 45 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 46 | + |
| 47 | +import org.junit.Assert; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.experimental.categories.Category; |
| 50 | + |
| 51 | +@Category(UnitTest.class) |
| 52 | +public class PointTest { |
| 53 | + private static double EPSILON_COMPARISON = 1E-12; |
| 54 | + |
| 55 | + @Test |
| 56 | + public void defaultConstructorTest() { |
| 57 | + Point first = new Point(); |
| 58 | + Assert.assertEquals(0, first.x, EPSILON_COMPARISON); |
| 59 | + Assert.assertEquals(0, first.y, EPSILON_COMPARISON); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void doubleParamConstructorTest() { |
| 64 | + Point first = new Point(0.13, 1.1); |
| 65 | + Assert.assertEquals(0.13, first.getX(), EPSILON_COMPARISON); |
| 66 | + Assert.assertEquals(1.1, first.getY(), EPSILON_COMPARISON); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void intParamConstructorTest() { |
| 71 | + Point first = new Point(2, 3); |
| 72 | + Assert.assertEquals(2, first.x, EPSILON_COMPARISON); |
| 73 | + Assert.assertEquals(3, first.y, EPSILON_COMPARISON); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void copyConstructorTest() { |
| 78 | + Point second = new Point(new Point(0.13, 1.1)); |
| 79 | + Assert.assertEquals(0.13, second.getX(), EPSILON_COMPARISON); |
| 80 | + Assert.assertEquals(1.1, second.getY(), EPSILON_COMPARISON); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void equalsItselfTest() { |
| 85 | + Point first = new Point(1.23, 1.1); |
| 86 | + |
| 87 | + Assert.assertTrue(first.equals(first)); |
| 88 | + Assert.assertEquals(first.hashCode(), first.hashCode()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void equalsToAnotherPointTest() { |
| 93 | + Point first = new Point(1.23, 1.1); |
| 94 | + Point second = new Point(1.23, 1.1); |
| 95 | + |
| 96 | + Assert.assertTrue(first.equals(second)); |
| 97 | + Assert.assertTrue(second.equals(first)); |
| 98 | + Assert.assertEquals(first.hashCode(), second.hashCode()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void notEqualsToAnotherPointTest() { |
| 103 | + Point first = new Point(1.23, 1.1); |
| 104 | + Point second = new Point(1.23, 1.2); |
| 105 | + |
| 106 | + Assert.assertFalse(first.equals(second)); |
| 107 | + Assert.assertFalse(second.equals(first)); |
| 108 | + Assert.assertNotEquals(first.hashCode(), second.hashCode()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void notEqualsToNullTest() { |
| 113 | + Point first = new Point(1.23, 1.1); |
| 114 | + Assert.assertFalse(first.equals(null)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void distanceSquareBetweenCoordinatesTest() { |
| 119 | + Point first = new Point(1, 1); |
| 120 | + Point second = new Point(1.1, 1.1); |
| 121 | + |
| 122 | + double expected = 0.02; |
| 123 | + Assert.assertEquals(expected, Point.distanceSq(first.x, first.y, second.x, second.y), EPSILON_COMPARISON); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void distanceSquareByCoordinatesTest() { |
| 128 | + Point first = new Point(1, 1); |
| 129 | + Point second = new Point(1.1, 1.1); |
| 130 | + |
| 131 | + double expected = 0.02; |
| 132 | + Assert.assertEquals(expected, first.distanceSq(second.x, second.y), EPSILON_COMPARISON); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void distanceSquareByPointTest() { |
| 137 | + Point first = new Point(1, 1); |
| 138 | + Point second = new Point(1.1, 1.1); |
| 139 | + |
| 140 | + double expected = 0.02; |
| 141 | + Assert.assertEquals(expected, first.distanceSq(second), EPSILON_COMPARISON); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void distanceItselfSquareTest() { |
| 146 | + Point first = new Point(1, 1); |
| 147 | + Assert.assertEquals(0, first.distanceSq(first), EPSILON_COMPARISON); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void distanceBetweenCoordinatesTest() { |
| 152 | + Point first = new Point(1, 1); |
| 153 | + Point second = new Point(1.1, 1.1); |
| 154 | + |
| 155 | + double expected = Math.sqrt(0.02); |
| 156 | + Assert.assertEquals(expected, Point.distance(first.x, first.y, second.x, second.y), EPSILON_COMPARISON); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void distanceByCoordinatesTest() { |
| 161 | + Point first = new Point(1, 1); |
| 162 | + Point second = new Point(1.1, 1.1); |
| 163 | + |
| 164 | + double expected = Math.sqrt(0.02); |
| 165 | + Assert.assertEquals(expected, first.distance(second.x, second.y), EPSILON_COMPARISON); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void distanceByPointTest() { |
| 170 | + Point first = new Point(1, 1); |
| 171 | + Point second = new Point(1.1, 1.1); |
| 172 | + |
| 173 | + double expected = Math.sqrt(0.02); |
| 174 | + Assert.assertEquals(expected, first.distance(second), EPSILON_COMPARISON); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void distanceItselfTest() { |
| 179 | + Point first = new Point(1, 1); |
| 180 | + Assert.assertEquals(0, first.distance(first), EPSILON_COMPARISON); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void toStringTest() { |
| 185 | + Point first = new Point(1.23, 1.1); |
| 186 | + Assert.assertEquals("Point: [x=1.23,y=1.1]", first.toString()); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void cloneTest() { |
| 191 | + Point first = new Point(1.23, 1.1); |
| 192 | + Point clone = (Point) first.clone(); |
| 193 | + Assert.assertEquals(first, clone); |
| 194 | + Assert.assertEquals(first.hashCode(), clone.hashCode()); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void translateTest() { |
| 199 | + float w = 3.73f; |
| 200 | + float h = 5.23f; |
| 201 | + Rectangle rectangle = new Rectangle(0, 0, w, h); |
| 202 | + Point[] expectedPoints = rectangle.toPointsArray(); |
| 203 | + |
| 204 | + Point point = new Point(0, 0); |
| 205 | + |
| 206 | + point.translate(w, 0); |
| 207 | + Assert.assertEquals(expectedPoints[1], point); |
| 208 | + point.translate(0, h); |
| 209 | + Assert.assertEquals(expectedPoints[2], point); |
| 210 | + point.translate(-w, 0); |
| 211 | + Assert.assertEquals(expectedPoints[3], point); |
| 212 | + point.translate(0, -h); |
| 213 | + Assert.assertEquals(expectedPoints[0], point); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void pointVsItLocationTest() { |
| 218 | + Point first = new Point(1.23, 1.1); |
| 219 | + |
| 220 | + Point location = first.getLocation(); |
| 221 | + Assert.assertTrue(first != location && first.equals(location)); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + public void setLocationByPointTest() { |
| 226 | + Point first = new Point(1.23, 1.1); |
| 227 | + Point second = new Point(3.59, 0.87); |
| 228 | + |
| 229 | + Assert.assertNotEquals(first, second); |
| 230 | + first.setLocation(second); |
| 231 | + Assert.assertEquals(first, second); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + public void setLocationByDoubleParamTest() { |
| 236 | + Point first = new Point(1.23, 1.1); |
| 237 | + Point second = new Point(3.59, 0.87); |
| 238 | + |
| 239 | + Assert.assertNotEquals(first, second); |
| 240 | + first.setLocation(second.x, second.y); |
| 241 | + Assert.assertEquals(first, second); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void setLocationByIntParamTest() { |
| 246 | + Point first = new Point(1.23, 1.1); |
| 247 | + Point second = new Point(3.59, 0.87); |
| 248 | + |
| 249 | + Assert.assertNotEquals(first, second); |
| 250 | + first.setLocation((int) second.x, (int) second.y); |
| 251 | + Assert.assertEquals(first, new Point(3, 0)); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + public void movePointTest() { |
| 256 | + Point first = new Point(1.23, 1.1); |
| 257 | + Point second = new Point(3.59, 0.87); |
| 258 | + |
| 259 | + Assert.assertNotEquals(first, second); |
| 260 | + first.move(second.x, second.y); |
| 261 | + Assert.assertEquals(first, second); |
| 262 | + } |
| 263 | +} |
0 commit comments