#include "utilities/IndexSubindexSelection.h" #include "unittest/TestSuite.h" #include static void testInit(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); TestCase_assertUIntEqual(selection->pairCount, 0); TestCase_assertPointerNULL(selection->pairs); TestCase_assertPointerNonNULL(selection->reverseLookup); TestCase_assertUIntEqual(selection->selectionDirtyValue, 0); TestCase_assertUIntEqual(selection->reverseLookupDirtyValue, 0); IndexSubindexSelection_dispose(selection); } static void testBasicOperations(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); unsigned int selectionIndex = UINT_MAX; TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 0, 1, &selectionIndex)); TestCase_assertBoolTrue(IndexSubindexSelection_selectIndexPair(selection, 0, 1, true)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 0, 1, &selectionIndex)); TestCase_assertUIntEqual(selectionIndex, 0); TestCase_assertBoolFalse(IndexSubindexSelection_selectIndexPair(selection, 0, 1, false)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 1, 2, &selectionIndex)); TestCase_assertBoolTrue(IndexSubindexSelection_selectIndexPair(selection, 1, 2, true)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 1, 2, &selectionIndex)); TestCase_assertUIntEqual(selectionIndex, 1); TestCase_assertBoolFalse(IndexSubindexSelection_deselectIndexPair(selection, 2, 3)); IndexSubindexSelection_selectIndexPair(selection, 2, 3, true); TestCase_assertBoolTrue(IndexSubindexSelection_deselectIndexPair(selection, 2, 3)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 2, 3, NULL)); TestCase_assertBoolFalse(IndexSubindexSelection_toggleIndexPair(selection, 0, 1)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 0, 1, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_toggleIndexPair(selection, 2, 4)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 2, 4, NULL)); IndexSubindexSelection_deselectItemAtIndex(selection, 0); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 1, 2, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_selectIndexPair(selection, 1, 2, true)); TestCase_assertBoolTrue(IndexSubindexSelection_truncateSelectionToMaxIndexCount(selection, 2)); TestCase_assertUIntEqual(selection->pairCount, 1); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 1, 2, &selectionIndex)); TestCase_assertUIntEqual(selectionIndex, 0); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 2, 4, &selectionIndex)); TestCase_assertBoolTrue(IndexSubindexSelection_deselectAll(selection)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 1, 2, NULL)); TestCase_assertUIntEqual(selection->pairCount, 0); TestCase_assertBoolFalse(IndexSubindexSelection_deselectAll(selection)); IndexSubindexSelection_dispose(selection); } static void testCopy(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); IndexSubindexSelection * copy = IndexSubindexSelection_copy(selection); IndexSubindexSelection_dispose(selection); TestCase_assertUIntEqual(copy->pairCount, 0); TestCase_assertPointerNULL(copy->pairs); TestCase_assertPointerNonNULL(copy->reverseLookup); TestCase_assertUIntEqual(copy->selectionDirtyValue, 0); TestCase_assertUIntEqual(copy->reverseLookupDirtyValue, 0); IndexSubindexSelection_dispose(copy); selection = IndexSubindexSelection_create(); IndexSubindexSelection_selectIndexPair(selection, 1, 2, true); IndexSubindexSelection_selectIndexPair(selection, 3, 5, true); copy = IndexSubindexSelection_copy(selection); IndexSubindexSelection_dispose(selection); TestCase_assertUIntEqual(copy->pairCount, 2); TestCase_assertPointerNonNULL(copy->pairs); TestCase_assertUIntEqual(copy->pairs[0].index, 1); TestCase_assertUIntEqual(copy->pairs[0].subindex, 2); TestCase_assertUIntEqual(copy->pairs[1].index, 3); TestCase_assertUIntEqual(copy->pairs[1].subindex, 5); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(copy, 0, 1, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(copy, 1, 2, NULL)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(copy, 2, 3, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(copy, 3, 5, NULL)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(copy, 4, 5, NULL)); IndexSubindexSelection * copy2 = IndexSubindexSelection_create(); IndexSubindexSelection_copySelectionFrom(copy2, copy); IndexSubindexSelection_dispose(copy); TestCase_assertUIntEqual(copy2->pairCount, 2); TestCase_assertPointerNonNULL(copy2->pairs); TestCase_assertUIntEqual(copy2->pairs[0].index, 1); TestCase_assertUIntEqual(copy2->pairs[0].subindex, 2); TestCase_assertUIntEqual(copy2->pairs[1].index, 3); TestCase_assertUIntEqual(copy2->pairs[1].subindex, 5); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(copy2, 1, 2, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(copy2, 3, 5, NULL)); IndexSubindexSelection_dispose(copy2); } static void testAdjustIndexes(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); IndexSubindexSelection_selectIndexPair(selection, 0, 1, true); IndexSubindexSelection_selectIndexPair(selection, 2, 7, true); IndexSubindexSelection_selectIndexPair(selection, 5, 6, true); IndexSubindexSelection_selectIndexPair(selection, 9, 10, true); IndexSubindexSelection_adjustIndexes(selection, 3, 1); TestCase_assertUIntEqual(selection->pairCount, 4); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 1); TestCase_assertUIntEqual(selection->pairs[1].index, 2); TestCase_assertUIntEqual(selection->pairs[1].subindex, 7); TestCase_assertUIntEqual(selection->pairs[2].index, 6); TestCase_assertUIntEqual(selection->pairs[2].subindex, 6); TestCase_assertUIntEqual(selection->pairs[3].index, 10); TestCase_assertUIntEqual(selection->pairs[3].subindex, 10); IndexSubindexSelection_adjustIndexes(selection, 1, -3); TestCase_assertUIntEqual(selection->pairCount, 3); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 1); TestCase_assertUIntEqual(selection->pairs[1].index, 3); TestCase_assertUIntEqual(selection->pairs[1].subindex, 6); TestCase_assertUIntEqual(selection->pairs[2].index, 7); TestCase_assertUIntEqual(selection->pairs[2].subindex, 10); IndexSubindexSelection_dispose(selection); selection = IndexSubindexSelection_create(); IndexSubindexSelection_selectIndexPair(selection, 0, 0, true); IndexSubindexSelection_selectIndexPair(selection, 0, 1, true); IndexSubindexSelection_selectIndexPair(selection, 0, 3, true); IndexSubindexSelection_selectIndexPair(selection, 1, 3, true); IndexSubindexSelection_adjustSubindexes(selection, 0, 1, 1); TestCase_assertUIntEqual(selection->pairCount, 4); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 0); TestCase_assertUIntEqual(selection->pairs[1].index, 0); TestCase_assertUIntEqual(selection->pairs[1].subindex, 2); TestCase_assertUIntEqual(selection->pairs[2].index, 0); TestCase_assertUIntEqual(selection->pairs[2].subindex, 4); TestCase_assertUIntEqual(selection->pairs[3].index, 1); TestCase_assertUIntEqual(selection->pairs[3].subindex, 3); IndexSubindexSelection_adjustSubindexes(selection, 0, 3, -4); TestCase_assertUIntEqual(selection->pairCount, 3); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 0); TestCase_assertUIntEqual(selection->pairs[1].index, 0); TestCase_assertUIntEqual(selection->pairs[1].subindex, 2); TestCase_assertUIntEqual(selection->pairs[2].index, 1); TestCase_assertUIntEqual(selection->pairs[2].subindex, 3); IndexSubindexSelection_adjustSubindexes(selection, 1, 1, -1); TestCase_assertUIntEqual(selection->pairCount, 3); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 0); TestCase_assertUIntEqual(selection->pairs[1].index, 0); TestCase_assertUIntEqual(selection->pairs[1].subindex, 2); TestCase_assertUIntEqual(selection->pairs[2].index, 1); TestCase_assertUIntEqual(selection->pairs[2].subindex, 2); IndexSubindexSelection_dispose(selection); } static void testRemoveDuplicates(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); unsigned int result = IndexSubindexSelection_removeDuplicates(selection); TestCase_assertUIntEqual(result, 0); IndexSubindexSelection_selectIndexPair(selection, 0, 1, true); IndexSubindexSelection_selectIndexPair(selection, 1, 2, true); result = IndexSubindexSelection_removeDuplicates(selection); TestCase_assertUIntEqual(result, 0); TestCase_assertUIntEqual(selection->pairCount, 2); IndexSubindexSelection_selectIndexPair(selection, 0, 1, true); result = IndexSubindexSelection_removeDuplicates(selection); TestCase_assertUIntEqual(result, 1); TestCase_assertUIntEqual(selection->pairCount, 2); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 1); TestCase_assertUIntEqual(selection->pairs[1].index, 1); TestCase_assertUIntEqual(selection->pairs[1].subindex, 2); IndexSubindexSelection_selectIndexPair(selection, 3, 4, true); IndexSubindexSelection_selectIndexPair(selection, 4, 5, true); IndexSubindexSelection_selectIndexPair(selection, 3, 4, true); IndexSubindexSelection_selectIndexPair(selection, 1, 2, true); result = IndexSubindexSelection_removeDuplicates(selection); TestCase_assertUIntEqual(result, 2); TestCase_assertUIntEqual(selection->pairCount, 4); TestCase_assertUIntEqual(selection->pairs[0].index, 0); TestCase_assertUIntEqual(selection->pairs[0].subindex, 1); TestCase_assertUIntEqual(selection->pairs[1].index, 1); TestCase_assertUIntEqual(selection->pairs[1].subindex, 2); TestCase_assertUIntEqual(selection->pairs[2].index, 3); TestCase_assertUIntEqual(selection->pairs[2].subindex, 4); TestCase_assertUIntEqual(selection->pairs[3].index, 4); TestCase_assertUIntEqual(selection->pairs[3].subindex, 5); IndexSubindexSelection_dispose(selection); } TEST_SUITE(IndexSubindexSelectionTest, testInit, testBasicOperations, testCopy, testAdjustIndexes, testRemoveDuplicates)