简体   繁体   English

在测试中获取重复数据

[英]Getting duplicated data in tests

When I run the 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 get the 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 from the above test case is rotated by rotateCW() by 90 degrees ClockWise and also changes its character. 上面测试用例的String layout通过rotateCW()旋转90度ClockWise并且还改变其角色。 My question is why am I getting: 我的问题是为什么我得到:

b....    <<<<----- WHY AM I GETTING THIS ?
.....    <<<<----- WHY AM I GETTING THIS ?
....b    <<<<----- WHY AM I GETTING THIS ?
..z
...
...
...
z..

instead of just: 而不只是:

    ..z
    ...
    ...
    ...
    z..

Code: 码:

import java.util.*; 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;
        for (int i = 0; i < shape.length; i++) {
            for (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];
        for (int i = 0; i < h; i++) {
            for (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 {
            for (int r = 0; r <= height; r++) {
                for (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 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);
        for (int i = 0; i < height; i++) {
            String line = data2.nextLine();
            for (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 your rotateCW() method, you forget to clear the layout . rotateCW()方法中,您忘记清除layout You can add layout = "" before the for loop and have a try. 您可以在for循环之前添加layout = ""并尝试一下。

When you rotate the text, you are appending the rotated lines to the current layout. 旋转文本时,您将旋转的线条附加到当前布局。

You are not starting from a fresh String . 你不是从一个新的String So your original layout is preserved in the result. 因此,您的原始布局将保留在结果中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM