tests/test_list.cpp

changeset 801
04aa3913c0e3
parent 800
1274e46b3013
equal deleted inserted replaced
800:1274e46b3013 801:04aa3913c0e3
846 846
847 TEST_F(PointerArrayList, AdvancedDestructor) { 847 TEST_F(PointerArrayList, AdvancedDestructor) {
848 verifyAdvancedDestructor(pointerArrayListFromTestData()); 848 verifyAdvancedDestructor(pointerArrayListFromTestData());
849 } 849 }
850 850
851 TEST_F(PointerLinkedList, cxListStorePointers) { 851
852 auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, 47)); 852
853 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
854 cxListStorePointers(list);
855 EXPECT_EQ(list->item_size, sizeof(void *));
856 CX_TEST_ASSERT(list->cl != NULL);
857 CX_TEST_ASSERT(list->climpl != NULL);
858 CX_TEST_ASSERT(cxListIsStoringPointers(list));
859 cxListStoreObjects(list);
860 CX_TEST_ASSERT(list->cl != NULL);
861 EXPECT_EQ(list->climpl, NULL);
862 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
863 }
864
865 TEST_F(LinkedList, cxLinkedListCreate) {
866 CxList *list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int)));
867 ASSERT_NE(list, NULL);
868 EXPECT_EQ(list->item_size, sizeof(int));
869 EXPECT_EQ(list->simple_destructor, NULL);
870 EXPECT_EQ(list->advanced_destructor, NULL);
871 EXPECT_EQ(list->destructor_data, NULL);
872 EXPECT_EQ(cxListSize(list), 0);
873 EXPECT_EQ(list->allocator, &testingAllocator);
874 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
875 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
876 }
877
878 TEST_F(LinkedList, cxLinkedListCreateSimple) {
879 CxList *list = autofree(cxLinkedListCreateSimple(sizeof(int)));
880 ASSERT_NE(list, NULL);
881 EXPECT_EQ(list->item_size, sizeof(int));
882 EXPECT_EQ(list->cmpfunc, NULL);
883 EXPECT_EQ(list->allocator, cxDefaultAllocator);
884 EXPECT_EQ(list->simple_destructor, NULL);
885 EXPECT_EQ(list->advanced_destructor, NULL);
886 EXPECT_EQ(list->destructor_data, NULL);
887 EXPECT_EQ(cxListSize(list), 0);
888 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
889 }
890
891 TEST_F(PointerLinkedList, cxLinkedListCreateSimpleForPointers) {
892 CxList *list = autofree(cxLinkedListCreateSimple(CX_STORE_POINTERS));
893 ASSERT_NE(list, NULL);
894 EXPECT_EQ(list->item_size, sizeof(void *));
895 EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
896 EXPECT_EQ(list->allocator, cxDefaultAllocator);
897 EXPECT_EQ(list->simple_destructor, NULL);
898 EXPECT_EQ(list->advanced_destructor, NULL);
899 EXPECT_EQ(list->destructor_data, NULL);
900 EXPECT_EQ(cxListSize(list), 0);
901 CX_TEST_ASSERT(cxListIsStoringPointers(list));
902 }
903
904 TEST_F(ArrayList, cxArrayListCreate) {
905 CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 8));
906 ASSERT_NE(list, NULL);
907 EXPECT_EQ(list->item_size, sizeof(int));
908 EXPECT_EQ(list->simple_destructor, NULL);
909 EXPECT_EQ(list->advanced_destructor, NULL);
910 EXPECT_EQ(list->destructor_data, NULL);
911 EXPECT_EQ(cxListSize(list), 0);
912 EXPECT_EQ(list->allocator, &testingAllocator);
913 EXPECT_EQ(list->cmpfunc, cx_cmp_int);
914 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
915 }
916
917 TEST_F(ArrayList, cxArrayListCreateSimple) {
918 CxList *list = autofree(cxArrayListCreateSimple(sizeof(int), 8));
919 ASSERT_NE(list, NULL);
920 EXPECT_EQ(list->cmpfunc, NULL);
921 EXPECT_EQ(list->allocator, cxDefaultAllocator);
922 EXPECT_EQ(list->item_size, sizeof(int));
923 EXPECT_EQ(list->simple_destructor, NULL);
924 EXPECT_EQ(list->advanced_destructor, NULL);
925 EXPECT_EQ(list->destructor_data, NULL);
926 EXPECT_EQ(cxListSize(list), 0);
927 CX_TEST_ASSERT(!cxListIsStoringPointers(list));
928 }
929
930 TEST_F(PointerArrayList, cxArrayListCreateSimpleForPointers) {
931 CxList *list = autofree(cxArrayListCreateSimple(CX_STORE_POINTERS, 8));
932 ASSERT_NE(list, NULL);
933 EXPECT_EQ(list->cmpfunc, cx_cmp_ptr);
934 EXPECT_EQ(list->allocator, cxDefaultAllocator);
935 EXPECT_EQ(list->item_size, sizeof(void *));
936 CX_TEST_ASSERT(cxListIsStoringPointers(list));
937 }
938
939 TEST_F(PointerLinkedList, DestroyNoDestructor) {
940 void *item = cxMalloc(&testingAllocator, sizeof(int));
941 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
942 cxListAdd(list, item);
943 CX_TEST_ASSERT(!testingAllocator.verify());
944 cxListDestroy(list);
945 CX_TEST_ASSERT(!testingAllocator.verify());
946 cxFree(&testingAllocator, item);
947 CX_TEST_ASSERT(testingAllocator.verify());
948 }
949
950 TEST_F(PointerLinkedList, DestroySimpleDestructor) {
951 int item = 0;
952 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
953 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
954 cxListAdd(list, &item);
955 cxListDestroy(list);
956 EXPECT_EQ(item, 42);
957 }
958
959 TEST_F(PointerLinkedList, DestroyAdvancedDestructor) {
960 void *item = cxMalloc(&testingAllocator, sizeof(int));
961 auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
962 list->destructor_data = &testingAllocator;
963 list->advanced_destructor = (cx_destructor_func2) cxFree;
964 cxListAdd(list, item);
965 CX_TEST_ASSERT(!testingAllocator.verify());
966 cxListDestroy(list);
967 CX_TEST_ASSERT(testingAllocator.verify());
968 }
969
970 TEST_F(PointerArrayList, DestroyNoDestructor) {
971 void *item = cxMalloc(&testingAllocator, sizeof(int));
972 auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
973 cxListAdd(list, item);
974 CX_TEST_ASSERT(!testingAllocator.verify());
975 cxListDestroy(list);
976 CX_TEST_ASSERT(!testingAllocator.verify());
977 cxFree(&testingAllocator, item);
978 CX_TEST_ASSERT(testingAllocator.verify());
979 }
980
981 TEST_F(PointerArrayList, DestroySimpleDestructor) {
982 int item = 0;
983 auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
984 list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
985 cxListAdd(list, &item);
986 cxListDestroy(list);
987 EXPECT_EQ(item, 42);
988 }
989
990 TEST_F(PointerArrayList, DestroyAdvancedDestructor) {
991 void *item = cxMalloc(&testingAllocator, sizeof(int));
992 auto list = cxArrayListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS, 4);
993 list->destructor_data = &testingAllocator;
994 list->advanced_destructor = (cx_destructor_func2) cxFree;
995 cxListAdd(list, item);
996 CX_TEST_ASSERT(!testingAllocator.verify());
997 cxListDestroy(list);
998 CX_TEST_ASSERT(testingAllocator.verify());
999 }
1000

mercurial