xserver

xserver with xephyr scale patch
git clone https://git.neptards.moe/u3shit/xserver.git
Log | Files | Refs | README | LICENSE

hashtabletest.c (3032B)


      1 #ifdef HAVE_DIX_CONFIG_H
      2 #include <dix-config.h>
      3 #endif
      4 
      5 #include <misc.h>
      6 #include <stdlib.h>
      7 #include <stdio.h>
      8 #include "hashtable.h"
      9 #include "resource.h"
     10 
     11 #include "tests-common.h"
     12 
     13 static void
     14 print_xid(void* ptr, void* v)
     15 {
     16     XID *x = v;
     17     printf("%ld", (long)(*x));
     18 }
     19 
     20 static void
     21 print_int(void* ptr, void* v)
     22 {
     23     int *x = v;
     24     printf("%d", *x);
     25 }
     26 
     27 static int
     28 test1(void)
     29 {
     30     HashTable h;
     31     int c;
     32     int ok = 1;
     33     const int numKeys = 420;
     34 
     35     printf("test1\n");
     36     h = ht_create(sizeof(XID), sizeof(int), ht_resourceid_hash, ht_resourceid_compare, NULL);
     37 
     38     for (c = 0; c < numKeys; ++c) {
     39       int *dest;
     40       XID id = c;
     41       dest = ht_add(h, &id);
     42       if (dest) {
     43         *dest = 2 * c;
     44       }
     45     }
     46 
     47     printf("Distribution after insertion\n");
     48     ht_dump_distribution(h);
     49     ht_dump_contents(h, print_xid, print_int, NULL);
     50 
     51     for (c = 0; c < numKeys; ++c) {
     52       XID id = c;
     53       int* v = ht_find(h, &id);
     54       if (v) {
     55         if (*v == 2 * c) {
     56           // ok
     57         } else {
     58           printf("Key %d doesn't have expected value %d but has %d instead\n",
     59                  c, 2 * c, *v);
     60           ok = 0;
     61         }
     62       } else {
     63         ok = 0;
     64         printf("Cannot find key %d\n", c);
     65       }
     66     }
     67 
     68     if (ok) {
     69       printf("%d keys inserted and found\n", c);
     70 
     71       for (c = 0; c < numKeys; ++c) {
     72         XID id = c;
     73         ht_remove(h, &id);
     74       }
     75 
     76       printf("Distribution after deletion\n");
     77       ht_dump_distribution(h);
     78     }
     79 
     80     ht_destroy(h);
     81 
     82     return ok;
     83 }
     84 
     85 static int
     86 test2(void)
     87 {
     88     HashTable h;
     89     int c;
     90     int ok = 1;
     91     const int numKeys = 420;
     92 
     93     printf("test2\n");
     94     h = ht_create(sizeof(XID), 0, ht_resourceid_hash, ht_resourceid_compare, NULL);
     95 
     96     for (c = 0; c < numKeys; ++c) {
     97       XID id = c;
     98       ht_add(h, &id);
     99     }
    100 
    101     for (c = 0; c < numKeys; ++c) {
    102       XID id = c;
    103       if (!ht_find(h, &id)) {
    104         ok = 0;
    105         printf("Cannot find key %d\n", c);
    106       }
    107     }
    108 
    109     {
    110         XID id = c + 1;
    111         if (ht_find(h, &id)) {
    112             ok = 0;
    113             printf("Could find a key that shouldn't be there\n");
    114         }
    115     }
    116 
    117     ht_destroy(h);
    118 
    119     if (ok) {
    120         printf("Test with empty keys OK\n");
    121     } else {
    122         printf("Test with empty keys FAILED\n");
    123     }
    124 
    125     return ok;
    126 }
    127 
    128 static int
    129 test3(void)
    130 {
    131     int ok = 1;
    132     HtGenericHashSetupRec hashSetup = {
    133         .keySize = 4
    134     };
    135     HashTable h;
    136     printf("test3\n");
    137     h = ht_create(4, 0, ht_generic_hash, ht_generic_compare, &hashSetup);
    138 
    139     if (!ht_add(h, "helo") ||
    140         !ht_add(h, "wrld")) {
    141         printf("Could not insert keys\n");
    142     }
    143 
    144     if (!ht_find(h, "helo") ||
    145         !ht_find(h, "wrld")) {
    146         ok = 0;
    147         printf("Could not find inserted keys\n");
    148     }
    149 
    150     printf("Hash distribution with two strings\n");
    151     ht_dump_distribution(h);
    152 
    153     ht_destroy(h);
    154 
    155     return ok;
    156 }
    157 
    158 int
    159 hashtabletest_test(void)
    160 {
    161     int ok = test1();
    162     ok = ok && test2();
    163     ok = ok && test3();
    164 
    165     return ok ? 0 : 1;
    166 }