File: test_hashtable.c

package info (click to toggle)
netproc 0.6.6-0.1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: ansic: 7,876; makefile: 101; sh: 12
file content (131 lines) | stat: -rw-r--r-- 2,995 bytes parent folder | download
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

#include <stddef.h>

#include "hashtable.h"
#include "unity.h"
#include "../src/macro_util.h"

static int
cb_func ( hashtable_t *ht, void *value, void *user_data )
{
  // ugly
  ht = ht;
  value = value;

  ( *( int * ) user_data )++;

  return 0;
}

#define FROM_PTR( p ) ( ( uintptr_t ) p )
#define TO_PTR( v ) ( ( void * ) ( uintptr_t ) v )

static bool
cb_compare ( const void *key1, const void *key2 )
{
  return ( key1 == key2 );
}

static hash_t
cb_hash ( const void *key )
{
  int n = ( int ) FROM_PTR ( key );

  return n;
}

void
test1 ( void )
{
  hashtable_t *ht = hashtable_new ( cb_hash, cb_compare, NULL );

  TEST_ASSERT_NOT_NULL ( ht );
  TEST_ASSERT_EQUAL_INT ( 0, hashtable_get_nentries ( ht ) );
  TEST_ASSERT_GREATER_THAN ( 0, hashtable_get_size ( ht ) );

  TEST_ASSERT_NULL ( hashtable_get ( ht, 0 ) );

  int values[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0 };
  int *p;

  size_t i;
  for ( i = 0; i < ARRAY_SIZE ( values ); i++ )
    {
      p = hashtable_set ( ht, TO_PTR ( values[i] ), &values[i] );
      TEST_ASSERT_EQUAL_INT ( values[i], *p );
    }

  TEST_ASSERT_EQUAL_INT ( ARRAY_SIZE ( values ),
                          hashtable_get_nentries ( ht ) );
  TEST_ASSERT_GREATER_THAN ( hashtable_get_nentries ( ht ),
                             hashtable_get_size ( ht ) );

  for ( i = 0; i < ARRAY_SIZE ( values ); i++ )
    {
      p = hashtable_get ( ht, TO_PTR ( values[i] ) );
      TEST_ASSERT_EQUAL_INT ( *p, values[i] );
    }

  p = hashtable_remove ( ht, TO_PTR ( values[0] ) );
  TEST_ASSERT_EQUAL_INT ( *p, values[0] );
  TEST_ASSERT_EQUAL_INT ( ARRAY_SIZE ( values ) - 1,
                          hashtable_get_nentries ( ht ) );
  TEST_ASSERT_NULL ( hashtable_remove ( ht, TO_PTR ( values[0] ) ) );

  int count = 0;
  TEST_ASSERT_EQUAL_INT ( 0, hashtable_foreach ( ht, cb_func, &count ) );
  TEST_ASSERT_EQUAL_INT ( ARRAY_SIZE ( values ) - 1, count );
  TEST_ASSERT_EQUAL_INT ( count, hashtable_get_nentries ( ht ) );

  hashtable_destroy ( ht );
}

static bool
cb_compare2 ( const void *key1, const void *key2 )
{
  return ( *( int * ) key1 == *( int * ) key2 );
}

static hash_t
cb_hash2 ( const void *key )
{
  return *( int * ) key;
}

static int
remove_foreach ( UNUSED hashtable_t *ht,
                 UNUSED void *value,
                 UNUSED void *user_data )
{
  return 1;
}

static void
test_hashtable_foreach_safe ( void )
{
  hashtable_t *ht = hashtable_new ( cb_hash2, cb_compare2, NULL );

  int values[] = { 10, 20,  30,  40,  50,  60,  70, 80,
                   90, 100, 110, 120, 130, 140, 150 };
  int *p;

  size_t i;
  for ( i = 0; i < ARRAY_SIZE ( values ); i++ )
    {
      p = hashtable_set ( ht, &values[i], &values[i] );
      TEST_ASSERT_EQUAL_INT ( values[i], *p );
    }

  hashtable_foreach_remove ( ht, remove_foreach, NULL );

  TEST_ASSERT_EQUAL_INT ( 0, hashtable_get_nentries ( ht ) );

  hashtable_destroy ( ht );
}

void
test_hashtable ( void )
{
  test1 ();
  test_hashtable_foreach_safe ();
}