summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/libpkix/pkix/util/test_list2.c
blob: b802ff0e667aaae4162b3670038d9de60b672758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
 * test_list2.c
 *
 * Performs an in-place sort on a list
 *
 */

#include "testutil.h"
#include "testutil_nss.h"

static void *plContext = NULL;

int
test_list2(int argc, char *argv[])
{

    PKIX_List *list;
    char *temp;
    PKIX_UInt32 i = 0;
    PKIX_UInt32 j = 0;
    PKIX_Int32 cmpResult;
    PKIX_PL_OID *testOID;
    PKIX_PL_String *testString;
    PKIX_PL_Object *obj, *obj2;
    PKIX_UInt32 size = 10;
    char *testOIDString[10] = {
        "2.9.999.1.20",
        "1.2.3.4.5.6.7",
        "0.1",
        "1.2.3.5",
        "0.39",
        "1.2.3.4.7",
        "1.2.3.4.6",
        "0.39.1",
        "1.2.3.4.5",
        "0.39.1.300"
    };
    PKIX_UInt32 actualMinorVersion;

    PKIX_TEST_STD_VARS();

    startTests("List Sorting");

    PKIX_TEST_EXPECT_NO_ERROR(
        PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));

    subTest("Creating Unsorted Lists");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&list, plContext));
    for (i = 0; i < size; i++) {
        /* Create a new OID object */
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create(
            testOIDString[i],
            &testOID,
            plContext));
        /* Insert it into the list */
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem(list, (PKIX_PL_Object *)testOID, plContext));
        /* Decref the string object */
        PKIX_TEST_DECREF_BC(testOID);
    }

    subTest("Outputting Unsorted List");

    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)list,
                                                      &testString,
                                                      plContext));
    temp = PKIX_String2ASCII(testString, plContext);
    if (temp) {
        (void)printf("%s \n", temp);
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
    }
    PKIX_TEST_DECREF_BC(testString);

    subTest("Performing Bubble Sort");

    for (i = 0; i < size; i++)
        for (j = 9; j > i; j--) {
            PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem(list, j, &obj, plContext));
            PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem(list, j - 1,
                                                        &obj2, plContext));

            PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare(obj, obj2, &cmpResult, plContext));
            if (cmpResult < 0) {
                /* Exchange the items */
                PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetItem(list, j, obj2, plContext));
                PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_SetItem(list, j - 1,
                                                            obj, plContext));
            }
            /* DecRef objects */
            PKIX_TEST_DECREF_BC(obj);
            PKIX_TEST_DECREF_BC(obj2);
        }

    subTest("Outputting Sorted List");
    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)list,
                                                      &testString,
                                                      plContext));
    temp = PKIX_String2ASCII(testString, plContext);
    if (temp) {
        (void)printf("%s \n", temp);
        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
    }

cleanup:

    PKIX_TEST_DECREF_AC(testString);
    PKIX_TEST_DECREF_AC(list);

    PKIX_Shutdown(plContext);

    PKIX_TEST_RETURN();

    endTests("List Sorting");

    return (0);
}