#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); 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, false)); IndexSubindexSelection_selectIndexPair(selection, 2, 3, true); TestCase_assertBoolTrue(IndexSubindexSelection_deselectIndexPair(selection, 2, 3, false)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 2, 3, NULL)); TestCase_assertBoolFalse(IndexSubindexSelection_toggleIndexPair(selection, 0, 1, false)); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 0, 1, NULL)); TestCase_assertBoolTrue(IndexSubindexSelection_toggleIndexPair(selection, 2, 4, false)); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 2, 4, NULL)); IndexSubindexSelection_deselectItemAtIndex(selection, 0, false); 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); 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); } static void testSetLastSelectedPair(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); bool result = IndexSubindexSelection_setLastSelectedPair(selection, 0, 1); TestCase_assertBoolFalse(result); IndexSubindexSelection_selectIndexPair(selection, 0, 1, true); IndexSubindexSelection_selectIndexPair(selection, 1, 2, true); IndexSubindexSelection_selectIndexPair(selection, 2, 3, true); result = IndexSubindexSelection_setLastSelectedPair(selection, 0, 1); TestCase_assertBoolTrue(result); TestCase_assertUIntEqual(selection->pairs[0].index, 1); TestCase_assertUIntEqual(selection->pairs[0].subindex, 2); TestCase_assertUIntEqual(selection->pairs[1].index, 2); TestCase_assertUIntEqual(selection->pairs[1].subindex, 3); TestCase_assertUIntEqual(selection->pairs[2].index, 0); TestCase_assertUIntEqual(selection->pairs[2].subindex, 1); result = IndexSubindexSelection_setLastSelectedPair(selection, 2, 3); TestCase_assertBoolTrue(result); TestCase_assertUIntEqual(selection->pairs[0].index, 1); TestCase_assertUIntEqual(selection->pairs[0].subindex, 2); TestCase_assertUIntEqual(selection->pairs[1].index, 0); TestCase_assertUIntEqual(selection->pairs[1].subindex, 1); TestCase_assertUIntEqual(selection->pairs[2].index, 2); TestCase_assertUIntEqual(selection->pairs[2].subindex, 3); result = IndexSubindexSelection_setLastSelectedPair(selection, 2, 3); TestCase_assertBoolFalse(result); IndexSubindexSelection_dispose(selection); } static void testScribble(void) { IndexSubindexSelection * selection = IndexSubindexSelection_create(); IndexSubindexSelection_selectIndexPair(selection, 1, 2, true); IndexSubindexSelection_selectIndexPair(selection, 3, 0, true); IndexSubindexSelection_selectIndexPair(selection, 7, 9, true); IndexSubindexSelection_selectIndexPair(selection, 20, 20, true); IndexSubindexSelection_selectIndexPair(selection, 22, 3, true); IndexSubindexSelection_selectIndexPair(selection, 26, 3, true); TestCase_assertUIntEqual(selection->pairs[1].index, 3); TestCase_assertUIntEqual(selection->pairs[1].subindex, 0); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 3, 0, NULL)); bool result = IndexSubindexSelection_deselectIndexPair(selection, 3, 0, true); TestCase_assertBoolTrue(result); TestCase_assertUIntEqual(selection->pairs[1].index, UINT_MAX); TestCase_assertUIntEqual(selection->pairs[1].subindex, UINT_MAX); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 3, 0, NULL)); result = IndexSubindexSelection_deselectIndexPair(selection, 3, 0, true); TestCase_assertBoolFalse(result); result = IndexSubindexSelection_deselectIndexPair(selection, 90, 91, true); TestCase_assertBoolFalse(result); TestCase_assertUIntEqual(selection->pairs[2].index, 7); TestCase_assertUIntEqual(selection->pairs[2].subindex, 9); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 7, 9, NULL)); IndexSubindexSelection_deselectItemAtIndex(selection, 2, true); TestCase_assertUIntEqual(selection->pairs[2].index, UINT_MAX); TestCase_assertUIntEqual(selection->pairs[2].subindex, UINT_MAX); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 7, 9, NULL)); TestCase_assertUIntEqual(selection->pairs[4].index, 22); TestCase_assertUIntEqual(selection->pairs[4].subindex, 3); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 22, 3, NULL)); result = IndexSubindexSelection_toggleIndexPair(selection, 22, 3, true); TestCase_assertBoolFalse(result); TestCase_assertUIntEqual(selection->pairs[4].index, UINT_MAX); TestCase_assertUIntEqual(selection->pairs[4].subindex, UINT_MAX); TestCase_assertBoolFalse(IndexSubindexSelection_isPairSelected(selection, 22, 3, NULL)); TestCase_assertUIntEqual(selection->pairCount, 6); result = IndexSubindexSelection_toggleIndexPair(selection, 22, 3, true); TestCase_assertBoolTrue(result); TestCase_assertUIntEqual(selection->pairCount, 7); TestCase_assertUIntEqual(selection->pairs[6].index, 22); TestCase_assertUIntEqual(selection->pairs[6].subindex, 3); TestCase_assertBoolTrue(IndexSubindexSelection_isPairSelected(selection, 22, 3, NULL)); IndexSubindexSelection_removeScribbles(selection); TestCase_assertUIntEqual(selection->pairCount, 4); TestCase_assertUIntEqual(selection->pairs[0].index, 1); TestCase_assertUIntEqual(selection->pairs[0].subindex, 2); TestCase_assertUIntEqual(selection->pairs[1].index, 20); TestCase_assertUIntEqual(selection->pairs[1].subindex, 20); TestCase_assertUIntEqual(selection->pairs[2].index, 26); TestCase_assertUIntEqual(selection->pairs[2].subindex, 3); TestCase_assertUIntEqual(selection->pairs[3].index, 22); TestCase_assertUIntEqual(selection->pairs[3].subindex, 3); IndexSubindexSelection_dispose(selection); } TEST_SUITE(IndexSubindexSelectionTest, testInit, testBasicOperations, testCopy, testAdjustIndexes, testRemoveDuplicates, testSetLastSelectedPair, testScribble)