package test01;
public class TM1 {
public String tm1(int x,int y) {
if(x<=0 || y<=0) {
return "输入不符合要求";
}else if(x == y) {
return "可以构建圆形或正方形";
}else if(Math.abs(x-y)>2 && Math.abs(x-y)<=5) {
return "可以构建椭圆";
}else if(Math.abs(x-y)>0 && Math.abs(x-y)<=2) {
return "可以构建长方形";
}else if(Math.abs(x-y)>5) {
return "可以构建矩形";
}
return null;
}
}
package test01;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class TM1Test {
private TM1 tm1 =new TM1();
@Test
public void Test01() {
assertEquals(tm1.tm1(0, 0),"输入不符合要求");
}
@Test
public void Test02() {
assertEquals(tm1.tm1(1, 1),"可以构建圆形或正方形");
}
@Test
public void Test03() {
assertEquals(tm1.tm1(6, 3),"可以构建椭圆");
}
@Test
public void Test04() {
assertEquals(tm1.tm1(3, 1),"可以构建长方形");
}
@Test
public void Test05() {
assertEquals(tm1.tm1(10, 1),"可以构建矩形");
}
}
package text02;
public class TM2 {
public String tm2(char first,char second) {
if (first == 'M'){
return "Monday";
}else if (first == 'W'){
return "Wednesday";
}else if (first == 'F'){
return "Friday";
}else if (first == 'T'){
if (second == 'u'){
return "Tuesday";
}else if (second == 'h'){
return "Thursday";
}
}else if (first == 'S'){
if (second == 'a'){
return "Saturday";
}else if (second == 'u'){
return "Sunday";
}
}
return "UnKnow";
}
}
package text02;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class TM2Test {
private TM2 tm2 = new TM2();
@Test
public void Test01(){
assertEquals(tm2.tm2('M','u'),"Monday");
}
@Test
public void Test02(){
assertEquals(tm2.tm2('W','u'),"Wednesday");
}
@Test
public void Test03(){
assertEquals(tm2.tm2('F','u'),"Friday");
}
@Test
public void Test04(){
assertEquals(tm2.tm2('T','h'),"Thursday");
}
@Test
public void Test05(){
assertEquals(tm2.tm2('T','u'),"Tuesday");
}
@Test
public void Test06(){
assertEquals(tm2.tm2('S','a'),"Saturday");
}
@Test
public void Test07(){
assertEquals(tm2.tm2('S','u'),"Sunday");
}
}
package test03;
public class TM3 {
public String tm3(String username,String password) {
boolean usernameCheck = username.matches("[a-zA-Z]{8}");
boolean pwdCheck = password.matches("[0-9]{6,}");
if(usernameCheck && pwdCheck) {
return "注册成功";
}else if(!usernameCheck && pwdCheck) {
return "用户名不符合要求";
}else if(usernameCheck && !pwdCheck) {
return "密码不符合要求";
}else {
return "用户名和密码不符合要求";
}
}
}
package test03;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
public class TM3Test {
private TM3 tm3 = new TM3();
@Test
public void Test01() {
assertThat(tm3.tm3("adminadm", "123456"),containsString("注册成功"));
}
@Test
public void Test02() {
assertThat(tm3.tm3("admin", "123456"),containsString("用户名不符合要求"));
}
@Test
public void Test03() {
assertThat(tm3.tm3("adminadm", "126"),containsString("密码不符合要求"));
}
@Test
public void Test04() {
assertThat(tm3.tm3("admi", "1236"),containsString("用户名和密码不符合要求"));
}
}
package test04;
public class TM4 {
public String tm4(String str) {
if (str.matches("ab.*")){
return "替换前缀后的字符串为:".concat(str.replaceFirst("ab","ef"));
}else if (str.matches(".*cd")){
return "替换cd后的字符串为:".concat(str.substring(0,str.length()-2)+"gh");
}else {
return "大写字符的字符串是".concat(str.toUpperCase());
}
}
}
package test04;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class TM4Test {
private TM4 tm4 = new TM4();
private String str;
private String result;
public TM4Test(String str,String result){
this.str = str;
this.result = result;
}
@Parameterized.Parameters
public static Collection parm(){
return Arrays.asList(new Object[][]{
{"abcdef","替换前缀后的字符串为:efcdef"},
{"cdcdcd","替换cd后的字符串为:cdcdgh"},
{"wrwr","大写字符的字符串是WRWR"}
});
}
@Test
public void Test01(){
assertEquals(result,tm4.tm4(str));
}
}
package test05;
public class TM5 {
public static double tm5(int profit) {
double jiangjin = 0;
if (profit <= 100) {
jiangjin += profit * 0.10;
} else if (profit <= 200) {
jiangjin += 100 * 0.10 + (profit - 100) * 0.075;
} else if (profit <= 400) {
jiangjin += 100 * 0.10 + 100 * 0.075 + (profit - 200) * 0.05;
} else if (profit <= 600) {
jiangjin += 100 * 0.10 + 100 * 0.075 + 200 * 0.05 + (profit - 400) * 0.03;
} else if (profit <= 1000) {
jiangjin += 100 * 0.10 + 100 * 0.075 + 200 * 0.05 + 200 * 0.03 + (profit - 600) * 0.015;
} else {
jiangjin += 100 * 0.10 + 100 * 0.075 + 200 * 0.05 + 200 * 0.03 + 400 * 0.015 + (profit - 1000) * 0.01;
}
return jiangjin;
}
public static String getTotalBonus(int profit) {
double jiangjin = tm5(profit);
return String.format("总提成:%.2f万", jiangjin);
}
}
package test05;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import org.junit.Test;
public class TM5Test {
@Test
public void test01() {
assertThat(TM5.getTotalBonus(100), is("总提成:10.00万"));
}
@Test
public void test02() {
assertThat(TM5.getTotalBonus(150), is("总提成:13.75万"));
}
@Test
public void test30() {
assertThat(TM5.getTotalBonus(300), is("总提成:22.50万"));
}
@Test
public void test04() {
assertThat(TM5.getTotalBonus(500), is("总提成:30.50万"));
}
@Test
public void test05() {
assertThat(TM5.getTotalBonus(800), is("总提成:36.50万"));
}
@Test
public void test06() {
assertThat(TM5.getTotalBonus(1200), is("总提成:41.50万"));
}
}
package text06;
public class TM6 {
public static String tm6(String username, String password) {
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
return "用户名或密码不能为空";
}
if ("admin".equals(username) && "123".equals(password)) {
return "登录成功";
} else if (!"admin".equals(username) && "123".equals(password)) {
return "请输入正确的用户名";
} else if ("admin".equals(username) && !"123".equals(password)) {
return "请输入正确的密码";
} else {
return "请输入正确的用户名和密码";
}
}
}
package text06;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
public class TM6Test {
private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("", "", "用户名或密码不能为空"), // 测试用户名和密码为空
Arguments.of("admin", "123", "登录成功"), // 测试正确的用户名和密码
Arguments.of("user", "123", "请输入正确的用户名"), // 测试用户名错误,密码正确
Arguments.of("admin", "456", "请输入正确的密码"), // 测试用户名正确,密码错误
Arguments.of("user", "456", "请输入正确的用户名和密码") // 测试用户名和密码均错误
);
}
@ParameterizedTest
@MethodSource("testCases")
public void testLoginProcess(String username, String password, String expectedResult) {
String actualResult = TM6.tm6(username, password);
assertEquals(expectedResult, actualResult);
}
}
package text07;
public class TM7 {
public String tm7(int x, int y) {
if (x >= 80 && y >= 60) {
if(x >= 90 || y >= 90) {
return "NULL";
} else {
return "a=2";
}
} else {
if(x<=70 || y<=70) {
return "a=3";
}else {
return "a=4";
}
}
}
}
package text07;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class TM7Test {
private TM7 tm7 = new TM7();
@Test
public void test0l() {
String result = tm7.tm7(90, 65); // x >= 90, y >= 60
assertThat(result, equalTo("NULL"));
}
@Test
public void test02() {
String result = tm7.tm7(85, 65); // 80 <= x < 90, 60 <= y < 90
assertThat(result, equalTo("a=2"));
}
@Test
public void test03() {
String result = tm7.tm7(70, 50); // x <= 70, y <= 60
assertThat(result, equalTo("a=3"));
}
@Test
public void test04() {
String result = tm7.tm7(75, 75); // 70 < x < 80, 70 < y < 80
assertThat(result, equalTo("a=4"));
}
}
package text08;
public class TM8 {
public String tm8(int year, int month) {
if (month < 1 || month > 12) {
return "月份输入不正确。";
}
int days;
switch (month) {
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
if (isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 31;
}
return year + "年" + month + "月份的天数是" + days + "天。";
}
private boolean isLeapYear(int year) {
// 世纪闰年条件:被400整除
if (year % 400 == 0) {
return true;
}
// 普通闰年条件:被4整除但不被100整除
if (year % 4 == 0 && year % 100 != 0) {
return true;
}
return false;
}
}
package text08;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class TM8Test {
private TM8 tm8 = new TM8();
@Test
public void test01() {
String result = tm8.tm8(2023, 13); // 无效的月份
assertThat(result, equalTo("月份输入不正确。"));
}
@Test
public void test02() {
String result = tm8.tm8(2023, 2); // 非闰年2月
assertThat(result, equalTo("2023年2月份的天数是28天。"));
}
@Test
public void test03() {
String result = tm8.tm8(2024, 2); // 普通闰年2月
assertThat(result, equalTo("2024年2月份的天数是29天。"));
}
@Test
public void test04() {
String result = tm8.tm8(2000, 2); // 世纪闰年2月
assertThat(result, equalTo("2000年2月份的天数是29天。"));
}
@Test
public void test05() {
String result = tm8.tm8(2023, 4); // 4月有30天
assertThat(result, equalTo("2023年4月份的天数是30天。"));
}
}
package text09;
public class TM9 {
public String tm9(String name, String phone, String address) {
boolean isPhoneInvalid = !phone.matches("\\d{11}");
boolean isAddressInvalid = !address.matches("^[A-Za-z][A-Za-z0-9]{0,9}$");
if (isPhoneInvalid && isAddressInvalid) {
return "手机号和地址不符合要求";
} else if (isPhoneInvalid) {
return "手机号不符合要求";
} else if (isAddressInvalid) {
return "地址不符合要求";
}
return "OK";
}
}
package text09;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class TM9Test {
private TM9 tm9 = new TM9();
@Test
public void test01() {
String result = tm9.tm9("Alice", "12345678901", "Address1");
assertThat(result, equalTo("OK"));
}
@Test
public void test02() {
String result = tm9.tm9("Alice", "12345", "Address1"); // 手机号不符合要求
assertThat(result, equalTo("手机号不符合要求"));
}
@Test
public void test03() {
String result = tm9.tm9("Alice", "12345678901", "1Address"); // 地址不符合要求
assertThat(result, equalTo("地址不符合要求"));
}
@Test
public void test04() {
String result = tm9.tm9("Alice", "12345", "1Address"); // 手机号和地址均不符合要求
assertThat(result, equalTo("手机号和地址不符合要求"));
}
}
package text10;
public class TM10 {
public String tm10(int x,int y) {
double j;
if(x>60 && y<35) {
j=10*x-y;
}else if(x==25 && y>50) {
j=y*Math.log(x+10);
}else {
j=(x-y)*(100000%7);
}
return "NULL";
}
}
package text10;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class TM10Test {
private TM10 tm10 = new TM10();
@Test
public void testTm1_case1() {
// 测试条件: x > 60 && y < 35
String result = tm10.tm10(70, 30);
assertTrue(result.startsWith("NULL"));
}
@Test
public void testTm1_case2() {
// 测试条件: x == 25 && y > 50
String result = tm10.tm10(25, 51);
assertTrue(result.startsWith("NULL"));
}
@Test
public void testTm1_case3() {
// 测试其他情况
String result = tm10.tm10(40, 20);
assertTrue(result.startsWith("NULL"));
}
}
package text11;
public class TM11 {
public String tm11(String email, String password) {
if (!email.matches("^[a-zA-Z]{5}@(163|126)\\.(com|com\\.cn)$")) {
return "邮箱地址不符合要求";
}else if (!password.matches("\\d{6,}")) {
return "密码不符合要求";
}else if(!email.matches("^[a-zA-Z]{5}@(163|126)\\.(com|com\\.cn)$") && !password.matches("\\d{6,}") ) {
return "邮箱地址和密码不符合要求";
}
return "信息正确";
}
}
package text11;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import org.junit.Test;
public class TM11Test {
private TM11 tm11 = new TM11();
@Test
public void testValidRegistration() {
String result = tm11.tm11("abcde@163.com", "123456");
assertThat(result, equalTo("信息正确"));
}
@Test
public void testInvalidEmail() {
String result = tm11.tm11("abcd@163.com", "123456"); // 登录名不符合要求
assertThat(result, equalTo("邮箱地址不符合要求"));
}
@Test
public void testInvalidPassword() {
String result = tm11.tm11("abcde@126.com", "12345"); // 密码不符合要求
assertThat(result, equalTo("密码不符合要求"));
}
@Test
public void testInvalidEmailAndPassword() {
String result = tm11.tm11("abcd@xyz.com", "123"); // 邮箱和密码均不符合要求
assertThat(result, equalTo("邮箱地址不符合要求"));
}
}
package text12;
public class TM12 {
public String tm12(int a, int b, int c) {
if (a < 1 || a > 10 || b < 1 || b > 10 || c < 1 || c > 10) {
return "输入边值不在范围内";
}else if (a + b <= c || a + c <= b || b + c <= a) {
return "输入边值不能组成三角形";
}else if (a == b && b == c) {
return "能组成等边三角形";
}else if (a == b || b == c || a == c) {
return "能组成等腰三角形";
}else {
return "能组成普通三角形";
}
}
}
package text12;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TM12Test {
private TM12 tm12 = new TM12();
@Test
public void test01() {
// 测试边值不在范围内
String result = tm12.tm12(0, 5, 3);
assertEquals("输入边值不在范围内", result);
}
@Test
public void test02() {
// 测试不能组成三角形
String result = tm12.tm12(1, 2, 3);
assertEquals("输入边值不能组成三角形", result);
}
@Test
public void test03() {
// 测试等边三角形
String result = tm12.tm12(5, 5, 5);
assertEquals("能组成等边三角形", result);
}
@Test
public void test04() {
// 测试等腰三角形
String result = tm12.tm12(5, 5, 3);
assertEquals("能组成等腰三角形", result);
}
@Test
public void test05() {
// 测试普通三角形
String result = tm12.tm12(3, 4, 5);
assertEquals("能组成普通三角形", result);
}
}
package text13;
public class TM13 {
public String tm13(int a, int b) {
int x;
// 判断条件
if ((a > 9) && (b > 5)) {
x = a * a * b;
return "x=a*a*b的值:" + x;
} else if ((a < 0) && (b < 0)) {
x = a - b;
return "x=a-b的值:" + x;
} else {
x = a + b;
return "x=a+b的值:" + x;
}
}
}
package text13;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class TM13Test {
private TM13 tm13 = new TM13();
@Test
public void testAnalyze_aGreaterThan9_bGreaterThan5() {
// 测试条件 (a > 9) && (b > 5)
String result = tm13.tm13(10, 6);
assertTrue(result.startsWith("x=a*a*b的值:"));
}
@Test
public void testAnalyze_aLessThan0_bLessThan0() {
// 测试条件 (a < 0) && (b < 0)
String result = tm13.tm13(-1, -1);
assertTrue(result.startsWith("x=a-b的值:"));
}
@Test
public void testAnalyze_otherConditions() {
// 测试不满足上述条件的情况
String result = tm13.tm13(5, 3);
assertTrue(result.startsWith("x=a+b的值:"));
}
}
package text14;
public class TM14 {
public int tm14(int iRecordNum, int iType) {
int x = 0;
int y = 0;
if (iRecordNum == 0 && iType == 0) {
return x;
} else if (iType == 1) {
x = y + 2;
return x;
} else {
x = y + 10;
x = y + 20;
return x;
}
}
}
package text14;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class TM14Test {
private TM14 tm14 = new TM14();
@Test
public void testCalculate_iRecordNumIsZero() {
// 测试条件: iRecordNum == 0
int result = tm14.tm14(0, 0);
assertEquals(0, result); // 期望结果0
}
@Test
public void testCalculate_iTypeIsOne() {
// 测试条件: iType == 1
int result = tm14.tm14(1, 1);
assertEquals(2, result); // 期望结果2
}
@Test
public void testCalculate_otherConditions() {
// 测试其他条件
int result = tm14.tm14(1, 0);
assertEquals(20, result); // 期望结果20
}
}