aboutsummaryrefslogtreecommitdiff
path: root/tests/libtest/lib558.c
blob: d5d1d146acf14662fa0cc0a1937f3d294502e0cf (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
/*****************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * $Id$
 */



#include "test.h"

#include "memdebug.h"

#define TABLE_SIZE 10


struct element_st {
  int idx;
  int dummy;
};


struct root_st {
  struct element_st **table;
  int size;
};


static
struct root_st * new_root(void)
{
  struct root_st *r;

  r = malloc(sizeof(struct root_st));
  if(r != NULL)
    printf("malloc of root struct OK\n");
  else {
    printf("malloc of root struct failed\n");
    return NULL;
  }

  r->size = TABLE_SIZE;
  r->table = malloc(r->size * sizeof(struct element_st *));
  if(r->table != NULL)
    printf("malloc of pointer table OK\n");
  else {
    printf("malloc of pointer table failed\n");
    free(r);
    return NULL;
  }

  return r;
}


static
struct element_st * new_element(int idx)
{
  struct element_st *e;

  e = malloc(sizeof(struct element_st));
  if(e != NULL)
    printf("malloc of pointed element (idx %d) OK\n", idx);
  else {
    printf("malloc of pointed element (idx %d) failed\n", idx);
    return NULL;
  }

  e->idx = e->dummy = idx;

  return e;
}


int test(char *URL)
{
  struct root_st *root;
  int error;
  int i;
  (void)URL; /* not used */

  root = new_root();
  if(!root)
    return TEST_ERR_MAJOR_BAD;

  printf("initializing table...\n");
  for (i = 0; i < root->size; ++i) {
    root->table[i] = NULL;
  }
  printf("table initialized OK\n");

  printf("filling pointer table...\n");
  error = 0;
  for (i = 0; i < root->size; ++i) {
    root->table[i] = new_element(i);
    if(!root->table[i]) {
      error = 1;
      break;
    }
  }
  if(error) {
    printf("pointer table filling failed\n");
    return TEST_ERR_MAJOR_BAD;
  }
  else
    printf("pointer table filling OK\n");

  printf("freeing pointers in table...\n");
  for (i = 0; i < root->size; ++i) {
    if(root->table[i])
      free(root->table[i]);
  }
  printf("freeing pointers in table OK\n");

  printf("freeing table...\n");
  free(root->table);
  printf("freeing table OK\n");

  printf("freeing root struct...\n");
  free(root);
  printf("freeing root struct OK\n");

  return 0; /* OK */
}