java - Getting duplicated data in tests -
when run following test case:
@test(timeout=1000) public void shape_displaychar2_rot(){ string layout = "b....\n"+ ".....\n"+ "....b\n"+ ""; string expect = "shape z\n"+ "height: 5; width: 3; rotation: cw90\n"+ "..z\n"+ "...\n"+ "...\n"+ "...\n"+ "z..\n"+ ""; char newdc = 'z'; char dc = getdisplaychar(layout); shape shape = fitit.makeshape(layout,dc); shape.setdisplaychar(newdc); shape.rotatecw(); assertequals(newdc, shape.getdisplaychar()); string actual = shape.tostring(); assertequals(expect,actual); }
i following failure:
expected: shape z height: 5; width: 3; rotation: cw90 ..z ... ... ... z..
my actual code's result:
actual: shape z height: 5; width: 3; rotation: cw90 b.... ..... ....b ..z ... ... ... z..
the string layout
above test case rotated rotatecw()
90 degrees clockwise , changes character. question why getting:
b.... <<<<----- why getting ? ..... <<<<----- why getting ? ....b <<<<----- why getting ? ..z ... ... ... z..
instead of just:
..z ... ... ... z..
code:
import java.util.*;
public class createshape implements shape { private string layout; private int height; private int width; private char dc; private rotation initialpos; private char[][] shape; //private char[][] rotatedshape = new char[height][width];// = new char[shape.length][shape[0].length]; public createshape(int height, int width, char dc, char[][] charlayout, string layout) { this.height = height; this.width = width; this.dc = dc; this.shape = charlayout; this.layout = layout; initialpos = rotation.cw0; //nextpos = rotation.cw0; } public rotation getrotation() { return initialpos; } public int getheight() { return this.height; } public int getwidth() { return this.width; } public char getdisplaychar() { return dc; } public void setdisplaychar(char c) { this.dc = c; (int = 0; < shape.length; i++) { (int j = 0; j < shape[0].length; j++) { //if(shape[i][j] == '.') //this.newlayout += shape[i][j]; if (shape[i][j] != '.') { shape[i][j] = c; } } //this.newlayout+="\n"; } } public void rotatecw() { initialpos = initialpos.next(); int w = shape.length; int h = shape[0].length; char[][] swapped = new char[h][w]; (int = 0; < h; i++) { (int j = 0; j < w; j++) { swapped[i][j] = shape[w - j - 1][i]; layout += swapped[i][j]; } layout += "\n"; } height = swapped.length; width = swapped[0].length; } public boolean isfilledat(int row, int col) { if (row < 0 || row >= height || col < 0 || col >= width) { throw new fititexception("oops! out of bounds!"); } else { (int r = 0; r <= height; r++) { (int c = 0; c <= width; c++) { if (shape[row][col] == dc) { return true; } } } } return false; } public string tostring() { return "shape " + this.dc + "\n" + "height: " + this.height + ";" + " width: " + this.width + "; " + getrotation().tostring() + "\n" + this.layout; } }
class fitit
public class fitit { public static shape makeshape(string layout, char displaychar) { shape result; int height = 0; int width = 0; scanner data = new scanner(layout); while (data.hasnextline()) { string line = data.nextline(); width = line.length(); height++; } char[][] charlayout = new char[height][width]; scanner data2 = new scanner(layout); (int = 0; < height; i++) { string line = data2.nextline(); (int j = 0; j < width; j++) { if (line.charat(j) != '.') { charlayout[i][j] = displaychar; } if (line.charat(j) == '.') { charlayout[i][j] = line.charat(j); } } } data2.close(); result = new createshape(height, width, displaychar, charlayout, layout); return result; }
in rotatecw()
method, forget clear layout
. can add layout = ""
before loop , have try.
Comments
Post a Comment