File: hash_test.c

package info (click to toggle)
clearsilver 0.10.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: ansic: 24,586; python: 4,233; sh: 2,502; cs: 1,429; ruby: 819; java: 735; makefile: 589; perl: 120; lisp: 34; sql: 21
file content (118 lines) | stat: -rw-r--r-- 2,523 bytes parent folder | download | duplicates (10)
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

#include "cs_config.h"
#include <unistd.h>
#include <string.h>
#include "util/neo_misc.h"
#include "util/neo_err.h"
#include "util/neo_hash.h"

void dump_string_hash(NE_HASH *hash)
{
  NE_HASHNODE *node;
  int x;

  for (x = 0; x < hash->size; x++)
  {
    ne_warn("Node %d", x);
    for (node = hash->nodes[x]; node; node = node->next)
    {
      ne_warn("  %s = %s [%8x | %d]", node->key, node->value, node->hashv, node->hashv & (hash->size - 1));
    }
  }
}

NEOERR *dictionary_test (void)
{
  NEOERR *err = STATUS_OK;
  int x;
  char *word;
  NE_HASH *hash = NULL;
  FILE *fp;
  char buf[256];

  err = ne_hash_init(&hash, ne_hash_str_hash, ne_hash_str_comp);
  if (err)
    return nerr_pass(err);

  fp = fopen ("/usr/dict/words", "r");
  if (fp == NULL) {
    fp = fopen ("/usr/share/dict/words", "r");
    if (fp == NULL)
      return nerr_raise_errno(NERR_IO, "Unable to open file /usr/dict/words");
  }

  ne_warn("Loading words into hash");
  while (fgets (buf, sizeof(buf), fp) != NULL)
  {
    x = strlen (buf);
    if (buf[x-1] == '\n')
      buf[x-1] = '\0';

    word = strdup(buf);
    err = ne_hash_insert(hash, word, word);
    if (err) break;
    word = ne_hash_lookup(hash, buf);
    if (word == NULL)
    {
      err = nerr_raise(NERR_ASSERT, "Unable to find word %s in hash", buf);
      break;
    }
    if (strcmp(word, buf))
    {
      err = nerr_raise(NERR_ASSERT, "Lookup returned wrong word: %s != %s", buf, word);
      break;
    }
  }
  fclose (fp);
  ne_warn("Loaded %d words", hash->num);
  if (err) 
  {
    dump_string_hash(hash);
    return nerr_pass(err);
  }

  fp = fopen ("/usr/dict/words", "r");
  if (fp == NULL) {
    fp = fopen ("/usr/share/dict/words", "r");
    if (fp == NULL)
      return nerr_raise_errno(NERR_IO, "Unable to open file /usr/dict/words");
  }

  ne_warn("Testing words in hash");
  while (fgets (buf, sizeof(buf), fp) != NULL)
  {
    x = strlen (buf);
    if (buf[x-1] == '\n')
      buf[x-1] = '\0';

    if (!(word = ne_hash_lookup(hash, buf)))
    {
      err = nerr_raise(NERR_ASSERT, "Unable to find word %s in hash", buf);
      break;
    }
    if (strcmp(word, buf))
    {
      err = nerr_raise(NERR_ASSERT, "Lookup returned wrong word: %s != %s", buf, word);
      break;
    }
  }
  fclose (fp);
  ne_hash_destroy(&hash);

  return nerr_pass(err);
}

int main(int argc, char **argv)
{
  NEOERR *err;

  err = dictionary_test();
  if (err)
  {
    nerr_log_error(err);
    printf("FAIL\n");
    return -1;
  }
  printf("PASS\n");
  return 0;
}