123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include "../../src/ErrorSimulator.hpp"
- #include "../Tests.hpp"
- #include "core/data/List.hpp"
- #include "core/math/Math.hpp"
- template class Core::List<size_t>;
- using IntList = Core::List<size_t>;
- static void testAdd() {
- IntList list;
- list.add(5u);
- CORE_TEST_EQUAL(5, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- }
- static void testMultipleAdd() {
- IntList list;
- list.add(4u).add(3u).add(2u);
- 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;
- list.add(5u);
- list[0] = 3;
- CORE_TEST_EQUAL(3, list[0]);
- }
- static void testClear() {
- IntList list;
- list.add(5u).add(4u);
- list.clear();
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testShrink() {
- IntList list;
- list.add(5u).add(4u).add(3u);
- CORE_TEST_TRUE(list.getCapacity() >= 3);
- list.shrink();
- CORE_TEST_TRUE(list.getLength() == 3);
- CORE_TEST_TRUE(list.getCapacity() == 3);
- CORE_TEST_EQUAL(5, list[0]);
- CORE_TEST_EQUAL(4, list[1]);
- CORE_TEST_EQUAL(3, list[2]);
- }
- static void testBigAdd(bool light) {
- size_t limit = light ? 10000 : 100000;
- IntList list;
- for(size_t i = 0; i < limit; i++) {
- list.add(i);
- }
- for(size_t i = 0; i < list.getLength(); i++) {
- CORE_TEST_EQUAL(i, list[i]);
- }
- CORE_TEST_EQUAL(limit, list.getLength());
- }
- static void testCopy() {
- IntList list;
- list.add(1u).add(2u).add(3u);
- IntList copy = list;
- CORE_TEST_EQUAL(list.getLength(), copy.getLength());
- size_t limit = Core::Math::min(copy.getLength(), list.getLength());
- for(size_t i = 0; i < limit; i++) {
- CORE_TEST_EQUAL(list[i], copy[i]);
- }
- }
- static void testMove() {
- IntList list;
- list.add(1u).add(2u).add(3u);
- 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;
- list.add(1u).add(2u).add(3u);
- 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;
- list.add(1u).add(243u).add(423u);
- CORE_TEST_STRING("[1, 243, 423]", list);
- }
- static void testToString2() {
- IntList list;
- list.add(1u);
- CORE_TEST_STRING("[1]", list);
- }
- static void testToString3() {
- IntList list;
- CORE_TEST_STRING("[]", list);
- }
- static void testRemoveBySwap() {
- IntList list;
- list.add(4u).add(3u).add(2u);
- list.removeBySwap(0);
- CORE_TEST_EQUAL(2, list[0]);
- CORE_TEST_EQUAL(3, list[1]);
- CORE_TEST_EQUAL(2, list.getLength());
- list.removeBySwap(1);
- CORE_TEST_EQUAL(2, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- list.removeBySwap(0);
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testRemove() {
- IntList list;
- list.add(4u).add(3u).add(2u);
- list.remove(0);
- CORE_TEST_EQUAL(3, list[0]);
- CORE_TEST_EQUAL(2, list[1]);
- CORE_TEST_EQUAL(2, list.getLength());
- list.remove(1);
- CORE_TEST_EQUAL(3, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- list.remove(0);
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testRemoveLast() {
- IntList list;
- list.add(4u).add(3u).add(2u);
- list.removeLast();
- CORE_TEST_EQUAL(4, list[0]);
- CORE_TEST_EQUAL(3, list[1]);
- CORE_TEST_EQUAL(2, list.getLength());
- list.removeLast();
- CORE_TEST_EQUAL(4, list[0]);
- CORE_TEST_EQUAL(1, list.getLength());
- list.removeLast();
- CORE_TEST_EQUAL(0, list.getLength());
- }
- static void testResize() {
- IntList list;
- list.resize(5, 10);
- CORE_TEST_EQUAL(5, list.getLength());
- for(size_t i = 0; i < 5; i++) {
- CORE_TEST_EQUAL(10, list[i]);
- }
- }
- static void testDefaultResize() {
- IntList list;
- list.resize(5);
- CORE_TEST_EQUAL(5, list.getLength());
- for(size_t i = 0; i < 5; i++) {
- CORE_TEST_EQUAL(0, list[i]);
- }
- }
- static void testInvalidReserve() {
- IntList list;
- list.reserve(0);
- }
- static void testShrinkExact() {
- IntList list;
- list.resize(50);
- list.shrink();
- }
- static void testShrinkResize() {
- IntList list;
- list.resize(50);
- list.resize(20, 5);
- list.resize(10);
- }
- static void testCopyEmpty() {
- IntList list;
- IntList copy = list;
- }
- void Core::testList(bool light) {
- testAdd();
- testMultipleAdd();
- testAddReplace();
- testClear();
- testShrink();
- testBigAdd(light);
- testCopy();
- testMove();
- testMoveAssignment();
- testToString1();
- testToString2();
- testToString3();
- testRemoveBySwap();
- testRemove();
- testRemoveLast();
- testResize();
- testDefaultResize();
- testInvalidReserve();
- testShrinkExact();
- testShrinkResize();
- testCopyEmpty();
- }
|