summaryrefslogtreecommitdiff
path: root/libhurd-ihash/t-ihash.c
blob: 634cc748356adbda7069574e213d23e98bf22db2 (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
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
/* t-ihash.c - Integer-keyed hash table function unit-tests.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.
   
   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   The GNU Hurd is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#define _GNU_SOURCE

#include <stdio.h>
#include <assert.h>

char *program_name = "t-ihash";

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>

#include "ihash.h"

#define F(format, args...) \
  ({ \
    printf ("fail: line %d ", __LINE__); \
    printf (format "... ", ##args); \
    return 1; \
  })

int
main (int argc, char *argv[])
{
  struct hurd_ihash hash;

  hurd_ihash_init (&hash, TEST_LARGE, HURD_IHASH_NO_LOCP);

  printf ("Testing hurd_ihash_add... ");

  int k;
  hurd_ihash_value_t v;
  for (k = 1; k < 100; k ++)
    if (hurd_ihash_add (&hash, (hurd_ihash_key_t) k,
			(hurd_ihash_value_t) k) != 0)
      F ("failed to insert %d\n", k);
#if TEST_LARGE == true
  for (k = 1; k < 100; k ++)
    if (hurd_ihash_add (&hash, (hurd_ihash_key64_t) (1ULL << 33) + k,
			(hurd_ihash_value_t) (1000 + k)) != 0)
      F ("failed to insert %d\n", k);
#endif
  for (k = 1; k < 100; k ++)
    {
      v = hurd_ihash_find (&hash, (hurd_ihash_key_t) k);
      if (v != (hurd_ihash_value_t) k)
	F ("unexpected value %d for key %d\n", (int) v, k);
    }
#if TEST_LARGE == true
  for (k = 1; k < 100; k ++)
    {
      v = hurd_ihash_find (&hash, (hurd_ihash_key64_t) (1ULL << 33) + k);
      if (v != (hurd_ihash_value_t) (1000 + k))
	F ("unexpected value %d for key %d\n", (int) v, 1000 + k);
    }
#endif

  int c = 0;
  HURD_IHASH_ITERATE(&hash, i)
    c ++;
#if TEST_LARGE == true
  assert (c == 2 * 99);
#else
  assert (c == 99);
#endif

  printf ("ok\n");

  printf ("Testing hurd_ihash_replace... ");

  bool had_value;
  for (k = 1; k < 100; k ++)
    if (hurd_ihash_replace (&hash,
			    (hurd_ihash_key_t) k,
			    (hurd_ihash_value_t) k + 1,
			    &had_value, &v) != 0)
      F ("failed to replace key %d\n", k);
    else if (! had_value)
      F ("No value for key %d\n", k);
    else if ((int) v != k)
      F ("Value for key %d is %d, not %d\n", k, (int) v, k);

  for (k = 100; k < 200; k ++)
    if (hurd_ihash_replace (&hash,
			    (hurd_ihash_key_t) k,
			    (hurd_ihash_value_t) (k + 1),
			    &had_value, &v) != 0)
      F ("failed to replace value of key %d\n", k);
    else if (had_value)
      F ("key %d had the unexpected value %d!\n", k, (int) v);

  for (k = 1; k < 100; k ++)
    if (hurd_ihash_replace (&hash, (hurd_ihash_key_t) k,
			    (hurd_ihash_value_t) k,
			    &had_value, &v) != 0)
      F ("failed to replace the value of key %d\n", k);
    else if (! had_value)
      F ("No value for key %d\n", k);
    else if ((int) v != k + 1)
      F ("Value for key %d is %d, not %d\n", k, (int) v, k + 1);

  printf ("ok\n");

  printf ("Checking hurd_ihash_locp_remove... ");

  struct s
  {
    int value;
    hurd_ihash_locp_t locp;
  };
  hurd_ihash_init (&hash, TEST_LARGE, (int) &(((struct s *)0)->locp));

  if (hurd_ihash_find (&hash, 1))
    F ("Found object with key 1 in otherwise empty hash");

  struct s s;
  s.value = 1;
  hurd_ihash_add (&hash, 1, &s);
  if (! hurd_ihash_find (&hash, 1))
    F ("Did not find recently inserted object with key 1");

  hurd_ihash_locp_remove (&hash, s.locp);
  if (hurd_ihash_find (&hash, 1))
    F ("Found recently removed object with key 1");

  printf ("ok\n");

  return 0;
}