/
eikoshkarova
/
homework3
Обзор
Документация
Войти
/
eikoshkarova
/
homework3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
feature/CustomArrayImpl
src/test/java/CustomArrayImplTest.java
251 строка
8 KB
eikoshkarova
CustomArrayImplTest.java created
28 окт 2025, 22:59
28 окт 2025, 22:59
0ea6787
Код
Авторство
О чём код?
import org.junit.Assert; import org.junit.Test; import ru.sbrf.edu.CustomArrayImpl; import java.util.ArrayList; public class CustomArrayImplTest { private CustomArrayImpl<String> testArrayStr; private CustomArrayImpl<Integer> testArrayInt; /** * Проверка метода проверки вместимости массива getCapacity * на массивах с разными типами объектов */ @Test public void getCapacityTest() { this.testArrayStr = new CustomArrayImpl<String>(3); this.testArrayInt = new CustomArrayImpl<Integer>(5); Assert.assertEquals(3,testArrayStr.getCapacity()); Assert.assertEquals(5,testArrayInt.getCapacity()); } /** * Проверка метода containsTest */ @Test public void containsTest() { this.testArrayStr = new CustomArrayImpl<String>(3); this.testArrayInt = new CustomArrayImpl<Integer>(5); this.testArrayStr.add("aaa"); this.testArrayInt.add(1); Assert.assertTrue(this.testArrayStr.contains("aaa")); Assert.assertTrue(this.testArrayInt.contains(1)); } /** * Проверка метода indexOf на массивах с разными типами объектов */ @Test public void indexOfTest() { this.testArrayStr = new CustomArrayImpl<String>(3); this.testArrayInt = new CustomArrayImpl<Integer>(); this.testArrayStr.add("aaa"); this.testArrayStr.add("bbb"); this.testArrayStr.add("ccc"); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.add(3); Assert.assertEquals(0, this.testArrayStr.indexOf("ccc")); Assert.assertEquals(1, this.testArrayInt.indexOf(2)); } /** * Проверка метода обеспечения вместимости массива ensureCapacity: * создадим массив на 3 элемента и добавим к нему еще 5 * нужно проверить, что он правильно растянулся */ @Test public void ensureCapacityTest() { this.testArrayStr = new CustomArrayImpl<String>(3); this.testArrayInt = new CustomArrayImpl<Integer>(5); this.testArrayStr.ensureCapacity(5); this.testArrayInt.ensureCapacity(6); Assert.assertEquals(8, this.testArrayStr.getCapacity()); Assert.assertEquals(11, this.testArrayInt.getCapacity()); } /** * Проверка метода setTest */ @Test public void setTest() { this.testArrayStr = new CustomArrayImpl<String>(3); this.testArrayInt = new CustomArrayImpl<Integer>(3); this.testArrayStr.add("aaa"); this.testArrayStr.add("bbb"); this.testArrayStr.set(2,"ccc"); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.set(2, 3); Assert.assertEquals("ccc", this.testArrayStr.get(2)); Assert.assertEquals(3, this.testArrayInt.get(2).intValue()); } /** * Проверка метода добавления элемента в массив addTest */ @Test public void addTest() { this.testArrayStr = new CustomArrayImpl<String>(); this.testArrayInt = new CustomArrayImpl<Integer>(); this.testArrayStr.add("aaa"); this.testArrayStr.add("bbb"); this.testArrayStr.add("vvv"); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.add(3); Assert.assertFalse(this.testArrayStr.isEmpty()); Assert.assertEquals("bbb", testArrayStr.get(1)); Assert.assertFalse(this.testArrayInt.isEmpty()); Assert.assertEquals(1, this.testArrayInt.get(0).intValue()); } /** * Проверка метода добавления элементов в массив addAllTest */ @Test public void addAllTest() { this.testArrayStr = new CustomArrayImpl<String>(); String[] tempArrStr = new String[]{"aaa", "bbb", "ccc"}; testArrayStr.addAll(tempArrStr); this.testArrayInt = new CustomArrayImpl<Integer>(); Integer[] tempArrInt = new Integer[]{1,2,3}; testArrayInt.addAll(tempArrInt); Assert.assertFalse(testArrayStr.isEmpty()); Assert.assertEquals("bbb", testArrayStr.get(1)); Assert.assertFalse(testArrayInt.isEmpty()); Assert.assertEquals(2, testArrayInt.get(1).intValue()); } /** * Проверка метода добавления коллекции элементов в массив addAllCollectionTest */ @Test public void addAllCollectionTest() { this.testArrayStr = new CustomArrayImpl<String>(); ArrayList<String> tempArrStr = new ArrayList<>(); tempArrStr.add("aaa"); tempArrStr.add("bbb"); tempArrStr.add("ccc"); testArrayStr.addAll(tempArrStr); this.testArrayInt = new CustomArrayImpl<Integer>(); ArrayList<Integer> tempArrInt = new ArrayList<>(); tempArrInt.add(1); tempArrInt.add(2); tempArrInt.add(3); testArrayInt.addAll(tempArrInt); Assert.assertFalse(testArrayStr.isEmpty()); Assert.assertEquals("ccc", testArrayStr.get(2)); Assert.assertFalse(testArrayInt.isEmpty()); Assert.assertEquals(3, testArrayInt.get(2).intValue()); } /** * Проверка метода удаления элемента из массива по его индексу removeByIndexTest */ @Test public void removeByIndexTest() { this.testArrayStr = new CustomArrayImpl<String>(); testArrayStr.add("aaa"); testArrayStr.add("bbb"); testArrayStr.add("ccc"); this.testArrayInt = new CustomArrayImpl<Integer>(); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.add(3); Assert.assertEquals(3, testArrayStr.getCapacity()); testArrayStr.remove(2); Assert.assertEquals(2, testArrayStr.getCapacity()); Assert.assertEquals(3, testArrayInt.getCapacity()); testArrayInt.remove(2); testArrayInt.remove(1); Assert.assertEquals(1, testArrayInt.getCapacity()); } /** * Проверка метода удаления элемента из массива removeItemTest */ @Test public void removeItemTest() { this.testArrayStr = new CustomArrayImpl<String>(); testArrayStr.add("aaa"); testArrayStr.add("bbb"); testArrayStr.add("ccc"); this.testArrayInt = new CustomArrayImpl<Integer>(); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.add(3); Assert.assertEquals(3, testArrayStr.getCapacity()); testArrayStr.remove(testArrayStr.get(2)); Assert.assertEquals(2, testArrayStr.getCapacity()); Assert.assertEquals(3, testArrayInt.getCapacity()); testArrayInt.remove(testArrayInt.get(2)); testArrayInt.remove(testArrayInt.get(1)); Assert.assertEquals(1, testArrayInt.getCapacity()); } /** * Проверка метода сохранения элемента массива в обратном порядке reverseTest */ @Test public void reverseTest() { this.testArrayStr = new CustomArrayImpl<String>(); testArrayStr.add("aaa"); testArrayStr.add("bbb"); testArrayStr.add("ccc"); this.testArrayInt = new CustomArrayImpl<Integer>(); this.testArrayInt.add(1); this.testArrayInt.add(2); this.testArrayInt.add(3); Assert.assertEquals(3, testArrayStr.getCapacity()); Assert.assertEquals("ccc", testArrayStr.get(2)); testArrayStr.reverse(); Assert.assertEquals(3, testArrayStr.getCapacity()); Assert.assertEquals("aaa", testArrayStr.get(2)); Assert.assertEquals(3, testArrayInt.getCapacity()); Assert.assertEquals(3, testArrayInt.get(2).intValue()); testArrayInt.reverse(); Assert.assertEquals(3, testArrayInt.getCapacity()); Assert.assertEquals(1, testArrayInt.get(2).intValue()); } @Test public void toArrayTest() { this.testArrayStr = new CustomArrayImpl<String>(); String[] tempArrStr = new String[]{"aaa", "bbb", "ccc"}; this.testArrayStr.addAll(tempArrStr); Assert.assertArrayEquals(new String[]{"aaa", "bbb", "ccc"}, testArrayStr.toArray()); } }