summaryrefslogtreecommitdiff
blob: 836a9f1fde6a8119c488b814b02daddee9c4b2f6 (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
/*
 * test-dynbuf.c
 *
 * Test for the dynamic buffer module.
 *
 * Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
 *
 *
 *      This program 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 version 2 of the License.
 *
 *      This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 *      675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Header$
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rcscripts/rcutil.h"

#define TEST_STRING1	 "Hello"
#define TEST_STRING2	 " "
#define TEST_STRING3	 "world"
#define TEST_STRING4	 "!\n"
#define TEST_STRING_FULL TEST_STRING1 TEST_STRING2 TEST_STRING3 TEST_STRING4

int
main (void)
{
  dyn_buf_t *dynbuf;
  char buf[1024 * 4];
  int length, total = 0;

  dynbuf = new_dyn_buf ();
  if (NULL == dynbuf)
    {
      fprintf (stderr, "Failed to allocate dynamic buffer.\n");
      return 1;
    }

  length = sprintf_dyn_buf (dynbuf, TEST_STRING1);
  if (length != strlen (TEST_STRING1))
    {
      fprintf (stderr, "sprintf_dyn_buf() returned wrong length (pass 1)!\n");
      goto error;
    }
  total += length;

  length = write_dyn_buf (dynbuf, TEST_STRING2, strlen (TEST_STRING2));
  if (length != strlen (TEST_STRING2))
    {
      fprintf (stderr, "write_dyn_buf() returned wrong length (pass 1)!\n");
      goto error;
    }
  total += length;

  length = sprintf_dyn_buf (dynbuf, TEST_STRING3);
  if (length != strlen (TEST_STRING3))
    {
      fprintf (stderr, "sprintf_dyn_buf() returned wrong length (pass 2)!\n");
      goto error;
    }
  total += length;

  length = write_dyn_buf (dynbuf, TEST_STRING4, strlen (TEST_STRING4));
  if (length != strlen (TEST_STRING4))
    {
      fprintf (stderr, "write_dyn_buf() returned wrong length (pass 2)!\n");
      goto error;
    }
  total += length;

  length = read_dyn_buf (dynbuf, buf, total / 2);
  if (length != total / 2)
    {
      fprintf (stderr, "read_dyn_buf() returned wrong length (pass 1)!\n");
      goto error;
    }

  length = read_dyn_buf (dynbuf, (buf + (total / 2)), total);
  if (length != (total - (total / 2)))
    {
      fprintf (stderr, "read_dyn_buf() returned wrong length (pass 2)!\n");
      goto error;
    }

  if (0 != strncmp (buf, TEST_STRING_FULL, strlen (TEST_STRING_FULL)))
    {
      fprintf (stderr, "Written and read strings differ!\n");
      goto error;
    }

  while (strlen (dynbuf->data) < DYNAMIC_BUFFER_SIZE)
    {
      length = sprintf_dyn_buf (dynbuf, TEST_STRING_FULL);
      if (length != strlen (TEST_STRING_FULL))
	{
	  fprintf (stderr, "sprintf_dyn_buf() returned wrong length (%i)!\n",
		   (int)dynbuf->length);
	  goto error;
	}
      total += length;

      length = write_dyn_buf (dynbuf, TEST_STRING_FULL,
			      strlen (TEST_STRING_FULL));
      if (length != strlen (TEST_STRING_FULL))
	{
	  fprintf (stderr, "write_dyn_buf() returned wrong length (%i)!\n",
		   (int)dynbuf->length);
	  goto error;
	}
      total += length;
    }

  if (total != strlen (dynbuf->data))
    {
      fprintf (stderr, "Written string length should be %i, but is %i!\n",
	       total, (int)strlen (dynbuf->data));
      goto error;
    }

  free_dyn_buf (dynbuf);

  return 0;

error:
  free_dyn_buf (dynbuf);
  return 1;
}