Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/examplehub/basics/system/SystemExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.system;

public class SystemExample {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.examplehub.basics.time;

public class DateTimeFormatExample {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,101 @@
import org.junit.jupiter.api.Test;

class StringBuilderExampleTest {

@Test
void testToString() {
StringBuilder builder = new StringBuilder("hello123");
assertEquals("hello123", builder.toString());
}
@Test
void testCapacity() {
StringBuilder sb = new StringBuilder();
assertEquals(16, sb.capacity());

sb = new StringBuilder(20);
assertEquals(20, sb.capacity());

sb = new StringBuilder("abc123");
assertEquals(22, sb.capacity());
}

@Test
void testAppend() {
StringBuilder builder = new StringBuilder();
builder.append("Hello").append(",").append("World").append("!");
assertEquals("Hello,World!", builder.toString());
StringBuilder builder = new StringBuilder();
builder.append("Hello").append(",").append("World").append("!");
assertEquals("Hello,World!", builder.toString());
}

@Test
void testInsert() {
StringBuilder builder = new StringBuilder("abc");
builder.insert(0, "123");
assertEquals("123abc", builder.toString());
builder.insert(3, "45");
assertEquals("12345abc", builder.toString());
builder.insert(builder.length(), "!");
assertEquals("12345abc!", builder.toString());
}

@Test
void testDeleteCharAt() {
StringBuilder builder = new StringBuilder("hello123abc");
builder.deleteCharAt(0);
assertEquals("ello123abc", builder.toString());

//delete ello
for (int i = 1; i <= 4; i++) {
builder.deleteCharAt(0);
}
assertEquals("123abc", builder.toString());
builder.deleteCharAt(builder.length() - 1);
assertEquals("123ab", builder.toString());
}

@Test
void testInitCapacity() {
String firstStr = "123456789";
String secondStr = "987654321";
String thirdStr = "abcef";
void testDelete() {
StringBuilder builder = new StringBuilder("123abcABC");
builder.delete(0, 3); // delete "123"
assertEquals("abcABC", builder.toString());
builder.delete(3, builder.length()); // delete "ABC"
assertEquals("abc", builder.toString());
}

@Test
void testCharAt() {
StringBuilder builder = new StringBuilder("0123456789abc");
assertEquals('a', builder.charAt(10));

for (int i = 0; i <= 9; ++i) {
assertEquals('0' + i, builder.charAt(i));
}
}

@Test
void testSetCharAt() {
StringBuilder builder = new StringBuilder("abC123");
builder.setCharAt(2, 'c');
assertEquals("abc123", builder.toString());
}

StringBuilder builder =
new StringBuilder(firstStr.length() + secondStr.length() + thirdStr.length());
assertEquals(
"123456789987654321abcef",
builder.append(firstStr).append(secondStr).append(thirdStr).toString());
@Test
void testReplace() {
StringBuilder builder = new StringBuilder("abc123");
builder.replace(0, 3, "ABC");
assertEquals("ABC123", builder.toString());
}

@Test
void testIndexOf() {
StringBuilder builder = new StringBuilder("abc123abc");
assertEquals(1, builder.indexOf("bc"));
assertEquals(7, builder.indexOf("bc", 3));
}

@Test
void testLastIndexOf() {
StringBuilder builder = new StringBuilder("abc123abc");
assertEquals(7, builder.lastIndexOf("bc"));
assertEquals(6, builder.lastIndexOf("abc", 6));
}
}
19 changes: 19 additions & 0 deletions src/test/java/com/examplehub/basics/system/SystemExampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.examplehub.basics.system;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class SystemExampleTest {
@Test
void testCurrentTime() {
long startTime = System.currentTimeMillis();
int sum = 0;
for (int i = 1; i <= 100; ++i) {
sum += i;
}
assertEquals(5050, sum);
long endTime = System.currentTimeMillis();
assertTrue(endTime >= startTime);
}
}
34 changes: 32 additions & 2 deletions src/test/java/com/examplehub/basics/time/CalendarExampleTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package com.examplehub.basics.time;

import org.junit.jupiter.api.Test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CalendarExampleTest {

@Test
void testInit() {
Calendar calendar = Calendar.getInstance();
calendar.set(2099, Calendar.DECEMBER, 31, 23, 59, 59);
assertEquals("2099-12-31 23:59:59",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
}

@Test
void test() {
Calendar calendar = Calendar.getInstance();
Expand All @@ -30,6 +42,24 @@ void testSetCustomTime() {
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 59);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));

assertEquals("2088-09-25 16:33:59",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
}

@Test
void testAddField() {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, 2088);
calendar.set(Calendar.MONTH, 8);
calendar.set(Calendar.DATE, 25);
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 59);

calendar.add(Calendar.YEAR, 12);
assertEquals("2100-09-25 16:33:59",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
}
}
40 changes: 38 additions & 2 deletions src/test/java/com/examplehub/basics/time/DateExampleTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package com.examplehub.basics.time;

import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DateExampleTest {

@Test
void testInit() {
Date firstDate = new Date();
Date secondDate = new Date(System.currentTimeMillis());
assertEquals(firstDate, secondDate);
}

@Test
void testGetTime() {
Date date = new Date(1636200610202L);
assertEquals(1636200610202L, date.getTime());
}

@Test
void testSetTime() {
long time = System.currentTimeMillis();
Date date = new Date(time);
assertEquals(time, date.getTime());
}

@Test
void test() {
Date date = new Date();
Expand All @@ -19,6 +45,16 @@ void test() {
@Test
void testSimpleDateForMate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()));
long time = 1636201275057L;
Date date = new Date(time);
String formatDate = format.format(date);
assertTrue("2021-11-06 20:21:15".equals(formatDate) || "2021-11-06 12:21:15".equals(formatDate));
}

@Test
void testParse() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long time = format.parse("2021-11-06 20:21:15").getTime();
// assertEquals(1636201275057L, time); TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.examplehub.basics.time;

import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.*;

class DateTimeFormatExampleTest {
@Test
void test() {
LocalDateTime localDateTime = LocalDateTime.of(2099, Month.DECEMBER, 30, 23, 59, 59);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
assertEquals("2099-12-30 23:59:59", formatter.format(localDateTime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class LocalDateTimeExampleTest {
@Test
void test() {
void testInit() {
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
Expand All @@ -16,25 +17,26 @@ void test() {
System.out.println("localDateTime: " + localDateTime);
}

@Test
void test2() {
LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
System.out.println("localDate: " + localDate);
System.out.println("localTime: " + localTime);
System.out.println("localDateTime: " + localDateTime);
}

@Test
void test3() {
LocalDate d2 = LocalDate.of(2019, 11, 30);
LocalTime t2 = LocalTime.of(15, 16, 17);
LocalDateTime dt2 = LocalDateTime.of(2019, 11, 30, 15, 16, 17);
LocalDateTime dt3 = LocalDateTime.of(d2, t2);
System.out.println(d2);
System.out.println(t2);
System.out.println(dt2);
System.out.println(dt3);
void testOf() {
LocalDate localDate = LocalDate.of(2019, 11, 30);
LocalTime localTime = LocalTime.of(15, 16, 17);
LocalDateTime localDateTime = LocalDateTime.of(2019, 11, 30, 15, 16, 17);

assertEquals(2019, localDate.getYear());
assertEquals(11, localDate.getMonth().getValue());
assertEquals(30, localDate.getDayOfMonth());

assertEquals(15, localTime.getHour());
assertEquals(16, localTime.getMinute());
assertEquals(17, localTime.getSecond());

assertEquals(2019, localDateTime.getYear());
assertEquals(11, localDateTime.getMonthValue());
assertEquals(30, localDateTime.getDayOfMonth());
assertEquals(15, localDateTime.getHour());
assertEquals(16, localDateTime.getMinute());
assertEquals(17, localDateTime.getSecond());
}
}