diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eda8f78 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Java +*.class +*.jar + +# Maven +target/ + +# Eclipse +.classpath +.project +.settings diff --git a/pom.xml b/pom.xml index 14e3fb6..2623a4a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,12 @@ 1.0 jar - + + + junit + junit + 4.11 + diff --git a/src/main/java/com/getintent/interview/impl/MonochromeScreenImpl.java b/src/main/java/com/getintent/interview/impl/MonochromeScreenImpl.java new file mode 100644 index 0000000..c7af762 --- /dev/null +++ b/src/main/java/com/getintent/interview/impl/MonochromeScreenImpl.java @@ -0,0 +1,80 @@ +package com.getintent.interview.impl; + +import java.util.BitSet; + +import com.getintent.interview.MonochromeScreen; + +public class MonochromeScreenImpl implements MonochromeScreen { + + private BitSet[] screen; + private int width; + + @Override + public void init(int width, int height) { + checkInitRange(width, height); + screen = new BitSet[height]; + this.width = width; + } + + @Override + public void drawHorizontalLine(int x1, int x2, int y) { + checkLineRange(x1, x2, y); + if (screen[y] == null) { + screen[y] = new BitSet(width); + } + screen[y].set(x1, x2 + 1); + } + + @Override + public void drawCircle(int x, int y, int r) { + throw new UnsupportedOperationException(); + } + + @Override + public void drawLine(int x1, int y1, int x2, int y2) { + throw new UnsupportedOperationException(); + } + + public byte getPoint(int x, int y) { + checkPointRange(x, y); + if (screen[y] == null) { + return 0; + } else { + return (byte) (screen[y].get(x) == false ? 0 : 1); + } + } + + private void checkInitRange(int width, int height) { + if (width <= 0) { + throw new IllegalArgumentException("Width must be greater then zero!"); + } + if (height <= 0) { + throw new IllegalArgumentException("Height must be greater then zero!"); + } + } + + private void checkLineRange(int x1, int x2, int y) { + if (x1 < 0 || x2 < 0 || y < 0) { + throw new IllegalArgumentException("coordinates must be positive"); + } + if (y >= screen.length) { + throw new IllegalArgumentException("y must be <" + screen.length); + } + if (x1 > x2) { + throw new IllegalArgumentException("x1 must be less or equals then x2"); + } + if (x2 >= width) { + throw new IllegalArgumentException("x must be <" + width); + } + } + + private void checkPointRange(int x, int y) { + if (y < 0 || y >= screen.length) { + throw new IllegalArgumentException("y must be >=0 and <" + screen.length); + } + if (x < 0 || x >= width) { + throw new IllegalArgumentException("x must be >=0 and <" + width); + } + } + +} diff --git a/src/test/java/com/getintent/test/MonochromeScreenTest.java b/src/test/java/com/getintent/test/MonochromeScreenTest.java new file mode 100644 index 0000000..ab64e29 --- /dev/null +++ b/src/test/java/com/getintent/test/MonochromeScreenTest.java @@ -0,0 +1,135 @@ +package com.getintent.test; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.getintent.interview.MonochromeScreen; +import com.getintent.interview.impl.MonochromeScreenImpl; + +public class MonochromeScreenTest { + + @Test + public void testInit() { + MonochromeScreen screen = new MonochromeScreenImpl(); + try{ + screen.init(0, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail("0 0 must throw IAE exception"); + } + } + try{ + screen.init(1, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail("1 0 must throw IAE exception"); + } + } + try{ + screen.init(-1, 1); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail("-1 1 must throw IAE exception"); + } + } + try{ + screen.init(-1, -1); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail("-1 -1 must throw IAE exception"); + } + } + screen.init(1, 1); + screen.init(100, 100); + } + + @Test + public void testDrawHorizontalLine(){ + MonochromeScreenImpl screen = createScreen(); + try{ + screen.drawHorizontalLine(0, 100, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + try{ + screen.drawHorizontalLine(-1, 1, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + try{ + screen.drawHorizontalLine(0, 0, 100); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + try{ + screen.drawHorizontalLine(10, 0, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + screen.drawHorizontalLine(0, 99, 99); + screen.drawHorizontalLine(99, 99, 99); + screen.drawHorizontalLine(0, 0, 0); + } + + @Test + public void testScreen(){ + MonochromeScreenImpl screen = createScreen(); + try{ + screen.getPoint(0, -1); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + try{ + screen.getPoint(-1, 0); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + try{ + screen.getPoint(100, 100); + } catch (Exception e){ + if (!(e instanceof IllegalArgumentException)){ + fail(e.getMessage()); + } + } + + assertEquals(0, screen.getPoint(0, 0)); + assertEquals(0, screen.getPoint(99, 99)); + + screen.drawHorizontalLine(0, 99, 99); + assertEquals(1, screen.getPoint(99, 99)); + assertEquals(1, screen.getPoint(45, 99)); + assertEquals(0, screen.getPoint(0, 0)); + assertEquals(0, screen.getPoint(45, 45)); + + screen = createScreen(); + screen.drawHorizontalLine(0, 0, 0); + assertEquals(1, screen.getPoint(0, 0)); + assertEquals(0, screen.getPoint(0, 1)); + + screen = createScreen(); + screen.drawHorizontalLine(99, 99, 99); + assertEquals(1, screen.getPoint(99, 99)); + assertEquals(0, screen.getPoint(0, 0)); + assertEquals(0, screen.getPoint(45, 99)); + } + + private MonochromeScreenImpl createScreen() { + MonochromeScreenImpl screen = new MonochromeScreenImpl(); + screen.init(100, 100); + return screen; + } + +}