summaryrefslogtreecommitdiff
path: root/libmailutils/tests/temp_stream.c
blob: e509e30a60bf815a030baf2ea8f9796865aad9e1 (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
/*
 */
#include <mailutils/mailutils.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/temp_stream.h>

#define MAXMEM 32

extern int mu_temp_stream_create (mu_stream_t *pstream, size_t max_size);

static void
verify (mu_stream_t str, int len)
{
  char buf[2*MAXMEM];
  int i;
  
  MU_ASSERT (mu_stream_seek (str, 0, MU_SEEK_SET, NULL));
  MU_ASSERT (mu_stream_read (str, buf, len, NULL));
  for (i = 0; i < len; i++)
    {
      if (buf[i] != i)
	{
	  mu_error ("bad octet %d: %d", i, buf[i]);
	  exit (1);
	}
    }
}

static int
is_file_backed_stream (mu_stream_t str)
{
  int state;
  return mu_stream_ioctl (str, MU_IOCTL_FD, MU_IOCTL_FD_GET_BORROW, &state)
         == 0;
}

int
main (int argc, char **argv)
{
  mu_stream_t str;
  char i;
  
  MU_ASSERT (mu_temp_stream_create (&str, MAXMEM));
  for (i = 0; i < MAXMEM; i++)
    {
      MU_ASSERT (mu_stream_write (str, &i, 1, NULL));
    }

  verify (str, MAXMEM);

  if (is_file_backed_stream (str))
    {
      mu_error ("stream switched to file backend too early");
      return 1;
    }

  MU_ASSERT (mu_stream_write (str, &i, 1, NULL));
  ++i;
  if (!is_file_backed_stream (str))
    {
      mu_error ("stream failed to switch to file backend");
      return 1;
    }
      
  for (; i < 2*MAXMEM; i++)
    {
      MU_ASSERT (mu_stream_write (str, &i, 1, NULL));
    }

  verify (str, 2*MAXMEM);

  mu_stream_destroy (&str);

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.