diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 14:22:20 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-04-09 15:48:01 -0700 |
commit | 3102d8bce3426d9cf41aeaf201c360d342677770 (patch) | |
tree | 38c4f1e8828f9af9c4b77a173bee0d312b321698 /src/test/java/com/p4square/grow/model/PointTest.java | |
parent | bbf907e51dfcf157bdee24dead1d531122aa25db (diff) |
Switching from Ivy+Ant to Maven.
Diffstat (limited to 'src/test/java/com/p4square/grow/model/PointTest.java')
-rw-r--r-- | src/test/java/com/p4square/grow/model/PointTest.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/test/java/com/p4square/grow/model/PointTest.java b/src/test/java/com/p4square/grow/model/PointTest.java new file mode 100644 index 0000000..a4e7cc0 --- /dev/null +++ b/src/test/java/com/p4square/grow/model/PointTest.java @@ -0,0 +1,129 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.model; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests for the Point class. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class PointTest { + private static final double DELTA = 1e-15; + + public static void main(String... args) { + org.junit.runner.JUnitCore.main(PointTest.class.getName()); + } + + /** + * Verify that the constructor works properly. + */ + @Test + public void testHappyCase() { + Point p = new Point(1, 2); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + } + + /** + * Verify distance is computed correctly. + */ + @Test + public void testDistance() { + Point p1, p2; + + // Simple line + p1 = new Point(2, 1); + p2 = new Point(-2, 1); + assertEquals(4, p1.distance(p2), DELTA); + assertEquals(4, p2.distance(p1), DELTA); + + // Across origin + p1 = new Point(5, 1); + p2 = new Point(-3, -2); + assertEquals(Math.sqrt(73), p1.distance(p2), DELTA); + assertEquals(Math.sqrt(73), p2.distance(p1), DELTA); + } + + /** + * Verify toString returns the expected string. + */ + @Test + public void testToString() { + Point p = new Point(-1.12345, 2.3); + assertEquals("-1.12,2.30", p.toString()); + } + + /** + * Verify that valueOf correctly parses a variety of strings. + */ + @Test + public void testValueOfHappyCase() { + Point p; + + p = Point.valueOf("1,2"); + assertEquals(1, p.getX(), DELTA); + assertEquals(2, p.getY(), DELTA); + + p = Point.valueOf("1.5,2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(2.0, p.getY(), DELTA); + + p = Point.valueOf("1.5,-2.0"); + assertEquals(1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + + p = Point.valueOf("-1.5,-2.0"); + assertEquals(-1.5, p.getX(), DELTA); + assertEquals(-2.0, p.getY(), DELTA); + } + + /** + * Verify that valueOf fails on null string. + */ + @Test(expected = NullPointerException.class) + public void testValueOfNull() { + Point.valueOf(null); + } + + /** + * Verify that valueOf fails on empty string. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfEmptyString() { + Point.valueOf(""); + } + + /** + * Verify that valueOf fails on missing comma. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingComma() { + Point.valueOf("123"); + } + + /** + * Verify that valueOf fails on missing x. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingX() { + Point.valueOf(",12"); + } + + /** + * Verify that valueOf fails on missing y. + */ + @Test(expected = IllegalArgumentException.class) + public void testValueOfMissingY() { + Point.valueOf("12,"); + } +} |