void object::test<1>() { CPLList* list; list = CPLListInsert(NULL, (void*)0, 0); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListInsert(NULL, (void*)0, 2); ensure(CPLListCount(list) == 3); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 2); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListAppend(list, (void*)1); ensure(CPLListGet(list,0) == list); ensure(CPLListGet(list,1) == NULL); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 3); CPLListDestroy(list); list = NULL; list = CPLListAppend(list, (void*)1); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)4, 3); CPLListGet(list,2)->pData = (void*)3; ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); CPLListDestroy(list); list = NULL; list = CPLListInsert(list, (void*)4, 1); CPLListGet(list,0)->pData = (void*)2; list = CPLListInsert(list, (void*)1, 0); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); list = CPLListRemove(list, 1); list = CPLListRemove(list, 1); list = CPLListRemove(list, 0); list = CPLListRemove(list, 0); ensure(list == NULL); }
CPLList *CPLListInsert( CPLList *psList, void *pData, int nPosition ) { if ( nPosition < 0 ) return psList; /* Nothing to do!*/ if ( nPosition == 0) { CPLList *psNew = static_cast<CPLList *>( CPLMalloc( sizeof(CPLList) ) ); psNew->pData = pData; psNew->psNext = psList; psList = psNew; return psList; } const int nCount = CPLListCount( psList ); if ( nCount < nPosition ) { /* Allocate room for the new object */ CPLList* psLast = CPLListGetLast(psList); for ( int i = nCount; i <= nPosition - 1; i++ ) { psLast = CPLListAppend( psLast, NULL ); if (psList == NULL) psList = psLast; else psLast = psLast->psNext; } psLast = CPLListAppend( psLast, pData ); if (psList == NULL) psList = psLast; /* coverity[leaked_storage] */ return psList; } CPLList *psNew = static_cast<CPLList *>( CPLMalloc( sizeof(CPLList) ) ); psNew->pData = pData; CPLList *psCurrent = psList; for ( int i = 0; i < nPosition - 1; i++ ) psCurrent = psCurrent->psNext; psNew->psNext = psCurrent->psNext; psCurrent->psNext = psNew; return psList; }