MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / test_dataBlockRemoveItem

Function test_dataBlockRemoveItem

tests/unit/test_datablock.c:131–183  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129}
130
131void test_dataBlockRemoveItem() {
132 DataBlock *dataBlock = DataBlock_New(DATABLOCK_BLOCK_CAP, 1024, sizeof(int), NULL);
133 uint itemCount = 32;
134 DataBlock_Accommodate(dataBlock, itemCount);
135
136 // Set items.
137 for(int i = 0 ; i < itemCount; i++) {
138 int *item = (int *)DataBlock_AllocateItem(dataBlock, NULL);
139 *item = i;
140 }
141
142 // Validate item at position 0.
143 int *item = (int *)DataBlock_GetItem(dataBlock, 0);
144 TEST_ASSERT(item != NULL);
145 TEST_ASSERT(*item == 0);
146
147 // Remove item at position 0 and perform validations
148 // Index 0 should be added to datablock deletedIdx array.
149 DataBlock_DeleteItem(dataBlock, 0);
150 TEST_ASSERT(dataBlock->itemCount == itemCount - 1);
151 TEST_ASSERT(array_len(dataBlock->deletedIdx) == 1);
152 DataBlockItemHeader *header = (DataBlockItemHeader *)dataBlock->blocks[0]->data;
153 TEST_ASSERT(IS_ITEM_DELETED(header));
154
155 // Try to get item from deleted cell.
156 item = (int *)DataBlock_GetItem(dataBlock, 0);
157 TEST_ASSERT(item == NULL);
158
159 // Iterate over datablock, deleted item should be skipped.
160 DataBlockIterator *it = DataBlock_Scan(dataBlock);
161 uint counter = 0;
162 while(DataBlockIterator_Next(it, NULL)) counter++;
163 TEST_ASSERT(counter == itemCount - 1);
164 DataBlockIterator_Free(it);
165
166 // There's no harm in deleting a deleted item.
167 DataBlock_DeleteItem(dataBlock, 0);
168
169 // Add a new item, expecting deleted cell to be reused.
170 int *newItem = (int *)DataBlock_AllocateItem(dataBlock, NULL);
171 TEST_ASSERT(dataBlock->itemCount == itemCount);
172 TEST_ASSERT(array_len(dataBlock->deletedIdx) == 0);
173 TEST_ASSERT((void *)newItem == (void *)((dataBlock->blocks[0]->data) + ITEM_HEADER_SIZE));
174
175 it = DataBlock_Scan(dataBlock);
176 counter = 0;
177 while(DataBlockIterator_Next(it, NULL)) counter++;
178 TEST_ASSERT(counter == itemCount);
179 DataBlockIterator_Free(it);
180
181 // Cleanup.
182 DataBlock_Free(dataBlock);
183}
184
185void test_dataBlockOutOfOrderBuilding() {
186 // This test checks for a fragmented, data block out of order re-construction.

Callers

nothing calls this directly

Calls 10

DataBlock_NewFunction · 0.85
DataBlock_AccommodateFunction · 0.85
DataBlock_AllocateItemFunction · 0.85
DataBlock_GetItemFunction · 0.85
DataBlock_DeleteItemFunction · 0.85
array_lenFunction · 0.85
DataBlock_ScanFunction · 0.85
DataBlockIterator_NextFunction · 0.85
DataBlockIterator_FreeFunction · 0.85
DataBlock_FreeFunction · 0.85

Tested by

no test coverage detected