mindex.c

In-memory ordered store and fetch library
git clone git://git.finwo.net/lib/mindex.c
Log | Files | Refs | README | LICENSE

test.c (3562B)


      1 #include <stdio.h>
      2 
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "finwo/assert.h"
      7 #include "mindex.h"
      8 
      9 static void fn_purge(void *item, void *udata) {
     10   // Intentionally empty
     11 }
     12 
     13 static int fn_compare_string(const void *a, const void *b, void *udata) {
     14   const char *sa = (char*)a;
     15   const char *sb = (char*)b;
     16   return strcmp(sa, sb);
     17 }
     18 
     19 struct character {
     20   char *name;
     21   char *description;
     22   char *species;
     23 };
     24 
     25 static int fn_compare_struct(const void *a, const void *b, void *udata) {
     26   const struct character *ca = a;
     27   const struct character *cb = b;
     28   return strcmp(ca->name, cb->name);
     29 }
     30 
     31 void test_mindex_string() {
     32   char *test_a00 = "Hello World";
     33   char *test_a01 = calloc(1, strlen(test_a00)+1);
     34   char *test_a02 = "Foobar";
     35   strcpy(test_a01, test_a00);
     36 
     37   ASSERT("test_a00 != test_a01", test_a00 != test_a01);
     38   ASSERT("test_a01 != test_a02", test_a01 != test_a02);
     39   ASSERT("test_a00 != test_a02", test_a00 != test_a02);
     40 
     41   struct mindex_t *mindex = mindex_init(fn_compare_string, fn_purge, NULL);
     42   ASSERT("mindex_init returns non-null", mindex != NULL);
     43 
     44   mindex_set(mindex, test_a01);
     45   mindex_set(mindex, test_a00);
     46   mindex_set(mindex, test_a02);
     47   ASSERT("length = 2 after inserting 3 strings with 1 duplicate", mindex_length(mindex) == 2);
     48 
     49   char *found = mindex_get(mindex, test_a00);
     50   ASSERT("Fetching by original = get original", found == test_a00);
     51 
     52   found = mindex_get(mindex, test_a01);
     53   ASSERT("Fetching by same content = get original", found == test_a00);
     54 
     55   found = mindex_get(mindex, test_a02);
     56   ASSERT("Fetching by other = get other", found == test_a02);
     57 
     58   mindex_set(mindex, test_a01);
     59   found = mindex_get(mindex, test_a00);
     60   ASSERT("Overridden get by org returns new", found == test_a01);
     61 
     62   found = mindex_get(mindex, test_a01);
     63   ASSERT("Overridden get by new returns new", found == test_a01);
     64 
     65   mindex_free(mindex);
     66 }
     67 
     68 void test_mindex_structs() {
     69   struct mindex_t *mindex = mindex_init(fn_compare_struct, fn_purge, NULL);
     70 
     71   // Insert data
     72   mindex_set(mindex, &(struct character){ .name= "C-3PO"           , .description= "Protocol droid."                , .species= "Droid" });
     73   mindex_set(mindex, &(struct character){ .name= "R2-D2"           , .description= "Astromech droid built on Naboo.", .species= "Droid" });
     74   mindex_set(mindex, &(struct character){ .name= "Anakin Skywalker", .description= "Fallen Jedi, the chosen one."   , .species= "Human" });
     75   mindex_set(mindex, &(struct character){ .name= "Obi-Wan Kenobi"  , .description= "Jedi Master."                   , .species= "Human" });
     76   mindex_set(mindex, &(struct character){ .name= "Moon Moon"       , .description= "Mentally challenged wolf."      , .species= "Wolf"  });
     77   ASSERT("Length = 5", mindex_length(mindex) == 5);
     78 
     79   // Check fetching a bunch of data
     80   struct character *found = mindex_get(mindex, &(struct character){ .name="Moon Moon" });
     81   if (!found) return;
     82 
     83   // Check if the jedi master can be fetched
     84   ASSERT("{Obi-Wan Kenobi}.description = Jedi Master", strcmp(((struct character *)mindex_get(mindex, &(struct character){ .name="Obi-Wan Kenobi" }))->description, "Jedi Master.") == 0);
     85   ASSERT("{C-3PO}.species = Droid", strcmp(((struct character *)mindex_get(mindex, &(struct character){ .name="C-3PO" }))->species, "Droid") == 0);
     86 
     87 }
     88 
     89 int main() {
     90 
     91   // Seed random
     92   unsigned int seed;
     93   FILE* urandom = fopen("/dev/urandom", "r");
     94   fread(&seed, sizeof(int), 1, urandom);
     95   fclose(urandom);
     96   srand(seed);
     97 
     98   // Run the actual tests
     99   RUN(test_mindex_string);
    100   RUN(test_mindex_structs);
    101   return TEST_REPORT();
    102 }