EE308 LAB11

Luv丶 2021-12-17 12:16:41
The Link Your Class
https://bbs.csdn.net/forums/MUEE308FZ?category=0

The Link of Requirement of This Assignment

https://bbs.csdn.net/topics/603748107
The Aim of This AssignmentSoftware Testing-Junit Test
MUID&FZUID19105045& 831901126
Name

Junye Wu

 

Preparation

Learn related knowledge about Junit Test for Java and Unittest for python. You can finish the following assignment or test Your Project.

 

Task

  1. Here are Junit Test task and Unittest task for choose. If you are good at Java , we suggest you choose the first one, if you are good at Python, then, the latter one.
  2. Follow the requests and tips, complete the test task .

 

Requirements

  1. You should be familiar with Junit Test or Unittest and the usage of these tools.Such as ‘assert, suite, timeout’, check problems for detail.
  2. Fix the missing codes according to requirement for every problem.

 

Junit assert

import static org.junit.Assert.*;
import org.junit.Test;

public class AssertionsTest {
String obj1 = "junit";
String obj2 = "junit";
String obj3 = "test";
String obj4 = "test";
String obj5 = null;
int var1 = 1;
int var2 = 2;
int[] arithmetic1 = { 1, 2, 3 };
int[] arithmetic2 = { 1, 2, 3 };

@Test
public void test() {
// add assert test code between Begin and End, no other change allowed     
    /***********************Begin**************************/                      
    assertEquals(obj1,obj2);
    assertNull(obj5);                                                                                                      
    /************************End***************************/
    }
}

junit time test

import org.junit.Test;
public class TestTimeOut {

// Fix timeout assert in Test function below. Test fail if running 1000ms longer /***********************Begin**************************/                    @Test(timeout=1000)
    public void test() {
        while(true){
            break;
        }
    }
/************************End***************************/ }

 junit parameterized test

import static org.junit.Assert.assertEquals; // static import
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import step1.Calculator;
/**
* JUnit4 parameterized test
*/
@RunWith(Parameterized.class)
    public class ParameterTest {
    private int input11;
    private int input22;
    private int expected;
    public ParameterTest(int input11, int input22, int expected){
    this.input11 = input11;
    this.input22 = input22;
    this.expected = expected;
}
@Parameters
public static Collection prepareData(){
/**
*the type of the two-dimension array must be Object.
*data in the two-dimension array is ready of test sub() in Calculator
*  every  element  in  the  two-dimension  array  should  corresponds  to  position  of  parameters  in construct method ParameterTest
*let the third element equals the first subtract the second element according to parameters’ postion
*fix missing codes under ‘Begin’ and above ‘End’ ,pass 4 groups of parameters to test sub method in Calculator is right or not
*tip:only two lines of codes
*/                                                                                                                                                                                  /*********************************Begin********************************************/     /**********************************End********************************************/
}
@Test
public void testSub(){
    Calculator cal = new Calculator();
    assertEquals(cal.sub(input11, input22), expected);
    }
}
// Calculator.java Junit Parameterized Test
/**
* Mathematical Calculation  subtract
*/
public class Calculator {
    public int sub(int a, int b) {
        return a - b;
    }
}

 junit exception test

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import step2.Person;



public class JunitException {

/**
*add a  line of annotation  in  Begin/End ,check the age of Person Object  is legal or  not.
*throw IllegalArgumentException exception
*/

/***********************************Begin***********************************/

/************************************End************************************/
public void checkage() {
    Person person = new Person();
    person.setAge(-1);
    }
}
//Person.java
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
    if (age < 0 ) {
        throw new IllegalArgumentException("age is invalid");
    }
    this.age = age;
    }
}

 junit suite test

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import step3.Calculate;
import step3.CalculateTest;
import step3.Car;
import step3.CarTest;

/*
add two lines of annotations. Implement Suite Test of CalculateTest and CarTest Suite Test codes must next to Class SuiteTest, no shift allowed!
*/                                                                                                                                         //************************************************************** 
public class SuiteTest {
}
//Calculate.java
public class Calculate {
    public int add(int a, int b) {
        return a + b;
    }
}

//CalculateTest.java
public class CalculateTest {
    Calculate calculate;
    @Before
    public void setUp() throws Exception {
        calculate = new Calculate();
    }
@Test
    public void testAdd() {
        int result = calculate.add(12, 12);
        assertEquals(24, result);
    }
}

//CarTest.java
public class CarTest {
    Car car;
@Before
    public void setUp() throws Exception {
        car = new Car();
}
@Test
    public void testGetWheels() {
        int result = car.getWheels();
        assertEquals(4, result);
    }
}

//Car.java
public class Car {
    public int getWheels() {
        return 4;
    }
}

 

...全文
237 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
代码转载自:https://pan.quark.cn/s/a4b39357ea24 自动驾驶是当前科技界高度关注的研究方向,其研究范畴涵盖了人工智能、计算机视觉、机器学习以及传感器融合等多个重要学科。这份题为"自动驾驶论文"的资料,无疑为人们提供了一个深入了解这一复杂系统的机会。以下是基于其标题和描述所呈现的一些关键知识点的详细阐述: 1. **自动驾驶技术**:自动驾驶指的是车辆在无需人类驾驶员参与的情况下,借助各类传感器和智能算法来感知周围环境,制定行驶路线,并执行驾驶行为。这种技术的核心宗旨在于提升交通安全性、缓解交通压力,并改善出行体验。 2. **Python编程语言**:Python是一种在数据处理和科学计算领域得到广泛应用的高级编程语言,由于其语法简洁且拥有丰富的库支持,经常被应用于自动驾驶领域的数据处理、模型构建和系统集成。 3. **源代码分析**:论文中提供的源代码可能包含了自动驾驶算法的实现细节,可能涵盖环境感知模块(包括图像处理、激光雷达数据解读)、决策模块(涉及路径规划、行为分析)、控制模块(涵盖车辆动力学建模、控制策略设计)等,这些代码可作为学习和研究自动驾驶算法的实践范例。 4. **计算机视觉**:自动驾驶系统中的计算机视觉技术主要用于识别道路标识、行人、其他车辆等,这通常涉及图像分类、目标识别和语义分割等任务,常见的技术包括卷积神经网络(CNN)、区域提议网络(RPN)、YOLO、Faster R-CNN等。 5. **机器学习**:机器学习是自动驾驶技术的核心,用于训练模型以理解和预测复杂的驾驶情境。深度学习,特别是深度强化学习,在决策制定和控制策略方面已取得显著进展。 6. **传感器融合**:自动驾驶汽车通常装备多种传...

183

社区成员

发帖
与我相关
我的任务
社区描述
福州大学 梅努斯国际工程学院 软件工程 教学
软件工程 高校
社区管理员
  • 单步调试
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧