test-hash.c 2.92 KB
Newer Older
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
#include "../src/fchash.c"
#include "../src/fcstr.c"

FcChar8 *
FcLangNormalize (const FcChar8 *lang)
{
    return NULL;
}

FcChar8 *
FcConfigHome (void)
{
    return NULL;
}

typedef struct _Test
{
    FcHashTable *table;
} Test;

static Test *
init (void)
{
    Test *ret;

    ret = malloc (sizeof (Test));
    if (ret)
    {
	ret->table = FcHashTableCreate ((FcHashFunc) FcStrHashIgnoreCase,
					(FcCompareFunc) FcStrCmp,
					FcHashStrCopy,
					FcHashUuidCopy,
					(FcDestroyFunc) FcStrFree,
					FcHashUuidFree);
    }

    return ret;
}

static void
fini (Test *test)
{
    FcHashTableDestroy (test->table);
    free (test);
}

static FcBool
48
test_add (Test *test, char *key, FcBool replace)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
    uuid_t uuid;
    void *u;
    FcBool (*hash_add) (FcHashTable *, void *, void *);
    FcBool ret = FcFalse;

    uuid_generate_random (uuid);
    if (replace)
	hash_add = FcHashTableReplace;
    else
	hash_add = FcHashTableAdd;
    if (!hash_add (test->table, key, uuid))
	return FcFalse;
    if (!FcHashTableFind (test->table, key, &u))
	return FcFalse;
    ret = (uuid_compare (uuid, u) == 0);
    FcHashUuidFree (u);

    return ret;
}

static FcBool
71
test_remove (Test *test, char *key)
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
{
    void *u;

    if (!FcHashTableFind (test->table, key, &u))
	return FcFalse;
    FcHashUuidFree (u);
    if (!FcHashTableRemove (test->table, key))
	return FcFalse;
    if (FcHashTableFind (test->table, key, &u))
	return FcFalse;

    return FcTrue;
}

int
main (void)
{
    Test *test;
    int ret = 0;

    test = init ();
    /* first op to add */
    if (!test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    /* second op to add */
    if (!test_add (test, "bar", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    /* dup not allowed */
    if (test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    /* replacement */
    if (!test_add (test, "foo", FcTrue))
    {
	ret = 1;
	goto bail;
    }
    /* removal */
    if (!test_remove (test, "foo"))
    {
	ret = 1;
	goto bail;
    }
    /* not found to remove */
    if (test_remove (test, "foo"))
    {
	ret = 1;
	goto bail;
    }
    /* complex op in pointer */
    if (!test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    if (test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    if (!test_remove (test, "foo"))
    {
	ret = 1;
	goto bail;
    }
    if (!test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    if (!test_remove (test, "bar"))
    {
	ret = 1;
	goto bail;
    }
    /* completely remove */
    if (!test_remove (test, "foo"))
    {
	ret = 1;
	goto bail;
    }
    /* completely remove from the last one */
    if (!test_add (test, "foo", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    if (!test_add (test, "bar", FcFalse))
    {
	ret = 1;
	goto bail;
    }
    if (!test_remove (test, "bar"))
    {
	ret = 1;
	goto bail;
    }
    if (!test_remove (test, "foo"))
    {
	ret = 1;
	goto bail;
    }
bail:
    fini (test);

    return ret;
}