java - 比较 JUnitTesting 中的 HashMap
问题描述
我必须:
- 编写一个函数,该函数接受一个字符串数组/列表,计算每个字符串的出现次数,然后返回一个包含每个唯一字符串及其出现次数的数据结构。例如,给定一个包含以下内容的输入:
[“苹果”、“蝙蝠”、“苹果”、“汽车”]
该函数应返回一个数据结构,其中“apple”的计数为 2,“bat”的计数为 1,“car”的计数为 1。
- 为我的解决方案编写单元测试覆盖率。涵盖确保正/负功能正确的排列。同样,涵盖有意义的边缘情况,以确保正确的功能并确保代码执行没有错误。
现在,我已经完成了任务一,这里是代码:
package Testing;
import java.util.*;
class CountString{
public static void main(String []args){
List strings = Arrays.asList("appLe","b at","APPle","car!");
Map<String,Integer> datastructure = countStrings(strings);
for (Map.Entry<String, Integer> entry : datastructure.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
public static Map<String,Integer>countStrings(List<String> words){
Map<String,Integer> map = new HashMap<>();
List<String>newList = new ArrayList<>();
String regex = "abcdefghijklmnopqrstuvwxyz";
regex+=regex.toUpperCase();
String w;
for(String s:words){
w="";
for(int i=0;i<s.length();i++){
if(regex.contains(s.charAt(i)+"")){
w+=s.charAt(i)+"";
}
}
newList.add(w.toLowerCase());
}
String countedWords = "";
int count;
for(int i=0;i<newList.size();i++){
count=1;
if(!countedWords.contains(newList.get(i))){
for(int j=i+1;j<newList.size();j++){
if(newList.get(i).equals(newList.get(j))){
count++;
}
}
countedWords+=newList.get(i)+"";
map.put(newList.get(i),count);
}
}
return map;
}
}
现在我必须设计这个的单元测试,但我在比较 hashmap 时有点困惑,请帮助我,
这是我所做的一个小尝试:
package Testing;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class JUnitTest {
@Test
void test() {
CountString test = new CountString();
List strings = Arrays.asList("appLe","b at","APPle","car!");
Map<String,Integer> output = test.countStrings(strings);
for(Map.Entry<String, Integer> entry : output.entrySet()) {
assertEquals(expecteds, output);
}
}
}
我不知道 assertEqual 函数在 Map 中是如何工作的。
解决方案
我已经尝试过自己,这个解决方案对我有用!
package Testing;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class JUnitTest {
@Test
void test() {
CountString test = new CountString();
List strings = Arrays.asList("appLe","b at","APPle","car!","truck!","tr UcK2");
Map<String,Integer> output = test.countStrings(strings);
Map<String,Integer> expecteds = new HashMap<>();
expecteds.put("apple", 2);
expecteds.put("bat", 1);
expecteds.put("car", 1);
expecteds.put("truck", 2);
for(Map.Entry<String, Integer> entry : output.entrySet()) {
assertEquals(expecteds.get(entry.getKey()), output.get(entry.getKey()));
}
}
}
推荐阅读
- javascript - 如何在 Vite 和 Vue 3 中使用 Webassembly/wasm 模块?
- paypal - Braintree Google Pay 沙盒打开实时域
- javascript - 使用矩形移动绝对定位调整图标大小
- sql - SQL查询以获取包含所有B列的A列
- graphviz - 我可以在同一个节点上放置多个箭头吗?(图形化)
- scala - 从时间戳获取精确的毫秒数 - Spark Scala
- java - 即使授予读/写权限,也无法从外部存储读取或创建文件
- algorithm - 为什么我们将数组的数量与中位数进行比较,而不是在 CSES 的长度问题中的平均值(下面提到的问题)?
- java - 嵌套类类型参数的注释方法有什么区别?
- python-3.x - 如何使用相机矩阵在图像中以毫米为单位查找点的位置?