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