123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "tests/ArrayListTests.h"
- #include "data/ArrayList.h"
- #include "test/Test.h"
- using IntList = Core::ArrayList<int, 20>;
- static void testAdd() {
- IntList list;
- CORE_TEST_ERROR(list.add(5));
- CORE_TEST_EQUAL(5, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- }
- static void testMultipleAdd() {
- IntList list;
- CORE_TEST_ERROR(list.add(4));
- CORE_TEST_ERROR(list.add(3));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_EQUAL(4, list[0]);
- CORE_TEST_EQUAL(3, list[1]);
- CORE_TEST_EQUAL(2, list[2]);
- CORE_TEST_EQUAL(3, list.getLength());
- }
- static void testAddReplace() {
- IntList list;
- CORE_TEST_ERROR(list.add(5));
- list[0] = 3;
- CORE_TEST_EQUAL(3, list[0]);
- }
- static void testClear() {
- IntList list;
- CORE_TEST_ERROR(list.add(5));
- CORE_TEST_ERROR(list.add(4));
- list.clear();
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testOverflow() {
- IntList list;
- for(int i = 0; i < 20; i++) {
- CORE_TEST_ERROR(list.add(i));
- }
- for(int i = 0; i < 100000; i++) {
- CORE_TEST_EQUAL(Core::Error::CAPACITY_REACHED, list.add(i));
- }
- for(int i = 0; i < list.getLength(); i++) {
- CORE_TEST_EQUAL(i, list[i]);
- }
- }
- static void testCopy() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(3));
- IntList copy(list);
- CORE_TEST_EQUAL(list.getLength(), copy.getLength());
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- CORE_TEST_EQUAL(list[i], copy[i]);
- }
- }
- static void testCopyAssignment() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(3));
- IntList copy;
- copy = list;
- CORE_TEST_EQUAL(list.getLength(), copy.getLength());
- for(int i = 0; i < copy.getLength() && i < list.getLength(); i++) {
- CORE_TEST_EQUAL(list[i], copy[i]);
- }
- }
- static void testMove() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(3));
- IntList move(Core::move(list));
- CORE_TEST_EQUAL(0, list.getLength());
- CORE_TEST_EQUAL(3, move.getLength());
- CORE_TEST_EQUAL(1, move[0]);
- CORE_TEST_EQUAL(2, move[1]);
- CORE_TEST_EQUAL(3, move[2]);
- }
- static void testMoveAssignment() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.add(3));
- IntList move;
- move = Core::move(list);
- CORE_TEST_EQUAL(0, list.getLength());
- CORE_TEST_EQUAL(3, move.getLength());
- CORE_TEST_EQUAL(1, move[0]);
- CORE_TEST_EQUAL(2, move[1]);
- CORE_TEST_EQUAL(3, move[2]);
- }
- static void testToString1() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_ERROR(list.add(243));
- CORE_TEST_ERROR(list.add(-423));
- CORE_TEST_STRING("[1, 243, -423]", list);
- }
- static void testToString2() {
- IntList list;
- CORE_TEST_ERROR(list.add(1));
- CORE_TEST_STRING("[1]", list);
- }
- static void testToString3() {
- IntList list;
- CORE_TEST_STRING("[]", list);
- }
- static void testRemove() {
- IntList list;
- CORE_TEST_ERROR(list.add(4));
- CORE_TEST_ERROR(list.add(3));
- CORE_TEST_ERROR(list.add(2));
- CORE_TEST_ERROR(list.removeBySwap(0));
- CORE_TEST_EQUAL(2, list[0]);
- CORE_TEST_EQUAL(3, list[1]);
- CORE_TEST_EQUAL(2, list.getLength());
- CORE_TEST_ERROR(list.removeBySwap(1));
- CORE_TEST_EQUAL(2, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- CORE_TEST_ERROR(list.removeBySwap(0));
- CORE_TEST_EQUAL(0, list.getLength());
- }
- void Core::ArrayListTests::test() {
- testAdd();
- testMultipleAdd();
- testAddReplace();
- testClear();
- testOverflow();
- testCopy();
- testCopyAssignment();
- testMove();
- testMoveAssignment();
- testToString1();
- testToString2();
- testToString3();
- testRemove();
- }
|