测试用例失败,我在做什么错? [英] Failed test case, what am I doing wrong?

查看:56
本文介绍了测试用例失败,我在做什么错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使它起作用,但是我不确定自己要去哪里。稍微考虑一下测试用例就可以了。关于我在做什么的一些事情。该列表必须包含一个或多个行。如果没有至少一行,则抛出IllegalArgumentException。每行都有某种字符串,然后是一个制表符,然后是一个双精度字,然后是一个换行符。如果有任何行不遵循此模式,则也会抛出IllegalArgumentException。理想情况下,如果您创建了任意数量的列表,这应该行得通,因此这就是我尝试这种方式的原因

I've been trying to get this to work, but I'm not quite sure where I'm going wrong. It'll make sense with the test case in a bit. Some things about what I'm doing. The list must have one or more lines. If there's not at least one line, an IllegalArgumentException is thrown. Each line has some kind of String, then a tab character, then a double, then a newline. If any lines don't follow this pattern, then an IllegalArgumentException would also be thrown. This is ideally supposed to work if you made any number of lists, so that's why I attempted it this way

基本上,该程序应该获得一个列表,说些什么像...

Basically, the program is supposed to get a list, say something like...

California   6
Nevada      11
California   1
California  14
Arizona     21
Utah         2
California   7
Utah        10
Nevada       3
Utah         2

,并且应返回以下内容:

and should return something like this:

California {6, 1, 14, 7} 
Arizona    {21}
Utah       {2, 10, 2}
Nevada     {11, 3}

请注意,在本示例中,我仅使用了四个。我正在尝试使其能够与任意数量的列表一起使用。
这是我的代码...

Note that I only used four for the sake of the example. I'm trying to get it so that it's work with any number of lists. Here is my code...

 public static TreeMap<String, ArrayList<Double>> readTable (Scanner dataSource)
    {

        ArrayList<String> dataFromFile = new ArrayList<String>();

        while(dataSource.hasNext()){
            dataFromFile.add(dataSource.next());
       }

        //Populate TreeMap
        ArrayList<Double> statePopData = new ArrayList<>();
        TreeMap<String, ArrayList<Double>> map = new TreeMap<>();

        for (int i = 0; i < dataFromFile.size(); i++) {

            boolean isDouble;
            String state = "";


          try {
              Double.parseDouble(dataFromFile.get(i));
              isDouble = true;
          } catch (NumberFormatException | NullPointerException nfe) {
              isDouble = false;
          }


          if(isDouble) {
              statePopData.add(Double.parseDouble(dataFromFile.get(i)));
          } else { //means its a string

              statePopData.clear();
              state = dataFromFile.get(i);
          } 
          if (statePopData.isEmpty()) {
              map.put(state, statePopData);
          }

        } return map;
}

我在列表中所有其他州的所有产品都获得了相同的价值我对为什么会这样感到困惑。

I keep getting one same value for everything for all the other states in the list and I'm confused as to why this is happening.

这是我之前提到的测试用例...

Here's the test case I mentioned before...

@Test
    public void testReadTable ()
    {
        try (Scanner scn = new Scanner(
                "Utah\t10\nNevada\t3\nUtah\t2\nCalifornia\t14\nArizona\t21\nUtah\t2\nCalifornia\t7\nCalifornia\t6\nNevada\t11\nCalifornia\t1\n"))
        {
            TreeMap<String, ArrayList<Double>> actual = GraphingMethods.readTable(scn);

            TreeMap<String, ArrayList<Double>> expected = new TreeMap<>();
            ArrayList<Double> azList = new ArrayList<>();
            azList.add(21.0);
            expected.put("Arizona", azList);

            ArrayList<Double> caList = new ArrayList<>();
            caList.add(6.0);
            caList.add(1.0);
            caList.add(14.0);
            caList.add(7.0);
            expected.put("California", caList); 

            ArrayList<Double> nvList = new ArrayList<>();
            nvList.add(11.0);
            nvList.add(3.0);
            expected.put("Nevada", nvList);     

            ArrayList<Double> utList = new ArrayList<>();
            utList.add(2.0);
            utList.add(10.0);
            utList.add(2.0);
            expected.put("Utah", utList);

            assertEquals(expected, actual);
        }
    }

我是一名新程序员,所以任何建议都会非常感谢!我很想知道自己在做什么错,以便我可以学习更多。 :)

I'm a new programmer, so any advice would be greatly appreciated! I'd love to know what I'm doing wrong so that I can learn more. :)

推荐答案

我认为主要问题是您正在修改并插入相同的 ArrayList 实例。没有健壮性检查的简单版本可能是:

I think the main problem is you are modifying and inserting the same ArrayList instance for each state. A simple version, without robustness checking might be:

    TreeMap<String, ArrayList<Double>> map = new TreeMap<>();
    while (dataSource.hasNext()) {
        String state = dataSource.next();
        Double d = Double.parseDouble(dataSource.next());
        map.computeIfAbsent(state, k -> new ArrayList<>()).add(d);
    }
    return map;

computeIfAbsent 允许您添加新的 ArrayList 当您看到一个新状态(Java 8+)时。

The computeIfAbsent allows you to add a new ArrayList when you see a new state (Java 8+).

另一个问题是 assertEquals 。由于期望的数字列表数据的顺序与实际顺序不同,因此 ArrayLists 不相等。您可以像这样验证键和值:

Another issue is the assertEquals. Since the expected number list data is in a different order than the actual, the ArrayLists are not equal. You could verify the keys and values like this:

    assertEquals(expected.keySet(), actual.keySet());
    expected.forEach((state, list) -> {
        Collections.sort(list);
        Collections.sort(actual.get(state));
        assertEquals(list, actual.get(state));
    });

这篇关于测试用例失败,我在做什么错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆