test_list2.c (3557B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 /* 5 * test_list2.c 6 * 7 * Performs an in-place sort on a list 8 * 9 */ 10 11 #include "testutil.h" 12 #include "testutil_nss.h" 13 14 static void *plContext = NULL; 15 16 int 17 test_list2(int argc, char *argv[]) 18 { 19 20 PKIX_List *list; 21 char *temp; 22 PKIX_UInt32 i = 0; 23 PKIX_UInt32 j = 0; 24 PKIX_Int32 cmpResult; 25 PKIX_PL_OID *testOID; 26 PKIX_PL_String *testString; 27 PKIX_PL_Object *obj, *obj2; 28 PKIX_UInt32 size = 10; 29 char *testOIDString[10] = { 30 "2.9.999.1.20", 31 "1.2.3.4.5.6.7", 32 "0.1", 33 "1.2.3.5", 34 "0.39", 35 "1.2.3.4.7", 36 "1.2.3.4.6", 37 "0.39.1", 38 "1.2.3.4.5", 39 "0.39.1.300" 40 }; 41 PKIX_UInt32 actualMinorVersion; 42 43 PKIX_TEST_STD_VARS(); 44 45 startTests("List Sorting"); 46 47 PKIX_TEST_EXPECT_NO_ERROR( 48 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext)); 49 50 subTest("Creating Unsorted Lists"); 51 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&list, plContext)); 52 for (i = 0; i < size; i++) { 53 /* Create a new OID object */ 54 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create( 55 testOIDString[i], 56 &testOID, 57 plContext)); 58 /* Insert it into the list */ 59 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem(list, (PKIX_PL_Object *)testOID, plContext)); 60 /* Decref the string object */ 61 PKIX_TEST_DECREF_BC(testOID); 62 } 63 64 subTest("Outputting Unsorted List"); 65 66 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)list, 67 &testString, 68 plContext)); 69 temp = PKIX_String2ASCII(testString, plContext); 70 if (temp) { 71 (void)printf("%s \n", temp); 72 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext)); 73 } 74 PKIX_TEST_DECREF_BC(testString); 75 76 subTest("Performing Bubble Sort"); 77 78 for (i = 0; i < size; i++) 79 for (j = 9; j > i; j--) { 80 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem(list, j, &obj, plContext)); 81 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem(list, j - 1, 82 &obj2, plContext)); 83 84 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare(obj, obj2, &cmpResult, plContext)); 85 if (cmpResult < 0) { 86 /* Exchange the items */ 87 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetItem(list, j, obj2, plContext)); 88 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetItem(list, j - 1, 89 obj, plContext)); 90 } 91 /* DecRef objects */ 92 PKIX_TEST_DECREF_BC(obj); 93 PKIX_TEST_DECREF_BC(obj2); 94 } 95 96 subTest("Outputting Sorted List"); 97 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)list, 98 &testString, 99 plContext)); 100 temp = PKIX_String2ASCII(testString, plContext); 101 if (temp) { 102 (void)printf("%s \n", temp); 103 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext)); 104 } 105 106 cleanup: 107 108 PKIX_TEST_DECREF_AC(testString); 109 PKIX_TEST_DECREF_AC(list); 110 111 PKIX_Shutdown(plContext); 112 113 PKIX_TEST_RETURN(); 114 115 endTests("List Sorting"); 116 117 return (0); 118 }