Commit 9d16fae1 authored by Pierre Lamot's avatar Pierre Lamot
Browse files

diff util add tests on Qt partial update algorithm

parent fcccf2a5
......@@ -178,9 +178,157 @@ static void check_changelist(const char* oldInput, const char* newInput, int fla
vlc_diffutil_free_snake(snake);
}
struct QtData
{
const char* list;
size_t loadedCount;
size_t totalCount;
};
static char partialupdate_get(const struct QtData* m_oldData, const struct QtData* m_cachedData,
size_t index,
size_t m_partialLoadedCount, size_t m_partialIndex, size_t m_partialX)
{
//the view may access the model while we're updating it
//everything before m_partialIndex is updated in the new model,
//everything after m_partialIndex is still valid in the old model
if (unlikely(m_oldData))
{
if (m_cachedData)
{
if (index >= m_partialLoadedCount)
return false;
else if (index >= m_partialIndex)
{
printf("<");
assert(index + (m_partialX - m_partialIndex) < strlen(m_oldData->list));
return m_oldData->list[index + (m_partialX - m_partialIndex)];
}
else
{
printf(">");
assert(index < strlen(m_cachedData->list));
return m_cachedData->list[index];
}
}
else
{
if (index >= m_oldData->loadedCount)
return 0;
printf("<");
assert(index < strlen(m_oldData->list));
return m_oldData->list[index];
}
}
if (!m_cachedData)
return 0;
if (index + 1 > m_cachedData->loadedCount)
return 0;
assert(index < strlen(m_cachedData->list));
return m_cachedData->list[index];
}
static void CHECK_PARTIAL_STATE(const struct QtData* m_oldData, const struct QtData* m_cachedData,
size_t m_partialLoadedCount, size_t m_partialIndex, size_t m_partialX,
const char* intermediate) { \
printf("inter %s index %lu X: %lu\n"
" old %s\n"
" new %s\n", intermediate,
m_partialIndex, m_partialX,
m_oldData->list, m_cachedData->list);
for (size_t j = 0; j < m_partialLoadedCount; j++) {
char c = partialupdate_get(m_oldData, m_cachedData, j, m_partialLoadedCount, m_partialIndex, m_partialX);
printf("%c #%c \n", c, intermediate[j]);
assert(c == intermediate[j]);
}
printf("\n");
}
static void check_partialUpdate(const char* oldString, const char* newString, const char** intermediate)
{
printf("================================\n");
vlc_diffutil_callback_t diffOp = {
.getOldSize = &getSize,
.getNewSize = &getSize,
.isSame = &isSame,
};
struct QtData* m_oldData = malloc(sizeof(struct QtData));
struct QtData* m_cachedData = malloc(sizeof(struct QtData));
m_oldData->list = oldString;
m_oldData->loadedCount = strlen(oldString);
m_oldData->totalCount = m_oldData->loadedCount;
m_cachedData->list = newString;
m_cachedData->loadedCount = strlen(newString);
m_cachedData->totalCount = m_cachedData->loadedCount;
diffutil_snake_t* snake = vlc_diffutil_build_snake(&diffOp, m_oldData->list, m_cachedData->list);
int diffutilFlags = VLC_DIFFUTIL_RESULT_AGGREGATE;
vlc_diffutil_changelist_t* changes = vlc_diffutil_build_change_list(
snake, &diffOp, m_oldData->list, m_cachedData->list,
diffutilFlags);
size_t m_partialX = 0;
size_t m_partialIndex = 0;
size_t m_partialLoadedCount = m_oldData->loadedCount;
size_t partialTotalCount = m_oldData->totalCount;
for (size_t i = 0; i < changes->size; i++)
{
vlc_diffutil_change_t op = changes->data[i];
switch (op.type)
{
case VLC_DIFFUTIL_OP_IGNORE:
break;
case VLC_DIFFUTIL_OP_INSERT:
m_partialX = op.op.insert.x;
m_partialIndex = op.op.insert.index;
printf("beginInsertRows(%u, %u)\n", op.op.insert.index, op.op.insert.index + op.count - 1);
CHECK_PARTIAL_STATE(m_oldData, m_cachedData, m_partialLoadedCount, m_partialIndex, m_partialX, intermediate[i]);
m_partialIndex += op.count;
m_partialLoadedCount += op.count;
partialTotalCount += op.count;
printf("endInsertRows()\n");
CHECK_PARTIAL_STATE(m_oldData, m_cachedData, m_partialLoadedCount, m_partialIndex, m_partialX, intermediate[i+1]);
printf("localSizeChanged(%lu)\n", m_partialLoadedCount);
break;
case VLC_DIFFUTIL_OP_REMOVE:
m_partialX = op.op.remove.x;
m_partialIndex = op.op.remove.index;
printf("beginRemoveRows(%u, %u)\n", op.op.insert.index, op.op.insert.index + op.count - 1);
CHECK_PARTIAL_STATE(m_oldData, m_cachedData, m_partialLoadedCount, m_partialIndex, m_partialX, intermediate[i]);
m_partialLoadedCount -= op.count;
m_partialX += op.count;
m_partialIndex = op.op.remove.index;
partialTotalCount -= op.count;
printf("endRemoveRows()\n");
CHECK_PARTIAL_STATE(m_oldData, m_cachedData, m_partialLoadedCount, m_partialIndex, m_partialX, intermediate[i+1]);
printf("localSizeChanged(%lu)\n", m_partialLoadedCount);
break;
case VLC_DIFFUTIL_OP_MOVE:
assert(false);
break;
}
}
vlc_diffutil_free_change_list(changes);
vlc_diffutil_free_snake(snake);
free(m_oldData);
free(m_cachedData);
}
#define CHECK_CHANGELIST(from, to, flags, ...) \
do { \
const changelist_check_t res[] = { __VA_ARGS__ };\
const changelist_check_t res[] = { __VA_ARGS__ };\
check_changelist(from, to, flags, ARRAY_SIZE(res), res); \
} while (0)
......@@ -193,9 +341,15 @@ static void check_changelist(const char* oldInput, const char* newInput, int fla
#define CHECK_MOVE_AG(from, to, ...) \
CHECK_CHANGELIST(from, to, VLC_DIFFUTIL_RESULT_MOVE | VLC_DIFFUTIL_RESULT_AGGREGATE, __VA_ARGS__)
#define CHECK_PARTIAL_UPDATE(from, to, ...) \
do { \
const char* intermediateResults[] = { __VA_ARGS__ }; \
check_partialUpdate(from, to, intermediateResults); \
} while(0)
int main()
{
check_snake("", "", "");
check_snake("unchanged", "unchanged", "=u=n=c=h=a=n=g=e=d");
check_snake("", "add only", "+a+d+d+ +o+n+l+y");
......@@ -308,5 +462,23 @@ int main()
CHECK_MOVE_AG("_123___", "___312_", {'m', CHECK_MOVE_PACK(1, 6), "12"}, {'m', CHECK_MOVE_PACK(1, 4), "3"});
CHECK_MOVE_AG("______123_", "_312______", {'m', CHECK_MOVE_PACK(8, 1), "3"}, {'m', CHECK_MOVE_PACK(7, 2), "12"});
CHECK_PARTIAL_UPDATE("abcdef", "def", "abcdef", "def");
CHECK_PARTIAL_UPDATE("abcdef", "abc", "abcdef", "abc");
CHECK_PARTIAL_UPDATE("abcdefghi", "def", "abcdefghi", "defghi", "def");
CHECK_PARTIAL_UPDATE("def", "abcdef", "def", "abcdef");
CHECK_PARTIAL_UPDATE("abc", "abcdef", "abc", "abcdef");
CHECK_PARTIAL_UPDATE("def", "abcdefghi", "def", "abcdef", "abcdefghi");
CHECK_PARTIAL_UPDATE("abhcdef", "abcdefgh", "abhcdef", "abcdef", "abcdefgh");
CHECK_PARTIAL_UPDATE("abcdefgh", "abhcdef", "abcdefgh", "abhcdefgh", "abhcdef");
CHECK_PARTIAL_UPDATE("abcdefbghijklm", "aefbgl", "abcdefbghijklm", "aefbghijklm", "aefbglm", "aefbgl");
CHECK_PARTIAL_UPDATE("abfm", "abcdefghijklmnop",
"abfm",
"abcdefm",
"abcdefghijklm",
"abcdefghijklmnop");
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment