Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Тестовое задание #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Java
*.class
*.jar

# Maven
target/

# Eclipse
.classpath
.project
.settings
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>


<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
135 changes: 135 additions & 0 deletions src/test/java/com/getintent/test/MonochromeScreenTest.java
Original file line number Diff line number Diff line change
@@ -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;
}

}